Refactoring

* adds simple getter methods for UserActionLogEntry
* replaces manual creation of UserActionLogEntry with new methods
This commit is contained in:
mpeltriaux 2021-11-16 13:15:15 +01:00
parent 71e4aa9a2f
commit 779065ec91
12 changed files with 108 additions and 152 deletions

View File

@ -18,7 +18,7 @@ from compensation.models import Compensation, EcoAccount
from intervention.inputs import GenerateInput from intervention.inputs import GenerateInput
from intervention.models import Intervention, Responsibility, Legal from intervention.models import Intervention, Responsibility, Legal
from konova.forms import BaseForm, SimpleGeomForm from konova.forms import BaseForm, SimpleGeomForm
from user.models import UserActionLogEntry, UserAction from user.models import UserActionLogEntry
class AbstractCompensationForm(BaseForm): class AbstractCompensationForm(BaseForm):
@ -210,10 +210,7 @@ class NewCompensationForm(AbstractCompensationForm, CEFCompensationFormMixin, Co
comment = self.cleaned_data.get("comment", None) comment = self.cleaned_data.get("comment", None)
# Create log entry # Create log entry
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_created_action(user)
user=user,
action=UserAction.CREATED,
)
# Process the geometry form # Process the geometry form
geometry = geom_form.save(action) geometry = geom_form.save(action)
@ -270,10 +267,7 @@ class EditCompensationForm(NewCompensationForm):
comment = self.cleaned_data.get("comment", None) comment = self.cleaned_data.get("comment", None)
# Create log entry # Create log entry
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_edited_action(user)
user=user,
action=UserAction.EDITED,
)
# Process the geometry form # Process the geometry form
geometry = geom_form.save(action) geometry = geom_form.save(action)
@ -364,10 +358,7 @@ class NewEcoAccountForm(AbstractCompensationForm, CompensationResponsibleFormMix
comment = self.cleaned_data.get("comment", None) comment = self.cleaned_data.get("comment", None)
# Create log entry # Create log entry
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_created_action(user)
user=user,
action=UserAction.CREATED,
)
# Process the geometry form # Process the geometry form
geometry = geom_form.save(action) geometry = geom_form.save(action)
@ -444,10 +435,8 @@ class EditEcoAccountForm(NewEcoAccountForm):
comment = self.cleaned_data.get("comment", None) comment = self.cleaned_data.get("comment", None)
# Create log entry # Create log entry
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_edited_action(user)
user=user,
action=UserAction.EDITED,
)
# Process the geometry form # Process the geometry form
geometry = geom_form.save(action) geometry = geom_form.save(action)

View File

