add upload schema system

This commit is contained in:
Cayo Puigdefabregas 2023-10-22 20:01:41 +02:00
parent 5fbffa873c
commit e292caf85f
5 changed files with 81 additions and 2 deletions

View file

@ -25,3 +25,7 @@ class ServiceForm(forms.ModelForm):
class UserRolForm(forms.ModelForm): class UserRolForm(forms.ModelForm):
MANDATORY_FIELDS = ['service'] MANDATORY_FIELDS = ['service']
class SchemaForm(forms.Form):
file_template = forms.FileField()

View file

@ -19,7 +19,8 @@ from idhub.admin.forms import (
MembershipForm, MembershipForm,
RolForm, RolForm,
ServiceForm, ServiceForm,
UserRolForm UserRolForm,
SchemaForm,
) )
@ -451,6 +452,46 @@ class AdminSchemasView(SchemasMix):
return context return context
class AdminSchemasNewView(SchemasMix):
template_name = "idhub/admin/schemas_new.html"
subtitle = _('Upload Template')
icon = ''
success_url = reverse_lazy('idhub:admin_schemas')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'form': SchemaForm(),
})
return context
def post(self, request, *args, **kwargs):
form = SchemaForm(request.POST, request.FILES)
if form.is_valid():
schema = self.handle_uploaded_file()
if not schema:
messages.error(request, _("There are some errors in the file"))
return super().get(request, *args, **kwargs)
return redirect(self.success_url)
else:
return super().get(request, *args, **kwargs)
return super().post(request, *args, **kwargs)
def handle_uploaded_file(self):
f = self.request.FILES.get('file_template')
if not f:
return
file_name = f.name
if Schemas.objects.filter(file_schema=file_name).exists():
messages.error(self.request, _("This template already exists!"))
return
data = f.read().decode('utf-8')
schema = Schemas.objects.create(file_schema=file_name, data=data)
schema.save()
return schema
class AdminSchemasImportView(SchemasMix): class AdminSchemasImportView(SchemasMix):
template_name = "idhub/admin/schemas_import.html" template_name = "idhub/admin/schemas_import.html"
subtitle = _('Import Template') subtitle = _('Import Template')

View file

@ -26,7 +26,7 @@
</tbody> </tbody>
</table> </table>
<div class="form-actions-no-box"> <div class="form-actions-no-box">
<a class="btn btn-green-admin" href="{# url 'idhub:admin_schemas_new' #}">{% translate "Add template" %} <i class="bi bi-plus"></i></a> <a class="btn btn-green-admin" href="{% url 'idhub:admin_schemas_new' %}">{% translate "Add template" %} <i class="bi bi-plus"></i></a>
</div> </div>
</div> </div>
</div> </div>

View file

@ -0,0 +1,32 @@
{% extends "idhub/base_admin.html" %}
{% load i18n %}
{% block content %}
<h3>
<i class="{{ icon }}"></i>
{{ subtitle }}
</h3>
{% load django_bootstrap5 %}
<form role="form" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert">
<div class="icon"><span class="mdi mdi-close-circle-o"></span></div>
<div class="message">
<button class="close" type="button" data-dismiss="alert" aria-label="Close">
<span class="mdi mdi-close" aria-hidden="true"></span>
</button>
{% for field, error in form.errors.items %}
{{ error }}
{% endfor %}
</div>
</div>
{% endif %}
{% bootstrap_form form %}
<div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'idhub:admin_schemas_import' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
</form>
{% endblock %}

View file

@ -137,6 +137,8 @@ urlpatterns = [
name='admin_wallet_config_issue'), name='admin_wallet_config_issue'),
path('admin/schemas/', views_admin.AdminSchemasView.as_view(), path('admin/schemas/', views_admin.AdminSchemasView.as_view(),
name='admin_schemas'), name='admin_schemas'),
path('admin/schemas/new', views_admin.AdminSchemasNewView.as_view(),
name='admin_schemas_new'),
path('admin/schemas/import', views_admin.AdminSchemasImportView.as_view(), path('admin/schemas/import', views_admin.AdminSchemasImportView.as_view(),
name='admin_schemas_import'), name='admin_schemas_import'),
path('admin/schemas/import/<str:file_schema>', views_admin.AdminSchemasImportAddView.as_view(), path('admin/schemas/import/<str:file_schema>', views_admin.AdminSchemasImportAddView.as_view(),