Beispiel #1
0
 def forwards(self):
     # Update content types to last actual state
     update_contenttypes(noc.sa.models, None)
     # Convert groups to tags
     ctype_id = db.execute(
         "SELECT id FROM django_content_type WHERE model='managedobject'"
     )[0][0]
     for category, entry_id in db.execute(
             "SELECT g.name,o.managedobject_id FROM sa_managedobject_groups o JOIN sa_objectgroup g ON (o.objectgroup_id=g.id)"
     ):
         if db.execute("SELECT COUNT(*) FROM tagging_tag WHERE name=%s",
                       [category])[0][0] == 0:
             db.execute("INSERT INTO tagging_tag(name) VALUES(%s)",
                        [category])
         tag_id = db.execute("SELECT id FROM tagging_tag WHERE name=%s",
                             [category])[0][0]
         db.execute(
             "INSERT INTO tagging_taggeditem(tag_id,content_type_id,object_id) VALUES(%s,%s,%s)",
             [tag_id, ctype_id, entry_id])
     # Drop groups and fields
     for t in [
             "sa_managedobject_groups",
             "sa_managedobjectselector_filter_groups", "sa_objectgroup"
     ]:
         db.drop_table(t)
Beispiel #2
0
def add_admin_perms(models, perm_parts=('add', 'change', 'delete')):
    """Designed to be run from a data migration script,
    adds create, edit and delete permissions to the site admin group"""
    from django.contrib.contenttypes.management import update_contenttypes
    from django.contrib.contenttypes.models import ContentType
    from django.contrib.auth.management import create_permissions
    from django.apps import apps
    from django.contrib.auth.models import Group, Permission

    if not isinstance(models, tuple):
        # Allow single model to be passed in
        models = (models,)

    # First, we need to create permissions in case this hasn't run yet
    # http://andrewingram.net/2012/dec/ \
    # common-pitfalls-django-south/#contenttypes-and-permissions
    app_set = set([model._meta.app_label for model in models])
    for app in app_set:
        app_config = apps.get_app_config(app)
        update_contenttypes(app_config)
        create_permissions(app_config)

    # Get group for site admin
    group, created = Group.objects.get_or_create(name='site admin')

    # Add permissions to the site admin
    for model in models:
        for perm_part in perm_parts:
            codename = "%s_%s" % (perm_part, model._meta.model_name)
            content_type = ContentType.objects.get_for_model(model)
            permission = Permission.objects.get(codename=codename,
                                                content_type=content_type)
            group.permissions.add(permission)

    group.save()
    def forwards(self, orm):
        # Adding model 'SiteCategory'
        db.create_table(u'public_project_sitecategory', (
            (u'id',
             self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('category', self.gf('django.db.models.fields.CharField')(
                unique=True, max_length=50)),
            ('intro_text', self.gf('django.db.models.fields.TextField')(
                null=True, blank=True)),
            ('comments',
             self.gf('django.db.models.fields.TextField')(blank=True)),
            ('date_added', self.gf('django.db.models.fields.DateTimeField')(
                auto_now_add=True, blank=True)),
        ))
        db.send_create_signal(u'public_project', ['SiteCategory'])

        # Adding M2M table for field documents on 'SiteCategory'
        m2m_table_name = db.shorten_name(
            u'public_project_sitecategory_documents')
        db.create_table(
            m2m_table_name,
            (('id',
              models.AutoField(
                  verbose_name='ID', primary_key=True, auto_created=True)),
             ('sitecategory',
              models.ForeignKey(orm[u'public_project.sitecategory'],
                                null=False)),
             ('document',
              models.ForeignKey(orm[u'public_project.document'], null=False))))
        db.create_unique(m2m_table_name, ['sitecategory_id', 'document_id'])

        #Update content types (necessary for migration 0006)
        update_contenttypes(get_app('public_project'), get_models())
        create_permissions(get_app('public_project'), get_models(), 0)
Beispiel #4
0
    def setUpTestData(cls):
        # We disable synchronisation of test models. So we need to create it manually.
        with connection.schema_editor() as editor:
            for model in [Child1, Parent, Child2, GenericChild, Child3, CodedGenericChild]:
                editor.create_model(model)

        update_contenttypes(apps.get_app_config('writable_serializers'))
def create_realtime_rest_group(apps, schema_editor):
    """Populate Groups for realtime group.

    :param apps: App registry.
    :type apps: django.apps.apps

    :param schema_editor: Django db abstraction for turning model into db.
    :type schema_editor: django.db.backends.schema
    """
    # import apps registry, somehow it was only loaded fully from import
    realtime_app_config = apps_registry.get_app_config('realtime')
    # update content types
    update_contenttypes(realtime_app_config, interactive=False)
    # update permissions
    create_permissions(realtime_app_config, interactive=False)
    Group = apps.get_model('auth', 'Group')
    try:
        realtime_group = Group.objects.get(name=REST_GROUP)
    except Group.DoesNotExist:
        realtime_group = Group.objects.create(name=REST_GROUP)

    Permission = apps.get_model('auth', 'Permission')
    realtime_permissions = Permission.objects.filter(
        content_type__app_label='realtime')
    realtime_group.permissions.add(*realtime_permissions)
    realtime_group.save()
Beispiel #6
0
def create_realtime_rest_group(apps, schema_editor):
    """Populate Groups for realtime group.

    :param apps: App registry.
    :type apps: django.apps.apps

    :param schema_editor: Django db abstraction for turning model into db.
    :type schema_editor: django.db.backends.schema
    """
    # import apps registry, somehow it was only loaded fully from import
    realtime_app_config = apps_registry.get_app_config('realtime')
    # update content types
    update_contenttypes(realtime_app_config, interactive=False)
    # update permissions
    create_permissions(realtime_app_config, interactive=False)
    Group = apps.get_model('auth', 'Group')
    try:
        realtime_group = Group.objects.get(name=REST_GROUP)
    except Group.DoesNotExist:
        realtime_group = Group.objects.create(name=REST_GROUP)

    Permission = apps.get_model('auth', 'Permission')
    realtime_permissions = Permission.objects.filter(
        content_type__app_label='realtime')
    realtime_group.permissions.add(*realtime_permissions)
    realtime_group.save()
    def forwards(self, orm):
        db.start_transaction()
        cursor = connection.cursor()
        cursor.execute('select * from zorna_forms_form')
        qs = cursor.fetchall()

        # Adding model 'FormsFormActionUrl'
        db.create_table('zorna_forms_form_action_urls', (
            ('id',
             self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('form', self.gf('django.db.models.fields.related.ForeignKey')(
                to=orm['forms.FormsForm'], null=True)),
            ('url',
             self.gf('django.db.models.fields.CharField')(max_length=255)),
        ))
        db.send_create_signal('forms', ['FormsFormActionUrl'])

        # Adding model 'FormsFormAction'
        db.create_table('zorna_forms_form_actions', (
            ('id',
             self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('form', self.gf('django.db.models.fields.related.ForeignKey')(
                to=orm['forms.FormsForm'], null=True)),
            ('content_type',
             self.gf('django.db.models.fields.related.ForeignKey')(
                 to=orm['contenttypes.ContentType'])),
            ('object_id', self.gf('django.db.models.fields.IntegerField')()),
        ))
        db.send_create_signal('forms', ['FormsFormAction'])

        # Adding model 'FormsFormActionMessage'
        db.create_table('zorna_forms_form_action_messages', (
            ('id',
             self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('form', self.gf('django.db.models.fields.related.ForeignKey')(
                to=orm['forms.FormsForm'], null=True)),
            ('message', self.gf('django.db.models.fields.TextField')()),
        ))
        db.send_create_signal('forms', ['FormsFormActionMessage'])

        # Deleting field 'FormsForm.response'
        db.delete_column('zorna_forms_form', 'response')

        from django.contrib.contenttypes.management import update_contenttypes
        from django.db.models import get_app, get_models
        update_contenttypes(get_app('forms'), get_models())
        if not db.dry_run:
            db.commit_transaction()
            db.start_transaction()
            ct = orm['contenttypes.ContentType'].objects.get(
                app_label="forms", model="formsformactionmessage")
            for form in qs:
                rep = orm.FormsFormActionMessage(message=form[13],
                                                 form_id=form[0])
                rep.save()
                orm.FormsFormAction.objects.create(form_id=form[0],
                                                   object_id=rep.pk,
                                                   content_type=ct)
        db.commit_transaction()
    def forwards(self, orm):
        "Write your forwards methods here."
        # Note: Don't use "from appname.models import ModelName". 
        # Use orm.ModelName to refer to models in this application,
        # and orm['appname.ModelName'] for models in other applications.

        update_contenttypes(get_app('activities'), get_models())
        create_permissions(get_app('activities'), get_models(), 0)
Beispiel #9
0
 def test_interactive_false(self):
     """
     non-interactive mode of update_contenttypes() shouldn't delete stale
     content types.
     """
     management.update_contenttypes(self.app_config, interactive=False)
     self.assertIn("Stale content types remain.", sys.stdout.getvalue())
     self.assertEqual(ContentType.objects.count(), self.before_count + 1)
Beispiel #10
0
    def forwards(self, orm):
        "Write your forwards methods here."
        # Note: Don't use "from appname.models import ModelName".
        # Use orm.ModelName to refer to models in this application,
        # and orm['appname.ModelName'] for models in other applications.

        update_contenttypes(get_app('activities'), get_models())
        create_permissions(get_app('activities'), get_models(), 0)
Beispiel #11
0
 def test_interactive_false(self):
     """
     non-interactive mode of update_contenttypes() shouldn't delete stale
     content types.
     """
     management.update_contenttypes(self.app_config, interactive=False)
     self.assertIn("Stale content types remain.", sys.stdout.getvalue())
     self.assertEqual(ContentType.objects.count(), self.before_count + 1)
Beispiel #12
0
    def handle_noargs(self, **options):
        app = get_app('services')
        try:
            boris_config = apps.get_app_config('boris')
        except:
            raise EnvironmentError(
                'Cannot find app `boris`. App configs are: %s' %
                apps.get_app_configs())
        update_contenttypes(boris_config, 2, interactive=False)
        app_models = models.get_models(app)
        # This will hold the permissions we're looking for as
        # (content_type, (codename, name))
        searched_perms = list()
        # The codenames and ctypes that should exist.
        ctypes = set()
        for model in app_models:
            opts = model._meta
            # We can't use `get_for_model` here since it doesn't return
            # the correct `ContentType` for proxy models.
            # see https://code.djangoproject.com/ticket/17648
            app_label, model = opts.app_label, opts.object_name.lower()
            if app_label == 'services' and model == 'encounter':
                ctype = ContentType.objects.get_by_natural_key(
                    app_label, model)
                ctypes.add(ctype)
                for perm in _get_all_permissions(opts, model):
                    searched_perms.append((ctype, perm))

        # Find all the Permissions that have a content_type for a model we're
        # looking for. We don't need to check for codenames since we already have
        # a list of the ones we're going to create.
        all_perms = set(
            Permission.objects.filter(content_type__in=ctypes, ).values_list(
                "content_type", "codename"))

        group, created = Group.objects.get_or_create(name=u'Terén')
        print 'group: %s' % group
        if created:
            print 'ERROR: skupina Teren neexistovala!'
            return
        for ctype, (codename, name) in searched_perms:
            if (ctype.pk, codename) not in all_perms:
                Permission.objects.filter(codename=codename,
                                          name=name).delete()
                perm = Permission.objects.create(codename=codename,
                                                 name=name,
                                                 content_type=ctype)
                group.permissions.add(perm)
                sys.stdout.write("Adding encounter permission '%s'" % perm)

        for perm in Permission.objects.filter(
                codename__endswith='_groupcontact'):
            group.permissions.add(perm)
            sys.stdout.write("Adding group encounter permission '%s'" % perm)

        for perm in Permission.objects.filter(codename__endswith='_encounter'):
            group.permissions.add(perm)
            sys.stdout.write("Adding service permission '%s'" % perm)
Beispiel #13
0
 def test_interactive_true(self):
     """
     interactive mode of update_contenttypes() (the default) should delete
     stale contenttypes.
     """
     management.input = lambda x: force_str("yes")
     management.update_contenttypes(self.app_config)
     self.assertIn("Deleting stale content type", sys.stdout.getvalue())
     self.assertEqual(ContentType.objects.count(), self.before_count)
Beispiel #14
0
 def test_unavailable_content_type_model(self):
     """
     #24075 - A ContentType shouldn't be created or deleted if the model
     isn't available.
     """
     apps = Apps()
     with self.assertNumQueries(0):
         contenttypes_management.update_contenttypes(self.app_config, interactive=False, verbosity=0, apps=apps)
     self.assertEqual(ContentType.objects.count(), self.before_count + 1)
Beispiel #15
0
 def test_interactive_true(self):
     """
     interactive mode of update_contenttypes() (the default) should delete
     stale contenttypes.
     """
     management.input = lambda x: force_str("yes")
     management.update_contenttypes(self.app_config)
     self.assertIn("Deleting stale content type", sys.stdout.getvalue())
     self.assertEqual(ContentType.objects.count(), self.before_count)
Beispiel #16
0
 def test_unavailable_content_type_model(self):
     """
     #24075 - A ContentType shouldn't be created or deleted if the model
     isn't available.
     """
     apps = Apps()
     with self.assertNumQueries(0):
         contenttypes_management.update_contenttypes(self.app_config, interactive=False, verbosity=0, apps=apps)
     self.assertEqual(ContentType.objects.count(), self.before_count + 1)
Beispiel #17
0
 def test_interactive_true_without_dependent_objects(self):
     """
     interactive mode of update_contenttypes() (the default) should delete
     stale contenttypes and inform there are no dependent objects
     """
     contenttypes_management.input = lambda x: force_str("yes")
     with captured_stdout() as stdout:
         contenttypes_management.update_contenttypes(self.app_config)
     self.assertIn("Deleting stale content type", stdout.getvalue())
     self.assertEqual(ContentType.objects.count(), self.before_count)
Beispiel #18
0
 def test_interactive_true_without_dependent_objects(self):
     """
     interactive mode of update_contenttypes() (the default) should delete
     stale contenttypes even if there aren't any dependent objects.
     """
     contenttypes_management.input = lambda x: force_str("yes")
     with captured_stdout() as stdout:
         contenttypes_management.update_contenttypes(self.app_config)
     self.assertIn("Deleting stale content type", stdout.getvalue())
     self.assertEqual(ContentType.objects.count(), self.before_count)
Beispiel #19
0
def cms_editor(apps, schema_editor):
    from django.contrib.contenttypes.management import update_contenttypes
    from django.apps import apps as configured_apps
    for app in configured_apps.get_app_configs():
        update_contenttypes(app, interactive=True, verbosity=0)

    from django.contrib.auth.management import create_permissions
    for app in configured_apps.get_app_configs():
        create_permissions(app, verbosity=0)

    create_perms(apps)
Beispiel #20
0
def add_episodes_to_codes(apps, schema_editor):
    app_config = apps.get_app_config("activities")
    app_config.models_module = app_config.models_module or True
    ContentType = apps.get_model('contenttypes', 'ContentType')
    Collection = apps.get_model('niqati', 'Collection')
    update_contenttypes(app_config)
    episode_content_type = ContentType.objects.get(model="episode")

    for collection in Collection.objects.filter(codes__isnull=False).iterator():
        episode = collection.order.episode
        collection.codes.update(object_id=episode.pk,
                                content_type=episode_content_type)
Beispiel #21
0
    def handle_noargs(self, **options):
        app = get_app('services')
        try:
            boris_config = apps.get_app_config('boris')
        except:
            raise EnvironmentError('Cannot find app `boris`. App configs are: %s' % apps.get_app_configs())
        update_contenttypes(boris_config, 2, interactive=False)
        app_models = models.get_models(app)
        # This will hold the permissions we're looking for as
        # (content_type, (codename, name))
        searched_perms = list()
        # The codenames and ctypes that should exist.
        ctypes = set()
        for model in app_models:
            opts = model._meta
            # We can't use `get_for_model` here since it doesn't return
            # the correct `ContentType` for proxy models.
            # see https://code.djangoproject.com/ticket/17648
            app_label, model = opts.app_label, opts.object_name.lower()
            if app_label == 'services' and model == 'encounter':
                ctype = ContentType.objects.get_by_natural_key(app_label, model)
                ctypes.add(ctype)
                for perm in _get_all_permissions(opts, model):
                    searched_perms.append((ctype, perm))

        # Find all the Permissions that have a content_type for a model we're
        # looking for. We don't need to check for codenames since we already have
        # a list of the ones we're going to create.
        all_perms = set(Permission.objects.filter(
            content_type__in=ctypes,
        ).values_list(
            "content_type", "codename"
        ))

        group, created = Group.objects.get_or_create(name=u'Terén')
        print 'group: %s' % group
        if created:
            print 'ERROR: skupina Teren neexistovala!'
            return
        for ctype, (codename, name) in searched_perms:
            if (ctype.pk, codename) not in all_perms:
                Permission.objects.filter(codename=codename, name=name).delete()
                perm = Permission.objects.create(codename=codename, name=name, content_type=ctype)
                group.permissions.add(perm)
                sys.stdout.write("Adding encounter permission '%s'" % perm)

        for perm in Permission.objects.filter(codename__endswith='_groupcontact'):
            group.permissions.add(perm)
            sys.stdout.write("Adding group encounter permission '%s'" % perm)

        for perm in Permission.objects.filter(codename__endswith='_encounter'):
            group.permissions.add(perm)
            sys.stdout.write("Adding service permission '%s'" % perm)
def create_realtime_rest_group(apps, schema_editor):
    """Populate Groups for volcano ash realtime group.

    :param apps: App registry.
    :type apps: django.apps.apps

    :param schema_editor: Django db abstraction for turning model into db.
    :type schema_editor: django.db.backends.schema
    """
    # import apps registry, somehow it was only loaded fully from import
    realtime_app_config = apps_registry.get_app_config('realtime')
    # update content types
    update_contenttypes(realtime_app_config, interactive=False)
    # update permissions
    create_permissions(realtime_app_config, interactive=False)
    Group = apps.get_model('auth', 'Group')
    group_list = [
        (REST_GROUP, [
            'ash',
            'floodreport',
            'earthquakereport',
            'impacteventboundary',
            'flood',
            'userpush',
            'boundary',
            'boundaryalias',
            'floodeventboundary',
            'earthquake',
            'volcano',
            'ashreport'
        ]),
        (VOLCANO_GROUP, [
            'volcano',
        ]),
        (ASH_GROUP, [
            'ash',
            'ashreport',
        ]),
    ]

    for g in group_list:
        try:
            realtime_group = Group.objects.get(name=g[0])
        except Group.DoesNotExist:
            realtime_group = Group.objects.create(name=g[0])

        Permission = apps.get_model('auth', 'Permission')
        for m in g[1]:
            realtime_permissions = Permission.objects.filter(
                content_type__app_label='realtime',
                content_type__model=m)
            realtime_group.permissions.add(*realtime_permissions)
            realtime_group.save()
 def forwards(self, orm):
     contenttypes = orm['contenttypes.ContentType'].objects
     removed = set()
     for ct in contenttypes.filter(model__endswith='_hist')\
                           .exclude(name__endswith=' history'):
         removed.add(ct.app_label)
         Permission.objects.filter(content_type=ct).delete()
         ct.delete()
     for app_label in removed:
         app = get_app(app_label)
         update_contenttypes(app, None)
         create_permissions(app, None, 2)
 def forwards(self, orm):
     contenttypes = orm['contenttypes.ContentType'].objects
     removed = set()
     for ct in contenttypes.filter(model__endswith='_hist')\
                           .exclude(name__endswith=' history'):
         removed.add(ct.app_label)
         Permission.objects.filter(content_type=ct).delete()
         ct.delete()
     for app_label in removed:
         app = get_app(app_label)
         update_contenttypes(app, None)
         create_permissions(app, None, 2)
Beispiel #25
0
 def test_interactive_true_with_dependent_objects(self):
     """
     interactive mode of update_contenttypes() (the default) should delete
     stale contenttypes and warn of dependent objects
     """
     Post.objects.create(title='post', content_type=self.content_type)
     contenttypes_management.input = lambda x: force_str("yes")
     with captured_stdout() as stdout:
         contenttypes_management.update_contenttypes(self.app_config)
     self.assertEqual(Post.objects.count(), 0)
     self.assertIn("1 object of type contenttypes_tests.post:", stdout.getvalue())
     self.assertIn("Deleting stale content type", stdout.getvalue())
     self.assertEqual(ContentType.objects.count(), self.before_count)
    def handle(self, *args, **options):
        if not args:
            apps = []
            for model in get_models():
                apps.append(get_app(model._meta.app_label))
        else:
            apps = []
            for arg in args:
                apps.append(get_app(arg))

        for app in apps:
            update_contenttypes(app, None, options.get('verbosity', 2), interactive=True)
            create_permissions(app, get_models(), options.get('verbosity', 0))
Beispiel #27
0
def remove_planned_disbursements(apps, schema_editor):
    """
    add budget status 1 as its used as default in iati migration 0036
    """
    update_contenttypes(apps.get_app_config('iati'),
                        interactive=False)  # make sure all content types exist

    try:  # don't run on first migration
        PlannedDisbursement = apps.get_model('iati', 'PlannedDisbursement')
    except:
        return

    PlannedDisbursement.objects.all().delete()
 def forwards(self, orm):
     "Write your forwards methods here."
     # We are forcing contenttypes to update themselves instead of waiting on
     # the django signal post_syncdb because we need them now. Otherwise this
     # fails and the migration fails
     update_contenttypes(hwdoc.models, None)
     ct = orm['contenttypes.ContentType'].objects.get(
         name='Equipment', model='equipment', app_label='hwdoc') # model must be lowercase!
     try:
         perm = orm['auth.permission'].objects.get(
             content_type=ct, codename='can_change_comment')
     except Exception:
         orm['auth.permission'].objects.create(
             content_type=ct, codename='can_change_comment', name=u'Can change comments')
Beispiel #29
0
def create_proxy_permissions(app, created_models, verbosity, **kwargs):
    """
        Creates permissions for proxy models which are not created automatically
        by `django.contrib.auth.management.create_permissions`.
        see https://code.djangoproject.com/ticket/11154
        This method is inspired by `django.contrib.auth.managment.create_permissions`.

        Since we can't rely on `get_for_model' we must fallback to `get_by_natural_key`.
        However, this method doesn't automatically create missing `ContentType` so
        we must ensure all the model's `ContentType` are created before running this method.
        We do so by unregistering the `update_contenttypes` `post_syncdb` signal and calling
        it in here just before doing everything.
    """
    update_contenttypes(app, created_models, verbosity, **kwargs)
    app_models = models.get_models(app)
    # This will hold the permissions we're looking for as
    # (content_type, (codename, name))
    searched_perms = list()
    # The codenames and ctypes that should exist.
    ctypes = set()
    for model in app_models:
        opts = model._meta
        if opts.proxy:
            # We can't use `get_for_model` here since it doesn't return
            # the correct `ContentType` for proxy models.
            # see https://code.djangoproject.com/ticket/17648
            app_label, model = opts.app_label, opts.object_name.lower()
            ctype = ContentType.objects.get_by_natural_key(app_label, model)
            ctypes.add(ctype)
            for perm in _get_all_permissions(opts):
                searched_perms.append((ctype, perm))

    # Find all the Permissions that have a content_type for a model we're
    # looking for. We don't need to check for codenames since we already have
    # a list of the ones we're going to create.
    all_perms = set(Permission.objects.filter(
        content_type__in=ctypes,
    ).values_list(
        "content_type", "codename"
    ))

    objs = [
    Permission(codename=codename, name=name, content_type=ctype)
    for ctype, (codename, name) in searched_perms
    if (ctype.pk, codename) not in all_perms
    ]
    Permission.objects.bulk_create(objs)
    if verbosity >= 2:
        for obj in objs:
            sys.stdout.write("Adding permission '%s'" % obj)
def ensure_view_permission(app, created_models, **kwargs):
    update_contenttypes(app, created_models, **kwargs)  # This is normally called by post_syncdb, but we cannot guarantee ordering so we call it here

    for m in created_models:
        content_type = ContentType.objects.get_for_model(m)
        meta = m._meta
        obj_name = meta.object_name.lower()

        perm, created = Permission.objects.get_or_create(
            name="Can view %s" % obj_name,
            content_type=content_type,
            codename="view_%s" % obj_name)

        if created:
            print "Added view_%s permission" % obj_name
Beispiel #31
0
    def setUp(self):
        # Create the schema for our test model
        self._style = no_style()
        sql, _ = connection.creation.sql_create_model(self.model, self._style)

        with connection.cursor() as c:
            for statement in sql:
                c.execute(statement)

        content_model = self.content_model if hasattr(self, 'content_model') else self.model
        app_config = apps.get_app_config(content_model._meta.app_label)
        update_contenttypes(app_config, verbosity=1, interactive=False)
        create_permissions(app_config, verbosity=1, interactive=False)

        super(RuntimeModel, self).setUp()
Beispiel #32
0
def create_proxy_permissions(app_config, **kwargs):
    update_contenttypes(app_config, kwargs)
    app_models = app_config.get_models()
    for model in app_models:
        opts = model._meta
        if opts.proxy:
            proxy_model = opts.proxy_for_model
            proxy_opts = proxy_model._meta
            proxy_app_label, proxy_model_name = proxy_opts.app_label, proxy_opts.object_name.lower()
            proxy_ctype = ContentType.objects.get_by_natural_key(proxy_app_label, proxy_model_name)
            app_label, model_name = opts.app_label, opts.object_name.lower()
            ctype = ContentType.objects.get_by_natural_key(app_label, model_name)
            for name, codename in proxy_ctype.permission_set.all().values_list('name', 'codename'):
                if not ctype.permission_set.filter(content_type=ctype, codename=codename):
                    Permission(name=name, content_type=ctype, codename=codename).save()
Beispiel #33
0
def add_budget_status(apps, schema_editor):
    """
    add budget status 1 as its used as default in iati migration 0036
    """
    update_contenttypes(apps.get_app_config('iati'), interactive=False) # make sure all content types exist

    try: # don't run on first migration
        BudgetStatus = apps.get_model('iati_codelists', 'BudgetStatus')
    except:
        return

    if not BudgetStatus.objects.filter(code='1').exists(): 
        BudgetStatus(
            code='1', 
            name='Indicative', 
            description='A non-binding estimate for the described budget.').save()
    def handle(self, *args, **options):
        if not args:
            apps = []
            for model in get_models():
                apps.append(get_app(model._meta.app_label))
        else:
            apps = []
            for arg in args:
                apps.append(get_app(arg))

        for app in apps:
            update_contenttypes(app,
                                None,
                                options.get('verbosity', 2),
                                interactive=True)
            create_permissions(app, get_models(), options.get('verbosity', 0))
 def forwards(self, orm):
     "Write your forwards methods here."
     # We are forcing contenttypes to update themselves instead of waiting on
     # the django signal post_syncdb because we need them now. Otherwise this
     # fails and the migration fails
     update_contenttypes(hwdoc.models, None)
     ct = orm['contenttypes.ContentType'].objects.get(
         name='Equipment', model='equipment',
         app_label='hwdoc')  # model must be lowercase!
     try:
         perm = orm['auth.permission'].objects.get(
             content_type=ct, codename='can_change_comment')
     except Exception:
         orm['auth.permission'].objects.create(
             content_type=ct,
             codename='can_change_comment',
             name=u'Can change comments')
Beispiel #36
0
def ensure_view_permission(app, created_models, **kwargs):
    update_contenttypes(
        app, created_models, **kwargs
    )  # This is normally called by post_syncdb, but we cannot guarantee ordering so we call it here

    for m in created_models:
        content_type = ContentType.objects.get_for_model(m)
        meta = m._meta
        obj_name = meta.object_name.lower()

        perm, created = Permission.objects.get_or_create(
            name="Can view %s" % obj_name,
            content_type=content_type,
            codename="view_%s" % obj_name)

        if created:
            print "Added view_%s permission" % obj_name
Beispiel #37
0
def create_proxy_permissions(app, created_models, verbosity, **kwargs):
    """
        Creates permissions for proxy models which are not created automatically
        by `django.contrib.auth.management.create_permissions`.
        see https://code.djangoproject.com/ticket/11154
        This method is inspired by `django.contrib.auth.managment.create_permissions`.

        Since we can't rely on `get_for_model' we must fallback to `get_by_natural_key`.
        However, this method doesn't automatically create missing `ContentType` so
        we must ensure all the model's `ContentType` are created before running this method.
        We do so by unregistering the `update_contenttypes` `post_syncdb` signal and calling
        it in here just before doing everything.
    """
    update_contenttypes(app, created_models, verbosity, **kwargs)
    app_models = models.get_models(app)
    # This will hold the permissions we're looking for as
    # (content_type, (codename, name))
    searched_perms = list()
    # The codenames and ctypes that should exist.
    ctypes = set()
    for model in app_models:
        opts = model._meta
        if opts.proxy:
            # We can't use `get_for_model` here since it doesn't return
            # the correct `ContentType` for proxy models.
            # see https://code.djangoproject.com/ticket/17648
            app_label, model = opts.app_label, opts.object_name.lower()
            ctype = ContentType.objects.get_by_natural_key(app_label, model)
            ctypes.add(ctype)
            for perm in _get_all_permissions(opts):
                searched_perms.append((ctype, perm))

    # Find all the Permissions that have a content_type for a model we're
    # looking for. We don't need to check for codenames since we already have
    # a list of the ones we're going to create.
    all_perms = set(Permission.objects.filter(
        content_type__in=ctypes,
    ).values_list(
        "content_type", "codename"
    ))
    for ctype, (codename, name) in searched_perms:
        if (ctype.pk, codename) not in all_perms:
            Permission.objects.filter(codename=codename, name=name).delete()
            perm = Permission.objects.create(codename=codename, name=name, content_type=ctype)
            if verbosity >= 2:
                sys.stdout.write("Adding permission '%s'" % perm)
def set_attachment_sort_order(apps, schema_editor):
    Review = apps.get_model('demotime', 'Review')
    Attachment = apps.get_model('demotime', 'Attachment')
    ContentType = apps.get_model('contenttypes', 'ContentType')
    app = apps.get_app_config('demotime')
    app.models_module = app.models_module or True
    update_contenttypes(app)
    content_type = ContentType.objects.get(model='reviewrevision',
                                           app_label='demotime')
    for review in Review.objects.all():
        for revision in review.reviewrevision_set.all():
            attachments = Attachment.objects.filter(
                content_type=content_type,
                object_id=revision.pk).order_by('pk')
            for count, attachment in enumerate(attachments, start=1):
                attachment.sort_order = count
                attachment.save(update_fields=['sort_order'])
    def forwards(self, orm):
        "Write your forwards methods here."
        # Note: Don't use "from appname.models import ModelName".
        # Use orm.ModelName to refer to models in this application,
        # and orm['appname.ModelName'] for models in other applications.
        update_contenttypes(get_app('notes'), get_models())
        create_permissions(get_app('notes'), get_models(), 0)

        students = orm['auth.Group'].objects.get(name="students")
        students.permissions.add(orm['auth.Permission'].objects.get(content_type__app_label="notes", codename="add_note"))
        students.permissions.add(orm['auth.Permission'].objects.get(content_type__app_label="notes", codename="change_note"))
        students.permissions.add(orm['auth.Permission'].objects.get(content_type__app_label="notes", codename="delete_note"))
        students.save()

        professors = orm['auth.Group'].objects.get(name="professors")
        professors.permissions.add(orm['auth.Permission'].objects.get(content_type__app_label="core", codename="add_course"))
        professors.save()
Beispiel #40
0
    def remove_organisations(apps, schema_editor):
        """
        convert all transaction's xdr values
        takes ~20 mins
        """
        update_contenttypes(
            apps.get_app_config('iati'),
            interactive=False)  # make sure all content types exist

        try:  # don't run on first migration
            ActivityParticipatingOrganisation = apps.get_model(
                'iati', 'ActivityParticipatingOrganisation')
            ActivityParticipatingOrganisation.objects.filter(
                organisation__isnull=False).update(organisation=None)
        except:
            pass

        try:
            ActivityReportingOrganisation = apps.get_model(
                'iati', 'ActivityReportingOrganisation')
            ActivityReportingOrganisation.objects.filter(
                organisation__isnull=False).update(organisation=None)
        except:
            pass

        try:
            TransactionReceiver = apps.get_model('iati', 'TransactionReceiver')
            TransactionReceiver.objects.filter(
                organisation__isnull=False).update(organisation=None)
        except:
            pass

        try:
            TransactionProvider = apps.get_model('iati', 'TransactionProvider')
            TransactionProvider.objects.filter(
                organisation__isnull=False).update(organisation=None)
        except:
            pass

        try:  # don't run on first migration
            Organisation = apps.get_model('iati_organisation', 'Organisation')
            Organisation.objects.all().delete()
        except:
            pass

        return
    def forwards(self, orm):
        db.start_transaction()
        cursor = connection.cursor()
        cursor.execute('select * from zorna_forms_form')
        qs = cursor.fetchall()
        
        # Adding model 'FormsFormActionUrl'
        db.create_table('zorna_forms_form_action_urls', (
            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('form', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['forms.FormsForm'], null=True)),
            ('url', self.gf('django.db.models.fields.CharField')(max_length=255)),
        ))
        db.send_create_signal('forms', ['FormsFormActionUrl'])

        # Adding model 'FormsFormAction'
        db.create_table('zorna_forms_form_actions', (
            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('form', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['forms.FormsForm'], null=True)),
            ('content_type', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['contenttypes.ContentType'])),
            ('object_id', self.gf('django.db.models.fields.IntegerField')()),
        ))
        db.send_create_signal('forms', ['FormsFormAction'])

        # Adding model 'FormsFormActionMessage'
        db.create_table('zorna_forms_form_action_messages', (
            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('form', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['forms.FormsForm'], null=True)),
            ('message', self.gf('django.db.models.fields.TextField')()),
        ))
        db.send_create_signal('forms', ['FormsFormActionMessage'])

        # Deleting field 'FormsForm.response'
        db.delete_column('zorna_forms_form', 'response')
        
        from django.contrib.contenttypes.management import update_contenttypes
        from django.db.models import get_app, get_models        
        update_contenttypes(get_app('forms'), get_models())
        if not db.dry_run:
            db.commit_transaction()
            db.start_transaction()
            ct = orm['contenttypes.ContentType'].objects.get(app_label="forms", model="formsformactionmessage")
            for form in qs:
                rep = orm.FormsFormActionMessage(message=form[13], form_id=form[0])
                rep.save()
                orm.FormsFormAction.objects.create(form_id=form[0], object_id=rep.pk, content_type=ct)
        db.commit_transaction()