@ -17,7 +17,7 @@ from compensation.models import CompensationState, CompensationAction
from compensation.utils.quality import CompensationQualityChecker from compensation.utils.quality import CompensationQualityChecker
from konova.models import BaseObject, AbstractDocument, Geometry, Deadline, generate_document_file_upload_path from konova.models import BaseObject, AbstractDocument, Geometry, Deadline, generate_document_file_upload_path
from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE
from user.models import UserActionLogEntry, UserAction from user.models import UserActionLogEntry
class AbstractCompensation(BaseObject): class AbstractCompensation(BaseObject):
@ -57,21 +57,16 @@ class AbstractCompensation(BaseObject):
form_data = form.cleaned_data form_data = form.cleaned_data
user = form.user user = form.user
with transaction.atomic(): with transaction.atomic():
created_action = UserActionLogEntry.objects.create( created_action = UserActionLogEntry.get_created_action(user)
user=user, edited_action = UserActionLogEntry.get_edited_action(user, _("Added deadline"))
action=UserAction.CREATED
)
deadline = Deadline.objects.create( deadline = Deadline.objects.create(
type=form_data["type"], type=form_data["type"],
date=form_data["date"], date=form_data["date"],
comment=form_data["comment"], comment=form_data["comment"],
created=created_action, created=created_action,
) )
edited_action = UserActionLogEntry.objects.create(
user=user,
action=UserAction.EDITED,
comment=_("Added deadline")
)
self.modified = edited_action self.modified = edited_action
self.save() self.save()
self.log.add(edited_action) self.log.add(edited_action)
@ -90,10 +85,9 @@ class AbstractCompensation(BaseObject):
form_data = form.cleaned_data form_data = form.cleaned_data
user = form.user user = form.user
with transaction.atomic(): with transaction.atomic():
user_action = UserActionLogEntry.objects.create( user_action = UserActionLogEntry.get_created_action(user)
user=user, edited_action = UserActionLogEntry.get_edited_action(user, _("Added action"))
action=UserAction.CREATED,
)
comp_action = CompensationAction.objects.create( comp_action = CompensationAction.objects.create(
action_type=form_data["action_type"], action_type=form_data["action_type"],
amount=form_data["amount"], amount=form_data["amount"],
@ -101,11 +95,6 @@ class AbstractCompensation(BaseObject):
comment=form_data["comment"], comment=form_data["comment"],
created=user_action, created=user_action,
) )
edited_action = UserActionLogEntry.objects.create(
user=user,
action=UserAction.EDITED,
comment=_("Added action"),
)
self.modified = edited_action self.modified = edited_action
self.save() self.save()
self.log.add(edited_action) self.log.add(edited_action)
@ -267,11 +256,7 @@ class Compensation(AbstractCompensation, CEFMixin, CoherenceMixin):
form_data = form.cleaned_data form_data = form.cleaned_data
user = form.user user = form.user
with transaction.atomic(): with transaction.atomic():
user_action = UserActionLogEntry.objects.create( user_action = UserActionLogEntry.get_edited_action(user, _("Added state"))
user=user,
action=UserAction.EDITED,
comment=_("Added state")
)
self.log.add(user_action) self.log.add(user_action)
self.modified = user_action self.modified = user_action
self.save() self.save()

View File

@ -19,7 +19,7 @@ from compensation.utils.quality import EcoAccountQualityChecker
from konova.models import ShareableObjectMixin, RecordableObjectMixin, AbstractDocument, BaseResource, \ from konova.models import ShareableObjectMixin, RecordableObjectMixin, AbstractDocument, BaseResource, \
generate_document_file_upload_path generate_document_file_upload_path
from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE 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): class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMixin):
@ -178,14 +178,8 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
with transaction.atomic(): with transaction.atomic():
# Create log entry # Create log entry
user_action_edit = UserActionLogEntry.objects.create( user_action_create = UserActionLogEntry.get_created_action(user)
user=user, user_action_edit = UserActionLogEntry.get_edited_action(user)
action=UserAction.EDITED
)
user_action_create = UserActionLogEntry.objects.create(
user=user,
action=UserAction.CREATED
)
self.log.add(user_action_edit) self.log.add(user_action_edit)
self.modified = user_action_edit self.modified = user_action_edit
self.save() self.save()

View File

@ -16,7 +16,7 @@ from compensation.forms.forms import AbstractCompensationForm, CompensationRespo
from ema.models import Ema, EmaDocument from ema.models import Ema, EmaDocument
from intervention.models import Responsibility from intervention.models import Responsibility
from konova.forms import SimpleGeomForm, NewDocumentForm from konova.forms import SimpleGeomForm, NewDocumentForm
from user.models import UserActionLogEntry, UserAction from user.models import UserActionLogEntry
class NewEmaForm(AbstractCompensationForm, CompensationResponsibleFormMixin): class NewEmaForm(AbstractCompensationForm, CompensationResponsibleFormMixin):
@ -59,10 +59,7 @@ class NewEmaForm(AbstractCompensationForm, CompensationResponsibleFormMixin):
comment = self.cleaned_data.get("comment", None) comment = self.cleaned_data.get("comment", None)
# Create log entry # Create log entry
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_created_action(user)
user=user,
action=UserAction.CREATED,
)
# Process the geometry form # Process the geometry form
geometry = geom_form.save(action) geometry = geom_form.save(action)
@ -130,10 +127,7 @@ class EditEmaForm(NewEmaForm):
comment = self.cleaned_data.get("comment", None) comment = self.cleaned_data.get("comment", None)
# Create log entry # Create log entry
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_edited_action(user)
user=user,
action=UserAction.EDITED,
)
# Process the geometry form # Process the geometry form
geometry = geom_form.save(action) geometry = geom_form.save(action)

