fix bug about type of credentials

This commit is contained in:
Cayo Puigdefabregas 2024-01-24 16:10:10 +01:00
parent 815f43d980
commit b6360f27fc
6 changed files with 26 additions and 33 deletions

View File

@ -186,7 +186,7 @@ class ImportForm(forms.Form):
return user return user
def create_credential(self, user, row): def create_credential(self, user, row):
return VerificableCredential( cred = VerificableCredential(
verified=False, verified=False,
user=user, user=user,
csv_data=json.dumps(row), csv_data=json.dumps(row),
@ -194,6 +194,8 @@ class ImportForm(forms.Form):
schema=self._schema, schema=self._schema,
eidas1_did=self._eidas1 eidas1_did=self._eidas1
) )
cred.set_type()
return cred
def exception(self, msg): def exception(self, msg):
File_datas.objects.create(file_name=self.file_name, success=False) File_datas.objects.create(file_name=self.file_name, success=False)

View File

@ -165,22 +165,12 @@ class CredentialTable(tables.Table):
orderable=False orderable=False
) )
def render_type(self, record):
return record.type()
def render_details(self, record): def render_details(self, record):
return record.description() return record.description()
def render_view_credential(self): def render_view_credential(self):
return format_html('<i class="bi bi-eye"></i>') return format_html('<i class="bi bi-eye"></i>')
def order_type(self, queryset, is_descending):
queryset = queryset.order_by(
("-" if is_descending else "") + "schema__type"
)
return (queryset, True)
class Meta: class Meta:
model = VerificableCredential model = VerificableCredential
template_name = "idhub/custom_table.html" template_name = "idhub/custom_table.html"

View File

@ -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.conf import settings
from django.db import migrations, models from django.db import migrations, models
@ -160,6 +160,7 @@ class Migration(migrations.Migration):
verbose_name='ID', verbose_name='ID',
), ),
), ),
('type', models.CharField(max_length=250)),
('id_string', models.CharField(max_length=250)), ('id_string', models.CharField(max_length=250)),
('verified', models.BooleanField()), ('verified', models.BooleanField()),
('created_on', models.DateTimeField(auto_now=True)), ('created_on', models.DateTimeField(auto_now=True)),

View File

