Merge pull request 'bugfix/136-messages-errors-validation' (#155) from bugfix/136-messages-errors-validation into release

Reviewed-on: https://gitea.pangea.org/trustchain-oc1-orchestral/IdHub/pulls/155
This commit is contained in:
cayop 2024-02-28 17:28:43 +00:00
commit 4706644826
11 changed files with 33 additions and 15 deletions

Binary file not shown.

Binary file not shown.

View File

@ -4,6 +4,7 @@ import jsonschema
import pandas as pd
from nacl.exceptions import CryptoError
from openpyxl import load_workbook
from django import forms
from django.core.cache import cache
from django.utils.translation import gettext_lazy as _
@ -164,7 +165,8 @@ class ImportForm(forms.Form):
self.fields['did'].choices = [
(x.did, x.label) for x in dids.filter(eidas1=False)
]
self.fields['schema'].choices = [(0, _('Select one'))] + [
txt_select_one = _("Please choose a data schema ...")
self.fields['schema'].choices = [(0,txt_select_one)] + [
(x.id, x.name) for x in Schemas.objects.filter()
]
if dids.filter(eidas1=True).exists():
@ -184,7 +186,7 @@ class ImportForm(forms.Form):
)
if not did.exists():
raise ValidationError("Did is not valid!")
raise ValidationError(_("Did not valid!"))
self._did = did.first()
@ -204,13 +206,13 @@ class ImportForm(forms.Form):
id=data
)
if not schema.exists():
raise ValidationError("Schema is not valid!")
raise ValidationError(_("Schema is not valid!"))
self._schema = schema.first()
try:
self.json_schema = self._schema.get_credential_subject_schema()
except Exception:
raise ValidationError("Schema is not valid!")
raise ValidationError(_("Schema not valid!"))
return data
@ -224,30 +226,46 @@ class ImportForm(forms.Form):
df = pd.read_excel(data)
df.fillna('', inplace=True)
try:
workbook = load_workbook(data)
# if no there are schema meen than is a excel costum and you
# don't have control abour that
if 'Schema' in workbook.custom_doc_props.names:
excel_schema = workbook.custom_doc_props['Schema'].value
file_schema = self._schema.file_schema.split('.json')[0]
assert file_schema in excel_schema
except Exception:
txt = _("This File does not correspond to this scheme!")
raise ValidationError(txt)
# convert dates to iso 8601
for col in df.select_dtypes(include='datetime').columns:
df[col] = df[col].dt.strftime("%Y-%m-%d")
# convert numbers to strings if this is indicate in schema
props = self.json_schema.get("properties", {})
for col in df.select_dtypes(include=['number']).columns:
type_col = props.get(col, {}).get("type")
if "string" in type_col:
if type_col and "string" in type_col:
df[col] = df[col].astype(str)
data_pd = df.to_dict()
data_pd = df.to_dict(orient='index')
if not data_pd or df.last_valid_index() is None:
self.exception("The file you try to import is empty!")
self.exception(_("The file you try to import is empty!"))
for n in range(df.last_valid_index()+1):
for n in data_pd.keys():
row = {}
for k in data_pd.keys():
if data_pd[k][n] or data_pd[k][n] == 0:
row[k] = data_pd[k][n]
d = data_pd[n]
for k, v in d.items():
if d[k] or d[k] == 0:
row[k] = d[k]
user = self.validate_jsonld(n, row)
if row:
user = self.validate_jsonld(n+2, row)
if user:
self.rows[user] = row
return data
@ -273,7 +291,7 @@ class ImportForm(forms.Form):
format_checker=jsonschema.Draft202012Validator.FORMAT_CHECKER
)
except jsonschema.exceptions.ValidationError as err:
msg = "line {}: {}".format(line+1, err)
msg = "line {}: {}".format(line, err.message)
return self.exception(msg)
user, new = User.objects.get_or_create(email=row.get('email'))

View File

@ -1071,6 +1071,6 @@ class ImportAddView(NotifyActivateUserByEmail, ImportExport, FormView):
try:
self.send_email(user)
except SMTPException as e:
messages.error(self.request, e)
messages.error(self.request, e.message)
return super().form_valid(form)