konova/kspneo/models.py
2021-07-01 13:36:07 +02:00

105 lines
2.8 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 uuid
from django.contrib.auth.models import User, Group
from django.db import models
from django.db.models import QuerySet
from organisation.enums import RoleTypeEnum
class BaseResource(models.Model):
"""
A basic resource model, which defines attributes for every derived model
"""
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
)
is_active = models.BooleanField(default=True)
is_deleted = models.BooleanField(default=False)
created_on = models.DateTimeField(auto_now_add=True, null=True)
created_by = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
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)
class Meta:
abstract = True
class Deadline(BaseResource):
"""
Defines a deadline, which can be used to define dates with a semantic meaning
"""
type = models.CharField(max_length=500, null=True, blank=True)
date = models.DateField(null=True, blank=True)
def __str__(self):
return self.type
class Document(BaseResource):
"""
Documents can be attached to process, compensation or intervention for uploading legal documents or pictures.
"""
date_of_creation = models.DateField()
document = models.FileField()
comment = models.TextField()
class RoleType(BaseResource):
"""
Defines different role types
"""
type = models.CharField(max_length=255, choices=RoleTypeEnum.as_choices(drop_empty_choice=True), unique=True)
def __str__(self):
return self.type
class RoleGroup(Group):
"""
Role groups are specialized groups which hold information on which users are related to a certain organisation and
a role
"""
organisation = models.ForeignKey("organisation.Organisation", on_delete=models.CASCADE)
role = models.ForeignKey(RoleType, on_delete=models.CASCADE)
class Meta:
unique_together = [
["organisation", "role", ]
]
@staticmethod
def get_users_role_groups(user: User) -> QuerySet:
""" Get all role groups of a given user
Args:
user (User): The user
Returns:
qs (QuerySet)
"""
if user.is_anonymous:
return RoleGroup.objects.none()
return RoleGroup.objects.filter(
user=user
)