<s> from django . db import models <EOL> from django . contrib import admin <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib . sites . models import Site <EOL> from django . contrib . auth . models import User <EOL> from django . core . cache import cache <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from zorna . acl . models import ACLPermission <EOL> from zorna . account . models import UserAvatar <EOL> ENTITY_DATA_TYPES = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:user>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:object>' , '<STR_LIT>' ) , <EOL> ) <EOL> ENTITIES_OWNERS_CACHE_CACHE = '<STR_LIT>' <EOL> ENTITIES_AVATARS_CACHE_CACHE = '<STR_LIT>' <EOL> class ZornaEntityAdmin ( admin . ModelAdmin ) : <EOL> def save_model ( self , request , obj , form , change ) : <EOL> if not change : <EOL> obj . owner = request . user <EOL> obj . save ( ) <EOL> class ZornaEntity ( models . Model ) : <EOL> """<STR_LIT:U+0020>""" <EOL> site = models . ForeignKey ( <EOL> Site , null = True , related_name = '<STR_LIT>' , editable = False ) <EOL> owner = models . ForeignKey ( <EOL> User , null = True , related_name = '<STR_LIT>' , editable = False ) <EOL> modifier = models . ForeignKey ( <EOL> User , null = True , related_name = '<STR_LIT>' , editable = False ) <EOL> time_created = models . DateTimeField ( _ ( <EOL> '<STR_LIT>' ) , auto_now_add = True , editable = False ) <EOL> time_updated = models . DateTimeField ( _ ( <EOL> '<STR_LIT>' ) , auto_now = True , auto_now_add = True , editable = False ) <EOL> enabled = models . BooleanField ( _ ( '<STR_LIT>' ) , editable = False ) <EOL> class Meta : <EOL> abstract = True <EOL> def save ( self , * args , ** kwargs ) : <EOL> self . site = Site . objects . get_current ( ) <EOL> super ( ZornaEntity , self ) . save ( * args , ** kwargs ) <EOL> def delete ( self , * args , ** kwargs ) : <EOL> ct = ContentType . objects . get_for_model ( self ) <EOL> ACLPermission . objects . filter ( <EOL> object_id = self . pk , content_type = ct ) . delete ( ) <EOL> super ( ZornaEntity , self ) . delete ( * args , ** kwargs ) <EOL> def as_leaf_class ( self ) : <EOL> content_type = self . content_type <EOL> model = content_type . model_class ( ) <EOL> if ( model == ZornaEntity ) : <EOL> return self <EOL> return model . objects . get ( id = self . id ) <EOL> def get_owner_full_name ( self ) : <EOL> return self . get_user_full_name ( self . owner_id ) <EOL> def get_modifier_full_name ( self ) : <EOL> return self . get_user_full_name ( self . modifier_id ) <EOL> def get_user_avatar ( self , user_id ) : <EOL> entities_avatars_cache = cache . get ( ENTITIES_AVATARS_CACHE_CACHE ) <EOL> if entities_avatars_cache is None : <EOL> entities_avatars_cache = { } <EOL> try : <EOL> return entities_avatars_cache [ user_id ] <EOL> except : <EOL> try : <EOL> avatar_user = UserAvatar . objects . get ( user = user_id ) <EOL> except UserAvatar . DoesNotExist : <EOL> avatar_user = None <EOL> entities_avatars_cache [ <EOL> user_id ] = avatar_user . avatar if avatar_user else '<STR_LIT>' <EOL> cache . set ( ENTITIES_AVATARS_CACHE_CACHE , entities_avatars_cache ) <EOL> return entities_avatars_cache [ user_id ] <EOL> def get_owner_avatar ( self ) : <EOL> return self . get_user_avatar ( self . owner_id ) <EOL> def get_modifier_avatar ( self ) : <EOL> return self . get_user_avatar ( self . modifier_id ) <EOL> def get_user_full_name ( self , user_id ) : <EOL> entities_owners_cache = cache . get ( ENTITIES_OWNERS_CACHE_CACHE ) <EOL> if entities_owners_cache is None : <EOL> entities_owners_cache = { } <EOL> try : <EOL> return entities_owners_cache [ user_id ] . get_full_name ( ) <EOL> except : <EOL> try : <EOL> entities_owners_cache [ user_id ] = User . objects . get ( pk = user_id ) <EOL> cache . set ( ENTITIES_OWNERS_CACHE_CACHE , entities_owners_cache ) <EOL> except User . DoesNotExist : <EOL> return '<STR_LIT>' <EOL> return entities_owners_cache [ user_id ] . get_full_name ( ) <EOL> def get_acl_permissions ( ) : <EOL> return { } <EOL> get_acl_permissions = staticmethod ( get_acl_permissions ) </s>
