Files
MES_Core/shiftflow/views.py
ackFromRedmi 191d06d7d3
Some checks failed
Deploy MES Core / deploy (push) Failing after 1s
Поменял структуру моделей
2026-03-29 16:04:02 +03:00

64 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.views.generic import TemplateView, ListView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Item # Проверь, как точно называется твоя модель деталей/заказов
# Класс главной страницы (роутер)
class IndexView(TemplateView):
template_name = 'shiftflow/landing.html'
def get(self, request, *args, **kwargs):
# Если юзер авторизован — сразу отправляем его в реестр
if request.user.is_authenticated:
return redirect('registry')
# Если нет — показываем кнопку "Войти"
return super().get(request, *args, **kwargs)
# Класс реестра деталей (защищен LoginRequiredMixin)
class RegistryView(LoginRequiredMixin, ListView):
model = Item
template_name = 'shiftflow/registry.html'
context_object_name = 'items'
def get_queryset(self):
# Оптимизируем запросы, подгружая связанные данные сразу
queryset = Item.objects.select_related('task', 'task__deal', 'task__material', 'machine').all()
# Если это оператор, показываем только задания для его станков
if hasattr(self.request.user, 'profile') and self.request.user.profile.role == 'operator':
user_machines = self.request.user.profile.machines.all()
if user_machines.exists():
queryset = queryset.filter(machine__in=user_machines)
return queryset.order_by('status', '-date', 'machine__name', 'task__deal__number')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Передаем роль в шаблон, чтобы скрывать/показывать кнопки
if hasattr(self.request.user, 'profile'):
context['user_role'] = self.request.user.profile.role
return context
# Вьюха детального вида и редактирования
class ItemUpdateView(LoginRequiredMixin, UpdateView):
model = Item
template_name = 'shiftflow/item_detail.html'
# Перечисляем поля, которые можно редактировать в сменке
fields = [
'machine', 'quantity_plan', 'quantity_fact',
'status', 'is_synced_1c',
'material_taken', 'usable_waste', 'scrap_weight'
]
context_object_name = 'item'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Обязательно добавляем роль в контекст этого шаблона!
if hasattr(self.request.user, 'profile'):
context['user_role'] = self.request.user.profile.role
return context
def get_success_url(self):
# После сохранения возвращаемся в реестр
return reverse_lazy('registry')