<commit_before>from django.conf import settings
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.db.models.signals import post_syncdb
from django.utils.translation import ugettext_noop as _

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification

    def create_notice_types(app, created_models, verbosity, **kwargs):
        print "Creating notice types for SMS updates"
        notification.create_notice_type(
            "phone_added",
            _("Phone number added"),
            _("Your mobile phone has been added to your user profile")
        )
        notification.create_notice_type(
            "phone_confirmed",
            _("Phone confirmed"),
            _("Your mobile phone number has been confirmed for use in updating RSR projects")
        )
        notification.create_notice_type(
            "reporting_enabled",
            _("SMS updates enabled for project"),
            _("You can now send SMS-updates to an RSR project")
        )
        notification.create_notice_type(
            "reporting_cancelled",
            _("SMS updates cancelled"),
            _("SMS-updates to an RSR project has been cancelled")
        )
        notification.create_notice_type(
            "phone_disabled",
            _("Phone disabled"),
            _("SMS-updates from your mobile phone are no longer accepted")
        )
        notification.create_notice_type(
            "update_received",
            _("Update received"),
            _("Akvo has received an update from your phone")
        )
    post_syncdb.connect(create_notice_types, sender=notification)

    def create_model_instance(model, **kwargs):
        handle = kwargs.pop('handle')
        try:
            existing_instance = model.objects.get(**handle)
            updated = False
            for key in kwargs.keys():
                if kwargs[key] != getattr(existing_instance, key, False):
                    setattr(existing_instance, key, kwargs[key])
                    updated = True
            if updated:
                existing_instance.save()
                #if verbosity > 1:
                #    print "Updated %s %s" % (label, existing_instance)
            return existing_instance
        except model.DoesNotExist:
            kwargs.update(handle)
            return model.objects.create(**kwargs)
            #if verbosity > 1:
            #    print "Created %s NoticeType" % label


    #workflow for UserProfile
    #principalrolerelation group SMS updater - role SMS updater
    #principalrolerelation group SMS manager - role SMS manager
    #principalrolerelation group SMS manager - role SMS updater


    #if "permissions" in settings.INSTALLED_APPS:
    #    from permissions.utils import register_role
    #
    #    def create_permissions_objects(app, created_models, verbosity, **kwargs):
    #        #register_role('SMS Updater')
    #        print "Done creating permissions objects"
    #
    #    post_syncdb.connect(create_permissions_objects, sender=permissions)
    #else:
    #    print "Skipping creation of Permissions objects as permissions app not found"
    #

    from django.contrib.auth import models as auth_models
    def create_workflow_groups(sender, **kwargs):
        sms_updater_group = create_model_instance(Group, **{'handle': {'name': UserProfile.GROUP_SMS_UPDATER}})
        sms_manager_group = create_model_instance(Group, **{'handle': {'name': UserProfile.GROUP_SMS_MANAGER}})
        print "Done creating groups"

    post_syncdb.connect(create_workflow_groups, sender=auth_models)

    if "workflows" in settings.INSTALLED_APPS:
        from workflows import models as workflows
        from workflows.utils import set_workflow_for_model
        from .models import UserProfile

        def create_workflows_model_relation(app, created_models, verbosity, **kwargs):
            set_workflow_for_model(ContentType.objects.get_for_model(UserProfile), UserProfile.WORKFLOW_SMS_UPDATE)
            print "Created workflow relation for UserProfile"

        post_syncdb.connect(create_workflows_model_relation, sender=workflows)
    else:
        print "Skipping creation of Workflow objects as workflows app not found"


    if "permissions" in settings.INSTALLED_APPS:
        from permissions import models as permissions
        from permissions.utils import add_role

        def create_principal_role_relations(sender, **kwargs):
            try:
                sms_updater_group = Group.objects.get(name=UserProfile.GROUP_SMS_UPDATER)
                sms_manager_group = Group.objects.get(name=UserProfile.GROUP_SMS_MANAGER)
                sms_updater_role  = permissions.Role.objects.get(name=UserProfile.GROUP_SMS_UPDATER)
                sms_manager_role  = permissions.Role.objects.get(name=UserProfile.GROUP_SMS_MANAGER)

                add_role(sms_updater_group, sms_updater_role)
                add_role(sms_manager_group, sms_updater_role)
                add_role(sms_manager_group, sms_manager_role)

                print "Created principal role relations for %s and %s" % (UserProfile.GROUP_SMS_MANAGER, UserProfile.GROUP_SMS_UPDATER)
            except:
                print "Role relations could not be created since fixtures are missing. Run manage.py syncdb again please!"

        post_syncdb.connect(create_principal_role_relations, sender=permissions)
    else:
        print "Skipping creation of Roles as permissions app was not found"

    if "akvo.rsr" in settings.INSTALLED_APPS:
        from akvo.rsr import models as rsr
        def create_limited_change_permissions(sender, **kwargs):
            print "Adding RSR limited permissions"
            print
            models = [
                rsr.Organisation, rsr.Project,
                rsr.SmsReporter, rsr.UserProfile, rsr.PartnerSite
            ]
            for model in models:
                opts = model._meta
                model_name = opts.object_name.lower()
                permission, created = Permission.objects.get_or_create(
                    codename=u"rsr_limited_change_%s" % model_name,
                    defaults={
                        'name': u'RSR limited change %s' % model_name,
                        'content_type_id': ContentType.objects.get_for_model(model).id,
                    }
                )
                if created:
                    print 'Created RSR limited change %s Permission' % model_name
                else:
                    print 'RSR limited change %s Permission already exists in the database' % model_name

        post_syncdb.connect(create_limited_change_permissions, sender=rsr)



