made the long waited animation when add new truck or truck is completed. mouse over the list prevents the list from changing, only the animations work. moved top menu buttons to the right sidebar to free some vertical space. license text on button hover, not fixed above

This commit is contained in:
2026-10-03 16:40:36 +03:00
parent 4b0739ba06
commit 6cf4c74845
29 changed files with 2577 additions and 718 deletions
+71 -3
View File
@@ -2,6 +2,7 @@ from rest_framework import viewsets, status, mixins
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated, AllowAny
from .models import (ComPortReading, User, Report, ScaleProtocol, License,
StationSlot, generate_doc_number)
@@ -11,6 +12,7 @@ from .serializers import (
NomenclatureEntrySerializer, ReportSerializer, ExtraCombinationSerializer,
ScaleProtocolSerializer,
)
from django.db import transaction
from django.utils import timezone
from vehicles.models import Vehicle, ExtraCombination, combination_hash
from nomenclatures.models import Nomenclature, NomenclatureEntry
@@ -131,6 +133,24 @@ class ComPortReadingViewSet(viewsets.ModelViewSet):
serializer.save(source_ip=self.get_client_ip(self.request))
_WEIGHING_SUFFIXES = ('', '_date', '_user', '_manual', '_manual_user', '_manual_date', '_manual_note')
def _move_weighing(vehicle, src_prefix, dst_prefix):
"""Copy a recorded weighing to the other slot, carrying all its provenance.
The source slot is left untouched (callers overwrite or clear it as needed).
"""
for suffix in _WEIGHING_SUFFIXES:
setattr(vehicle, f'{dst_prefix}{suffix}', getattr(vehicle, f'{src_prefix}{suffix}'))
def _clear_weighing(vehicle, prefix):
"""Reset every field of a weighing slot to its empty state."""
for suffix in _WEIGHING_SUFFIXES:
setattr(vehicle, f'{prefix}{suffix}', False if suffix == '_manual' else None)
def _maybe_finalize(vehicle, user):
"""If both tare and gross are set and net hasn't been calculated yet, compute net and assign doc_number."""
if vehicle.tare is not None and vehicle.gross is not None and vehicle.net is None:
@@ -159,9 +179,7 @@ def _assign_weighing(vehicle, value, user, manual=False, note=None):
setattr(vehicle, f'{prefix}_manual_note', note if manual else None)
def move(src_prefix, dst_prefix):
"""Move a recorded weighing to the other slot, carrying all its provenance."""
for suffix in ('', '_date', '_user', '_manual', '_manual_user', '_manual_date', '_manual_note'):
setattr(vehicle, f'{dst_prefix}{suffix}', getattr(vehicle, f'{src_prefix}{suffix}'))
_move_weighing(vehicle, src_prefix, dst_prefix)
if vehicle.tare is not None and vehicle.gross is not None:
raise ValueError('Vehicle already has both weights.')
@@ -231,6 +249,7 @@ class VehicleViewSet(viewsets.ModelViewSet):
destroy: Disabled - vehicles are never deleted, see void() instead
by_number: Get vehicle by vehicle_number
measure: Record a weighing (app assigns tare vs gross by weight)
one_weight: Finalize a vehicle weighed once (weight becomes gross, net = gross)
void: Flag a completed vehicle as voided (corrective reversal, not a delete)
mark_notice_printed: Signal that a completed vehicle's notice was printed/downloaded
"""
@@ -333,6 +352,55 @@ class VehicleViewSet(viewsets.ModelViewSet):
sse_broadcast_update('vehicle', 'updated', serializer.data)
return Response(serializer.data)
@action(detail=True, methods=['post'], url_path='one-weight')
def one_weight(self, request, pk=None):
"""Finalize a vehicle weighed only once (external clients).
The single weight is treated as gross: net = gross, tare stays empty.
"""
if not request.user.can_measure:
return Response({'detail': 'You do not have permission to measure.'},
status=status.HTTP_403_FORBIDDEN)
station_key = (request.data.get('station_key') or '').strip()
if not _station_allowed(station_key):
limit = licensing.effective_station_limit()
return Response(
{'detail': f'This station is not licensed to measure '
f'(limit: {limit} stations).'},
status=status.HTTP_403_FORBIDDEN)
with transaction.atomic():
vehicle = get_object_or_404(self.get_queryset().select_for_update(of=('self',)),
pk=pk)
self.check_object_permissions(request, vehicle)
if vehicle.doc_number:
return Response({'detail': 'This vehicle is already finalized.'},
status=status.HTTP_400_BAD_REQUEST)
if vehicle.is_void:
return Response({'detail': 'A voided vehicle cannot be finalized.'},
status=status.HTTP_400_BAD_REQUEST)
if (vehicle.tare is None) == (vehicle.gross is None):
return Response(
{'detail': 'Exactly one weight must be recorded to finalize with one weight.'},
status=status.HTTP_400_BAD_REQUEST)
if vehicle.tare is not None:
_move_weighing(vehicle, 'tare', 'gross')
_clear_weighing(vehicle, 'tare')
now = timezone.now()
vehicle.net = vehicle.gross
vehicle.net_date = now
vehicle.net_user = request.user
vehicle.doc_number = generate_doc_number(now.year)
vehicle.save()
serializer = self.get_serializer(vehicle)
sse_broadcast_update('vehicle', 'updated', serializer.data)
return Response(serializer.data)
@action(detail=True, methods=['post'], url_path='void')
def void(self, request, pk=None):
"""Flag a completed vehicle as voided. Does not touch doc_number/net/tare/gross."""