Beispiel #42
0
def permissions(apps, schema_editor):
    try:
        # Django <= 1.10
        from django.contrib.contenttypes.management import update_contenttypes
    except ImportError:
        # Django 1.11 and over
        from django.contrib.contenttypes.management import \
            create_contenttypes as update_contenttypes

    from django.apps import apps as configured_apps
    for app in configured_apps.get_app_configs():
        update_contenttypes(app, interactive=True, verbosity=0)

    from django.contrib.auth.management import create_permissions
    for app in configured_apps.get_app_configs():
        create_permissions(app, verbosity=0)

    create_perms(apps)
Beispiel #43
0
def get_name_from_narrative(apps, schema_editor):
    update_contenttypes(apps.get_app_config('iati'), interactive=False) # make sure all content types exist

    try: # don't run on first migration
        APOContentType = ContentType.objects.get(model='activityparticipatingorganisation')
        APO = apps.get_model('iati', 'ActivityParticipatingOrganisation')
    except:
        return

    Narrative = apps.get_model('iati', 'Narrative')
    for item in APO.objects.all():
        narrative = Narrative.objects.filter(related_content_type_id=APOContentType.id, related_object_id=item.id)
        if narrative and narrative[0] and narrative[0].content:
            item.primary_name = narrative[0].content
            item.save()
        else: 
            item.primary_name = item.normalized_ref
            item.save()
