def save_model(self, request, obj, form, change): pre_save = None if obj is not None: if hasattr(obj, "pk") and obj.pk is not None: pre_save = obj.__class__.objects.get(pk=obj.pk) pre_save = create_dict_with_relations(pre_save) super(SnapshotModelAdmin, self).save_model(request, obj, form, change) create_snapshot(obj, pre_save, request.user)
def test_update(self): user = UserFactory() intervention = InterventionFactory() obj_dict = utils.create_dict_with_relations(intervention) fr = FundsReservationHeaderFactory(intervention=intervention) activity = utils.create_snapshot(intervention, obj_dict, user) self.assertEqual(activity.target, intervention) self.assertEqual(activity.action, activity.UPDATE) self.assertEqual(activity.by_user, user) self.assertEqual(activity.data["title"], intervention.title) self.assertEqual(activity.change, { "frs": { "before": [], "after": [fr.pk] } })
def test_additional_data(self): action_point = ActionPointFactory(status='pre_completed') initial_data = create_dict_with_relations(action_point) action_point.assigned_to = UserFactory() action_point.complete() action_point.save() author = UserFactory() create_snapshot(action_point, initial_data, author) self.assertEqual(action_point.history.count(), 1) snapshot = action_point.history.first() self.assertIn('key_events', snapshot.data) self.assertIn(ActionPoint.KEY_EVENTS.status_update, snapshot.data['key_events']) self.assertIn(ActionPoint.KEY_EVENTS.reassign, snapshot.data['key_events'])
def test_obj_none(self): obj_dict = utils.create_dict_with_relations(None) self.assertEqual(obj_dict, {})
def test_relation(self): intervention = InterventionFactory() fr = FundsReservationHeaderFactory(intervention=intervention) obj_dict = utils.create_dict_with_relations(intervention) self.assertEqual(obj_dict["frs"], [fr.pk])
def test_no_relation(self): intervention = InterventionFactory() obj_dict = utils.create_dict_with_relations(intervention) self.assertEqual(obj_dict["frs"], [])
def save(self, **kwargs): pre_save = create_dict_with_relations(self.instance) super(SnapshotModelSerializer, self).save(**kwargs) create_snapshot(self.instance, pre_save, self.context["request"].user) return self.instance