Identifier generating

* refactors identifier generating into BaseObject class
This commit is contained in:
mipel 2021-08-11 14:31:24 +02:00
parent c8b6d001ef
commit 244e300715
3 changed files with 42 additions and 57 deletions

View File

@ -9,15 +9,11 @@ from django.contrib.auth.models import User
from django.contrib.gis.db import models from django.contrib.gis.db import models
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
from django.db.models import Sum from django.db.models import Sum
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _ 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 intervention.models import Intervention, ResponsibilityData
from konova.models import BaseObject, BaseResource, Geometry, UuidModel from konova.models import BaseObject, BaseResource, Geometry, UuidModel
from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE
from konova.utils.generators import generate_random_string
from user.models import UserActionLogEntry from user.models import UserActionLogEntry
@ -146,22 +142,6 @@ class Compensation(AbstractCompensation):
def __str__(self): def __str__(self):
return "{}".format(self.identifier) 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): def save(self, *args, **kwargs):
if self.identifier is None or len(self.identifier) == 0: if self.identifier is None or len(self.identifier) == 0:
# Create new identifier # Create new identifier
@ -284,22 +264,6 @@ class EcoAccount(AbstractCompensation):
# ToDo # ToDo
pass 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): class EcoAccountWithdraw(BaseResource):
""" """

View File

@ -7,16 +7,11 @@ Created on: 17.11.20
""" """
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.gis.db import models 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.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.models import BaseObject, Geometry, UuidModel, BaseResource
from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE
from konova.utils import generators from konova.utils import generators
from konova.utils.generators import generate_random_string
from organisation.models import Organisation from organisation.models import Organisation
from user.models import UserActionLogEntry from user.models import UserActionLogEntry
@ -135,22 +130,6 @@ class Intervention(BaseObject):
def __str__(self): def __str__(self):
return "{} ({})".format(self.identifier, self.title) 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): def generate_access_token(self, make_unique: bool = False, rec_depth: int = 5):
""" Creates a new access token for the intervention """ Creates a new access token for the intervention

View File

@ -9,10 +9,15 @@ import os
import uuid import uuid
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.contrib.gis.db.models import MultiPolygonField from django.contrib.gis.db.models import MultiPolygonField
from django.db import models, transaction 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 from user.models import UserActionLogEntry, UserAction
@ -118,6 +123,43 @@ class BaseObject(BaseResource):
else: else:
return User.objects.none() 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): class DeadlineType(models.TextChoices):
""" """