Refactors triggering checked/recorded
* refactors BaseForm request/user initialization * introduces mark_as_edited() method for compensation models
This commit is contained in:
@@ -14,11 +14,10 @@ from django import forms
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.gis.forms import OSMWidget, MultiPolygonField
|
||||
from django.contrib.gis.geos import Polygon, MultiPolygon
|
||||
from django.contrib.gis.geos import MultiPolygon
|
||||
from django.db import transaction
|
||||
from django.http import HttpRequest, HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from konova.contexts import BaseContext
|
||||
@@ -39,15 +38,16 @@ class BaseForm(forms.Form):
|
||||
cancel_redirect = None
|
||||
form_caption = None
|
||||
instance = None # The data holding model object
|
||||
request = None
|
||||
form_attrs = {} # Holds additional attributes, that can be used in the template
|
||||
has_required_fields = False # Automatically set. Triggers hint rendering in templates
|
||||
show_cancel_btn = True
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.instance = kwargs.pop("instance", None)
|
||||
self.user = kwargs.pop("user", None)
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
if self.request is not None:
|
||||
self.user = self.request.user
|
||||
# Check for required fields
|
||||
for _field_name, _field_val in self.fields.items():
|
||||
if _field_val.required:
|
||||
|
||||
@@ -8,9 +8,10 @@ Created on: 15.11.21
|
||||
|
||||
import uuid
|
||||
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.utils import timezone
|
||||
from django.http import HttpRequest
|
||||
from django.utils.timezone import now
|
||||
from django.db import models, transaction
|
||||
from compensation.settings import COMPENSATION_IDENTIFIER_TEMPLATE, COMPENSATION_IDENTIFIER_LENGTH, \
|
||||
@@ -19,6 +20,7 @@ from ema.settings import EMA_ACCOUNT_IDENTIFIER_LENGTH, EMA_ACCOUNT_IDENTIFIER_T
|
||||
from intervention.settings import INTERVENTION_IDENTIFIER_LENGTH, INTERVENTION_IDENTIFIER_TEMPLATE
|
||||
from konova.utils import generators
|
||||
from konova.utils.generators import generate_random_string
|
||||
from konova.utils.message_templates import CHECKED_RECORDED_RESET
|
||||
from user.models import UserActionLogEntry, UserAction
|
||||
|
||||
|
||||
@@ -228,7 +230,7 @@ class RecordableObjectMixin(models.Model):
|
||||
self.log.add(action)
|
||||
return action
|
||||
|
||||
def mark_as_edited(self, performing_user: User):
|
||||
def mark_as_edited(self, performing_user: User, request: HttpRequest = None, edit_comment: str = None):
|
||||
""" In case the object or a related object changed, internal processes need to be started, such as
|
||||
unrecord and uncheck
|
||||
|
||||
@@ -238,9 +240,18 @@ class RecordableObjectMixin(models.Model):
|
||||
Returns:
|
||||
|
||||
"""
|
||||
action = UserActionLogEntry.get_edited_action(performing_user, edit_comment)
|
||||
self.modified = action
|
||||
self.log.add(action)
|
||||
self.save()
|
||||
|
||||
if self.recorded:
|
||||
self.set_unrecorded(performing_user)
|
||||
if request:
|
||||
messages.info(
|
||||
request,
|
||||
CHECKED_RECORDED_RESET
|
||||
)
|
||||
|
||||
|
||||
class CheckableObjectMixin(models.Model):
|
||||
|
||||
@@ -46,7 +46,7 @@ def remove_document(request: HttpRequest, doc: AbstractDocument):
|
||||
|
||||
"""
|
||||
title = doc.title
|
||||
form = RemoveModalForm(request.POST or None, instance=doc, user=request.user)
|
||||
form = RemoveModalForm(request.POST or None, instance=doc, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=_("Document '{}' deleted").format(title)
|
||||
|
||||
@@ -19,4 +19,10 @@ MISSING_GROUP_PERMISSION = _("You need to be part of another user group.")
|
||||
CHECKED_RECORDED_RESET = _("Status of Checked and Recorded reseted")
|
||||
|
||||
# ECO ACCOUNT
|
||||
CANCEL_ACC_RECORDED_OR_DEDUCTED = _("Action canceled. Eco account is recorded or deductions exist. Only conservation office member can perform this action.")
|
||||
CANCEL_ACC_RECORDED_OR_DEDUCTED = _("Action canceled. Eco account is recorded or deductions exist. Only conservation office member can perform this action.")
|
||||
|
||||
# Edited
|
||||
EDITED_GENERAL_DATA = _("Edited general data")
|
||||
ADDED_COMPENSATION_STATE = _("Added compensation state")
|
||||
ADDED_DEADLINE = _("Added deadline")
|
||||
ADDED_COMPENSATION_ACTION = _("Added compensation action")
|
||||
@@ -109,7 +109,7 @@ def remove_deadline_view(request: HttpRequest, id:str):
|
||||
|
||||
"""
|
||||
deadline = get_object_or_404(Deadline, id=id)
|
||||
form = RemoveModalForm(request.POST or None, instance=deadline, user=request.user)
|
||||
form = RemoveModalForm(request.POST or None, instance=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Deadline removed")
|
||||
|
||||
Reference in New Issue
Block a user