Beispiel #44
0
    def create_rest_group(self):
        """Helper file for unittests to generate REST Group."""
        # update content types
        update_contenttypes(self, interactive=False)
        # update permissions
        create_permissions(self, interactive=False)
        Group = apps.get_model('auth', 'Group')
        group_list = [
            (REST_GROUP, [
                'ash',
                'floodreport',
                'earthquakereport',
                'impacteventboundary',
                'flood',
                'userpush',
                'boundary',
                'boundaryalias',
                'floodeventboundary',
                'earthquake',
                'volcano',
                'ashreport'
            ]),
            (VOLCANO_GROUP, [
                'volcano',
            ]),
            (ASH_GROUP, [
                'ash',
                'ashreport',
            ]),
        ]
        for g in group_list:
            try:
                realtime_group = Group.objects.get(name=g[0])
            except Group.DoesNotExist:
                realtime_group = Group.objects.create(name=g[0])

            Permission = apps.get_model('auth', 'Permission')
            for m in g[1]:
                realtime_permissions = Permission.objects.filter(
                    content_type__app_label='realtime',
                    content_type__model=m)

                realtime_group.permissions.add(*realtime_permissions)
                realtime_group.save()
Beispiel #45
0
 def test_interactive_true_with_dependent_objects(self):
     """
     interactive mode of update_contenttypes() (the default) should delete
     stale contenttypes and warn of dependent objects.
     """
     post = Post.objects.create(title='post', content_type=self.content_type)
     # A related object is needed to show that a custom collector with
     # can_fast_delete=False is needed.
     ModelWithNullFKToSite.objects.create(post=post)
     contenttypes_management.input = lambda x: force_str("yes")
     with captured_stdout() as stdout:
         contenttypes_management.update_contenttypes(self.app_config)
     self.assertEqual(Post.objects.count(), 0)
     output = stdout.getvalue()
     self.assertIn('- Content type for contenttypes_tests.Fake', output)
     self.assertIn('- 1 contenttypes_tests.Post object(s)', output)
     self.assertIn('- 1 contenttypes_tests.ModelWithNullFKToSite', output)
     self.assertIn('Deleting stale content type', output)
     self.assertEqual(ContentType.objects.count(), self.before_count)
