witten
/
luminotes
Archived
1
0
Fork 0

New model.Group class to wrap group-related tables.

This commit is contained in:
Dan Helfman 2008-05-28 13:32:38 -07:00
parent d38ca756a2
commit f610577bf8
2 changed files with 132 additions and 0 deletions

90
model/Group.py Normal file
View File

@ -0,0 +1,90 @@
from Persistent import Persistent, quote
class Group( Persistent ):
"""
A group of users, used to represent an organization, department, or team.
"""
def __init__( self, object_id, revision = None, name = None, admin = None ):
"""
Create a new group with the given id and name.
@type object_id: unicode
@param object_id: id of the group
@type revision: datetime or NoneType
@param revision: revision timestamp of the object (optional, defaults to now)
@type name: unicode or NoneType
@param name: name of this group (optional)
@type admin: bool
@param admin: whether access to this group includes admin capabilities
@rtype: Group
@return: newly constructed group
"""
Persistent.__init__( self, object_id, revision )
self.__name = name
self.__admin = admin
@staticmethod
def create( object_id, name = None, admin = None ):
"""
Convenience constructor for creating a new group.
@type object_id: unicode
@param object_id: id of the group
@type name: unicode or NoneType
@param name: name of this group (optional)
@type admin: bool
@param admin: whether access to this group includes admin capabilities
@rtype: group
@return: newly constructed group
"""
return Group( object_id, name = name, admin = admin )
@staticmethod
def sql_load( object_id, revision = None ):
if revision:
return "select * from luminotes_group where id = %s and revision = %s;" % ( quote( object_id ), quote( revision ) )
return "select * from luminotes_group_current where id = %s;" % quote( object_id )
@staticmethod
def sql_id_exists( object_id, revision = None ):
if revision:
return "select id from luminotes_group where id = %s and revision = %s;" % ( quote( object_id ), quote( revision ) )
return "select id from luminotes_group_current where id = %s;" % quote( object_id )
def sql_exists( self ):
return Group.sql_id_exists( self.object_id, self.revision )
def sql_create( self ):
return \
"insert into luminotes_group ( id, revision, name ) " + \
"values ( %s, %s, %s );" % \
( quote( self.object_id ), quote( self.revision ), quote( self.__name ) )
def sql_update( self ):
return self.sql_create()
def to_dict( self ):
d = Persistent.to_dict( self )
d.update( dict(
name = self.__name,
admin = self.__admin,
) )
return d
def __set_name( self, name ):
self.__name = name
self.update_revision()
def __set_admin( self, admin ):
# The admin member isn't actually saved to the database, so setting it doesn't need to
# call update_revision().
self.__admin = admin
name = property( lambda self: self.__name, __set_name )
admin = property( lambda self: self.__admin, __set_admin )

42
model/test/Test_group.py Normal file
View File

@ -0,0 +1,42 @@
from pytz import utc
from datetime import datetime, timedelta
from model.Group import Group
class Test_group( object ):
def setUp( self ):
self.object_id = "17"
self.name = u"my group"
self.delta = timedelta( seconds = 1 )
self.admin = True
self.group = Group.create( self.object_id, self.name, self.admin )
def test_create( self ):
assert self.group.object_id == self.object_id
assert datetime.now( tz = utc ) - self.group.revision < self.delta
assert self.group.name == self.name
assert self.group.admin == True
def test_set_name( self ):
new_name = u"my new group"
previous_revision = self.group.revision
self.group.name = new_name
assert self.group.name == new_name
assert self.group.revision > previous_revision
def test_set_admin( self ):
original_revision = self.group.revision
self.group.admin = True
assert self.group.admin == True
assert self.group.revision == original_revision
def test_to_dict( self ):
d = self.group.to_dict()
assert d.get( "name" ) == self.name
assert d.get( "admin" ) == True
assert d.get( "object_id" ) == self.group.object_id
assert datetime.now( tz = utc ) - d.get( "revision" ) < self.delta