Пример #1
0
def update_contenttypes(app, created_models, verbosity=2):
    """
    Creates content types for models in the given app, removing any model
    entries that no longer have a matching model class.
    """
    ContentType.objects.clear_cache()
    content_types = list(
        ContentType.objects.filter(app_label=app.__name__.split('.')[-2]))
    app_models = get_models(app)
    if not app_models:
        return
    for klass in app_models:
        opts = klass._meta
        try:
            ct = ContentType.objects.get(app_label=opts.app_label,
                                         model=opts.object_name.lower())
            content_types.remove(ct)
        except ContentType.DoesNotExist:
            ct = ContentType(name=smart_unicode(opts.verbose_name_raw),
                             app_label=opts.app_label,
                             model=opts.object_name.lower())
            ct.save()
            if verbosity >= 2:
                print "Adding content type '%s | %s'" % (ct.app_label,
                                                         ct.model)
    # The presence of any remaining content types means the supplied app has an
    # undefined model and can safely be removed, which cascades to also remove
    # related permissions.
    for ct in content_types:
        if verbosity >= 2:
            print "Deleting stale content type '%s | %s'" % (ct.app_label,
                                                             ct.model)
        ct.delete()
Пример #2
0
def update_contenttypes(app, created_models, verbosity=2, **kwargs):
    """
    Creates content types for models in the given app, removing any model
    entries that no longer have a matching model class.
    """
    db = kwargs['db']
    ContentType.objects.clear_cache()
    content_types = list(ContentType.objects.using(db).filter(app_label=app.__name__.split('.')[-2]))
    app_models = get_models(app)
    if not app_models:
        return
    for klass in app_models:
        opts = klass._meta
        try:
            ct = ContentType.objects.using(db).get(app_label=opts.app_label,
                                                   model=opts.object_name.lower())
            content_types.remove(ct)
        except ContentType.DoesNotExist:
            ct = ContentType(name=smart_unicode(opts.verbose_name_raw),
                app_label=opts.app_label, model=opts.object_name.lower())
            ct.save(using=db)
            if verbosity >= 2:
                print "Adding content type '%s | %s'" % (ct.app_label, ct.model)
    # The presence of any remaining content types means the supplied app has an
    # undefined model and can safely be removed, which cascades to also remove
    # related permissions.
    for ct in content_types:
        if verbosity >= 2:
            print "Deleting stale content type '%s | %s'" % (ct.app_label, ct.model)
        ct.delete()
Пример #3
0
def update_contenttypes(app, created_models, verbosity=2, **kwargs):
    """
    Creates content types for models in the given app, removing any model
    entries that no longer have a matching model class.
    """
    #    print u"johan-----------------------------: 更新contenttypes 表"
    ContentType.objects.clear_cache()
    content_types = list(
        ContentType.objects.filter(
            app_label=app.__name__.split('.')[-2]))  # 得到app中的所有ContentType
    app_models = get_models(app)  # api 应用例子
    if not app_models:
        return
    for klass in app_models:  #-----------------------遍历 app 所有 model        添加新模型对应的 ContentType
        opts = klass._meta
        try:
            ct = ContentType.objects.get(app_label=opts.app_label,
                                         model=opts.object_name.lower())
            content_types.remove(ct)
        except ContentType.DoesNotExist:
            ct = ContentType(name=u"%s" % opts.verbose_name,
                             app_label=opts.app_label,
                             model=opts.object_name.lower())
            ct.save()
            if verbosity >= 2:  # ------------------------是否显示运行信息
                print "Adding content type '%s | %s'......" % (ct.app_label,
                                                               ct.model)
    # The presence of any remaining content types means the supplied app has an
    # undefined model. Confirm that the content type is stale before deletion.
    if content_types:  # ------------------------------去除已被删除model 对应的 content_type
        if kwargs.get('interactive', False):
            content_type_display = '\n'.join([
                '    %s | %s' % (ct.app_label, ct.model)
                for ct in content_types
            ])
            ok_to_delete = raw_input(
                """The following content types are stale and need to be deleted:

%s

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.

    Type 'yes' to continue, or 'no' to cancel: """ % content_type_display)
        else:
            ok_to_delete = False

        if ok_to_delete == 'yes':
            for ct in content_types:
                if verbosity >= 2:
                    print "Deleting stale content type '%s | %s'......" % (
                        ct.app_label, ct.model)
                ct.delete()
        else:
            if verbosity >= 2:
                print "Stale content types remain......"