Beispiel #46
0
 def test_interactive_true_with_dependent_objects(self):
     """
     interactive mode of update_contenttypes() (the default) should delete
     stale contenttypes and warn of dependent objects.
     """
     post = Post.objects.create(title="post", content_type=self.content_type)
     # A related object is needed to show that a custom collector with
     # can_fast_delete=False is needed.
     ModelWithNullFKToSite.objects.create(post=post)
     contenttypes_management.input = lambda x: force_str("yes")
     with captured_stdout() as stdout:
         contenttypes_management.update_contenttypes(self.app_config)
     self.assertEqual(Post.objects.count(), 0)
     output = stdout.getvalue()
     self.assertIn("- Content type for contenttypes_tests.Fake", output)
     self.assertIn("- 1 contenttypes_tests.Post object(s)", output)
     self.assertIn("- 1 contenttypes_tests.ModelWithNullFKToSite", output)
     self.assertIn("Deleting stale content type", output)
     self.assertEqual(ContentType.objects.count(), self.before_count)
def create_realtime_rest_group(apps, schema_editor):
    """Populate Groups for volcano ash realtime group.

    :param apps: App registry.
    :type apps: django.apps.apps

    :param schema_editor: Django db abstraction for turning model into db.
    :type schema_editor: django.db.backends.schema
    """
    # import apps registry, somehow it was only loaded fully from import
    realtime_app_config = apps_registry.get_app_config('realtime')
    # update content types
    update_contenttypes(realtime_app_config, interactive=False)
    # update permissions
    create_permissions(realtime_app_config, interactive=False)
    Group = apps.get_model('auth', 'Group')
    group_list = [
        (REST_GROUP, [
            'ash', 'floodreport', 'earthquakereport', 'impacteventboundary',
            'flood', 'userpush', 'boundary', 'boundaryalias',
            'floodeventboundary', 'earthquake', 'volcano', 'ashreport'
        ]),
        (VOLCANO_GROUP, [
            'volcano',
        ]),
        (ASH_GROUP, [
            'ash',
            'ashreport',
        ]),
    ]

    for g in group_list:
        try:
            realtime_group = Group.objects.get(name=g[0])
        except Group.DoesNotExist:
            realtime_group = Group.objects.create(name=g[0])

        Permission = apps.get_model('auth', 'Permission')
        for m in g[1]:
            realtime_permissions = Permission.objects.filter(
                content_type__app_label='realtime', content_type__model=m)
            realtime_group.permissions.add(*realtime_permissions)
            realtime_group.save()
