devicehub-django/admin/views.py

201 lines
5.8 KiB
Python
Raw Permalink Normal View History

2024-10-14 15:02:40 +00:00
from smtplib import SMTPException
2024-10-07 14:56:24 +00:00
from django.urls import reverse_lazy
from django.shortcuts import get_object_or_404
2024-10-03 15:46:02 +00:00
from django.utils.translation import gettext_lazy as _
from django.views.generic.base import TemplateView
2024-10-07 14:56:24 +00:00
from django.views.generic.edit import (
CreateView,
UpdateView,
DeleteView,
)
2024-10-31 04:56:27 +00:00
from django.views.generic import FormView
import logging
2024-10-11 15:15:37 +00:00
from dashboard.mixins import DashboardView, Http403
2024-10-11 15:04:33 +00:00
from user.models import User, Institution
2024-10-14 15:02:40 +00:00
from admin.email import NotifyActivateUserByEmail
2024-11-01 07:36:24 +00:00
from admin.forms import CustomStatusLabelForm, CustomStatusValueForm, CustomStatusFormSet
from evidence.models import Annotation, AllowedValue
import uuid
2024-10-31 04:56:27 +00:00
logger = logging.getLogger('dhub')
2024-10-03 15:46:02 +00:00
2024-10-11 15:15:37 +00:00
class AdminView(DashboardView):
def get(self, *args, **kwargs):
response = super().get(*args, **kwargs)
if not self.request.user.is_admin:
raise Http403
return response
class PanelView(AdminView, TemplateView):
2024-10-03 15:46:02 +00:00
template_name = "admin_panel.html"
title = _("Admin")
breadcrumb = _("admin") + " /"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
2024-10-11 15:15:37 +00:00
class UsersView(AdminView, TemplateView):
2024-10-03 15:46:02 +00:00
template_name = "admin_users.html"
title = _("Users")
breadcrumb = _("admin / Users") + " /"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
"users": User.objects.filter()
})
return context
2024-10-07 14:56:24 +00:00
2024-10-14 15:02:40 +00:00
class CreateUserView(AdminView, NotifyActivateUserByEmail, CreateView):
2024-10-07 14:56:24 +00:00
template_name = "user.html"
title = _("User")
breadcrumb = _("admin / User") + " /"
success_url = reverse_lazy('admin:users')
model = User
fields = (
"email",
"password",
"is_admin",
)
def form_valid(self, form):
form.instance.institution = self.request.user.institution
form.instance.set_password(form.instance.password)
response = super().form_valid(form)
2024-10-14 15:02:40 +00:00
try:
self.send_email(form.instance)
except SMTPException as e:
messages.error(self.request, e)
2024-10-07 14:56:24 +00:00
return response
2024-10-11 15:15:37 +00:00
class DeleteUserView(AdminView, DeleteView):
2024-10-07 14:56:24 +00:00
template_name = "delete_user.html"
title = _("Delete user")
breadcrumb = "admin / Delete user"
success_url = reverse_lazy('admin:users')
model = User
fields = (
"email",
"password",
"is_admin",
)
def form_valid(self, form):
response = super().form_valid(form)
return response
2024-10-11 15:15:37 +00:00
class EditUserView(AdminView, UpdateView):
2024-10-07 14:56:24 +00:00
template_name = "user.html"
title = _("Edit user")
breadcrumb = "admin / Edit user"
success_url = reverse_lazy('admin:users')
model = User
fields = (
"email",
"is_admin",
)
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
self.object = get_object_or_404(self.model, pk=pk)
#self.object.set_password(self.object.password)
kwargs = super().get_form_kwargs()
return kwargs
2024-10-11 15:04:33 +00:00
2024-10-11 15:15:37 +00:00
class InstitutionView(AdminView, UpdateView):
2024-10-11 15:04:33 +00:00
template_name = "institution.html"
title = _("Edit institution")
section = "admin"
subtitle = _('Edit institution')
model = Institution
success_url = reverse_lazy('admin:panel')
fields = (
"name",
"logo",
"location",
"responsable_person",
"supervisor_person"
)
def get_form_kwargs(self):
self.object = self.request.user.institution
kwargs = super().get_form_kwargs()
return kwargs
2024-10-31 04:56:27 +00:00
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')
2024-11-01 07:36:24 +00:00
2024-10-31 04:56:27 +00:00
form_class = CustomStatusLabelForm
2024-11-01 07:36:24 +00:00
formset_class = CustomStatusFormSet
2024-10-31 04:56:27 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2024-11-01 07:36:24 +00:00
2024-10-31 04:56:27 +00:00
if self.request.POST:
2024-11-01 07:36:24 +00:00
context['form'] = self.form_class(self.request.POST)
2024-10-31 04:56:27 +00:00
context['formset'] = self.formset_class(self.request.POST)
else:
2024-11-01 07:36:24 +00:00
context['form'] = self.form_class()
2024-10-31 04:56:27 +00:00
context['formset'] = self.formset_class()
2024-11-01 07:36:24 +00:00
2024-10-31 04:56:27 +00:00
context['subtitle'] = _("Add Custom Status Label")
return context
def form_valid(self, form):
context = self.get_context_data()
formset = context['formset']
2024-11-01 07:36:24 +00:00
form = context['form']
if form.is_valid():
label_name = form.cleaned_data['label_name']
else:
return self.form_invalid(form)
2024-10-31 04:56:27 +00:00
if formset.is_valid():
2024-11-01 07:36:24 +00:00
annotation = Annotation.objects.create(
uuid=uuid.uuid4(),
owner=self.request.user.institution,
user=self.request.user,
type=Annotation.Type.ADMIN,
key=label_name,
value=label_name
)
first_state = None
2024-10-31 04:56:27 +00:00
for form in formset:
2024-11-01 07:36:24 +00:00
state = form.cleaned_data.get('label_state')
if state and not first_state:
first_state = state
annotation.value = state
annotation.save()
2024-10-31 04:56:27 +00:00
if state:
2024-11-01 07:36:24 +00:00
AllowedValue.objects.create(
annotation=annotation,
value=state
)
2024-10-31 04:56:27 +00:00
2024-11-01 07:36:24 +00:00
logger.info("Saving custom label to db: " + label_name)
2024-10-31 04:56:27 +00:00
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)
2024-11-01 07:36:24 +00:00
return self.form_invalid(form)