Пример #4
0
    def test_ignore_delete(self):
        """
        Tests if deleting instance of a model from ignore list
        would not create db entry"""

        c = ContentType(app_label='hello', model='abc')
        c.save()
        old_count = ModelChange.objects.count()
        c.delete()
        new_count = ModelChange.objects.count()
        self.assertEqual(old_count, new_count)
Пример #5
0
def update_contenttypes(app, created_models, verbosity=2, **kwargs):
    """
        Creates content types for models in the given app, removing any model
        entries that no longer have a matching model class.
        """
    ContentType.objects.clear_cache()
    content_types = list(ContentType.objects.filter(app_label=app.__name__.split('.')[-2]))
    app_models = get_model_or_AppOperation_class_from_app(app)
    if not app_models:
        return
    for app_label, model in app_models:
        verbose_name = None
        if issubclass(model, AppOperation) or issubclass(model, AppPage):
            verbose_name = model.verbose_name
        else:
            verbose_name = model._meta.verbose_name
        try:
            ct = ContentType.objects.get(app_label=app_label,
                                         model=model.__name__.lower())
            content_types.remove(ct)
        except ContentType.DoesNotExist:
            ct = ContentType(name=u"%s" % verbose_name,
                             app_label=app_label, model=model.__name__.lower())
            ct.save()
            if verbosity >= 2:
                print("Adding content type '%s | %s'" % (ct.app_label, ct.model))
    # The presence of any remaining content types means the supplied app has an
    # undefined model. Confirm that the content type is stale before deletion.
    if content_types:
        if kwargs.get('interactive', False):
            content_type_display = '\n'.join(['    %s | %s' % (ct.app_label, ct.model) for ct in content_types])
            ok_to_delete = raw_input("""The following content types are stale and need to be deleted:

%s

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.

    Type 'yes' to continue, or 'no' to cancel: """ % content_type_display)
        else:
            ok_to_delete = False

        if ok_to_delete == 'yes':
            for ct in content_types:
                if verbosity >= 2:
                    print("Deleting stale content type '%s | %s'" % (ct.app_label, ct.model))
                ct.delete()
        else:
            if verbosity >= 2:
                print("Stale content types remain.")
Пример #6
0
def update_contenttypes(app, created_models, verbosity=2, **kwargs):
    """
    Creates content types for models in the given app, removing any model
    entries that no longer have a matching model class.
    """
#    print u"johan-----------------------------: 更新contenttypes 表"
    ContentType.objects.clear_cache()
    content_types = list(ContentType.objects.filter(app_label=app.__name__.split('.')[-2])) # 得到app中的所有ContentType
    app_models = get_models(app)    # api 应用例子
    if not app_models:
        return
    for klass in app_models:    #-----------------------遍历 app 所有 model        添加新模型对应的 ContentType
        opts = klass._meta
        try:
            ct = ContentType.objects.get(app_label=opts.app_label,
                                         model=opts.object_name.lower())
            content_types.remove(ct)
        except ContentType.DoesNotExist:
            ct = ContentType(name=u"%s"%opts.verbose_name,
                app_label=opts.app_label, model=opts.object_name.lower())
            ct.save()
            if verbosity >= 2:  # ------------------------是否显示运行信息
                print "Adding content type '%s | %s'......" % (ct.app_label, ct.model)
    # The presence of any remaining content types means the supplied app has an
    # undefined model. Confirm that the content type is stale before deletion.
    if content_types:   # ------------------------------去除已被删除model 对应的 content_type
        if kwargs.get('interactive', False):
            content_type_display = '\n'.join(['    %s | %s' % (ct.app_label, ct.model) for ct in content_types])
            ok_to_delete = raw_input("""The following content types are stale and need to be deleted:

%s

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.

    Type 'yes' to continue, or 'no' to cancel: """ % content_type_display)
        else:
            ok_to_delete = False

        if ok_to_delete == 'yes':
            for ct in content_types:
                if verbosity >= 2:
                    print "Deleting stale content type '%s | %s'......" % (ct.app_label, ct.model)
                ct.delete()
        else:
            if verbosity >= 2:
                print "Stale content types remain......"
