view and forms added

This commit is contained in:
Thomas Nahuel Rusiecki 2024-10-31 01:56:27 -03:00
parent 25d7a0de63
commit 7b554c1b1f
3 changed files with 75 additions and 0 deletions

20
admin/forms.py Normal file
View File

@ -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)

View File

@ -10,9 +10,15 @@
<div class="row">
<div class="col">
<a href="{% url 'admin:institution' user.institution.pk %}" class="btn btn-green-admin">
{% translate "Institution" %}
</a>
<a href="{% url 'admin:reserved'%}" class="btn btn-green-admin">
{% translate "Reserved Annotations" %}
</a>
</div>
</div>

View File

@ -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)