View File

@ -14,7 +14,7 @@ from ema.models import Ema
from intervention.models import Responsibility from intervention.models import Responsibility
from konova.models import Geometry from konova.models import Geometry
from konova.settings import DEFAULT_GROUP, ETS_GROUP from konova.settings import DEFAULT_GROUP, ETS_GROUP
from user.models import UserActionLogEntry, UserAction from user.models import UserActionLogEntry
class EmaViewTestCase(CompensationViewTestCase): class EmaViewTestCase(CompensationViewTestCase):
@ -61,10 +61,7 @@ class EmaViewTestCase(CompensationViewTestCase):
def create_dummy_data(cls): def create_dummy_data(cls):
# Create dummy data # Create dummy data
# Create log entry # Create log entry
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_created_action(cls.superuser)
user=cls.superuser,
action=UserAction.CREATED,
)
# Create responsible data object # Create responsible data object
responsibility_data = Responsibility.objects.create() responsibility_data = Responsibility.objects.create()
geometry = Geometry.objects.create() geometry = Geometry.objects.create()

View File

@ -10,7 +10,6 @@ from django import forms
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db import transaction from django.db import transaction
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.utils import timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from codelist.models import KonovaCode 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.inputs import GenerateInput
from intervention.models import Intervention, Legal, Responsibility from intervention.models import Intervention, Legal, Responsibility
from konova.forms import BaseForm, SimpleGeomForm from konova.forms import BaseForm, SimpleGeomForm
from user.models import UserActionLogEntry, UserAction from user.models import UserActionLogEntry
class NewInterventionForm(BaseForm): class NewInterventionForm(BaseForm):
@ -214,10 +213,7 @@ class NewInterventionForm(BaseForm):
comment = self.cleaned_data.get("comment", None) comment = self.cleaned_data.get("comment", None)
# Create log entry # Create log entry
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_created_action(user)
user=user,
action=UserAction.CREATED,
)
# Create legal data object (without M2M laws first) # Create legal data object (without M2M laws first)
legal_data = Legal.objects.create( legal_data = Legal.objects.create(
@ -337,11 +333,7 @@ class EditInterventionForm(NewInterventionForm):
self.instance.responsible.conservation_file_number = conservation_file_number self.instance.responsible.conservation_file_number = conservation_file_number
self.instance.responsible.save() self.instance.responsible.save()
user_action = UserActionLogEntry.objects.create( user_action = UserActionLogEntry.get_edited_action(user)
user=user,
timestamp=timezone.now(),
action=UserAction.EDITED,
)
geometry = geom_form.save(user_action) geometry = geom_form.save(user_action)
self.instance.geometry = geometry self.instance.geometry = geometry
@ -356,8 +348,10 @@ class EditInterventionForm(NewInterventionForm):
self.instance.save() self.instance.save()
# Uncheck and unrecord intervention due to changed data # Uncheck and unrecord intervention due to changed data
self.instance.set_unchecked() if self.instance.checked:
self.instance.set_unrecorded(user) self.instance.set_unchecked()
if self.instance.recorded:
self.instance.set_unrecorded(user)
return self.instance return self.instance

View File

@ -21,7 +21,7 @@ from intervention.utils.quality import InterventionQualityChecker
from konova.models import generate_document_file_upload_path, AbstractDocument, Geometry, BaseObject, ShareableObjectMixin, \ from konova.models import generate_document_file_upload_path, AbstractDocument, Geometry, BaseObject, ShareableObjectMixin, \
RecordableObjectMixin, CheckableObjectMixin RecordableObjectMixin, CheckableObjectMixin
from konova.settings import LANIS_LINK_TEMPLATE, LANIS_ZOOM_LUT, DEFAULT_SRID_RLP 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): class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, CheckableObjectMixin):
@ -189,15 +189,9 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec
form_data = form.cleaned_data form_data = form.cleaned_data
user = form.user user = form.user
with transaction.atomic(): with transaction.atomic():
created_action = UserActionLogEntry.objects.create( created_action = UserActionLogEntry.get_created_action(user)
user=user, edited_action = UserActionLogEntry.get_edited_action(user, _("Added payment"))
action=UserAction.CREATED,
)
edited_action = UserActionLogEntry.objects.create(
user=user,
action=UserAction.EDITED,
comment=_("Added payment"),
)
pay = Payment.objects.create( pay = Payment.objects.create(
created=created_action, created=created_action,
amount=form_data.get("amount", -1), amount=form_data.get("amount", -1),
@ -222,14 +216,9 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec
form_data = form.cleaned_data form_data = form.cleaned_data
user = form.user user = form.user
with transaction.atomic(): with transaction.atomic():
created_action = UserActionLogEntry.objects.create( created_action = UserActionLogEntry.get_created_action(user)
user=user, edited_action = UserActionLogEntry.get_edited_action(user)
action=UserAction.CREATED
)
edited_action = UserActionLogEntry.objects.create(
user=user,
action=UserAction.EDITED
)
revocation = Revocation.objects.create( revocation = Revocation.objects.create(
date=form_data["date"], date=form_data["date"],
legal=self.legal, legal=self.legal,
@ -264,14 +253,9 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec
with transaction.atomic(): with transaction.atomic():
# Create log entry # Create log entry
user_action_edit = UserActionLogEntry.objects.create( user_action_edit = UserActionLogEntry.get_edited_action(user)
user=user, user_action_create = UserActionLogEntry.get_created_action(user)
action=UserAction.EDITED
)
user_action_create = UserActionLogEntry.objects.create(
user=user,
action=UserAction.CREATED
)
self.log.add(user_action_edit) self.log.add(user_action_edit)
self.modified = user_action_edit self.modified = user_action_edit
self.save() self.save()

View File

@ -356,10 +356,7 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase):
# Prepare the account for a working situation (enough deductable surface, recorded and shared) # Prepare the account for a working situation (enough deductable surface, recorded and shared)
self.eco_account.deductable_surface = 10000.00 self.eco_account.deductable_surface = 10000.00
if self.eco_account.recorded is None: if self.eco_account.recorded is None:
rec_action = UserActionLogEntry.objects.create( rec_action = UserActionLogEntry.get_recorded_action(self.superuser)
user=self.superuser,
action=UserAction.RECORDED
)
self.eco_account.recorded = rec_action self.eco_account.recorded = rec_action
self.eco_account.share_with_list([self.superuser]) self.eco_account.share_with_list([self.superuser])
self.eco_account.save() self.eco_account.save()

View File

@ -25,7 +25,7 @@ from konova.contexts import BaseContext
from konova.models import BaseObject, Geometry, RecordableObjectMixin from konova.models import BaseObject, Geometry, RecordableObjectMixin
from konova.settings import DEFAULT_SRID from konova.settings import DEFAULT_SRID
from konova.utils.message_templates import FORM_INVALID from konova.utils.message_templates import FORM_INVALID
from user.models import UserActionLogEntry, UserAction from user.models import UserActionLogEntry
class BaseForm(forms.Form): class BaseForm(forms.Form):
@ -168,11 +168,7 @@ class RemoveForm(BaseForm):
if self.object_to_remove is not None and self.is_checked(): if self.object_to_remove is not None and self.is_checked():
with transaction.atomic(): with transaction.atomic():
self.object_to_remove.is_active = False self.object_to_remove.is_active = False
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_deleted_action(user)
user=user,
timestamp=timezone.now(),
action=UserAction.DELETED
)
self.object_to_remove.deleted = action self.object_to_remove.deleted = action
self.object_to_remove.save() self.object_to_remove.save()
return self.object_to_remove return self.object_to_remove
@ -397,10 +393,9 @@ class NewDocumentForm(BaseModalForm):
def save(self): def save(self):
with transaction.atomic(): with transaction.atomic():
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_created_action(self.user)
user=self.user, edited_action = UserActionLogEntry.get_edited_action(self.user, _("Added document"))
action=UserAction.CREATED,
)
doc = self.document_model.objects.create( doc = self.document_model.objects.create(
created=action, created=action,
title=self.cleaned_data["title"], title=self.cleaned_data["title"],
@ -410,11 +405,6 @@ class NewDocumentForm(BaseModalForm):
instance=self.instance, 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.log.add(edited_action)
self.instance.modified = edited_action self.instance.modified = edited_action
self.instance.save() self.instance.save()

View File

@ -108,11 +108,7 @@ class BaseObject(BaseResource):
return return
with transaction.atomic(): with transaction.atomic():
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_deleted_action(user)
user=user,
action=UserAction.DELETED,
timestamp=timezone.now()
)
self.deleted = action self.deleted = action
self.log.add(action) self.log.add(action)
self.save() self.save()
@ -207,10 +203,7 @@ class RecordableObjectMixin(models.Model):
Returns: Returns:
""" """
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_unrecorded_action(user)
user=user,
action=UserAction.UNRECORDED
)
self.recorded = None self.recorded = None
self.save() self.save()
self.log.add(action) self.log.add(action)
@ -225,10 +218,7 @@ class RecordableObjectMixin(models.Model):
Returns: Returns:
""" """
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_recorded_action(user)
user=user,
action=UserAction.RECORDED
)
self.recorded = action self.recorded = action
self.save() self.save()
self.log.add(action) self.log.add(action)
@ -287,10 +277,7 @@ class CheckableObjectMixin(models.Model):
Returns: Returns:
""" """
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_checked_action(user)
user=user,
action=UserAction.CHECKED
)
self.checked = action self.checked = action
self.save() self.save()
self.log.add(action) self.log.add(action)

