From b6360f27fc9db7c58a47bec32bedb82c5e472bf8 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 24 Jan 2024 16:10:10 +0100 Subject: [PATCH] fix bug about type of credentials --- idhub/admin/forms.py | 4 +++- idhub/admin/tables.py | 10 ---------- idhub/migrations/0001_initial.py | 3 ++- idhub/models.py | 33 +++++++++++++++++++------------- idhub/user/tables.py | 7 ------- oidc4vp/forms.py | 2 +- 6 files changed, 26 insertions(+), 33 deletions(-) diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py index 4264e03..5476507 100644 --- a/idhub/admin/forms.py +++ b/idhub/admin/forms.py @@ -186,7 +186,7 @@ class ImportForm(forms.Form): return user def create_credential(self, user, row): - return VerificableCredential( + cred = VerificableCredential( verified=False, user=user, csv_data=json.dumps(row), @@ -194,6 +194,8 @@ class ImportForm(forms.Form): schema=self._schema, eidas1_did=self._eidas1 ) + cred.set_type() + return cred def exception(self, msg): File_datas.objects.create(file_name=self.file_name, success=False) diff --git a/idhub/admin/tables.py b/idhub/admin/tables.py index e4d4ea2..2c3c9f2 100644 --- a/idhub/admin/tables.py +++ b/idhub/admin/tables.py @@ -165,22 +165,12 @@ class CredentialTable(tables.Table): orderable=False ) - def render_type(self, record): - return record.type() - def render_details(self, record): return record.description() def render_view_credential(self): return format_html('') - def order_type(self, queryset, is_descending): - queryset = queryset.order_by( - ("-" if is_descending else "") + "schema__type" - ) - - return (queryset, True) - class Meta: model = VerificableCredential template_name = "idhub/custom_table.html" diff --git a/idhub/migrations/0001_initial.py b/idhub/migrations/0001_initial.py index 4fabd27..96e247b 100644 --- a/idhub/migrations/0001_initial.py +++ b/idhub/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.5 on 2024-01-22 15:44 +# Generated by Django 4.2.5 on 2024-01-24 15:04 from django.conf import settings from django.db import migrations, models @@ -160,6 +160,7 @@ class Migration(migrations.Migration): verbose_name='ID', ), ), + ('type', models.CharField(max_length=250)), ('id_string', models.CharField(max_length=250)), ('verified', models.BooleanField()), ('created_on', models.DateTimeField(auto_now=True)), diff --git a/idhub/models.py b/idhub/models.py index 96d8559..d5e44ef 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -192,7 +192,7 @@ class Event(models.Model): @classmethod def set_EV_CREDENTIAL_DELETED_BY_ADMIN(cls, cred): msg = _("The credential of type '{type}' and ID: '{id}' was deleted").format( - type=cred.type(), + type=cred.type, id=cred.id, ) cls.objects.create( @@ -203,7 +203,7 @@ class Event(models.Model): @classmethod def set_EV_CREDENTIAL_DELETED(cls, cred): msg = _("The credential of type '{type}' and ID: '{id}' was deleted from your wallet").format( - type=cred.type(), + type=cred.type, id=cred.id ) cls.objects.create( @@ -215,7 +215,7 @@ class Event(models.Model): @classmethod def set_EV_CREDENTIAL_ISSUED_FOR_USER(cls, cred): msg = _("The credential of type '{type}' and ID: '{id}' was issued for user {username}").format( - type=cred.type(), + type=cred.type, id=cred.id, username=cred.user.username ) @@ -227,7 +227,7 @@ class Event(models.Model): @classmethod def set_EV_CREDENTIAL_ISSUED(cls, cred): msg = _("The credential of type '{type}' and ID: '{id}' was issued and stored in your wallet").format( - type=cred.type(), + type=cred.type, id=cred.id ) cls.objects.create( @@ -241,7 +241,7 @@ class Event(models.Model): msg = "The credential of type '{type}' and ID: '{id}' " msg += "was presented by user {username} to verifier '{verifier}" msg = _(msg).format( - type=cred.type(), + type=cred.type, id=cred.id, username=cred.user.username, verifier=verifier @@ -256,7 +256,7 @@ class Event(models.Model): msg = "The credential of type '{type}' and ID: '{id}' " msg += "was presented to verifier '{verifier}'" msg = _(msg).format( - type=cred.type(), + type=cred.type, id=cred.id, verifier=verifier ) @@ -269,7 +269,7 @@ class Event(models.Model): @classmethod def set_EV_CREDENTIAL_ENABLED(cls, cred): msg = _("The credential of type '{type}' was enabled for user {username}").format( - type=cred.type(), + type=cred.type, username=cred.user.username ) cls.objects.create( @@ -280,7 +280,7 @@ class Event(models.Model): @classmethod def set_EV_CREDENTIAL_CAN_BE_REQUESTED(cls, cred): msg = _("You can request the '{type}' credential").format( - type=cred.type() + type=cred.type ) cls.objects.create( type=cls.Types.EV_CREDENTIAL_CAN_BE_REQUESTED, @@ -291,7 +291,7 @@ class Event(models.Model): @classmethod def set_EV_CREDENTIAL_REVOKED_BY_ADMIN(cls, cred): msg = _("The credential of type '{type}' and ID: '{id}' was revoked for ").format( - type=cred.type(), + type=cred.type, id=cred.id ) cls.objects.create( @@ -302,7 +302,7 @@ class Event(models.Model): @classmethod def set_EV_CREDENTIAL_REVOKED(cls, cred): msg = _("The credential of type '{type}' and ID: '{id}' was revoked by admin").format( - type=cred.type(), + type=cred.type, id=cred.id ) cls.objects.create( @@ -550,6 +550,7 @@ class VerificableCredential(models.Model): REVOKED = 3, _("Revoked") EXPIRED = 4, _("Expired") + type = models.CharField(max_length=250) id_string = models.CharField(max_length=250) verified = models.BooleanField() created_on = models.DateTimeField(auto_now=True) @@ -599,9 +600,6 @@ class VerificableCredential(models.Model): def set_data(self, value, password): self.data = self.user.encrypt_data(value, password) - def type(self): - return self.schema.type - def get_description(self): return self.schema._description or '' @@ -704,6 +702,15 @@ class VerificableCredential(models.Model): return '' + def set_type(self): + template_name = 'credentials/{}'.format( + self.schema.file_schema + ) + tmpl = get_template(template_name) + d = json.loads(tmpl.render({})) + self.type = d.get('type')[-1] + + def filter_dict(self, dic): new_dict = OrderedDict() for key, value in dic.items(): diff --git a/idhub/user/tables.py b/idhub/user/tables.py index 0cdb4fb..28045e9 100644 --- a/idhub/user/tables.py +++ b/idhub/user/tables.py @@ -124,13 +124,6 @@ class CredentialsTable(tables.Table): def render_status(self, record): return record.get_status() - def order_type(self, queryset, is_descending): - queryset = queryset.order_by( - ("-" if is_descending else "") + "schema__type" - ) - - return (queryset, True) - def order_description(self, queryset, is_descending): queryset = queryset.order_by( ("-" if is_descending else "") + "schema__template_description" diff --git a/oidc4vp/forms.py b/oidc4vp/forms.py index d40c6a1..ea32d38 100644 --- a/oidc4vp/forms.py +++ b/oidc4vp/forms.py @@ -26,7 +26,7 @@ class AuthorizeForm(forms.Form): reg = r'({})'.format('|'.join(self.presentation_definition)) self.all_credentials = self.user.vcredentials.filter( - schema__type__iregex=reg, + type__iregex=reg, ) self.credentials = self.all_credentials.filter( status=VerificableCredential.Status.ISSUED.value