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.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)

View File

@ -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()

View File

@ -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()

View File

@ -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)

View File

@ -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()

View File

@ -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,7 +348,9 @@ class EditInterventionForm(NewInterventionForm):
self.instance.save()
# Uncheck and unrecord intervention due to changed data
if self.instance.checked:
self.instance.set_unchecked()
if self.instance.recorded:
self.instance.set_unrecorded(user)
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, \
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()

View File

@ -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()

View File

@ -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()

View File

@ -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)

View File

@ -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()

View File

@ -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