Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

George Hotelling http://george.hotelling.net/

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Get the inverse of a hex color in Ruby

This method will return the inverse of a hex color, which is useful if you want to make sure that your text will show up on a colored background.

def invert_color(color)
  color.gsub!(/^#/, '')
  sprintf("%X", color.hex ^ 0xFFFFFF)
end


Example:

invert_color('#c0c0c0') #=> "3F3F3F"


Limitations:

Doesn't handle named colors or 3 digit colors (i.e. #FFF == #FFFFFF)

Check that a user has permission on all parents in Zope

If you need to be sure that a user can view an object, you need to check the View permission not only on the object but also all of its parents. To do this, I slightly modified the script at http://zopelabs.com/cookbook/1018022911 so that it uses the context object and specified the View permission.

Create a Python script in your acquisition path (I used portal_skins/custom) named can_view and paste in this code:
permission = "View"
try:
  object=context
  if not object.acquiredRolesAreUsedBy( permission ):
    for p in object.rolesOfPermission( permission ):
      if p['selected']:
        if p['name'] in user.getRoles():
          return 1
  else:
   return 1
except:
    pass
return 0
Then you can check the permission in TAL like this:


<div tal:condition="some_object/can_view">
...
</div>
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS