From 244e30071524429ab9b90a470d5acd424205aa18 Mon Sep 17 00:00:00 2001 From: mipel Date: Wed, 11 Aug 2021 14:31:24 +0200 Subject: [PATCH] Identifier generating * refactors identifier generating into BaseObject class --- compensation/models.py | 36 ------------------------------------ intervention/models.py | 21 --------------------- konova/models.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 57 deletions(-) diff --git a/compensation/models.py b/compensation/models.py index c5df895..fba7100 100644 --- a/compensation/models.py +++ b/compensation/models.py @@ -9,15 +9,11 @@ from django.contrib.auth.models import User from django.contrib.gis.db import models from django.core.validators import MinValueValidator from django.db.models import Sum -from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ -from compensation.settings import COMPENSATION_IDENTIFIER_LENGTH, COMPENSATION_IDENTIFIER_TEMPLATE, \ - ECO_ACCOUNT_IDENTIFIER_LENGTH, ECO_ACCOUNT_IDENTIFIER_TEMPLATE from intervention.models import Intervention, ResponsibilityData from konova.models import BaseObject, BaseResource, Geometry, UuidModel from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE -from konova.utils.generators import generate_random_string from user.models import UserActionLogEntry @@ -146,22 +142,6 @@ class Compensation(AbstractCompensation): def __str__(self): return "{}".format(self.identifier) - @staticmethod - def _generate_new_identifier() -> str: - """ Generates a new identifier for the intervention object - - Returns: - str - """ - curr_month = str(now().month) - curr_year = str(now().year) - rand_str = generate_random_string( - length=COMPENSATION_IDENTIFIER_LENGTH, - only_numbers=True, - ) - _str = "{}{}{}".format(curr_month, curr_year, rand_str) - return COMPENSATION_IDENTIFIER_TEMPLATE.format(_str) - def save(self, *args, **kwargs): if self.identifier is None or len(self.identifier) == 0: # Create new identifier @@ -284,22 +264,6 @@ class EcoAccount(AbstractCompensation): # ToDo pass - @staticmethod - def _generate_new_identifier() -> str: - """ Generates a new identifier for the intervention object - - Returns: - str - """ - curr_month = str(now().month) - curr_year = str(now().year) - rand_str = generate_random_string( - length=ECO_ACCOUNT_IDENTIFIER_LENGTH, - only_numbers=True, - ) - _str = "{}{}{}".format(curr_month, curr_year, rand_str) - return ECO_ACCOUNT_IDENTIFIER_TEMPLATE.format(_str) - class EcoAccountWithdraw(BaseResource): """ diff --git a/intervention/models.py b/intervention/models.py index 40f2f67..762251b 100644 --- a/intervention/models.py +++ b/intervention/models.py @@ -7,16 +7,11 @@ Created on: 17.11.20 """ from django.contrib.auth.models import User from django.contrib.gis.db import models -from django.db import transaction -from django.utils import timezone from django.utils.translation import gettext_lazy as _ -from django.utils.timezone import now -from intervention.settings import INTERVENTION_IDENTIFIER_LENGTH, INTERVENTION_IDENTIFIER_TEMPLATE from konova.models import BaseObject, Geometry, UuidModel, BaseResource from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE from konova.utils import generators -from konova.utils.generators import generate_random_string from organisation.models import Organisation from user.models import UserActionLogEntry @@ -135,22 +130,6 @@ class Intervention(BaseObject): def __str__(self): return "{} ({})".format(self.identifier, self.title) - @staticmethod - def _generate_new_identifier() -> str: - """ Generates a new identifier for the intervention object - - Returns: - str - """ - curr_month = str(now().month) - curr_year = str(now().year) - rand_str = generate_random_string( - length=INTERVENTION_IDENTIFIER_LENGTH, - only_numbers=True, - ) - _str = "{}{}{}".format(curr_month, curr_year, rand_str) - return INTERVENTION_IDENTIFIER_TEMPLATE.format(_str) - def generate_access_token(self, make_unique: bool = False, rec_depth: int = 5): """ Creates a new access token for the intervention diff --git a/konova/models.py b/konova/models.py index e049420..ee581d1 100644 --- a/konova/models.py +++ b/konova/models.py @@ -9,10 +9,15 @@ import os import uuid from django.contrib.auth.models import User +from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ from django.contrib.gis.db.models import MultiPolygonField from django.db import models, transaction +from compensation.settings import COMPENSATION_IDENTIFIER_TEMPLATE, COMPENSATION_IDENTIFIER_LENGTH, \ + ECO_ACCOUNT_IDENTIFIER_TEMPLATE, ECO_ACCOUNT_IDENTIFIER_LENGTH +from intervention.settings import INTERVENTION_IDENTIFIER_LENGTH, INTERVENTION_IDENTIFIER_TEMPLATE +from konova.utils.generators import generate_random_string from user.models import UserActionLogEntry, UserAction @@ -118,6 +123,43 @@ class BaseObject(BaseResource): else: return User.objects.none() + def _generate_new_identifier(self) -> str: + """ Generates a new identifier for the intervention object + + Returns: + str + """ + from compensation.models import Compensation, EcoAccount + from intervention.models import Intervention + definitions = { + Intervention: { + "length": INTERVENTION_IDENTIFIER_LENGTH, + "template": INTERVENTION_IDENTIFIER_TEMPLATE, + }, + Compensation: { + "length": COMPENSATION_IDENTIFIER_LENGTH, + "template": COMPENSATION_IDENTIFIER_TEMPLATE, + }, + EcoAccount: { + "length": ECO_ACCOUNT_IDENTIFIER_LENGTH, + "template": ECO_ACCOUNT_IDENTIFIER_TEMPLATE, + }, + } + + if self.__class__ not in definitions: + # Not defined, yet. Create fallback identifier for this case + return generate_random_string(10) + + _now = now() + curr_month = str(_now.month) + curr_year = str(_now.year) + rand_str = generate_random_string( + length=definitions[self.__class__]["length"], + only_numbers=True, + ) + _str = "{}{}-{}".format(curr_month, curr_year, rand_str) + return definitions[self.__class__]["template"].format(_str) + class DeadlineType(models.TextChoices): """