From 96caebcae1f9cba6927401fdff52f48690678cc2 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Tue, 16 Nov 2021 13:15:15 +0100 Subject: [PATCH] Refactoring * adds simple getter methods for UserActionLogEntry * replaces manual creation of UserActionLogEntry with new methods --- compensation/forms/forms.py | 23 ++++-------- compensation/models/compensation.py | 33 +++++------------- compensation/models/eco_account.py | 12 ++----- ema/forms.py | 12 ++----- ema/tests/test_views.py | 7 ++-- intervention/forms/forms.py | 20 ++++------- intervention/models/intervention.py | 36 ++++++------------- intervention/tests/test_workflow.py | 5 +-- konova/forms.py | 20 +++-------- konova/models/object.py | 21 +++-------- konova/tests/test_views.py | 17 +++------ user/models/user_action.py | 54 +++++++++++++++++++++++++++++ 12 files changed, 108 insertions(+), 152 deletions(-) diff --git a/compensation/forms/forms.py b/compensation/forms/forms.py index 0616f4ff..85e8fd67 100644 --- a/compensation/forms/forms.py +++ b/compensation/forms/forms.py @@ -18,7 +18,7 @@ from compensation.models import Compensation, EcoAccount from intervention.inputs import GenerateInput from intervention.models import Intervention, Responsibility, Legal from konova.forms import BaseForm, SimpleGeomForm -from user.models import UserActionLogEntry, UserAction +from user.models import UserActionLogEntry class AbstractCompensationForm(BaseForm): @@ -210,10 +210,7 @@ class NewCompensationForm(AbstractCompensationForm, CEFCompensationFormMixin, Co comment = self.cleaned_data.get("comment", None) # Create log entry - action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.CREATED, - ) + action = UserActionLogEntry.get_created_action(user) # Process the geometry form geometry = geom_form.save(action) @@ -270,10 +267,7 @@ class EditCompensationForm(NewCompensationForm): comment = self.cleaned_data.get("comment", None) # Create log entry - action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.EDITED, - ) + action = UserActionLogEntry.get_edited_action(user) # Process the geometry form geometry = geom_form.save(action) @@ -364,10 +358,7 @@ class NewEcoAccountForm(AbstractCompensationForm, CompensationResponsibleFormMix comment = self.cleaned_data.get("comment", None) # Create log entry - action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.CREATED, - ) + action = UserActionLogEntry.get_created_action(user) # Process the geometry form geometry = geom_form.save(action) @@ -444,10 +435,8 @@ class EditEcoAccountForm(NewEcoAccountForm): comment = self.cleaned_data.get("comment", None) # Create log entry - action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.EDITED, - ) + action = UserActionLogEntry.get_edited_action(user) + # Process the geometry form geometry = geom_form.save(action) diff --git a/compensation/models/compensation.py b/compensation/models/compensation.py index 499940c6..3f00418a 100644 --- a/compensation/models/compensation.py +++ b/compensation/models/compensation.py @@ -17,7 +17,7 @@ from compensation.models import CompensationState, CompensationAction from compensation.utils.quality import CompensationQualityChecker from konova.models import BaseObject, AbstractDocument, Geometry, Deadline, generate_document_file_upload_path from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE -from user.models import UserActionLogEntry, UserAction +from user.models import UserActionLogEntry class AbstractCompensation(BaseObject): @@ -57,21 +57,16 @@ class AbstractCompensation(BaseObject): form_data = form.cleaned_data user = form.user with transaction.atomic(): - created_action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.CREATED - ) + created_action = UserActionLogEntry.get_created_action(user) + edited_action = UserActionLogEntry.get_edited_action(user, _("Added deadline")) + deadline = Deadline.objects.create( type=form_data["type"], date=form_data["date"], comment=form_data["comment"], created=created_action, ) - edited_action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.EDITED, - comment=_("Added deadline") - ) + self.modified = edited_action self.save() self.log.add(edited_action) @@ -90,10 +85,9 @@ class AbstractCompensation(BaseObject): form_data = form.cleaned_data user = form.user with transaction.atomic(): - user_action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.CREATED, - ) + user_action = UserActionLogEntry.get_created_action(user) + edited_action = UserActionLogEntry.get_edited_action(user, _("Added action")) + comp_action = CompensationAction.objects.create( action_type=form_data["action_type"], amount=form_data["amount"], @@ -101,11 +95,6 @@ class AbstractCompensation(BaseObject): comment=form_data["comment"], created=user_action, ) - edited_action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.EDITED, - comment=_("Added action"), - ) self.modified = edited_action self.save() self.log.add(edited_action) @@ -267,11 +256,7 @@ class Compensation(AbstractCompensation, CEFMixin, CoherenceMixin): form_data = form.cleaned_data user = form.user with transaction.atomic(): - user_action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.EDITED, - comment=_("Added state") - ) + user_action = UserActionLogEntry.get_edited_action(user, _("Added state")) self.log.add(user_action) self.modified = user_action self.save() diff --git a/compensation/models/eco_account.py b/compensation/models/eco_account.py index 80cab9f9..ec612bc3 100644 --- a/compensation/models/eco_account.py +++ b/compensation/models/eco_account.py @@ -19,7 +19,7 @@ from compensation.utils.quality import EcoAccountQualityChecker from konova.models import ShareableObjectMixin, RecordableObjectMixin, AbstractDocument, BaseResource, \ generate_document_file_upload_path from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE -from user.models import UserActionLogEntry, UserAction +from user.models import UserActionLogEntry class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMixin): @@ -178,14 +178,8 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix with transaction.atomic(): # Create log entry - user_action_edit = UserActionLogEntry.objects.create( - user=user, - action=UserAction.EDITED - ) - user_action_create = UserActionLogEntry.objects.create( - user=user, - action=UserAction.CREATED - ) + user_action_create = UserActionLogEntry.get_created_action(user) + user_action_edit = UserActionLogEntry.get_edited_action(user) self.log.add(user_action_edit) self.modified = user_action_edit self.save() diff --git a/ema/forms.py b/ema/forms.py index f0f15163..cb7f0cc8 100644 --- a/ema/forms.py +++ b/ema/forms.py @@ -16,7 +16,7 @@ from compensation.forms.forms import AbstractCompensationForm, CompensationRespo from ema.models import Ema, EmaDocument from intervention.models import Responsibility from konova.forms import SimpleGeomForm, NewDocumentForm -from user.models import UserActionLogEntry, UserAction +from user.models import UserActionLogEntry class NewEmaForm(AbstractCompensationForm, CompensationResponsibleFormMixin): @@ -59,10 +59,7 @@ class NewEmaForm(AbstractCompensationForm, CompensationResponsibleFormMixin): comment = self.cleaned_data.get("comment", None) # Create log entry - action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.CREATED, - ) + action = UserActionLogEntry.get_created_action(user) # Process the geometry form geometry = geom_form.save(action) @@ -130,10 +127,7 @@ class EditEmaForm(NewEmaForm): comment = self.cleaned_data.get("comment", None) # Create log entry - action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.EDITED, - ) + action = UserActionLogEntry.get_edited_action(user) # Process the geometry form geometry = geom_form.save(action) diff --git a/ema/tests/test_views.py b/ema/tests/test_views.py index 45092883..3d853e7b 100644 --- a/ema/tests/test_views.py +++ b/ema/tests/test_views.py @@ -14,7 +14,7 @@ from ema.models import Ema from intervention.models import Responsibility from konova.models import Geometry from konova.settings import DEFAULT_GROUP, ETS_GROUP -from user.models import UserActionLogEntry, UserAction +from user.models import UserActionLogEntry class EmaViewTestCase(CompensationViewTestCase): @@ -61,10 +61,7 @@ class EmaViewTestCase(CompensationViewTestCase): def create_dummy_data(cls): # Create dummy data # Create log entry - action = UserActionLogEntry.objects.create( - user=cls.superuser, - action=UserAction.CREATED, - ) + action = UserActionLogEntry.get_created_action(cls.superuser) # Create responsible data object responsibility_data = Responsibility.objects.create() geometry = Geometry.objects.create() diff --git a/intervention/forms/forms.py b/intervention/forms/forms.py index d8f88979..fdf88403 100644 --- a/intervention/forms/forms.py +++ b/intervention/forms/forms.py @@ -10,7 +10,6 @@ from django import forms from django.contrib.auth.models import User from django.db import transaction from django.urls import reverse, reverse_lazy -from django.utils import timezone from django.utils.translation import gettext_lazy as _ from codelist.models import KonovaCode @@ -19,7 +18,7 @@ from codelist.settings import CODELIST_PROCESS_TYPE_ID, CODELIST_LAW_ID, \ from intervention.inputs import GenerateInput from intervention.models import Intervention, Legal, Responsibility from konova.forms import BaseForm, SimpleGeomForm -from user.models import UserActionLogEntry, UserAction +from user.models import UserActionLogEntry class NewInterventionForm(BaseForm): @@ -214,10 +213,7 @@ class NewInterventionForm(BaseForm): comment = self.cleaned_data.get("comment", None) # Create log entry - action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.CREATED, - ) + action = UserActionLogEntry.get_created_action(user) # Create legal data object (without M2M laws first) legal_data = Legal.objects.create( @@ -337,11 +333,7 @@ class EditInterventionForm(NewInterventionForm): self.instance.responsible.conservation_file_number = conservation_file_number self.instance.responsible.save() - user_action = UserActionLogEntry.objects.create( - user=user, - timestamp=timezone.now(), - action=UserAction.EDITED, - ) + user_action = UserActionLogEntry.get_edited_action(user) geometry = geom_form.save(user_action) self.instance.geometry = geometry @@ -356,8 +348,10 @@ class EditInterventionForm(NewInterventionForm): self.instance.save() # Uncheck and unrecord intervention due to changed data - self.instance.set_unchecked() - self.instance.set_unrecorded(user) + if self.instance.checked: + self.instance.set_unchecked() + if self.instance.recorded: + self.instance.set_unrecorded(user) return self.instance diff --git a/intervention/models/intervention.py b/intervention/models/intervention.py index 64949559..225c6bb7 100644 --- a/intervention/models/intervention.py +++ b/intervention/models/intervention.py @@ -21,7 +21,7 @@ from intervention.utils.quality import InterventionQualityChecker from konova.models import generate_document_file_upload_path, AbstractDocument, Geometry, BaseObject, ShareableObjectMixin, \ RecordableObjectMixin, CheckableObjectMixin from konova.settings import LANIS_LINK_TEMPLATE, LANIS_ZOOM_LUT, DEFAULT_SRID_RLP -from user.models import UserAction, UserActionLogEntry +from user.models import UserActionLogEntry class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, CheckableObjectMixin): @@ -189,15 +189,9 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec form_data = form.cleaned_data user = form.user with transaction.atomic(): - created_action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.CREATED, - ) - edited_action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.EDITED, - comment=_("Added payment"), - ) + created_action = UserActionLogEntry.get_created_action(user) + edited_action = UserActionLogEntry.get_edited_action(user, _("Added payment")) + pay = Payment.objects.create( created=created_action, amount=form_data.get("amount", -1), @@ -222,14 +216,9 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec form_data = form.cleaned_data user = form.user with transaction.atomic(): - created_action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.CREATED - ) - edited_action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.EDITED - ) + created_action = UserActionLogEntry.get_created_action(user) + edited_action = UserActionLogEntry.get_edited_action(user) + revocation = Revocation.objects.create( date=form_data["date"], legal=self.legal, @@ -264,14 +253,9 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec with transaction.atomic(): # Create log entry - user_action_edit = UserActionLogEntry.objects.create( - user=user, - action=UserAction.EDITED - ) - user_action_create = UserActionLogEntry.objects.create( - user=user, - action=UserAction.CREATED - ) + user_action_edit = UserActionLogEntry.get_edited_action(user) + user_action_create = UserActionLogEntry.get_created_action(user) + self.log.add(user_action_edit) self.modified = user_action_edit self.save() diff --git a/intervention/tests/test_workflow.py b/intervention/tests/test_workflow.py index f4484c6d..23a3c116 100644 --- a/intervention/tests/test_workflow.py +++ b/intervention/tests/test_workflow.py @@ -356,10 +356,7 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase): # Prepare the account for a working situation (enough deductable surface, recorded and shared) self.eco_account.deductable_surface = 10000.00 if self.eco_account.recorded is None: - rec_action = UserActionLogEntry.objects.create( - user=self.superuser, - action=UserAction.RECORDED - ) + rec_action = UserActionLogEntry.get_recorded_action(self.superuser) self.eco_account.recorded = rec_action self.eco_account.share_with_list([self.superuser]) self.eco_account.save() diff --git a/konova/forms.py b/konova/forms.py index 5a4e954c..5e644e5c 100644 --- a/konova/forms.py +++ b/konova/forms.py @@ -25,7 +25,7 @@ from konova.contexts import BaseContext from konova.models import BaseObject, Geometry, RecordableObjectMixin from konova.settings import DEFAULT_SRID from konova.utils.message_templates import FORM_INVALID -from user.models import UserActionLogEntry, UserAction +from user.models import UserActionLogEntry class BaseForm(forms.Form): @@ -168,11 +168,7 @@ class RemoveForm(BaseForm): if self.object_to_remove is not None and self.is_checked(): with transaction.atomic(): self.object_to_remove.is_active = False - action = UserActionLogEntry.objects.create( - user=user, - timestamp=timezone.now(), - action=UserAction.DELETED - ) + action = UserActionLogEntry.get_deleted_action(user) self.object_to_remove.deleted = action self.object_to_remove.save() return self.object_to_remove @@ -397,10 +393,9 @@ class NewDocumentForm(BaseModalForm): def save(self): with transaction.atomic(): - action = UserActionLogEntry.objects.create( - user=self.user, - action=UserAction.CREATED, - ) + action = UserActionLogEntry.get_created_action(self.user) + edited_action = UserActionLogEntry.get_edited_action(self.user, _("Added document")) + doc = self.document_model.objects.create( created=action, title=self.cleaned_data["title"], @@ -410,11 +405,6 @@ class NewDocumentForm(BaseModalForm): instance=self.instance, ) - edited_action = UserActionLogEntry.objects.create( - user=self.user, - action=UserAction.EDITED, - comment=_("Added document"), - ) self.instance.log.add(edited_action) self.instance.modified = edited_action self.instance.save() diff --git a/konova/models/object.py b/konova/models/object.py index fce4d1bc..9eeb3fbc 100644 --- a/konova/models/object.py +++ b/konova/models/object.py @@ -108,11 +108,7 @@ class BaseObject(BaseResource): return with transaction.atomic(): - action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.DELETED, - timestamp=timezone.now() - ) + action = UserActionLogEntry.get_deleted_action(user) self.deleted = action self.log.add(action) self.save() @@ -207,10 +203,7 @@ class RecordableObjectMixin(models.Model): Returns: """ - action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.UNRECORDED - ) + action = UserActionLogEntry.get_unrecorded_action(user) self.recorded = None self.save() self.log.add(action) @@ -225,10 +218,7 @@ class RecordableObjectMixin(models.Model): Returns: """ - action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.RECORDED - ) + action = UserActionLogEntry.get_recorded_action(user) self.recorded = action self.save() self.log.add(action) @@ -287,10 +277,7 @@ class CheckableObjectMixin(models.Model): Returns: """ - action = UserActionLogEntry.objects.create( - user=user, - action=UserAction.CHECKED - ) + action = UserActionLogEntry.get_checked_action(user) self.checked = action self.save() self.log.add(action) diff --git a/konova/tests/test_views.py b/konova/tests/test_views.py index b3880623..ae5d4524 100644 --- a/konova/tests/test_views.py +++ b/konova/tests/test_views.py @@ -20,7 +20,7 @@ from konova.management.commands.setup_data import GROUPS_DATA from konova.models import Geometry from konova.settings import DEFAULT_GROUP from konova.utils.generators import generate_random_string -from user.models import UserActionLogEntry, UserAction +from user.models import UserActionLogEntry class BaseTestCase(TestCase): @@ -98,10 +98,7 @@ class BaseTestCase(TestCase): """ # Create dummy data # Create log entry - action = UserActionLogEntry.objects.create( - user=cls.superuser, - action=UserAction.CREATED, - ) + action = UserActionLogEntry.get_created_action(cls.superuser) # Create legal data object (without M2M laws first) legal_data = Legal.objects.create() # Create responsible data object @@ -131,10 +128,7 @@ class BaseTestCase(TestCase): cls.intervention = cls.create_dummy_intervention() # Create dummy data # Create log entry - action = UserActionLogEntry.objects.create( - user=cls.superuser, - action=UserAction.CREATED, - ) + action = UserActionLogEntry.get_created_action(cls.superuser) geometry = Geometry.objects.create() # Finally create main object, holding the other objects compensation = Compensation.objects.create( @@ -156,10 +150,7 @@ class BaseTestCase(TestCase): """ # Create dummy data # Create log entry - action = UserActionLogEntry.objects.create( - user=cls.superuser, - action=UserAction.CREATED, - ) + action = UserActionLogEntry.get_created_action(cls.superuser) geometry = Geometry.objects.create() # Create responsible data object lega_data = Legal.objects.create() diff --git a/user/models/user_action.py b/user/models/user_action.py index a37aab54..c1894f7d 100644 --- a/user/models/user_action.py +++ b/user/models/user_action.py @@ -67,3 +67,57 @@ class UserActionLogEntry(models.Model): if choice[0] == self.action: return choice[1] return None + + @classmethod + def get_created_action(cls, user: User, comment: str = None): + action = UserActionLogEntry.objects.create( + user=user, + action=UserAction.CREATED, + comment=comment, + ) + return action + + @classmethod + def get_edited_action(cls, user: User, comment: str = None): + action = UserActionLogEntry.objects.create( + user=user, + action=UserAction.EDITED, + comment=comment, + ) + return action + + @classmethod + def get_deleted_action(cls, user: User, comment: str = None): + action = UserActionLogEntry.objects.create( + user=user, + action=UserAction.DELETED, + comment=comment, + ) + return action + + @classmethod + def get_checked_action(cls, user: User, comment: str = None): + action = UserActionLogEntry.objects.create( + user=user, + action=UserAction.CHECKED, + comment=comment, + ) + return action + + @classmethod + def get_recorded_action(cls, user: User, comment: str = None): + action = UserActionLogEntry.objects.create( + user=user, + action=UserAction.RECORDED, + comment=comment, + ) + return action + + @classmethod + def get_unrecorded_action(cls, user: User, comment: str = None): + action = UserActionLogEntry.objects.create( + user=user, + action=UserAction.UNRECORDED, + comment=comment, + ) + return action