Identifier generating

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

View File

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