<s> from guardian . models import GroupObjectPermission <EOL> from django . contrib . auth . models import User , Group , Permission <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . db . models . signals import post_save , pre_save , post_delete , pre_delete <EOL> from django . dispatch import receiver <EOL> from pontoon . base import errors <EOL> from pontoon . base . models import ( <EOL> Locale , <EOL> Project , <EOL> ProjectLocale , <EOL> TranslatedResource , <EOL> UserProfile , <EOL> ) <EOL> @ receiver ( post_save , sender = ProjectLocale ) <EOL> def project_locale_added ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> created = kwargs . get ( '<STR_LIT>' , False ) <EOL> raw = kwargs . get ( '<STR_LIT>' , False ) <EOL> project_locale = kwargs . get ( '<STR_LIT>' , None ) <EOL> if created and not raw and project_locale is not None : <EOL> project = project_locale . project <EOL> project . has_changed = True <EOL> project . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> @ receiver ( post_delete , sender = ProjectLocale ) <EOL> def project_locale_removed ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> project_locale = kwargs . get ( '<STR_LIT>' , None ) <EOL> if project_locale is not None : <EOL> project = project_locale . project <EOL> locale = project_locale . locale <EOL> TranslatedResource . objects . filter ( resource__project = project , locale = locale ) . delete ( ) <EOL> project . aggregate_stats ( ) <EOL> locale . aggregate_stats ( ) <EOL> @ receiver ( pre_delete , sender = Locale ) <EOL> def locale_deleted ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> locale = kwargs . get ( '<STR_LIT>' , None ) <EOL> if locale is not None : <EOL> for project in locale . project_set . all ( ) : <EOL> project . aggregate_stats ( ) <EOL> @ receiver ( pre_delete , sender = Project ) <EOL> def project_deleted ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> project = kwargs . get ( '<STR_LIT>' , None ) <EOL> if project is not None : <EOL> for locale in project . locales . all ( ) : <EOL> locale . aggregate_stats ( ) <EOL> @ receiver ( pre_save , sender = Locale ) <EOL> def create_locale_translators_group ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> instance = kwargs [ '<STR_LIT>' ] <EOL> if kwargs [ '<STR_LIT>' ] or instance . translators_group is not None : <EOL> return <EOL> try : <EOL> locale_ct = ContentType . objects . get ( app_label = '<STR_LIT>' , model = '<STR_LIT>' ) <EOL> locale_group , _ = Group . objects . get_or_create ( name = '<STR_LIT>' . format ( instance . code ) ) <EOL> can_translate = Permission . objects . get ( content_type = locale_ct , codename = '<STR_LIT>' ) <EOL> locale_group . permissions . add ( can_translate ) <EOL> instance . translators_group = locale_group <EOL> except ObjectDoesNotExist as e : <EOL> errors . send_exception ( e ) <EOL> @ receiver ( pre_save , sender = Locale ) <EOL> def create_locale_managers_group ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> instance = kwargs [ '<STR_LIT>' ] <EOL> if kwargs [ '<STR_LIT>' ] or instance . managers_group is not None : <EOL> return <EOL> try : <EOL> locale_ct = ContentType . objects . get ( app_label = '<STR_LIT>' , model = '<STR_LIT>' ) <EOL> locale_group , _ = Group . objects . get_or_create ( name = '<STR_LIT>' . format ( instance . code ) ) <EOL> can_translate = Permission . objects . get ( content_type = locale_ct , codename = '<STR_LIT>' ) <EOL> can_manage = Permission . objects . get ( content_type = locale_ct , codename = '<STR_LIT>' ) <EOL> locale_group . permissions . add ( can_translate ) <EOL> locale_group . permissions . add ( can_manage ) <EOL> instance . managers_group = locale_group <EOL> except ObjectDoesNotExist as e : <EOL> errors . send_exception ( e ) <EOL> @ receiver ( post_save , sender = Locale ) <EOL> def assign_group_permissions ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs [ '<STR_LIT>' ] or not kwargs [ '<STR_LIT>' ] : <EOL> return <EOL> instance = kwargs [ '<STR_LIT>' ] <EOL> try : <EOL> locale_ct = ContentType . objects . get ( app_label = '<STR_LIT>' , model = '<STR_LIT>' ) <EOL> can_translate = Permission . objects . get ( content_type = locale_ct , codename = '<STR_LIT>' ) <EOL> can_manage = Permission . objects . get ( content_type = locale_ct , codename = '<STR_LIT>' ) <EOL> GroupObjectPermission . objects . get_or_create ( object_pk = instance . pk , <EOL> content_type = locale_ct , <EOL> group = instance . translators_group , <EOL> permission = can_translate ) <EOL> GroupObjectPermission . objects . get_or_create ( object_pk = instance . pk , <EOL> content_type = locale_ct , <EOL> group = instance . managers_group , <EOL> permission = can_translate ) <EOL> GroupObjectPermission . objects . get_or_create ( object_pk = instance . pk , <EOL> content_type = locale_ct , <EOL> group = instance . managers_group , <EOL> permission = can_manage ) <EOL> except ObjectDoesNotExist as e : <EOL> errors . send_exception ( e ) <EOL> @ receiver ( post_save , sender = User ) <EOL> def create_user_profile ( sender , instance , created , ** kwargs ) : <EOL> if created : <EOL> UserProfile . objects . create ( user = instance ) </s>
