107 lines
4.0 KiB
Python
107 lines
4.0 KiB
Python
import logging
|
|
from typing import Any
|
|
|
|
from apps.management.forms.workers.workshift_form import WorkShiftForm
|
|
from apps.workers.models.model_workshift import WorkShift
|
|
from django.contrib import messages
|
|
from django.db.models import Q, QuerySet
|
|
from django.http import HttpRequest, HttpResponseBase
|
|
from django.urls import reverse_lazy
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.views.generic import (
|
|
CreateView,
|
|
DeleteView,
|
|
ListView,
|
|
UpdateView,
|
|
)
|
|
|
|
logger = logging.getLogger("mfss")
|
|
path_template = "management/catalog/workshift/"
|
|
|
|
|
|
class WorkShiftListView(ListView):
|
|
model = WorkShift
|
|
template_name = f"{path_template}workshift_list.html"
|
|
context_object_name = "workshifts"
|
|
paginate_by = 20
|
|
ordering = ["name"]
|
|
|
|
def get_queryset(self) -> QuerySet:
|
|
queryset = super().get_queryset()
|
|
search_query = self.request.GET.get("search")
|
|
if search_query:
|
|
queryset = queryset.filter(
|
|
Q(name__icontains=search_query) | Q(is_weekday__icontains=search_query)
|
|
)
|
|
return queryset
|
|
|
|
def get_context_data(self, **kwargs: Any) -> dict:
|
|
context = super().get_context_data(**kwargs)
|
|
context["total_count"] = self.get_queryset().count()
|
|
context["search_query"] = self.request.GET.get("search", "")
|
|
context["title"] = _("Список смен")
|
|
return context
|
|
|
|
|
|
class WorkShiftCreateView(CreateView):
|
|
model = WorkShift
|
|
form_class = WorkShiftForm
|
|
template_name = f"{path_template}workshift_form.html"
|
|
success_url = reverse_lazy("management:eps:workshift-list")
|
|
|
|
def get_context_data(self, **kwargs: Any) -> dict:
|
|
context = super().get_context_data(**kwargs)
|
|
context["title"] = _("Создание новой смены")
|
|
context["action"] = _("Создать")
|
|
return context
|
|
|
|
def form_valid(self, form: WorkShiftForm) -> HttpResponseBase:
|
|
response = super().form_valid(form)
|
|
messages.success(self.request, _("Смена успешно создана"))
|
|
logger.info(f"Создана новая смена: {self.object.name}")
|
|
return response
|
|
|
|
|
|
class WorkShiftUpdateView(UpdateView):
|
|
model = WorkShift
|
|
form_class = WorkShiftForm
|
|
template_name = f"{path_template}workshift_form.html"
|
|
success_url = reverse_lazy("management:eps:workshift-list")
|
|
|
|
def get_context_data(self, **kwargs: Any) -> dict:
|
|
context = super().get_context_data(**kwargs)
|
|
context["title"] = _("Редактирование смены")
|
|
context["action"] = _("Сохранить")
|
|
return context
|
|
|
|
def form_valid(self, form: WorkShiftForm) -> HttpResponseBase:
|
|
response = super().form_valid(form)
|
|
messages.success(self.request, _("Смена успешно обновлена"))
|
|
logger.info(f"Обновлена смена: {self.object.name} (ID: {self.object.pk})")
|
|
return response
|
|
|
|
|
|
class WorkShiftDeleteView(DeleteView):
|
|
model = WorkShift
|
|
template_name = f"{path_template}confirm_delete.html"
|
|
success_url = reverse_lazy("management:eps:workshift-list")
|
|
context_object_name = "workshift"
|
|
|
|
def get_context_data(self, **kwargs: Any) -> dict:
|
|
context = super().get_context_data(**kwargs)
|
|
context["title"] = _("Удаление смены")
|
|
return context
|
|
|
|
def delete(
|
|
self, request: HttpRequest, *args: Any, **kwargs: Any
|
|
) -> HttpResponseBase:
|
|
try:
|
|
response = super().delete(request, *args, **kwargs)
|
|
messages.success(request, _("Смена успешно удалена"))
|
|
logger.info(f"Удалена смена: {self.object.name} (ID: {self.object.pk})")
|
|
return response
|
|
except Exception as e: # noqa: B902
|
|
messages.error(request, _("Ошибка при удалении смены"))
|
|
logger.error(f"Ошибка удаления смены: {str(e)}")
|
|
return self.get(request, *args, **kwargs)
|