Beispiel #48
0
def convert_xdr_values(apps, schema_editor):
    """
    convert all transaction's xdr values
    takes ~20 mins
    """
    update_contenttypes(apps.get_app_config('iati'),
                        interactive=False)  # make sure all content types exist

    try:  # don't run on first migration
        Budget = apps.get_model('iati', 'Budget')
        Transaction = apps.get_model('iati', 'Transaction')
        Activity = apps.get_model('iati', 'Activity')

        TranasctionSector = apps.get_model('iati', 'TranasctionSector')
        TransactionRecipientCountry = apps.get_model(
            'iati', 'TransactionRecipientCountry')
        TransactionRecipientSector = apps.get_model(
            'iati', 'TransactionRecipientSector')

    except:
        return

    for budget in Budget.objects.all().iterator():
        budget.xdr_value = convert.to_xdr(budget.currency_id,
                                          budget.value_date, budget.value)
        budget.save()

    for transaction in Transaction.objects.all().iterator():
        transaction.xdr_value = convert.to_xdr(transaction.currency_id,
                                               transaction.value_date,
                                               transaction.value)
        transaction.save()

    for activity in Activity.objects.all().iterator():
        TransactionSector.objects.all().filter(
            transaction__activity=activity).delete()
        TransactionRecipientCountry.objects.all().filter(
            transaction__activity=activity).delete()
        TransactionRecipientSector.objects.all().filter(
            transaction__activity=activity).delete()

        post_save.set_sector_transaction(activity)
        post_save.set_country_region_transaction(activity)
