IdHub/idhub/models.py

176 lines
4.5 KiB
Python
Raw Normal View History

2023-10-26 11:33:13 +00:00
import json
from django.db import models
2023-10-16 17:08:18 +00:00
from django.utils.translation import gettext_lazy as _
2023-10-25 15:49:17 +00:00
from idhub_auth.models import User
2023-09-28 09:01:14 +00:00
2023-10-16 17:08:18 +00:00
# class Event(models.Model):
2023-09-28 09:01:14 +00:00
# Para los "audit logs" que se requieren en las pantallas.
2023-10-16 17:08:18 +00:00
# timestamp = models.DateTimeField()
# Los eventos no tienen relación con otros objetos a nivel de BBDD.
2023-10-16 17:08:18 +00:00
# event_data = models.CharField(max_length=250)
2023-09-28 09:01:14 +00:00
class DID(models.Model):
2023-10-26 16:06:52 +00:00
created_at = models.DateTimeField(auto_now=True)
did = models.CharField(max_length=250, unique=True)
label = models.CharField(max_length=50)
2023-10-16 17:08:18 +00:00
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='dids',
2023-10-26 11:33:13 +00:00
null=True,
2023-10-16 17:08:18 +00:00
)
# kind = "KEY|WEB"
2023-09-28 09:01:14 +00:00
2023-10-26 16:09:07 +00:00
@property
def is_organization_did(self):
if not self.user:
return True
return False
2023-09-28 09:01:14 +00:00
2023-10-25 15:49:17 +00:00
class Schemas(models.Model):
file_schema = models.CharField(max_length=250)
data = models.TextField()
created_at = models.DateTimeField(auto_now=True)
2023-10-26 11:33:13 +00:00
@property
def get_schema(self):
if not self.data:
return {}
return json.loads(self.data)
def name(self):
return self.get_schema.get('name', '')
def description(self):
return self.get_schema.get('description', '')
2023-10-25 15:49:17 +00:00
2023-10-30 12:53:19 +00:00
class VerificableCredential(models.Model):
2023-10-26 11:33:13 +00:00
"""
Definition of Verificable Credentials
"""
class Status(models.IntegerChoices):
ENABLE = 1, _("Enable")
2023-10-30 17:29:21 +00:00
REQUIRED = 2, _("Required")
2023-10-31 10:48:22 +00:00
DENEGATED = 3, _("Denegated")
ISSUED = 4, _("Issued")
REVOKED = 5, _("Revoked")
EXPIRED = 6, _("Expired")
2023-10-26 11:33:13 +00:00
2023-09-28 09:01:14 +00:00
id_string = models.CharField(max_length=250)
verified = models.BooleanField()
2023-10-25 10:18:42 +00:00
created_on = models.DateTimeField(auto_now=True)
2023-10-26 11:33:13 +00:00
issuer_on = models.DateTimeField(null=True)
did_issuer = models.CharField(max_length=250)
did_subject = models.CharField(max_length=250)
2023-10-26 11:33:13 +00:00
data = models.TextField()
status = models.PositiveSmallIntegerField(
choices=Status.choices,
default=Status.ENABLE
)
2023-10-16 17:08:18 +00:00
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='vcredentials',
)
2023-10-26 11:33:13 +00:00
@property
def get_schema(self):
if not self.data:
return {}
return json.loads(self.data)
def type(self):
return self.get_schema.get('name', '')
def description(self):
return self.get_schema.get('description', '')
def get_status(self):
return self.Status(self.status).label
2023-10-30 12:53:19 +00:00
def get_datas(self):
data = json.loads(self.data).get('instance').items()
return data
# import pdb; pdb.set_trace()
class VCTemplate(models.Model):
wkit_template_id = models.CharField(max_length=250)
data = models.TextField()
2023-10-25 10:18:42 +00:00
class File_datas(models.Model):
file_name = models.CharField(max_length=250)
success = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now=True)
2023-10-16 17:08:18 +00:00
class Membership(models.Model):
"""
This model represent the relation of this user with the ecosystem.
"""
class Types(models.IntegerChoices):
BENEFICIARY = 1, _('Beneficiary')
EMPLOYEE = 2, _('Employee')
PARTNER = 3, _('Partner')
type = models.PositiveSmallIntegerField(_('Type of membership'), choices=Types.choices)
start_date = models.DateField(
_('Start date'),
help_text=_('What date did the membership start?'),
blank=True,
null=True
)
end_date = models.DateField(
_('End date'),
help_text=_('What date did the membership end?'),
blank=True,
null=True
)
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='memberships',
)
2023-10-17 08:53:03 +00:00
def get_type(self):
return dict(self.Types.choices).get(self.type)
2023-10-17 11:40:34 +00:00
class Rol(models.Model):
name = models.CharField(max_length=250)
2023-10-17 13:49:56 +00:00
def __str__(self):
return self.name
class Service(models.Model):
domain = models.CharField(max_length=250)
description = models.CharField(max_length=250)
2023-10-19 13:20:06 +00:00
rol = models.ManyToManyField(
2023-10-17 13:49:56 +00:00
Rol,
)
2023-10-17 15:42:48 +00:00
2023-10-19 13:20:06 +00:00
def get_roles(self):
return ", ".join([x.name for x in self.rol.all()])
2023-10-17 15:42:48 +00:00
def __str__(self):
2023-10-27 09:19:10 +00:00
return "{} -> {}".format(self.domain, self.get_roles())
2023-10-17 15:42:48 +00:00
class UserRol(models.Model):
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='roles',
)
service = models.ForeignKey(
Service,
on_delete=models.CASCADE,
related_name='users',
)