View File

@ -20,7 +20,7 @@ from konova.management.commands.setup_data import GROUPS_DATA
from konova.models import Geometry from konova.models import Geometry
from konova.settings import DEFAULT_GROUP from konova.settings import DEFAULT_GROUP
from konova.utils.generators import generate_random_string from konova.utils.generators import generate_random_string
from user.models import UserActionLogEntry, UserAction from user.models import UserActionLogEntry
class BaseTestCase(TestCase): class BaseTestCase(TestCase):
@ -98,10 +98,7 @@ class BaseTestCase(TestCase):
""" """
# Create dummy data # Create dummy data
# Create log entry # Create log entry
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_created_action(cls.superuser)
user=cls.superuser,
action=UserAction.CREATED,
)
# Create legal data object (without M2M laws first) # Create legal data object (without M2M laws first)
legal_data = Legal.objects.create() legal_data = Legal.objects.create()
# Create responsible data object # Create responsible data object
@ -131,10 +128,7 @@ class BaseTestCase(TestCase):
cls.intervention = cls.create_dummy_intervention() cls.intervention = cls.create_dummy_intervention()
# Create dummy data # Create dummy data
# Create log entry # Create log entry
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_created_action(cls.superuser)
user=cls.superuser,
action=UserAction.CREATED,
)
geometry = Geometry.objects.create() geometry = Geometry.objects.create()
# Finally create main object, holding the other objects # Finally create main object, holding the other objects
compensation = Compensation.objects.create( compensation = Compensation.objects.create(
@ -156,10 +150,7 @@ class BaseTestCase(TestCase):
""" """
# Create dummy data # Create dummy data
# Create log entry # Create log entry
action = UserActionLogEntry.objects.create( action = UserActionLogEntry.get_created_action(cls.superuser)
user=cls.superuser,
action=UserAction.CREATED,
)
geometry = Geometry.objects.create() geometry = Geometry.objects.create()
# Create responsible data object # Create responsible data object
lega_data = Legal.objects.create() lega_data = Legal.objects.create()

View File

@ -67,3 +67,57 @@ class UserActionLogEntry(models.Model):
if choice[0] == self.action: if choice[0] == self.action:
return choice[1] return choice[1]
return None 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