konova/konova/models.py
mipel cd5b2e264b Fixes and improvements
* moves diff_states message back to table top for direct presentation in compensation/detail/view.html
* removes diff_states rendering in deadline card in compensation/detail/view.html
* fixes before_state adding based on GET parameter
* refactors UserActionlogEntryEnum into a UserAction TextChoice (Django 3.x)
* adds ordering of compensation states depending on surface value
* refactors ServerMessageImportance from enum into TextChoice (Django 3.x)
* adds/updates translations
2021-08-03 17:22:41 +02:00

124 lines
3.3 KiB
Python

"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 17.11.20
"""
import os
import uuid
from django.utils.translation import gettext_lazy as _
from django.contrib.gis.db.models import MultiPolygonField
from django.db import models
from user.models import UserActionLogEntry
class UuidModel(models.Model):
"""
Encapsules identifying via uuid
"""
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False,
)
class Meta:
abstract = True
class BaseResource(UuidModel):
"""
A basic resource model, which defines attributes for every derived model
"""
created = models.ForeignKey(UserActionLogEntry, on_delete=models.SET_NULL, null=True, blank=True, related_name='+')
class Meta:
abstract = True
class BaseObject(BaseResource):
"""
A basic object model, which specifies BaseResource.
Mainly used for intervention, compensation, ecoaccount
"""
identifier = models.CharField(max_length=1000, null=True, blank=True)
title = models.CharField(max_length=1000, null=True, blank=True)
deleted = models.ForeignKey(UserActionLogEntry, on_delete=models.SET_NULL, null=True, blank=True, related_name='+')
comment = models.TextField(null=True, blank=True)
class Meta:
abstract = True
class DeadlineType(models.TextChoices):
"""
Django 3.x way of handling enums for models
"""
FINISHED = "finished", _("Finished")
MAINTAIN = "maintain", _("Maintain")
CONTROL = "control", _("Control")
OTHER = "other", _("Other")
class Deadline(BaseResource):
"""
Defines a deadline, which can be used to define dates with a semantic meaning
"""
type = models.CharField(max_length=255, null=True, blank=True, choices=DeadlineType.choices)
date = models.DateField(null=True, blank=True)
comment = models.CharField(max_length=1000, null=True, blank=True)
def __str__(self):
return self.type
@property
def type_humanized(self):
""" Returns humanized version of enum
Used for template rendering
Returns:
"""
choices = DeadlineType.choices
for choice in choices:
if choice[0] == self.type:
return choice[1]
return None
class Document(BaseResource):
"""
Documents can be attached to compensation or intervention for uploading legal documents or pictures.
"""
title = models.CharField(max_length=500, null=True, blank=True)
date_of_creation = models.DateField()
document = models.FileField()
comment = models.TextField()
def delete(self, using=None, keep_parents=False):
""" Custom delete function to remove the real file from the hard drive
Args:
using ():
keep_parents ():
Returns:
"""
os.remove(self.document.file.name)
super().delete(using=using, keep_parents=keep_parents)
class Geometry(BaseResource):
"""
Outsourced geometry model so multiple versions of the same object can refer to the same geometry if it is not changed
"""
from konova.settings import DEFAULT_SRID
geom = MultiPolygonField(null=True, blank=True, srid=DEFAULT_SRID)