From 7b554c1b1fb7eda00fcfb3c1bafbdcd1018bb7b7 Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Thu, 31 Oct 2024 01:56:27 -0300 Subject: [PATCH] view and forms added --- admin/forms.py | 20 +++++++++++++ admin/templates/admin_panel.html | 6 ++++ admin/views.py | 49 ++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 admin/forms.py diff --git a/admin/forms.py b/admin/forms.py new file mode 100644 index 0000000..b1e2c88 --- /dev/null +++ b/admin/forms.py @@ -0,0 +1,20 @@ +from django import forms +from utils.device import create_annotation, create_doc, create_index +from utils.save_snapshots import move_json, save_in_disk + +from django.forms import formset_factory + +class CustomStatusLabelForm(forms.Form): + annotation_name = forms.CharField( + label="Annotation Name", + max_length=50, + widget=forms.TextInput(attrs={'class': 'form-control'}) + ) + annotation_state = forms.CharField( + label="Possible State", + max_length=50, + widget=forms.TextInput(attrs={'class': 'form-control'}) + ) + + +CustomStatusLabelFormSet = formset_factory(CustomStatusLabelForm, extra=1) diff --git a/admin/templates/admin_panel.html b/admin/templates/admin_panel.html index 3dd5ed1..54aa1ad 100644 --- a/admin/templates/admin_panel.html +++ b/admin/templates/admin_panel.html @@ -10,9 +10,15 @@
diff --git a/admin/views.py b/admin/views.py index 9ce25c7..26a2a2c 100644 --- a/admin/views.py +++ b/admin/views.py @@ -8,9 +8,15 @@ from django.views.generic.edit import ( UpdateView, DeleteView, ) +from django.views.generic import FormView +import logging from dashboard.mixins import DashboardView, Http403 from user.models import User, Institution from admin.email import NotifyActivateUserByEmail +from admin.forms import CustomStatusLabelForm, CustomStatusLabelFormSet +from evidence.models import Annotation + +logger = logging.getLogger('dhub') class AdminView(DashboardView): @@ -124,3 +130,46 @@ class InstitutionView(AdminView, UpdateView): self.object = self.request.user.institution kwargs = super().get_form_kwargs() return kwargs + + + +class AddReservedAnnotationView(AdminView, FormView): + template_name = "reserved.html" + title = _("New Custom State Labels") + breadcrumb = "Admin / Custom State Labels (new name?)" + success_url = reverse_lazy('admin:panel') + model = Annotation + + form_class = CustomStatusLabelForm + formset_class = CustomStatusLabelFormSet + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + if self.request.POST: + context['formset'] = self.formset_class(self.request.POST) + else: + context['formset'] = self.formset_class() + context['subtitle'] = _("Add Custom Status Label") + return context + + def form_valid(self, form): + context = self.get_context_data() + formset = context['formset'] + if formset.is_valid(): + annotation_name = form.cleaned_data['annotation_name'] + + logger.info("Saving to db: " + annotation_name) + annotation = Annotation.objects.create(name=annotation_name) + for form in formset: + state = form.cleaned_data.get('annotation_state') + if state: + PossibleValue.objects.create(annotation=annotation, value=state) + + logger.info("Saved to db: " + annotation_name) + self.success_message = _("Custom status label has been added.") + self.success_url = reverse_lazy('admin:panel') + return super().form_valid(form) + else: + logger.error("Formset is not valid") + logger.error(formset.errors) + return self.form_invalid(form) \ No newline at end of file