add import datas
This commit is contained in:
parent
e7e17496d5
commit
ae48e75133
|
@ -1,7 +1,10 @@
|
||||||
import os
|
import os
|
||||||
|
import csv
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import pandas as pd
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from jsonschema import validate
|
||||||
from smtplib import SMTPException
|
from smtplib import SMTPException
|
||||||
|
|
||||||
from django.conf import settings
|
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.urls import reverse_lazy
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from idhub.models import Membership, Rol, Service, UserRol, Schemas
|
|
||||||
from idhub.mixins import AdminView
|
from idhub.mixins import AdminView
|
||||||
from idhub.email.views import NotifyActivateUserByEmail
|
from idhub.email.views import NotifyActivateUserByEmail
|
||||||
|
from idhub.models import (
|
||||||
|
File_datas,
|
||||||
|
Membership,
|
||||||
|
Rol,
|
||||||
|
Service,
|
||||||
|
Schemas,
|
||||||
|
UserRol,
|
||||||
|
VerifiableCredential,
|
||||||
|
)
|
||||||
from idhub.admin.forms import (
|
from idhub.admin.forms import (
|
||||||
ProfileForm,
|
ProfileForm,
|
||||||
MembershipForm,
|
MembershipForm,
|
||||||
|
@ -582,7 +593,7 @@ class AdminImportView(ImportExport):
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
context.update({
|
context.update({
|
||||||
'dates': [],
|
'dates': File_datas.objects,
|
||||||
})
|
})
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
@ -618,8 +629,8 @@ class AdminImportStep3View(ImportExport):
|
||||||
self.schema = get_object_or_404(Schemas, pk=self.pk)
|
self.schema = get_object_or_404(Schemas, pk=self.pk)
|
||||||
form = ImportForm(request.POST, request.FILES)
|
form = ImportForm(request.POST, request.FILES)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
schema = self.handle_uploaded_file()
|
result = self.handle_uploaded_file()
|
||||||
if not schema:
|
if not result:
|
||||||
messages.error(request, _("There are some errors in the file"))
|
messages.error(request, _("There are some errors in the file"))
|
||||||
return super().get(request, *args, **kwargs)
|
return super().get(request, *args, **kwargs)
|
||||||
return redirect(self.success_url)
|
return redirect(self.success_url)
|
||||||
|
@ -630,21 +641,22 @@ class AdminImportStep3View(ImportExport):
|
||||||
|
|
||||||
def handle_uploaded_file(self):
|
def handle_uploaded_file(self):
|
||||||
f = self.request.FILES.get('file_import')
|
f = self.request.FILES.get('file_import')
|
||||||
# if not f:
|
if not f:
|
||||||
# return
|
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
|
self.json_schema = json.loads(self.schema.data)
|
||||||
import csv
|
df = pd.read_csv (f, delimiter="\t", quotechar='"', quoting=csv.QUOTE_ALL)
|
||||||
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)
|
|
||||||
data_pd = df.fillna('').to_dict()
|
data_pd = df.fillna('').to_dict()
|
||||||
|
rows = {}
|
||||||
|
|
||||||
if not data_pd:
|
if not data_pd:
|
||||||
|
File_datas.objects.create(file_name=file_name, success=False)
|
||||||
return
|
return
|
||||||
|
|
||||||
for n in range(df.last_valid_index()+1):
|
for n in range(df.last_valid_index()+1):
|
||||||
|
@ -652,10 +664,38 @@ class AdminImportStep3View(ImportExport):
|
||||||
for k in data_pd.keys():
|
for k in data_pd.keys():
|
||||||
row[k] = data_pd[k][n]
|
row[k] = data_pd[k][n]
|
||||||
|
|
||||||
try:
|
user = self.validate(n, row)
|
||||||
validate(instance=row, schema=schema)
|
if not user:
|
||||||
except Exception as e:
|
File_datas.objects.create(file_name=file_name, success=False)
|
||||||
messages.error(self.request, e)
|
|
||||||
return
|
|
||||||
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)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ class Migration(migrations.Migration):
|
||||||
),
|
),
|
||||||
("id_string", models.CharField(max_length=250)),
|
("id_string", models.CharField(max_length=250)),
|
||||||
("verified", models.BooleanField()),
|
("verified", models.BooleanField()),
|
||||||
("created_on", models.DateTimeField()),
|
("created_on", models.DateTimeField(auto_now=True)),
|
||||||
("did_issuer", models.CharField(max_length=250)),
|
("did_issuer", models.CharField(max_length=250)),
|
||||||
("did_subject", models.CharField(max_length=250)),
|
("did_subject", models.CharField(max_length=250)),
|
||||||
("data", models.TextField()),
|
("data", models.TextField()),
|
||||||
|
|
|
@ -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)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
|
@ -33,7 +33,7 @@ class DID(models.Model):
|
||||||
class VerifiableCredential(models.Model):
|
class VerifiableCredential(models.Model):
|
||||||
id_string = models.CharField(max_length=250)
|
id_string = models.CharField(max_length=250)
|
||||||
verified = models.BooleanField()
|
verified = models.BooleanField()
|
||||||
created_on = models.DateTimeField()
|
created_on = models.DateTimeField(auto_now=True)
|
||||||
did_issuer = models.CharField(max_length=250)
|
did_issuer = models.CharField(max_length=250)
|
||||||
did_subject = models.CharField(max_length=250)
|
did_subject = models.CharField(max_length=250)
|
||||||
user = models.ForeignKey(
|
user = models.ForeignKey(
|
||||||
|
@ -55,6 +55,12 @@ class Schemas(models.Model):
|
||||||
created_at = models.DateTimeField(auto_now=True)
|
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):
|
class Membership(models.Model):
|
||||||
"""
|
"""
|
||||||
This model represent the relation of this user with the ecosystem.
|
This model represent the relation of this user with the ecosystem.
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
<tr>
|
<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 'Created at' %}</button></th>
|
||||||
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'File' %}</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>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
@ -22,7 +22,7 @@
|
||||||
<tr style="font-size:15px;">
|
<tr style="font-size:15px;">
|
||||||
<td>{{ f.created_at }}</td>
|
<td>{{ f.created_at }}</td>
|
||||||
<td>{{ f.file_name }}</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>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
Loading…
Reference in New Issue