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 @@
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 %} +