@ -192,7 +192,7 @@ class Event(models.Model):
@classmethod @classmethod
def set_EV_CREDENTIAL_DELETED_BY_ADMIN(cls, cred): def set_EV_CREDENTIAL_DELETED_BY_ADMIN(cls, cred):
msg = _("The credential of type '{type}' and ID: '{id}' was deleted").format( msg = _("The credential of type '{type}' and ID: '{id}' was deleted").format(
type=cred.type(), type=cred.type,
id=cred.id, id=cred.id,
) )
cls.objects.create( cls.objects.create(
@ -203,7 +203,7 @@ class Event(models.Model):
@classmethod @classmethod
def set_EV_CREDENTIAL_DELETED(cls, cred): def set_EV_CREDENTIAL_DELETED(cls, cred):
msg = _("The credential of type '{type}' and ID: '{id}' was deleted from your wallet").format( msg = _("The credential of type '{type}' and ID: '{id}' was deleted from your wallet").format(
type=cred.type(), type=cred.type,
id=cred.id id=cred.id
) )
cls.objects.create( cls.objects.create(
@ -215,7 +215,7 @@ class Event(models.Model):
@classmethod @classmethod
def set_EV_CREDENTIAL_ISSUED_FOR_USER(cls, cred): def set_EV_CREDENTIAL_ISSUED_FOR_USER(cls, cred):
msg = _("The credential of type '{type}' and ID: '{id}' was issued for user {username}").format( msg = _("The credential of type '{type}' and ID: '{id}' was issued for user {username}").format(
type=cred.type(), type=cred.type,
id=cred.id, id=cred.id,
username=cred.user.username username=cred.user.username
) )
@ -227,7 +227,7 @@ class Event(models.Model):
@classmethod @classmethod
def set_EV_CREDENTIAL_ISSUED(cls, cred): def set_EV_CREDENTIAL_ISSUED(cls, cred):
msg = _("The credential of type '{type}' and ID: '{id}' was issued and stored in your wallet").format( 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 id=cred.id
) )
cls.objects.create( cls.objects.create(
@ -241,7 +241,7 @@ class Event(models.Model):
msg = "The credential of type '{type}' and ID: '{id}' " msg = "The credential of type '{type}' and ID: '{id}' "
msg += "was presented by user {username} to verifier '{verifier}" msg += "was presented by user {username} to verifier '{verifier}"
msg = _(msg).format( msg = _(msg).format(
type=cred.type(), type=cred.type,
id=cred.id, id=cred.id,
username=cred.user.username, username=cred.user.username,
verifier=verifier verifier=verifier
@ -256,7 +256,7 @@ class Event(models.Model):
msg = "The credential of type '{type}' and ID: '{id}' " msg = "The credential of type '{type}' and ID: '{id}' "
msg += "was presented to verifier '{verifier}'" msg += "was presented to verifier '{verifier}'"
msg = _(msg).format( msg = _(msg).format(
type=cred.type(), type=cred.type,
id=cred.id, id=cred.id,
verifier=verifier verifier=verifier
) )
@ -269,7 +269,7 @@ class Event(models.Model):
@classmethod @classmethod
def set_EV_CREDENTIAL_ENABLED(cls, cred): def set_EV_CREDENTIAL_ENABLED(cls, cred):
msg = _("The credential of type '{type}' was enabled for user {username}").format( msg = _("The credential of type '{type}' was enabled for user {username}").format(
type=cred.type(), type=cred.type,
username=cred.user.username username=cred.user.username
) )
cls.objects.create( cls.objects.create(
@ -280,7 +280,7 @@ class Event(models.Model):
@classmethod @classmethod
def set_EV_CREDENTIAL_CAN_BE_REQUESTED(cls, cred): def set_EV_CREDENTIAL_CAN_BE_REQUESTED(cls, cred):
msg = _("You can request the '{type}' credential").format( msg = _("You can request the '{type}' credential").format(
type=cred.type() type=cred.type
) )
cls.objects.create( cls.objects.create(
type=cls.Types.EV_CREDENTIAL_CAN_BE_REQUESTED, type=cls.Types.EV_CREDENTIAL_CAN_BE_REQUESTED,
@ -291,7 +291,7 @@ class Event(models.Model):
@classmethod @classmethod
def set_EV_CREDENTIAL_REVOKED_BY_ADMIN(cls, cred): def set_EV_CREDENTIAL_REVOKED_BY_ADMIN(cls, cred):
msg = _("The credential of type '{type}' and ID: '{id}' was revoked for ").format( msg = _("The credential of type '{type}' and ID: '{id}' was revoked for ").format(
type=cred.type(), type=cred.type,
id=cred.id id=cred.id
) )
cls.objects.create( cls.objects.create(
@ -302,7 +302,7 @@ class Event(models.Model):
@classmethod @classmethod
def set_EV_CREDENTIAL_REVOKED(cls, cred): def set_EV_CREDENTIAL_REVOKED(cls, cred):
msg = _("The credential of type '{type}' and ID: '{id}' was revoked by admin").format( msg = _("The credential of type '{type}' and ID: '{id}' was revoked by admin").format(
type=cred.type(), type=cred.type,
id=cred.id id=cred.id
) )
cls.objects.create( cls.objects.create(
@ -550,6 +550,7 @@ class VerificableCredential(models.Model):
REVOKED = 3, _("Revoked") REVOKED = 3, _("Revoked")
EXPIRED = 4, _("Expired") EXPIRED = 4, _("Expired")
type = models.CharField(max_length=250)
id_string = models.CharField(max_length=250) id_string = models.CharField(max_length=250)
verified = models.BooleanField() verified = models.BooleanField()
created_on = models.DateTimeField(auto_now=True) created_on = models.DateTimeField(auto_now=True)
@ -599,9 +600,6 @@ class VerificableCredential(models.Model):
def set_data(self, value, password): def set_data(self, value, password):
self.data = self.user.encrypt_data(value, password) self.data = self.user.encrypt_data(value, password)
def type(self):
return self.schema.type
def get_description(self): def get_description(self):
return self.schema._description or '' return self.schema._description or ''
@ -704,6 +702,15 @@ class VerificableCredential(models.Model):
return '' 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): def filter_dict(self, dic):
new_dict = OrderedDict() new_dict = OrderedDict()
for key, value in dic.items(): for key, value in dic.items():

View File

@ -124,13 +124,6 @@ class CredentialsTable(tables.Table):
def render_status(self, record): def render_status(self, record):
return record.get_status() 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): def order_description(self, queryset, is_descending):
queryset = queryset.order_by( queryset = queryset.order_by(
("-" if is_descending else "") + "schema__template_description" ("-" if is_descending else "") + "schema__template_description"

View File

@ -26,7 +26,7 @@ class AuthorizeForm(forms.Form):
reg = r'({})'.format('|'.join(self.presentation_definition)) reg = r'({})'.format('|'.join(self.presentation_definition))
self.all_credentials = self.user.vcredentials.filter( self.all_credentials = self.user.vcredentials.filter(
schema__type__iregex=reg, type__iregex=reg,
) )
self.credentials = self.all_credentials.filter( self.credentials = self.all_credentials.filter(
status=VerificableCredential.Status.ISSUED.value status=VerificableCredential.Status.ISSUED.value