devicehub-django/lot/views.py

228 lines
6.8 KiB
Python
Raw Normal View History

2024-07-09 11:35:35 +00:00
from django.urls import reverse_lazy
from django.shortcuts import get_object_or_404
from django.utils.translation import gettext_lazy as _
2024-07-10 11:55:57 +00:00
from django.views.generic.base import TemplateView
2024-07-09 11:35:35 +00:00
from django.views.generic.edit import (
CreateView,
2024-07-10 11:55:57 +00:00
DeleteView,
2024-07-09 11:35:35 +00:00
UpdateView,
2024-07-19 15:40:01 +00:00
FormView,
2024-07-09 11:35:35 +00:00
)
2024-07-09 15:31:24 +00:00
from dashboard.mixins import DashboardView
2024-07-30 11:37:08 +00:00
from lot.models import Lot, LotTag, LotAnnotation
2024-07-09 15:31:24 +00:00
from lot.forms import LotsForm
2024-06-12 07:32:49 +00:00
2024-07-09 11:35:35 +00:00
class NewLotView(DashboardView, CreateView):
template_name = "new_lot.html"
title = _("New lot")
breadcrumb = "lot / New lot"
success_url = reverse_lazy('dashboard:unassigned_devices')
model = Lot
fields = (
"type",
"name",
"code",
"description",
"closed",
)
def form_valid(self, form):
form.instance.owner = self.request.user
response = super().form_valid(form)
return response
2024-07-10 11:55:57 +00:00
class DeleteLotView(DashboardView, DeleteView):
template_name = "delete_lot.html"
title = _("Delete lot")
breadcrumb = "lot / Delete lot"
success_url = reverse_lazy('dashboard:unassigned_devices')
model = Lot
fields = (
"type",
"name",
"code",
"description",
"closed",
)
def form_valid(self, form):
response = super().form_valid(form)
return response
2024-07-09 11:35:35 +00:00
class EditLotView(DashboardView, UpdateView):
template_name = "new_lot.html"
2024-07-10 11:55:57 +00:00
title = _("Edit lot")
breadcrumb = "Lot / Edit lot"
2024-07-09 11:35:35 +00:00
success_url = reverse_lazy('dashboard:unassigned_devices')
model = Lot
fields = (
"type",
"name",
"code",
"description",
"closed",
)
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
self.object = get_object_or_404(self.model, pk=pk)
# self.success_url = reverse_lazy('dashbiard:lot', args=[pk])
kwargs = super().get_form_kwargs()
return kwargs
2024-07-09 15:31:24 +00:00
2024-07-10 11:55:57 +00:00
2024-07-09 15:31:24 +00:00
class AddToLotView(DashboardView, FormView):
template_name = "list_lots.html"
title = _("Add to lots")
breadcrumb = "lot / add to lots"
success_url = reverse_lazy('dashboard:unassigned_devices')
form_class = LotsForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
lots = Lot.objects.filter(owner=self.request.user)
2024-07-18 15:21:22 +00:00
lot_tags = LotTag.objects.filter(owner=self.request.user)
2024-07-09 15:31:24 +00:00
context.update({
'lots': lots,
2024-07-18 15:21:22 +00:00
'lot_tags':lot_tags,
2024-07-09 15:31:24 +00:00
})
return context
def get_form(self):
form = super().get_form()
form.fields["lots"].queryset = Lot.objects.filter(owner=self.request.user)
return form
def form_valid(self, form):
form.devices = self.get_session_devices()
form.save()
response = super().form_valid(form)
return response
2024-07-10 08:24:40 +00:00
class DelToLotView(AddToLotView):
title = _("Remove from lots")
breadcrumb = "lot / remove from lots"
def form_valid(self, form):
form.devices = self.get_session_devices()
form.remove()
response = super().form_valid(form)
return response
2024-07-18 15:21:22 +00:00
class LotsTagsView(DashboardView, TemplateView):
2024-07-10 11:55:57 +00:00
template_name = "lots.html"
2024-07-18 15:21:22 +00:00
title = _("lots")
breadcrumb = _("lots") + " /"
2024-07-10 11:55:57 +00:00
success_url = reverse_lazy('dashboard:unassigned_devices')
def get_context_data(self, **kwargs):
2024-07-18 15:21:22 +00:00
self.pk = kwargs.get('pk')
2024-07-10 11:55:57 +00:00
context = super().get_context_data(**kwargs)
2024-07-18 15:21:22 +00:00
tag = get_object_or_404(LotTag, owner=self.request.user, id=self.pk)
self.title += " {}".format(tag.name)
self.breadcrumb += " {}".format(tag.name)
lots = Lot.objects.filter(owner=self.request.user).filter(type=tag)
2024-07-10 11:55:57 +00:00
context.update({
2024-07-18 15:21:22 +00:00
'lots': lots,
'title': self.title,
'breadcrumb': self.breadcrumb
2024-07-10 11:55:57 +00:00
})
return context
2024-07-30 11:37:08 +00:00
class LotAddDocumentView(DashboardView, CreateView):
template_name = "new_annotation.html"
title = _("New Document")
breadcrumb = "Device / New document"
success_url = reverse_lazy('dashboard:unassigned_devices')
model = LotAnnotation
fields = ("key", "value")
def form_valid(self, form):
form.instance.owner = self.request.user
form.instance.lot = self.lot
form.instance.type = LotAnnotation.Type.DOCUMENT
response = super().form_valid(form)
return response
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
self.lot = get_object_or_404(Lot, pk=pk, owner=self.request.user)
self.success_url = reverse_lazy('lot:documents', args=[pk])
kwargs = super().get_form_kwargs()
return kwargs
class LotDocumentsView(DashboardView, TemplateView):
template_name = "documents.html"
title = _("New Document")
breadcrumb = "Device / New document"
def get_context_data(self, **kwargs):
self.pk = kwargs.get('pk')
context = super().get_context_data(**kwargs)
lot = get_object_or_404(Lot, owner=self.request.user, id=self.pk)
documents = LotAnnotation.objects.filter(
lot=lot,
owner=self.request.user,
type=LotAnnotation.Type.DOCUMENT,
)
context.update({
'lot': lot,
'documents': documents,
'title': self.title,
'breadcrumb': self.breadcrumb
})
return context
class LotAnnotationsView(DashboardView, TemplateView):
template_name = "annotations.html"
title = _("New Annotation")
breadcrumb = "Device / New annotation"
def get_context_data(self, **kwargs):
self.pk = kwargs.get('pk')
context = super().get_context_data(**kwargs)
lot = get_object_or_404(Lot, owner=self.request.user, id=self.pk)
annotations = LotAnnotation.objects.filter(
lot=lot,
owner=self.request.user,
type=LotAnnotation.Type.USER,
)
context.update({
'lot': lot,
'annotations': annotations,
'title': self.title,
'breadcrumb': self.breadcrumb
})
return context
class LotAddAnnotationView(DashboardView, CreateView):
template_name = "new_annotation.html"
title = _("New Annotation")
breadcrumb = "Device / New annotation"
success_url = reverse_lazy('dashboard:unassigned_devices')
model = LotAnnotation
fields = ("key", "value")
def form_valid(self, form):
form.instance.owner = self.request.user
form.instance.lot = self.lot
form.instance.type = LotAnnotation.Type.USER
response = super().form_valid(form)
return response
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
self.lot = get_object_or_404(Lot, pk=pk, owner=self.request.user)
self.success_url = reverse_lazy('lot:annotations', args=[pk])
kwargs = super().get_form_kwargs()
return kwargs