Personal wiki notebook (not under development)

Group.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from Persistent import Persistent, quote
  2. class Group( Persistent ):
  3. """
  4. A group of users, used to represent an organization, department, or team.
  5. """
  6. def __init__( self, object_id, revision = None, name = None, admin = None ):
  7. """
  8. Create a new group with the given id and name.
  9. @type object_id: unicode
  10. @param object_id: id of the group
  11. @type revision: datetime or NoneType
  12. @param revision: revision timestamp of the object (optional, defaults to now)
  13. @type name: unicode or NoneType
  14. @param name: name of this group (optional)
  15. @type admin: bool
  16. @param admin: whether access to this group includes admin capabilities
  17. @rtype: Group
  18. @return: newly constructed group
  19. """
  20. Persistent.__init__( self, object_id, revision )
  21. self.__name = name
  22. self.__admin = admin
  23. @staticmethod
  24. def create( object_id, name = None, admin = None ):
  25. """
  26. Convenience constructor for creating a new group.
  27. @type object_id: unicode
  28. @param object_id: id of the group
  29. @type name: unicode or NoneType
  30. @param name: name of this group (optional)
  31. @type admin: bool
  32. @param admin: whether access to this group includes admin capabilities
  33. @rtype: group
  34. @return: newly constructed group
  35. """
  36. return Group( object_id, name = name, admin = admin )
  37. @staticmethod
  38. def sql_load( object_id, revision = None ):
  39. if revision:
  40. return "select * from luminotes_group where id = %s and revision = %s;" % ( quote( object_id ), quote( revision ) )
  41. return "select * from luminotes_group_current where id = %s;" % quote( object_id )
  42. @staticmethod
  43. def sql_id_exists( object_id, revision = None ):
  44. if revision:
  45. return "select id from luminotes_group where id = %s and revision = %s;" % ( quote( object_id ), quote( revision ) )
  46. return "select id from luminotes_group_current where id = %s;" % quote( object_id )
  47. def sql_exists( self ):
  48. return Group.sql_id_exists( self.object_id, self.revision )
  49. def sql_create( self ):
  50. return \
  51. "insert into luminotes_group ( id, revision, name ) " + \
  52. "values ( %s, %s, %s );" % \
  53. ( quote( self.object_id ), quote( self.revision ), quote( self.__name ) )
  54. def sql_update( self ):
  55. return self.sql_create()
  56. def sql_load_users( self, admin = None ):
  57. """
  58. Return a SQL string to load a list of the users with membership to this group.
  59. """
  60. if admin is True:
  61. admin_clause = " and user_group.admin = 't'"
  62. elif admin is False:
  63. admin_clause = " and user_group.admin = 'f'"
  64. else:
  65. admin_clause = ""
  66. return \
  67. """
  68. select
  69. luminotes_user_current.*
  70. from
  71. user_group, luminotes_user_current
  72. where
  73. user_group.group_id = %s and
  74. user_group.user_id = luminotes_user_current.id%s
  75. order by luminotes_user_current.username;
  76. """ % ( quote( self.object_id ), admin_clause )
  77. def to_dict( self ):
  78. d = Persistent.to_dict( self )
  79. d.update( dict(
  80. name = self.__name,
  81. admin = self.__admin,
  82. ) )
  83. return d
  84. def __set_name( self, name ):
  85. self.__name = name
  86. self.update_revision()
  87. def __set_admin( self, admin ):
  88. # The admin member isn't actually saved to the database, so setting it doesn't need to
  89. # call update_revision().
  90. self.__admin = admin
  91. name = property( lambda self: self.__name, __set_name )
  92. admin = property( lambda self: self.__admin, __set_admin )