<s> from dockit . models import DockitPermission <EOL> from dockit . schema . signals import document_registered <EOL> from dockit . schema . loading import get_documents <EOL> from django . contrib . auth import models as auth_app <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db . utils import DatabaseError <EOL> from django . db . transaction import commit_on_success <EOL> def _get_permission_codename ( action , opts ) : <EOL> return u'<STR_LIT>' % ( opts . collection , action ) <EOL> def _get_all_permissions ( opts ) : <EOL> "<STR_LIT>" <EOL> perms = [ ] <EOL> for action in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> perms . append ( ( _get_permission_codename ( action , opts ) , u'<STR_LIT>' % ( action , opts . verbose_name_raw ) ) ) <EOL> return perms + opts . permissions <EOL> def create_permissions ( documents , verbosity , ** kwargs ) : <EOL> searched_perms = list ( ) <EOL> ctype = ContentType . objects . get_for_model ( DockitPermission ) <EOL> for klass in documents : <EOL> for perm in _get_all_permissions ( klass . _meta ) : <EOL> searched_perms . append ( perm ) <EOL> all_perms = set ( auth_app . Permission . objects . filter ( <EOL> content_type = ctype , <EOL> ) . values_list ( <EOL> "<STR_LIT>" , flat = True <EOL> ) ) <EOL> for codename , name in searched_perms : <EOL> if codename in all_perms : <EOL> continue <EOL> p = auth_app . Permission . objects . create ( <EOL> codename = codename , <EOL> name = name , <EOL> content_type = ctype <EOL> ) <EOL> if verbosity >= <NUM_LIT:2> : <EOL> print "<STR_LIT>" % p <EOL> def on_document_registered ( document , ** kwargs ) : <EOL> create_permissions ( [ document ] , <NUM_LIT:1> ) <EOL> document_registered . connect ( on_document_registered ) <EOL> try : <EOL> commit_on_success ( create_permissions ) ( get_documents ( ) , <NUM_LIT:1> ) <EOL> except DatabaseError : <EOL> pass </s>
