add import datas

This commit is contained in:
Cayo Puigdefabregas 2023-10-25 12:18:42 +02:00
parent e7e17496d5
commit ae48e75133
5 changed files with 98 additions and 23 deletions

View File

@ -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)
return
user = self.validate(n, row)
if not user:
File_datas.objects.create(file_name=file_name, success=False)
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)
)

View File

@ -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()),

View File

@ -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)),
],
),
]

View File

@ -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.

View File

@ -14,7 +14,7 @@
<tr>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Created at' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'File' %}</button></th>
<th scope="col"></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'success' %}</button></th>
</tr>
</thead>
<tbody>
@ -22,7 +22,7 @@
<tr style="font-size:15px;">
<td>{{ f.created_at }}</td>
<td>{{ f.file_name }}</td>
<td></td>
<td>{% if f.success %}<i class="bi bi-check-circle text-primary"></i>{% else %}<i class="bi bi-x-circle text-danger"></i>{% endif %}</td>
</tr>
{% endfor %}
</tbody>