Exemple #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')
Exemple #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)
Exemple #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)
Exemple #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)
Exemple #5
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()
Exemple #6
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)
Exemple #7
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()
Exemple #8
0
 def test_disable_related(self):
     initial_count = SimpleLog.objects.count()
     params = {
         'char_field': 'test',
         'fk_field': self.other_model,
         'm2m_field': [self.other_model],
         'choice_field': TestModel.TWO
     }
     obj = self.add_object(TestModel, params)
     with atomic():
         with disable_related():
             params = {
                 'char_field': 'test2',
                 'fk_field': '',
                 'm2m_field': [],
                 'choice_field': TestModel.ONE
             }
             self.change_object(obj, params)
     self.assertEqual(SimpleLog.objects.count(), initial_count + 2)
     first_sl = SimpleLog.objects.all()[0]
     second_sl = SimpleLog.objects.all()[1]
     self.assertQuerysetEqual(first_sl.related_logs.all(), [])
     self.assertQuerysetEqual(second_sl.related_logs.all(), [])