2025-01-31 12:39:03 +00:00
|
|
|
from django.db import IntegrityError
|
2024-07-09 11:35:35 +00:00
|
|
|
from django.urls import reverse_lazy
|
2024-12-18 12:55:00 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, Http404
|
|
|
|
from django.contrib import messages
|
2024-07-09 11:35:35 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2024-07-10 11:55:57 +00:00
|
|
|
from django.views.generic.base import TemplateView
|
2025-02-21 17:40:05 +00:00
|
|
|
from django.db.models import Q
|
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
|
|
|
)
|
2025-02-21 17:40:05 +00:00
|
|
|
import django_tables2 as tables
|
2024-07-09 15:31:24 +00:00
|
|
|
from dashboard.mixins import DashboardView
|
2024-11-16 15:31:02 +00:00
|
|
|
from lot.models import Lot, LotTag, LotProperty
|
2024-07-09 15:31:24 +00:00
|
|
|
from lot.forms import LotsForm
|
2024-07-09 11:35:35 +00:00
|
|
|
|
|
|
|
class NewLotView(DashboardView, CreateView):
|
|
|
|
template_name = "new_lot.html"
|
|
|
|
title = _("New lot")
|
|
|
|
breadcrumb = "lot / New lot"
|
2025-02-04 14:48:47 +00:00
|
|
|
success_url = reverse_lazy('dashboard:unassigned')
|
2024-07-09 11:35:35 +00:00
|
|
|
model = Lot
|
|
|
|
fields = (
|
|
|
|
"type",
|
|
|
|
"name",
|
|
|
|
"code",
|
|
|
|
"description",
|
|
|
|
"closed",
|
|
|
|
)
|
|
|
|
|
2025-02-17 12:05:41 +00:00
|
|
|
def get_form(self):
|
|
|
|
form = super().get_form()
|
|
|
|
form.fields["type"].queryset = LotTag.objects.filter(
|
|
|
|
owner=self.request.user.institution,
|
|
|
|
inbox=False
|
|
|
|
)
|
|
|
|
return form
|
|
|
|
|
2024-07-09 11:35:35 +00:00
|
|
|
def form_valid(self, form):
|
2025-02-24 14:44:19 +00:00
|
|
|
try:
|
|
|
|
form.instance.owner = self.request.user.institution
|
|
|
|
form.instance.user = self.request.user
|
|
|
|
response = super().form_valid(form)
|
|
|
|
return response
|
|
|
|
|
|
|
|
except IntegrityError:
|
|
|
|
messages.error(self.request, _("Lot name is already defined."))
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
2024-07-09 11:35:35 +00:00
|
|
|
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"
|
2025-02-04 14:48:47 +00:00
|
|
|
success_url = reverse_lazy('dashboard:unassigned')
|
2024-07-10 11:55:57 +00:00
|
|
|
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"
|
2025-02-04 14:48:47 +00:00
|
|
|
success_url = reverse_lazy('dashboard:unassigned')
|
2024-07-09 11:35:35 +00:00
|
|
|
model = Lot
|
|
|
|
fields = (
|
|
|
|
"type",
|
|
|
|
"name",
|
|
|
|
"code",
|
|
|
|
"description",
|
|
|
|
"closed",
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
pk = self.kwargs.get('pk')
|
2024-10-09 16:00:56 +00:00
|
|
|
self.object = get_object_or_404(
|
|
|
|
self.model,
|
|
|
|
owner=self.request.user.institution,
|
|
|
|
pk=pk,
|
|
|
|
)
|
2024-07-09 11:35:35 +00:00
|
|
|
# self.success_url = reverse_lazy('dashbiard:lot', args=[pk])
|
|
|
|
kwargs = super().get_form_kwargs()
|
|
|
|
return kwargs
|
2024-07-09 15:31:24 +00:00
|
|
|
|
2025-02-20 17:10:37 +00:00
|
|
|
def get_form(self):
|
|
|
|
form = super().get_form()
|
|
|
|
form.fields["type"].queryset = LotTag.objects.filter(
|
|
|
|
owner=self.request.user.institution,
|
|
|
|
inbox=False
|
|
|
|
)
|
|
|
|
return form
|
|
|
|
|
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"
|
2025-02-04 14:48:47 +00:00
|
|
|
success_url = reverse_lazy('dashboard:unassigned')
|
2024-07-09 15:31:24 +00:00
|
|
|
form_class = LotsForm
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2024-10-09 16:00:56 +00:00
|
|
|
lots = Lot.objects.filter(owner=self.request.user.institution)
|
|
|
|
lot_tags = LotTag.objects.filter(owner=self.request.user.institution)
|
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()
|
2025-01-31 12:39:03 +00:00
|
|
|
form.fields["lots"].queryset = Lot.objects.filter(
|
|
|
|
owner=self.request.user.institution)
|
2024-07-09 15:31:24 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2025-02-21 17:40:05 +00:00
|
|
|
class LotTable(tables.Table):
|
|
|
|
name = tables.Column(linkify=("dashboard:lot", {"pk": tables.A("id")}), verbose_name=_("Lot Name"))
|
|
|
|
description = tables.Column(verbose_name=_("Description"), default="No description")
|
|
|
|
closed = tables.Column(verbose_name=_("Status"))
|
|
|
|
created = tables.DateColumn(format="Y-m-d", verbose_name=_("Created On"))
|
|
|
|
user = tables.Column(verbose_name=_("Created By"), default="Unknown")
|
|
|
|
actions = tables.TemplateColumn(
|
|
|
|
template_name="lot_actions.html",
|
|
|
|
verbose_name=_("Actions"),
|
|
|
|
orderable=False,
|
|
|
|
attrs={"td": {"class": "text-end"}}
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Lot
|
|
|
|
fields = ("name", "description", "closed", "created", "user", "actions")
|
|
|
|
attrs = {
|
|
|
|
"class": "table table-hover align-middle",
|
|
|
|
"thead": {"class": "table-light"}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class LotsTagsView(DashboardView, tables.SingleTableView):
|
2024-07-10 11:55:57 +00:00
|
|
|
template_name = "lots.html"
|
2024-07-18 15:21:22 +00:00
|
|
|
title = _("lots")
|
|
|
|
breadcrumb = _("lots") + " /"
|
2025-02-04 14:48:47 +00:00
|
|
|
success_url = reverse_lazy('dashboard:unassigned')
|
2025-02-21 17:40:05 +00:00
|
|
|
model = Lot
|
|
|
|
table_class = LotTable
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
self.pk = self.kwargs.get('pk')
|
|
|
|
self.tag = get_object_or_404(LotTag, owner=self.request.user.institution, id=self.pk)
|
2025-02-21 23:45:39 +00:00
|
|
|
self.show_open = self.request.GET.get('show_open', 'false') == 'true'
|
|
|
|
self.show_closed = self.request.GET.get('show_closed', 'false')
|
2025-02-21 17:40:05 +00:00
|
|
|
self.search_query = self.request.GET.get('q', '').strip()
|
|
|
|
|
|
|
|
queryset = Lot.objects.filter(owner=self.request.user.institution, type=self.tag)
|
|
|
|
|
2025-02-21 23:45:39 +00:00
|
|
|
if self.show_closed == 'true':
|
2025-02-21 17:40:05 +00:00
|
|
|
queryset = queryset.filter(closed=True)
|
2025-02-21 23:45:39 +00:00
|
|
|
elif self.show_closed == 'false':
|
|
|
|
queryset = queryset.filter(closed=False)
|
2025-02-21 17:40:05 +00:00
|
|
|
|
|
|
|
if self.search_query:
|
|
|
|
queryset = queryset.filter(
|
|
|
|
Q(name__icontains=self.search_query) |
|
|
|
|
Q(description__icontains=self.search_query) |
|
|
|
|
Q(code__icontains=self.search_query)
|
|
|
|
)
|
|
|
|
|
|
|
|
sort = self.request.GET.get('sort')
|
|
|
|
if sort:
|
|
|
|
queryset = queryset.order_by(sort)
|
|
|
|
|
|
|
|
return queryset
|
|
|
|
|
2024-07-10 11:55:57 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context.update({
|
2025-02-21 17:40:05 +00:00
|
|
|
'title': self.title + " " + self.tag.name,
|
|
|
|
'breadcrumb': self.breadcrumb + " " + self.tag.name,
|
|
|
|
'show_closed': self.show_closed,
|
2025-02-21 23:45:39 +00:00
|
|
|
'search_query': self.search_query,
|
2024-07-10 11:55:57 +00:00
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
2024-11-16 15:31:02 +00:00
|
|
|
class LotPropertiesView(DashboardView, TemplateView):
|
|
|
|
template_name = "properties.html"
|
|
|
|
title = _("New Lot Property")
|
|
|
|
breadcrumb = "Lot / New property"
|
2024-08-01 12:33:09 +00:00
|
|
|
|
2024-07-30 11:37:08 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
self.pk = kwargs.get('pk')
|
|
|
|
context = super().get_context_data(**kwargs)
|
2024-10-09 16:00:56 +00:00
|
|
|
lot = get_object_or_404(Lot, owner=self.request.user.institution, id=self.pk)
|
2024-11-16 15:31:02 +00:00
|
|
|
properties = LotProperty.objects.filter(
|
2024-07-30 11:37:08 +00:00
|
|
|
lot=lot,
|
2024-10-09 16:00:56 +00:00
|
|
|
owner=self.request.user.institution,
|
2024-12-10 19:53:07 +00:00
|
|
|
type=LotProperty.Type.USER,
|
2024-07-30 11:37:08 +00:00
|
|
|
)
|
|
|
|
context.update({
|
|
|
|
'lot': lot,
|
2024-11-16 15:31:02 +00:00
|
|
|
'properties': properties,
|
2024-07-30 11:37:08 +00:00
|
|
|
'title': self.title,
|
|
|
|
'breadcrumb': self.breadcrumb
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
2024-08-01 12:33:09 +00:00
|
|
|
|
2024-12-18 12:55:00 +00:00
|
|
|
class AddLotPropertyView(DashboardView, CreateView):
|
2024-11-16 15:31:02 +00:00
|
|
|
template_name = "new_property.html"
|
|
|
|
title = _("New Lot Property")
|
|
|
|
breadcrumb = "Device / New property"
|
2024-07-30 11:37:08 +00:00
|
|
|
success_url = reverse_lazy('dashboard:unassigned_devices')
|
2024-11-16 15:31:02 +00:00
|
|
|
model = LotProperty
|
2024-07-30 11:37:08 +00:00
|
|
|
fields = ("key", "value")
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2024-10-09 16:00:56 +00:00
|
|
|
form.instance.owner = self.request.user.institution
|
|
|
|
form.instance.user = self.request.user
|
2024-07-30 11:37:08 +00:00
|
|
|
form.instance.lot = self.lot
|
2024-12-10 19:53:07 +00:00
|
|
|
form.instance.type = LotProperty.Type.USER
|
2025-01-31 12:39:03 +00:00
|
|
|
try:
|
|
|
|
response = super().form_valid(form)
|
|
|
|
messages.success(self.request, _("Property successfully added."))
|
|
|
|
return response
|
|
|
|
except IntegrityError:
|
|
|
|
messages.error(self.request, _("Property is already defined."))
|
|
|
|
return self.form_invalid(form)
|
2024-07-30 11:37:08 +00:00
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
pk = self.kwargs.get('pk')
|
2024-10-09 16:00:56 +00:00
|
|
|
self.lot = get_object_or_404(Lot, pk=pk, owner=self.request.user.institution)
|
2024-11-16 15:31:02 +00:00
|
|
|
self.success_url = reverse_lazy('lot:properties', args=[pk])
|
2024-07-30 11:37:08 +00:00
|
|
|
kwargs = super().get_form_kwargs()
|
|
|
|
return kwargs
|
2024-12-18 12:55:00 +00:00
|
|
|
|
2025-01-31 12:39:03 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['lot_id'] = self.lot.id
|
|
|
|
return context
|
|
|
|
|
2024-12-18 12:55:00 +00:00
|
|
|
|
|
|
|
class UpdateLotPropertyView(DashboardView, UpdateView):
|
|
|
|
template_name = "properties.html"
|
|
|
|
title = _("Update lot Property")
|
|
|
|
breadcrumb = "Lot / Update Property"
|
|
|
|
model = LotProperty
|
|
|
|
fields = ("key", "value")
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
pk = self.kwargs.get('pk')
|
2025-01-31 12:39:03 +00:00
|
|
|
lot_property = get_object_or_404(
|
|
|
|
LotProperty,
|
|
|
|
pk=pk,
|
|
|
|
owner=self.request.user.institution
|
|
|
|
)
|
2024-12-18 12:55:00 +00:00
|
|
|
|
|
|
|
if not lot_property:
|
|
|
|
raise Http404
|
|
|
|
|
2025-01-31 12:39:03 +00:00
|
|
|
lot_pk = lot_property.lot.pk
|
|
|
|
self.success_url = reverse_lazy('lot:properties', args=[lot_pk])
|
2024-12-18 12:55:00 +00:00
|
|
|
kwargs = super().get_form_kwargs()
|
|
|
|
kwargs['instance'] = lot_property
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2025-01-31 12:39:03 +00:00
|
|
|
try:
|
|
|
|
response = super().form_valid(form)
|
|
|
|
messages.success(self.request, _("Property updated successfully."))
|
|
|
|
return response
|
|
|
|
except IntegrityError:
|
|
|
|
messages.error(self.request, _("Property is already defined."))
|
|
|
|
return self.form_invalid(form)
|
2024-12-18 12:55:00 +00:00
|
|
|
|
2025-01-31 12:39:03 +00:00
|
|
|
def form_invalid(self, form):
|
|
|
|
super().form_invalid(form)
|
|
|
|
return redirect(self.get_success_url())
|
2024-12-18 12:55:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteLotPropertyView(DashboardView, DeleteView):
|
|
|
|
model = LotProperty
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.pk = kwargs['pk']
|
|
|
|
self.object = get_object_or_404(
|
|
|
|
self.model,
|
|
|
|
pk=self.pk,
|
|
|
|
owner=self.request.user.institution
|
|
|
|
)
|
2025-01-31 12:39:03 +00:00
|
|
|
lot_pk = self.object.lot.pk
|
2024-12-18 12:55:00 +00:00
|
|
|
self.object.delete()
|
|
|
|
messages.success(self.request, _("Lot property deleted successfully."))
|
2025-01-31 12:39:03 +00:00
|
|
|
self.success_url = reverse_lazy('lot:properties', args=[lot_pk])
|
2024-12-18 12:55:00 +00:00
|
|
|
|
|
|
|
# Redirect back to the original URL
|
2025-01-31 12:39:03 +00:00
|
|
|
return redirect(self.success_url)
|