Beispiel #1
0
    def add_object(self, model, params, **kwargs):
        params, m2m = self.prepare_params(model, params)

        dl_ctx = 'disable_logging_context' in \
                 kwargs.get('additional_params', {})
        dl_dec = 'disable_logging_decorator' in \
                 kwargs.get('additional_params', {})
        dr_ctx = 'disable_related_context' in \
                 kwargs.get('additional_params', {})
        dr_dec = 'disable_related_decorator' in \
                 kwargs.get('additional_params', {})

        def save_obj():
            obj = model.objects.create(**params)
            for k, v in m2m.items():
                getattr(obj, k).add(*v)

        with disable_logging() if dl_ctx else noop_ctx(),\
                disable_related() if dr_ctx else noop_ctx():
            if dl_dec:
                save_obj = disable_logging()(save_obj)
            if dr_dec:
                save_obj = disable_related()(save_obj)
            save_obj()
        return model.objects.latest('pk')
Beispiel #2
0
    def change_object(self, obj, params, **kwargs):
        params, m2m = self.prepare_params(obj._meta.model, params)
        for k, v in params.items():
            setattr(obj, k, v)

        dl_ctx = 'disable_logging_context' in \
                 kwargs.get('additional_params', {})
        dl_dec = 'disable_logging_decorator' in \
                 kwargs.get('additional_params', {})
        dr_ctx = 'disable_related_context' in \
                 kwargs.get('additional_params', {})
        dr_dec = 'disable_related_decorator' in \
                 kwargs.get('additional_params', {})

        def save_obj():
            obj.save()
            for k, v in m2m.items():
                if not v:
                    getattr(obj, k).clear()
                else:
                    getattr(obj, k).add(*v)

        with disable_logging() if dl_ctx else noop_ctx(),\
                disable_related() if dr_ctx else noop_ctx():
            if dl_dec:
                save_obj = disable_logging()(save_obj)
            if dr_dec:
                save_obj = disable_related()(save_obj)
            save_obj()

        return obj._meta.model.objects.get(pk=obj.pk)
Beispiel #3
0
 def dispatch(self, request, *args, **kwargs):
     dl_ctx = 'disable_logging_context' in request.POST
     dl_dec = 'disable_logging_decorator' in request.POST
     dr_ctx = 'disable_related_context' in request.POST
     dr_dec = 'disable_related_decorator' in request.POST
     super_dispatch = super(DisableMixin, self).dispatch
     with disable_logging() if dl_ctx else noop_ctx(),\
             disable_related() if dr_ctx else noop_ctx():
         if dl_dec:
             super_dispatch = disable_logging()(super_dispatch)
         if dr_dec:
             super_dispatch = disable_related()(super_dispatch)
         return super_dispatch(request, *args, **kwargs)
Beispiel #4
0
 def _delete_view(self, request, object_id, extra_context):
     dl_ctx = 'disable_logging_context' in request.POST
     dl_dec = 'disable_logging_decorator' in request.POST
     dr_ctx = 'disable_related_context' in request.POST
     dr_dec = 'disable_related_decorator' in request.POST
     super_fn = super(BaseModelAdmin, self)._delete_view
     with disable_logging() if dl_ctx else noop_ctx(), \
             disable_related() if dr_ctx else noop_ctx():
         if dl_dec:
             super_fn = disable_logging()(super_fn)
         if dr_dec:
             super_fn = disable_related()(super_fn)
         return super_fn(request, object_id, extra_context)
 def setUp(self):
     self.create_initial_objects()
     self.user = User.objects.all()[0]
     self.user_repr = force_text(self.user)
     self.ip = '127.0.0.1'
     self.other_model = OtherModel.objects.all()[0]
     with disable_logging():
         self.client.login(username='******', password='******')
Beispiel #6
0
    def delete_object(self, obj, params=None):
        params = params or {}
        dl_ctx = 'disable_logging_context' in params
        dl_dec = 'disable_logging_decorator' in params
        dr_ctx = 'disable_related_context' in params
        dr_dec = 'disable_related_decorator' in params

        def delete_obj():
            obj.delete()

        with disable_logging() if dl_ctx else noop_ctx(), \
                disable_related() if dr_ctx else noop_ctx():
            if dl_dec:
                delete_obj = disable_logging()(delete_obj)
            if dr_dec:
                delete_obj = disable_related()(delete_obj)
            delete_obj()
Beispiel #7
0
 def _wrap_view(self, view, request, *args, **kwargs):
     ctx = get_ctx(
         'disable_logging_context' in request.POST,
         'disable_related_context' in request.POST,
     )
     with ctx[0](), ctx[1]():
         if 'disable_logging_decorator' in request.POST:
             view = disable_logging()(view)
         if 'disable_related_decorator' in request.POST:
             view = disable_related()(view)
         return view(request, *args, **kwargs)
Beispiel #8
0
    def delete_object(self, obj, params=None):
        params = (params or {}).copy()
        dl_dec = 'disable_logging_decorator' in params
        dr_dec = 'disable_related_decorator' in params

        def delete_obj():
            obj.delete()

        ctx = get_ctx(
            'disable_logging_context' in params,
            'disable_related_context' in params,
        )
        with ctx[0](), ctx[1]():
            if dl_dec:
                delete_obj = disable_logging()(delete_obj)
            if dr_dec:
                delete_obj = disable_related()(delete_obj)
            delete_obj()
Beispiel #9
0
 def test_disable_log(self):
     initial_count = SimpleLog.objects.count()
     with disable_logging():
         params = {
             'char_field': 'test',
             'fk_field': self.other_model,
             'm2m_field': [self.other_model],
             'choice_field': TestModel.TWO
         }
         obj = self.add_object(TestModel, params)
         params = {
             'char_field': 'test2',
             'fk_field': '',
             'm2m_field': [],
             'choice_field': TestModel.ONE
         }
         self.change_object(obj, params)
         self.delete_object(obj)
     self.assertEqual(SimpleLog.objects.count(), initial_count)
 def create_initial_objects(self):
     with disable_logging():
         User.objects.create_superuser('user', '*****@*****.**', 'pass')
         OtherModel.objects.create(char_field='other')
         tm = ThirdModel.objects.create(char_field='third')
         RelatedModel.objects.create(char_field='related', third_model=tm)