add new field type in model DID

This commit is contained in:
Cayo Puigdefabregas 2024-01-16 14:00:05 +01:00
parent c0dba1c423
commit 47cf19f129
2 changed files with 12 additions and 5 deletions

View File

@ -639,13 +639,13 @@ class DidRegisterView(Credentials, CreateView):
icon = 'bi bi-patch-check-fill' icon = 'bi bi-patch-check-fill'
wallet = True wallet = True
model = DID model = DID
fields = ('label',) fields = ('label', 'type')
success_url = reverse_lazy('idhub:admin_dids') success_url = reverse_lazy('idhub:admin_dids')
object = None object = None
def form_valid(self, form): def form_valid(self, form):
form.instance.user = self.request.user form.instance.user = self.request.user
form.instance.set_did(form.instance.type) form.instance.set_did()
form.save() form.save()
messages.success(self.request, _('DID created successfully')) messages.success(self.request, _('DID created successfully'))
Event.set_EV_ORG_DID_CREATED_BY_ADMIN(form.instance) Event.set_EV_ORG_DID_CREATED_BY_ADMIN(form.instance)

View File

@ -403,6 +403,13 @@ class Event(models.Model):
class DID(models.Model): class DID(models.Model):
class Types(models.IntegerChoices):
KEY = 1, "Key"
WEB = 2, "Web"
type = models.PositiveSmallIntegerField(
_("Type"),
choices=Types.choices,
)
created_at = models.DateTimeField(auto_now=True) created_at = models.DateTimeField(auto_now=True)
label = models.CharField(_("Label"), max_length=50) label = models.CharField(_("Label"), max_length=50)
did = models.CharField(max_length=250) did = models.CharField(max_length=250)
@ -424,11 +431,11 @@ class DID(models.Model):
return True return True
return False return False
def set_did(self, type): def set_did(self):
self.key_material = generate_did_controller_key() self.key_material = generate_did_controller_key()
if type == "key": if self.type == self.Types.KEY:
self.did = keydid_from_controller_key(self.key_material) self.did = keydid_from_controller_key(self.key_material)
elif type == "web": elif self.type == self.Types.WEB:
didurl, document = webdid_from_controller_key(self.key_material) didurl, document = webdid_from_controller_key(self.key_material)
self.did = didurl self.did = didurl
self.didweb_document = document self.didweb_document = document