From a33eadb877df58432262ef411b554c9e6a08a929 Mon Sep 17 00:00:00 2001 From: lexxai Date: Tue, 16 Apr 2024 13:20:46 +0300 Subject: [PATCH] PHOTOS - recover bug of registration_id check, now DEMO mode --- FRONTEND/fastparking/photos/forms.py | 16 +++++--- FRONTEND/fastparking/photos/repository.py | 40 +++++++++++-------- .../photos/templates/photos/upload.html | 27 +++++++++---- FRONTEND/fastparking/photos/views.py | 10 ++--- 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/FRONTEND/fastparking/photos/forms.py b/FRONTEND/fastparking/photos/forms.py index d48e6c5f..ae272c2e 100644 --- a/FRONTEND/fastparking/photos/forms.py +++ b/FRONTEND/fastparking/photos/forms.py @@ -10,7 +10,7 @@ class UploadFileForm(forms.Form): choices = TYPES.items() # [(k, v) for k, v in TYPES.items()] - type = forms.ChoiceField( + t_photo = forms.ChoiceField( choices=choices, widget=forms.Select( attrs={ @@ -57,6 +57,12 @@ class UploadFileForm(forms.Form): def clean(self): cleaned_data = super().clean() + t_photo = cleaned_data.get("t_photo") + if t_photo == "0": + cleaned_data["registration_id"] = None + cleaned_data["manual_registration_id"] = None + return cleaned_data + registration_id = cleaned_data.get("registration_id") manual_registration_id = cleaned_data.get("manual_registration_id") @@ -79,10 +85,10 @@ def clean(self): def clean_manual_registration_id(self): manual_registration_id = self.cleaned_data.get("manual_registration_id") - if manual_registration_id: - # Check if the manual_registration_id exists in the list of Registration.registration_id - if not Registration.objects.filter(pk=manual_registration_id).exists(): - raise forms.ValidationError("Entered registration ID does not exist.") + # if manual_registration_id: + # Check if the manual_registration_id exists in the list of Registration.registration_id + # if not Registration.objects.filter(pk=manual_registration_id).exists(): + # raise forms.ValidationError("Entered registration ID does not exist.") return manual_registration_id # class Meta: diff --git a/FRONTEND/fastparking/photos/repository.py b/FRONTEND/fastparking/photos/repository.py index 83fdbc25..2fd13cc8 100644 --- a/FRONTEND/fastparking/photos/repository.py +++ b/FRONTEND/fastparking/photos/repository.py @@ -1,4 +1,5 @@ import base64 +import random from datetime import datetime from io import BytesIO from pathlib import Path @@ -49,8 +50,12 @@ def save_image( destination.write(chunk) -def registration_car(utc_datetime, registration_data): +def registration_car(utc_datetime, registration_data) -> dict: print(f"registration_car: {utc_datetime=}, {registration_data=}") + # DEMO MODE + if registration_data.get("type") == "0": + registration_data["registration_id"] = random.randint(1, 999999) + return registration_data def build_base64_image(binary_image_data): @@ -126,19 +131,22 @@ def handle_uploaded_file( if binary_image_data: base64_image = build_base64_image(binary_image_data) predict["num_img"] = base64_image - date_formated = utc_datetime.strftime("%Y-%m-%d %H:%M:%S UTC") - registration_id = 1 - registration_id_formatted = f"{registration_id:06}" - parking_place = "L01-01" - reg_info = f"id:{registration_id},place:{parking_place},date:{int(utc_datetime.timestamp())}|" - encoded_text = sign_text(reg_info) - hash_code = encoded_text.split("|:")[-1] - qrcode_img = build_qrcode(encoded_text) - registration = { - "id": registration_id_formatted, - "parking_place": parking_place, - "qr_code": qrcode_img, - "date": date_formated, - "hash": hash_code, - } + if registration_result: + registration_id = registration_result.get("registration_id") + registration = None + if registration_id: + date_formated = utc_datetime.strftime("%Y-%m-%d %H:%M:%S UTC") + registration_id_formatted = f"{registration_id:06}" + parking_place = "L01-01" + reg_info = f"id:{registration_id},place:{parking_place},date:{int(utc_datetime.timestamp())}|" + encoded_text = sign_text(reg_info) + hash_code = encoded_text.split("|:")[-1] + qrcode_img = build_qrcode(encoded_text) + registration = { + "id": registration_id_formatted, + "parking_place": parking_place, + "qr_code": qrcode_img, + "date": date_formated, + "hash": hash_code, + } return {"info": info, "predict": predict, "registration": registration} diff --git a/FRONTEND/fastparking/photos/templates/photos/upload.html b/FRONTEND/fastparking/photos/templates/photos/upload.html index 294fd6e4..d8bd35d6 100644 --- a/FRONTEND/fastparking/photos/templates/photos/upload.html +++ b/FRONTEND/fastparking/photos/templates/photos/upload.html @@ -5,14 +5,27 @@

Upload photo: {{ target_type.desc}}

Upload photo

{% endif %}
- {% csrf_token %} -

{{ form.type.label_tag }} {{ form.type }}

-

{{ form.photo.label_tag }} {{ form.photo }}

+{% csrf_token %} +{% if form.errors %} + +{% endif %} +

{{ form.type.label_tag }} {{ form.t_photo }}

+

{{ form.photo.label_tag }} {{ form.photo }}

{% if target_type.desc == "OUT" %} -

{{ form.registration_id.label_tag }} {{ form.registration_id }}

-

{{ form.registration_id.errors }}

-

{{ form.manual_registration_id.label_tag }} {{ form.manual_registration_id }}

-

{{ form.manual_registration_id.errors }}

+

{{ form.registration_id.label_tag }} {{ form.registration_id }}

+ {% if form.registration_id.errors %} +

{{ form.registration_id.errors }}

+ {% endif %} +

{{ form.manual_registration_id.label_tag }} {{ form.manual_registration_id }}

+ {% if form.manual_registration_id.errors %} +

{{ form.manual_registration_id.errors }}

+ {% endif %} {% endif %} diff --git a/FRONTEND/fastparking/photos/views.py b/FRONTEND/fastparking/photos/views.py index 6ae5c8ef..da68a498 100644 --- a/FRONTEND/fastparking/photos/views.py +++ b/FRONTEND/fastparking/photos/views.py @@ -18,19 +18,19 @@ def upload_file(request): if tt: target_type = {"type": tt, "desc": TYPES.get(tt)} if request.method == "POST": - tt = request.POST.get("type") + tt = request.POST.get("t_photo") if tt: target_type = {"type": tt, "desc": TYPES.get(tt)} - print(f"{request.POST=}") + # print(f"{request.POST=}") form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = request.FILES.get("photo") if uploaded_file: filename = uploaded_file.name - type_of_photo = request.POST.get("type") + type_of_photo = form.cleaned_data.get("t_photo") file_in = request.FILES.get("photo") if file_in: - registration_id = request.POST.get("registration_id") + registration_id = form.cleaned_data.get("registration_id") img_predict = handle_uploaded_file( file_in, type_of_photo, filename, registration_id ) @@ -53,7 +53,7 @@ def upload_file(request): else: initial = None if target_type: - initial = {"type": target_type.get("type")} + initial = {"t_photo": target_type.get("type")} form = UploadFileForm(initial=initial) context = {"active_menu": active_menu, "form": form, "target_type": target_type} return render(request, "photos/upload.html", context)