From e292caf85fabe22c083276c94ddae5ea7bbd67fd Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Sun, 22 Oct 2023 20:01:41 +0200 Subject: [PATCH] add upload schema system --- idhub/admin/forms.py | 4 ++ idhub/admin/views.py | 43 ++++++++++++++++++- .../templates/idhub/admin/schemas_import.html | 2 +- idhub/templates/idhub/admin/schemas_new.html | 32 ++++++++++++++ idhub/urls.py | 2 + 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 idhub/templates/idhub/admin/schemas_new.html diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py index fc66f14..9660341 100644 --- a/idhub/admin/forms.py +++ b/idhub/admin/forms.py @@ -25,3 +25,7 @@ class ServiceForm(forms.ModelForm): class UserRolForm(forms.ModelForm): MANDATORY_FIELDS = ['service'] + + +class SchemaForm(forms.Form): + file_template = forms.FileField() diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 50c492f..c0ac73a 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -19,7 +19,8 @@ from idhub.admin.forms import ( MembershipForm, RolForm, ServiceForm, - UserRolForm + UserRolForm, + SchemaForm, ) @@ -451,6 +452,46 @@ class AdminSchemasView(SchemasMix): 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): template_name = "idhub/admin/schemas_import.html" subtitle = _('Import Template') diff --git a/idhub/templates/idhub/admin/schemas_import.html b/idhub/templates/idhub/admin/schemas_import.html index e301e76..b20608b 100644 --- a/idhub/templates/idhub/admin/schemas_import.html +++ b/idhub/templates/idhub/admin/schemas_import.html @@ -26,7 +26,7 @@
- {% translate "Add template" %} + {% translate "Add template" %}
diff --git a/idhub/templates/idhub/admin/schemas_new.html b/idhub/templates/idhub/admin/schemas_new.html new file mode 100644 index 0000000..83ad176 --- /dev/null +++ b/idhub/templates/idhub/admin/schemas_new.html @@ -0,0 +1,32 @@ +{% extends "idhub/base_admin.html" %} +{% load i18n %} + +{% block content %} +

+ + {{ subtitle }} +

+{% load django_bootstrap5 %} +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +{% bootstrap_form form %} + + +
+{% endblock %} diff --git a/idhub/urls.py b/idhub/urls.py index ea47b24..963d171 100644 --- a/idhub/urls.py +++ b/idhub/urls.py @@ -137,6 +137,8 @@ urlpatterns = [ name='admin_wallet_config_issue'), path('admin/schemas/', views_admin.AdminSchemasView.as_view(), 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(), name='admin_schemas_import'), path('admin/schemas/import/', views_admin.AdminSchemasImportAddView.as_view(),