Пример #7
0
    def test_recent_edits(self):
        user = self.u
        p = mkPlot(user)
        p2 = mkPlot(user)
        t3 = mkTree(user)
        acts = ReputationAction.objects.all()

        content_type_p = ContentType(app_label='auth', model='Plot')
        content_type_p.save()

        reputation1 = UserReputationAction(action=acts[0],
                                           user=user,
                                           originating_user=user,
                                           content_type=content_type_p,
                                           object_id=p.pk,
                                           content_object=p,
                                           value=20)
        reputation1.save()

        auth = base64.b64encode("%s:%s" % (user.username,user.username))
        withauth = dict(create_signer_dict(user).items() + [("HTTP_AUTHORIZATION", "Basic %s" % auth)])

        ret = self.client.get("%s/user/%s/edits" % (API_PFX, user.pk), **withauth)
        json = loads(ret.content)
        
        self.assertEqual(len(json), 1) # Just on reputation item
        self.assertEqual(json[0]['plot_id'], p.pk)
        self.assertEqual(json[0]['id'], reputation1.pk)

        reputation2 = UserReputationAction(action=acts[1 % len(acts)],
                                           user=user,
                                           originating_user=user,
                                           content_type=content_type_p,
                                           object_id=p2.pk,
                                           content_object=p2,
                                           value=20)
        reputation2.save()

        ret = self.client.get("%s/user/%s/edits" % (API_PFX, user.pk), **withauth)
        json = loads(ret.content)
        
        self.assertEqual(len(json), 2) # Just on reputation item
        self.assertEqual(json[0]['plot_id'], p2.pk)
        self.assertEqual(json[0]['id'], reputation2.pk)

        self.assertEqual(json[1]['plot_id'], p.pk)
        self.assertEqual(json[1]['id'], reputation1.pk)

        reputation3 = UserReputationAction(action=acts[2 % len(acts)],
                                           user=user,
                                           originating_user=user,
                                           content_type=content_type_p,
                                           object_id=t3.pk,
                                           content_object=t3,
                                           value=20)
        reputation3.save()


        ret = self.client.get("%s/user/%s/edits" % (API_PFX, user.pk), **withauth)
        json = loads(ret.content)
        
        self.assertEqual(len(json), 3) # Just on reputation item
        self.assertEqual(json[0]['plot_id'], t3.plot.pk)
        self.assertEqual(json[0]['id'], reputation3.pk)

        self.assertEqual(json[1]['plot_id'], p2.pk)
        self.assertEqual(json[1]['id'], reputation2.pk)

        self.assertEqual(json[2]['plot_id'], p.pk)
        self.assertEqual(json[2]['id'], reputation1.pk)

        ret = self.client.get("%s/user/%s/edits?offset=1" % (API_PFX, user.pk), **withauth)
        json = loads(ret.content)
        
        self.assertEqual(len(json), 2) # Just on reputation item
        self.assertEqual(json[0]['plot_id'], p2.pk)
        self.assertEqual(json[0]['id'], reputation2.pk)

        self.assertEqual(json[1]['plot_id'], p.pk)
        self.assertEqual(json[1]['id'], reputation1.pk)

        ret = self.client.get("%s/user/%s/edits?offset=2&length=1" % (API_PFX, user.pk), **withauth)
        json = loads(ret.content)
        
        self.assertEqual(len(json), 1) # Just on reputation item
        self.assertEqual(json[0]['plot_id'], p.pk)
        self.assertEqual(json[0]['id'], reputation1.pk)

        ret = self.client.get("%s/user/%s/edits?length=1" % (API_PFX, user.pk), **withauth)
        json = loads(ret.content)
        
        self.assertEqual(len(json), 1) # Just on reputation item
        self.assertEqual(json[0]['plot_id'], t3.plot.pk)
        self.assertEqual(json[0]['id'], reputation3.pk)
        
        reputation1.delete()
        reputation2.delete()
        reputation3.delete()
        content_type_p.delete()                
        p.delete()
        p2.delete()
        t3.delete()