<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import getpass <EOL> import unicodedata <EOL> from django . contrib . auth import ( models as auth_app , get_permission_codename , <EOL> get_user_model ) <EOL> from django . core import exceptions <EOL> from django . core . management . base import CommandError <EOL> from django . db import DEFAULT_DB_ALIAS , router <EOL> from django . db . models import get_model , get_models , signals , UnavailableApp <EOL> from django . utils . encoding import DEFAULT_LOCALE_ENCODING <EOL> from django . utils import six <EOL> from django . utils . six . moves import input <EOL> def _get_all_permissions ( opts , ctype ) : <EOL> """<STR_LIT>""" <EOL> builtin = _get_builtin_permissions ( opts ) <EOL> custom = list ( opts . permissions ) <EOL> _check_permission_clashing ( custom , builtin , ctype ) <EOL> return builtin + custom <EOL> def _get_builtin_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 ) , <EOL> '<STR_LIT>' % ( action , opts . verbose_name_raw ) ) ) <EOL> return perms <EOL> def _check_permission_clashing ( custom , builtin , ctype ) : <EOL> """<STR_LIT>""" <EOL> pool = set ( ) <EOL> builtin_codenames = set ( p [ <NUM_LIT:0> ] for p in builtin ) <EOL> for codename , _name in custom : <EOL> if codename in pool : <EOL> raise CommandError ( <EOL> "<STR_LIT>" % <EOL> ( codename , ctype . app_label , ctype . model_class ( ) . __name__ ) ) <EOL> elif codename in builtin_codenames : <EOL> raise CommandError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( codename , ctype . app_label , ctype . model_class ( ) . __name__ ) ) <EOL> pool . add ( codename ) <EOL> def create_permissions ( app , created_models , verbosity , db = DEFAULT_DB_ALIAS , ** kwargs ) : <EOL> try : <EOL> get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except UnavailableApp : <EOL> return <EOL> if not router . allow_syncdb ( db , auth_app . Permission ) : <EOL> return <EOL> from django . contrib . contenttypes . models import ContentType <EOL> app_models = get_models ( app ) <EOL> searched_perms = list ( ) <EOL> ctypes = set ( ) <EOL> for klass in app_models : <EOL> ctype = ContentType . objects . db_manager ( db ) . get_for_model ( klass ) <EOL> ctypes . add ( ctype ) <EOL> for perm in _get_all_permissions ( klass . _meta , ctype ) : <EOL> searched_perms . append ( ( ctype , perm ) ) <EOL> all_perms = set ( auth_app . Permission . objects . using ( db ) . filter ( <EOL> content_type__in = ctypes , <EOL> ) . values_list ( <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ) ) <EOL> perms = [ <EOL> auth_app . Permission ( codename = codename , name = name , content_type = ctype ) <EOL> for ctype , ( codename , name ) in searched_perms <EOL> if ( ctype . pk , codename ) not in all_perms <EOL> ] <EOL> auth_app . Permission . objects . using ( db ) . bulk_create ( perms ) <EOL> if verbosity >= <NUM_LIT:2> : <EOL> for perm in perms : <EOL> print ( "<STR_LIT>" % perm ) <EOL> def create_superuser ( app , created_models , verbosity , db , ** kwargs ) : <EOL> try : <EOL> get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> UserModel = get_user_model ( ) <EOL> except UnavailableApp : <EOL> return <EOL> from django . core . management import call_command <EOL> if UserModel in created_models and kwargs . get ( '<STR_LIT>' , True ) : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> confirm = input ( msg ) <EOL> while <NUM_LIT:1> : <EOL> if confirm not in ( '<STR_LIT:yes>' , '<STR_LIT>' ) : <EOL> confirm = input ( '<STR_LIT>' ) <EOL> continue <EOL> if confirm == '<STR_LIT:yes>' : <EOL> call_command ( "<STR_LIT>" , interactive = True , database = db ) <EOL> break <EOL> def get_system_username ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> result = getpass . getuser ( ) <EOL> except ( ImportError , KeyError ) : <EOL> return '<STR_LIT>' <EOL> if six . PY2 : <EOL> try : <EOL> result = result . decode ( DEFAULT_LOCALE_ENCODING ) <EOL> except UnicodeDecodeError : <EOL> return '<STR_LIT>' <EOL> return result <EOL> def get_default_username ( check_db = True ) : <EOL> """<STR_LIT>""" <EOL> if auth_app . User . _meta . swapped : <EOL> return '<STR_LIT>' <EOL> default_username = get_system_username ( ) <EOL> try : <EOL> default_username = unicodedata . normalize ( '<STR_LIT>' , default_username ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) . decode ( '<STR_LIT:ascii>' ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) . lower ( ) <EOL> except UnicodeDecodeError : <EOL> return '<STR_LIT>' <EOL> try : <EOL> auth_app . User . _meta . get_field ( '<STR_LIT:username>' ) . run_validators ( default_username ) <EOL> except exceptions . ValidationError : <EOL> return '<STR_LIT>' <EOL> if check_db and default_username : <EOL> try : <EOL> auth_app . User . _default_manager . get ( username = default_username ) <EOL> except auth_app . User . DoesNotExist : <EOL> pass <EOL> else : <EOL> return '<STR_LIT>' <EOL> return default_username <EOL> signals . post_syncdb . connect ( create_permissions , <EOL> dispatch_uid = "<STR_LIT>" ) <EOL> signals . post_syncdb . connect ( create_superuser , <EOL> sender = auth_app , dispatch_uid = "<STR_LIT>" ) </s>