#if "workflow" in settings.INSTALLED_APPS:
#
#    from workflow import models as workflow
#
#    def create_workflow_objects(app, created_models, verbosity, **kwargs):
#        try:
#            system_acct = User.objects.get(username='system')
#        except:
#            print "Can't create workflow objects without User 'system'."
#            return
#        # Role
#        role1_data = {
#            'handle':       {'name': 'SMS Updater'},
#            'description':  'Allows the user to create project updates via SMS.'
#        }
#        role1 = create_model_instance(workflow.Role, **role1_data)
#        # EventType
#        event_type1_data = {
#            'handle':       {'name': 'UserProfile change'},
#            'description':  "A change in a user's UserProfile has occured.",<commit_msg>[#203] Add object creation for PartnerType<commit_after>from django.conf import settings
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.db.models.signals import post_syncdb
from django.utils.translation import ugettext_noop as _

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification

    def create_notice_types(app, created_models, verbosity, **kwargs):
        print "Creating notice types for SMS updates"
        notification.create_notice_type(
            "phone_added",
            _("Phone number added"),
            _("Your mobile phone has been added to your user profile")
        )
        notification.create_notice_type(
            "phone_confirmed",
            _("Phone confirmed"),
            _("Your mobile phone number has been confirmed for use in updating RSR projects")
        )
        notification.create_notice_type(
            "reporting_enabled",
            _("SMS updates enabled for project"),
            _("You can now send SMS-updates to an RSR project")
        )
        notification.create_notice_type(
            "reporting_cancelled",
            _("SMS updates cancelled"),
            _("SMS-updates to an RSR project has been cancelled")
        )
        notification.create_notice_type(
            "phone_disabled",
            _("Phone disabled"),
            _("SMS-updates from your mobile phone are no longer accepted")
        )
        notification.create_notice_type(
            "update_received",
            _("Update received"),
            _("Akvo has received an update from your phone")
        )
    post_syncdb.connect(create_notice_types, sender=notification)

    def create_model_instance(model, **kwargs):
        handle = kwargs.pop('handle')
        try:
            existing_instance = model.objects.get(**handle)
            updated = False
            for key in kwargs.keys():
                if kwargs[key] != getattr(existing_instance, key, False):
                    setattr(existing_instance, key, kwargs[key])
                    updated = True
            if updated:
                existing_instance.save()
                #if verbosity > 1:
                #    print "Updated %s %s" % (label, existing_instance)
            return existing_instance
        except model.DoesNotExist:
            kwargs.update(handle)
            return model.objects.create(**kwargs)
            #if verbosity > 1:
            #    print "Created %s NoticeType" % label


    #workflow for UserProfile
    #principalrolerelation group SMS updater - role SMS updater
    #principalrolerelation group SMS manager - role SMS manager
    #principalrolerelation group SMS manager - role SMS updater


    #if "permissions" in settings.INSTALLED_APPS:
    #    from permissions.utils import register_role
    #
    #    def create_permissions_objects(app, created_models, verbosity, **kwargs):
    #        #register_role('SMS Updater')
    #        print "Done creating permissions objects"
    #
    #    post_syncdb.connect(create_permissions_objects, sender=permissions)
    #else:
    #    print "Skipping creation of Permissions objects as permissions app not found"
    #

    from django.contrib.auth import models as auth_models
    def create_workflow_groups(sender, **kwargs):
        sms_updater_group = create_model_instance(Group, **{'handle': {'name': UserProfile.GROUP_SMS_UPDATER}})
        sms_manager_group = create_model_instance(Group, **{'handle': {'name': UserProfile.GROUP_SMS_MANAGER}})
        print "Done creating groups"

    post_syncdb.connect(create_workflow_groups, sender=auth_models)

    if "workflows" in settings.INSTALLED_APPS:
        from workflows import models as workflows
        from workflows.utils import set_workflow_for_model
        from .models import UserProfile

        def create_workflows_model_relation(app, created_models, verbosity, **kwargs):
            set_workflow_for_model(ContentType.objects.get_for_model(UserProfile), UserProfile.WORKFLOW_SMS_UPDATE)
            print "Created workflow relation for UserProfile"

        post_syncdb.connect(create_workflows_model_relation, sender=workflows)
    else:
        print "Skipping creation of Workflow objects as workflows app not found"


    if "permissions" in settings.INSTALLED_APPS:
        from permissions import models as permissions
        from permissions.utils import add_role

        def create_principal_role_relations(sender, **kwargs):
            try:
                sms_updater_group = Group.objects.get(name=UserProfile.GROUP_SMS_UPDATER)
                sms_manager_group = Group.objects.get(name=UserProfile.GROUP_SMS_MANAGER)
                sms_updater_role  = permissions.Role.objects.get(name=UserProfile.GROUP_SMS_UPDATER)
                sms_manager_role  = permissions.Role.objects.get(name=UserProfile.GROUP_SMS_MANAGER)

                add_role(sms_updater_group, sms_updater_role)
                add_role(sms_manager_group, sms_updater_role)
                add_role(sms_manager_group, sms_manager_role)

                print "Created principal role relations for %s and %s" % (UserProfile.GROUP_SMS_MANAGER, UserProfile.GROUP_SMS_UPDATER)
            except:
                print "Role relations could not be created since fixtures are missing. Run manage.py syncdb again please!"

        post_syncdb.connect(create_principal_role_relations, sender=permissions)
    else:
        print "Skipping creation of Roles as permissions app was not found"

    if "akvo.rsr" in settings.INSTALLED_APPS:
        from akvo.rsr import models as rsr
        def create_limited_change_permissions(sender, **kwargs):
            print "Adding RSR limited permissions"
            print
            models = [
                rsr.Organisation, rsr.Project,
                rsr.SmsReporter, rsr.UserProfile, rsr.PartnerSite
            ]
            for model in models:
                opts = model._meta
                model_name = opts.object_name.lower()
                permission, created = Permission.objects.get_or_create(
                    codename=u"rsr_limited_change_%s" % model_name,
                    defaults={
                        'name': u'RSR limited change %s' % model_name,
                        'content_type_id': ContentType.objects.get_for_model(model).id,
                    }
                )
                if created:
                    print 'Created RSR limited change %s Permission' % model_name
                else:
                    print 'RSR limited change %s Permission already exists in the database' % model_name

        def create_partner_types(sender, **kwargs):
            print "Adding PartnerType objects"
            print
            for id, label in rsr.Partnership.PARTNER_TYPES:
                partner_type, created = rsr.PartnerType.objects.get_or_create(id=id, label=label)
                if created:
                    print 'Created PartnerType {partner_type}'.format(partner_type=partner_type)
                else:
                    print 'Found existing PartnerType {partner_type}'.format(partner_type=partner_type)

        post_syncdb.connect(create_limited_change_permissions, sender=rsr)
        post_syncdb.connect(create_partner_types, sender=rsr)



#if "workflow" in settings.INSTALLED_APPS:
#
#    from workflow import models as workflow
#
#    def create_workflow_objects(app, created_models, verbosity, **kwargs):
#        try:
#            system_acct = User.objects.get(username='system')
#        except:
#            print "Can't create workflow objects without User 'system'."
#            return
#        # Role
#        role1_data = {
#            'handle':       {'name': 'SMS Updater'},
#            'description':  'Allows the user to create project updates via SMS.'
#        }
#        role1 = create_model_instance(workflow.Role, **role1_data)
#        # EventType
#        event_type1_data = {
#            'handle':       {'name': 'UserProfile change'},
#            'description':  "A change in a user's UserProfile has occured.",
{'fraction_non_alphanumeric': 0.07753023551877784, 'fraction_numerical': 0.0009548058561425843, 'mean_word_length': 2.383803575274607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '316755', 'n_tokens_mistral': 4341, 'n_tokens_neox': 4179, 'n_words': 1073}