<s> '''<STR_LIT>''' <EOL> from django . db import models <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib . contenttypes . generic import GenericForeignKey <EOL> from django . contrib . auth . models import User <EOL> from expedient . common . permissions . exceptions import PermissionCannotBeDelegated <EOL> from expedient . common . permissions . managers import ExpedientPermissionManager , ObjectPermissionManager , PermitteeManager , PermissionOwnershipManager <EOL> import logging <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> class ExpedientPermission ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> objects = ExpedientPermissionManager ( ) <EOL> name = models . CharField ( max_length = <NUM_LIT:100> , unique = True ) <EOL> description = models . TextField ( blank = True , default = "<STR_LIT>" ) <EOL> view = models . CharField ( "<STR_LIT>" , max_length = <NUM_LIT> , <EOL> blank = True , null = True ) <EOL> def __unicode__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . name , self . description , self . view ) <EOL> class ObjectPermission ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> objects = ObjectPermissionManager ( ) <EOL> permission = models . ForeignKey ( ExpedientPermission ) <EOL> object_type = models . ForeignKey ( ContentType ) <EOL> object_id = models . PositiveIntegerField ( ) <EOL> target = GenericForeignKey ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> permittees = models . ManyToManyField ( <EOL> "<STR_LIT>" , through = "<STR_LIT>" ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . permission . name , <EOL> self . target ) <EOL> class Meta : <EOL> unique_together = ( <EOL> ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ) <EOL> def give_to ( self , receiver , giver = None , can_delegate = False ) : <EOL> """<STR_LIT>""" <EOL> if giver : <EOL> giver = Permittee . objects . get_as_permittee ( giver ) <EOL> can_give = Permittee . objects . filter_for_obj_permission ( <EOL> self , can_delegate = False ) . filter ( <EOL> id = giver . id ) . count ( ) > <NUM_LIT:0> <EOL> if not can_give : <EOL> raise PermissionCannotBeDelegated ( <EOL> giver , self . permission . name ) <EOL> receiver = Permittee . objects . get_as_permittee ( receiver ) <EOL> po , created = PermissionOwnership . objects . get_or_create ( <EOL> obj_permission = self , <EOL> permittee = receiver , <EOL> defaults = dict ( can_delegate = can_delegate ) , <EOL> ) <EOL> if po . can_delegate != can_delegate and ( created or ( not created and can_delegate ) ) : <EOL> po . can_delegate = can_delegate <EOL> po . save ( ) <EOL> return po <EOL> class Permittee ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> objects = PermitteeManager ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> object_type = models . ForeignKey ( ContentType ) <EOL> object_id = models . PositiveIntegerField ( ) <EOL> object = GenericForeignKey ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT:%s>" % self . object <EOL> class Meta : <EOL> unique_together = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> class PermissionOwnership ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> objects = PermissionOwnershipManager ( ) <EOL> obj_permission = models . ForeignKey ( ObjectPermission ) <EOL> permittee = models . ForeignKey ( Permittee ) <EOL> can_delegate = models . BooleanField ( ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . obj_permission , <EOL> self . permittee , <EOL> self . can_delegate ) <EOL> class Meta : <EOL> unique_together = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> class PermissionRequest ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> requesting_user = models . ForeignKey ( <EOL> User , related_name = "<STR_LIT>" ) <EOL> permittee = models . ForeignKey ( Permittee ) <EOL> permission_owner = models . ForeignKey ( <EOL> User , related_name = "<STR_LIT>" ) <EOL> requested_permission = models . ForeignKey ( ObjectPermission ) <EOL> message = models . TextField ( default = "<STR_LIT>" , blank = True ) <EOL> def allow ( self , can_delegate = False ) : <EOL> self . requested_permission . give_to ( <EOL> self . permittee , <EOL> giver = self . permission_owner , <EOL> can_delegate = can_delegate ) <EOL> self . delete ( ) <EOL> def deny ( self ) : <EOL> self . delete ( ) </s>
