diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 341cd90..0ff7a2f 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -1,7 +1,10 @@ import os +import csv import json import logging +import pandas as pd from pathlib import Path +from jsonschema import validate from smtplib import SMTPException from django.conf import settings @@ -13,9 +16,17 @@ from django.shortcuts import get_object_or_404, redirect from django.urls import reverse_lazy from django.http import HttpResponse from django.contrib import messages -from idhub.models import Membership, Rol, Service, UserRol, Schemas from idhub.mixins import AdminView from idhub.email.views import NotifyActivateUserByEmail +from idhub.models import ( + File_datas, + Membership, + Rol, + Service, + Schemas, + UserRol, + VerifiableCredential, +) from idhub.admin.forms import ( ProfileForm, MembershipForm, @@ -582,7 +593,7 @@ class AdminImportView(ImportExport): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ - 'dates': [], + 'dates': File_datas.objects, }) return context @@ -618,8 +629,8 @@ class AdminImportStep3View(ImportExport): self.schema = get_object_or_404(Schemas, pk=self.pk) form = ImportForm(request.POST, request.FILES) if form.is_valid(): - schema = self.handle_uploaded_file() - if not schema: + result = self.handle_uploaded_file() + if not result: messages.error(request, _("There are some errors in the file")) return super().get(request, *args, **kwargs) return redirect(self.success_url) @@ -630,21 +641,22 @@ class AdminImportStep3View(ImportExport): def handle_uploaded_file(self): f = self.request.FILES.get('file_import') - # if not f: - # return + if not f: + messages.error(self.request, _("There aren't file")) + return - # data = f.read().decode('utf-8') + file_name = f.name + if File_datas.objects.filter(file_name=file_name).exists(): + messages.error(self.request, _("This file already exists!")) + return - from jsonschema import validate - import csv - import pandas as pd - # import pdb; pdb.set_trace() - - schema = json.loads(self.schema.data) - df = pd.read_csv (r'examples/import1.csv', delimiter="\t", quotechar='"', quoting=csv.QUOTE_ALL) + self.json_schema = json.loads(self.schema.data) + df = pd.read_csv (f, delimiter="\t", quotechar='"', quoting=csv.QUOTE_ALL) data_pd = df.fillna('').to_dict() + rows = {} if not data_pd: + File_datas.objects.create(file_name=file_name, success=False) return for n in range(df.last_valid_index()+1): @@ -652,10 +664,38 @@ class AdminImportStep3View(ImportExport): for k in data_pd.keys(): row[k] = data_pd[k][n] - try: - validate(instance=row, schema=schema) - except Exception as e: - messages.error(self.request, e) + user = self.validate(n, row) + if not user: + File_datas.objects.create(file_name=file_name, success=False) return - return + + rows[user] = row + + File_datas.objects.create(file_name=file_name) + for k, v in rows.items(): + self.create_credential(k, v) + + return True + + def validate(self, line, row): + try: + validate(instance=row, schema=self.json_schema) + except Exception as e: + messages.error(self.request, "line {}: {}".format(line+1, e)) + return + + user = User.objects.filter(email=row.get('email')) + if not user: + txt = _('The user not exist!') + messages.error(self.request, "line {}: {}".format(line+1, txt)) + return + + return user.first() + + def create_credential(self, user, row): + return VerifiableCredential.objects.create( + verified=False, + user=user, + data=json.dumps(row) + ) diff --git a/idhub/migrations/0001_initial.py b/idhub/migrations/0001_initial.py index 81b98e5..45660e5 100644 --- a/idhub/migrations/0001_initial.py +++ b/idhub/migrations/0001_initial.py @@ -43,7 +43,7 @@ class Migration(migrations.Migration): ), ("id_string", models.CharField(max_length=250)), ("verified", models.BooleanField()), - ("created_on", models.DateTimeField()), + ("created_on", models.DateTimeField(auto_now=True)), ("did_issuer", models.CharField(max_length=250)), ("did_subject", models.CharField(max_length=250)), ("data", models.TextField()), diff --git a/idhub/migrations/0007_file_datas.py b/idhub/migrations/0007_file_datas.py new file mode 100644 index 0000000..5c290fa --- /dev/null +++ b/idhub/migrations/0007_file_datas.py @@ -0,0 +1,29 @@ +# Generated by Django 4.2.5 on 2023-10-25 09:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ('idhub', '0006_schemas'), + ] + + operations = [ + migrations.CreateModel( + name='File_datas', + fields=[ + ( + 'id', + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name='ID', + ), + ), + ('file_name', models.CharField(max_length=250)), + ('success', models.BooleanField(default=True)), + ('created_at', models.DateTimeField(auto_now=True)), + ], + ), + ] diff --git a/idhub/models.py b/idhub/models.py index 96357c4..054d001 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -33,7 +33,7 @@ class DID(models.Model): class VerifiableCredential(models.Model): id_string = models.CharField(max_length=250) verified = models.BooleanField() - created_on = models.DateTimeField() + created_on = models.DateTimeField(auto_now=True) did_issuer = models.CharField(max_length=250) did_subject = models.CharField(max_length=250) user = models.ForeignKey( @@ -55,6 +55,12 @@ class Schemas(models.Model): created_at = models.DateTimeField(auto_now=True) +class File_datas(models.Model): + file_name = models.CharField(max_length=250) + success = models.BooleanField(default=True) + created_at = models.DateTimeField(auto_now=True) + + class Membership(models.Model): """ This model represent the relation of this user with the ecosystem. diff --git a/idhub/templates/idhub/admin/import.html b/idhub/templates/idhub/admin/import.html index 975d289..a1b77be 100644 --- a/idhub/templates/idhub/admin/import.html +++ b/idhub/templates/idhub/admin/import.html @@ -14,7 +14,7 @@