Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHOTOS - recover bug of registration_id check, now DEMO mode #79

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions FRONTEND/fastparking/photos/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down Expand Up @@ -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")

Expand All @@ -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:
Expand Down
40 changes: 24 additions & 16 deletions FRONTEND/fastparking/photos/repository.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import random
from datetime import datetime
from io import BytesIO
from pathlib import Path
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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}
27 changes: 20 additions & 7 deletions FRONTEND/fastparking/photos/templates/photos/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@ <h1>Upload photo: {{ target_type.desc}}</h1>
<h1>Upload photo</h1>
{% endif %}
<form method="POST" action="" enctype="multipart/form-data">
{% csrf_token %}
<p {% if target_type %} class="d-none" {% endif %}>{{ form.type.label_tag }} {{ form.type }}</p>
<p>{{ form.photo.label_tag }} {{ form.photo }}</p>
{% csrf_token %}
{% if form.errors %}
<ul class="text-danger">
{% for field_name, error_list in form.errors.items %}
{% for error in error_list %}
<li>{{ error }}</li>
{% endfor %}
{% endfor %}
</ul>
{% endif %}
<p {% if target_type %} class="d-none" {% endif %}>{{ form.type.label_tag }} {{ form.t_photo }}</p>
<p>{{ form.photo.label_tag }} {{ form.photo }}</p>
{% if target_type.desc == "OUT" %}
<p>{{ form.registration_id.label_tag }} {{ form.registration_id }}</p>
<p class="text-warning">{{ form.registration_id.errors }}</p>
<p>{{ form.manual_registration_id.label_tag }} {{ form.manual_registration_id }}</p>
<p class="text-warning">{{ form.manual_registration_id.errors }}</p>
<p>{{ form.registration_id.label_tag }} {{ form.registration_id }}</p>
{% if form.registration_id.errors %}
<p class="text-danger">{{ form.registration_id.errors }}</p>
{% endif %}
<p>{{ form.manual_registration_id.label_tag }} {{ form.manual_registration_id }}</p>
{% if form.manual_registration_id.errors %}
<p class="text-danger">{{ form.manual_registration_id.errors }}</p>
{% endif %}
{% endif %}

<input class="btn btn-primary" type="submit" value="Submit" />
Expand Down
10 changes: 5 additions & 5 deletions FRONTEND/fastparking/photos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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)
Expand Down
Loading