Beispiel #49
0
def update_esp_contenttypes(app,
                            created_models,
                            content_type_class=ContentType,
                            verbosity=2,
                            **kwargs):
    """
    Removes any content type model entries in the given app that no longer have
    a matching model class, then creates content types.

    django.contrib.contenttypes.management.update_contenttypes() does this,
    except it requires interactive raw input from the command line in order to
    remove stale content types. So this function copies the deletion
    functionality without the interactivity, and then calls the function.
    See <https://github.com/django/django/blob/30eb916bdb9b6b9fc881dfda919b49d036953a3b/django/contrib/contenttypes/management.py>.

    The content_type_class defaults to ContentType, but allows a frozen ORM to
    be passed in for use during a migration.
    """
    app_models = get_models(app)
    if not app_models:
        return
    # They all have the same app_label, get the first one.
    app_label = app_models[0]._meta.app_label
    app_models = dict(
        (model._meta.object_name.lower(), model) for model in app_models)
    # Get all the content types
    content_types = dict(
        (ct.model, ct)
        for ct in content_type_class.objects.filter(app_label=app_label))
    to_remove = [
        ct for (model_name, ct) in content_types.iteritems()
        if model_name not in app_models
    ]

    if to_remove:
        for ct in to_remove:
            if verbosity >= 2:
                print "Deleting stale content type '%s | %s'" % (ct.app_label,
                                                                 ct.model)
            ct.delete()

    update_contenttypes(app, created_models, verbosity, **kwargs)
    def forwards(self, orm):
        from django.contrib.contenttypes.management import update_contenttypes
        from django.db.models import get_app, get_models

        update_contenttypes(get_app('remozilla'), get_models())
        name = 'Pending open questions'

        bug_type = orm['contenttypes.ContentType'].objects.get(
            app_label='remozilla', model='Bug')

        needinfo_user_ids = orm['remozilla.Bug'].objects.filter(
            budget_needinfo__isnull=False).values_list('budget_needinfo', flat=True)
        action_items = (orm['dashboard.ActionItem'].objects
                        .filter(content_type=bug_type, resolved=False, name=name)
                        .exclude(user__id__in=needinfo_user_ids))

        for action_item in action_items:
            action_item.resolved = True
            action_item.completed = True
            action_item.save()
    def forwards(self, orm):
        # Adding model 'VenueTicket'
        db.create_table(u'attendees_venueticket', (
            (u'ticket_ptr', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['attendees.Ticket'], unique=True, primary_key=True)),
            ('first_name_tmp', self.gf('django.db.models.fields.CharField')(max_length=250, blank=True)),
            ('last_name_tmp', self.gf('django.db.models.fields.CharField')(max_length=250, blank=True)),
            ('organisation', self.gf('django.db.models.fields.CharField')(max_length=100, blank=True)),
            ('shirtsize_tmp', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['attendees.TShirtSize'], null=True, blank=True)),
            ('voucher_tmp', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['attendees.Voucher'], null=True, blank=True)),
        ))
        db.send_create_signal(u'attendees', ['VenueTicket'])

        # Adding model 'SIMCardTicket'
        db.create_table(u'attendees_simcardticket', (
            (u'ticket_ptr', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['attendees.Ticket'], unique=True, primary_key=True)),
            ('first_name_tmp', self.gf('django.db.models.fields.CharField')(max_length=250)),
            ('last_name_tmp', self.gf('django.db.models.fields.CharField')(max_length=250)),
            ('date_of_birth', self.gf('django.db.models.fields.DateField')()),
            ('gender', self.gf('django.db.models.fields.CharField')(max_length=6)),
            ('hotel_name', self.gf('django.db.models.fields.CharField')(max_length=100, blank=True)),
            ('email', self.gf('django.db.models.fields.EmailField')(max_length=75)),
            ('street', self.gf('django.db.models.fields.CharField')(max_length=100)),
            ('zip_code', self.gf('django.db.models.fields.CharField')(max_length=20)),
            ('city', self.gf('django.db.models.fields.CharField')(max_length=100)),
            ('country', self.gf('django.db.models.fields.CharField')(max_length=100)),
            ('phone', self.gf('django.db.models.fields.CharField')(max_length=100)),
            ('sim_id', self.gf('django.db.models.fields.CharField')(max_length=20, blank=True)),
        ))
        db.send_create_signal(u'attendees', ['SIMCardTicket'])

        # Adding model 'SupportTicket'
        db.create_table(u'attendees_supportticket', (
            (u'ticket_ptr', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['attendees.Ticket'], unique=True, primary_key=True)),
        ))
        db.send_create_signal(u'attendees', ['SupportTicket'])


        update_contenttypes(get_app('attendees'), get_models(), interactive=False)
        db.add_column(u'attendees_tickettype', 'content_type',
                      self.gf('django.db.models.fields.related.ForeignKey')(default=orm['contenttypes.ContentType'].objects.get(app_label='attendees', model='venueticket').id, blank=False, to=orm['contenttypes.ContentType']),
                      keep_default=False)
def create_proxy_permissions(app, created_models, verbosity, **kwargs):
	import pdb; pdb.set_trace()
	kwargs.pop("interactive",None)
	update_contenttypes(app, created_models, verbosity, **kwargs)
	app_models = django.db.models.get_models(app)
	# This will hold the permissions we're looking for as
	# (content_type, (codename, name))
	searched_perms = list()
	# The codenames and ctypes that should exist.
	ctypes = set()
	for model in app_models:
		opts = model._meta
		if opts.proxy:
			# We can't use `get_for_model` here since it doesn't return 
			# the correct `ContentType` for proxy models.
			# see https://code.djangoproject.com/ticket/17648
			app_label, model = opts.app_label, opts.object_name.lower()
			ctype = ContentType.objects.get_by_natural_key(app_label, model)
			ctypes.add(ctype)
			for perm in _get_all_permissions(opts, ctypes):
				searched_perms.append((ctype, perm))
	
	# Find all the Permissions that have a content_type for a model we're
	# looking for. We don't need to check for codenames since we already have
	# a list of the ones we're going to create.
	all_perms = set(Permission.objects.filter(
		content_type__in=ctypes,
	).values_list(
		"content_type", "codename"
	))
	
	objs = [
		Permission(codename=codename, name=name, content_type=ctype)
		for ctype, (codename, name) in searched_perms
		if (ctype.pk, codename) not in all_perms
	]
	Permission.objects.bulk_create(objs)
	if verbosity >= 2:
		for obj in objs:
			sys.stdout.write("Adding permission '%s'" % obj)
    def forwards(self, orm):
        from django.utils.timezone import now
        from django.contrib.contenttypes.management import update_contenttypes
        from django.db.models import get_app, get_models

        update_contenttypes(get_app('voting'), get_models())

        poll_type = orm['contenttypes.ContentType'].objects.get(
            app_label='voting', model='Poll')

        # Close old polls
        polls_ids = (orm['voting.Poll'].objects.filter(end__lt=now())
                     .values_list('id', flat=True))
        action_items = orm['dashboard.ActionItem'].objects.filter(
            content_type=poll_type, object_id__in=polls_ids, resolved=False)
        for action_item in action_items:
            user = action_item.user
            poll = orm['voting.Poll'].objects.get(pk=action_item.object_id)
            if orm['voting.Vote'].objects.filter(user=user, poll=poll).exists():
                action_item.completed = True
            action_item.resolved = True
            action_item.save()
    def forwards(self, orm):
        # Adding model 'SiteCategory'
        db.create_table(u'public_project_sitecategory', (
            (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('category', self.gf('django.db.models.fields.CharField')(unique=True, max_length=50)),
            ('intro_text', self.gf('django.db.models.fields.TextField')(null=True, blank=True)),
            ('comments', self.gf('django.db.models.fields.TextField')(blank=True)),
            ('date_added', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)),
        ))
        db.send_create_signal(u'public_project', ['SiteCategory'])

        # Adding M2M table for field documents on 'SiteCategory'
        m2m_table_name = db.shorten_name(u'public_project_sitecategory_documents')
        db.create_table(m2m_table_name, (
            ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
            ('sitecategory', models.ForeignKey(orm[u'public_project.sitecategory'], null=False)),
            ('document', models.ForeignKey(orm[u'public_project.document'], null=False))
        ))
        db.create_unique(m2m_table_name, ['sitecategory_id', 'document_id'])
        
        #Update content types (necessary for migration 0006)
        update_contenttypes(get_app('public_project'), get_models())
        create_permissions(get_app('public_project'), get_models(), 0)
Beispiel #55
0
 def fake_permissions(self):
     ''' Add fake app missing content types and permissions'''
     app = get_app('fake')
     update_contenttypes(app, None, 0)
     create_permissions(app, None, 0)