Add tests for file extension, add return button to detect page

This commit is contained in:
Piotr Szkudlarek 2023-11-28 18:32:10 +01:00
parent 98445d7d22
commit e45210cfd9
8 changed files with 58 additions and 7 deletions

View File

@ -0,0 +1,25 @@
# Generated by Django 4.2.7 on 2023-11-28 16:25
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("DetectionApp", "0001_initial"),
]
operations = [
migrations.AlterField(
model_name="detectimage",
name="image",
field=models.ImageField(
upload_to="plankton/%Y/%m/%d/",
validators=[
django.core.validators.FileExtensionValidator(
allowed_extensions=["jpg", "png"]
)
],
),
),
]

View File

@ -1,6 +1,11 @@
from django.db import models
from django.core.validators import FileExtensionValidator
# Create your models here.
class DetectImage(models.Model):
image = models.ImageField(upload_to="plankton/%Y/%m/%d/")
image = models.ImageField(
upload_to="plankton/%Y/%m/%d/",
validators=[FileExtensionValidator(allowed_extensions=["jpg", "png"])],
)

View File

@ -1,3 +1,24 @@
from django.test import TestCase
from django.core.files.uploadedfile import SimpleUploadedFile
from .forms import DetectForm
from django.conf import settings
# Create your tests here.
# Test sprawdza czy wprowadzone pliki są w odpowiednim formacie
class CanUploadFiles(TestCase):
def test_upload_img_file(self):
file = SimpleUploadedFile(
name="jpg_file.jpg",
content=open(f"{settings.MEDIA_ROOT}/test_image.jpg", "rb").read(),
content_type="image/jpg",
)
form = DetectForm(files={"image": file})
print(form.errors)
self.assertTrue(form.is_valid())
def test_upload_other_file(self):
file = SimpleUploadedFile(
"txt_file.txt", content=b"some text", content_type="text/plain"
)
form = DetectForm(files={"image": file})
self.assertFalse(form.is_valid())

View File

@ -10,9 +10,8 @@ MODEL = YOLO("yolov8n.pt")
def view(request):
form = DetectForm()
print(form.as_p())
if request.method == "GET":
form = DetectForm()
return render(request, "upload.html", {"form": form})
form = DetectForm(request.POST, request.FILES)
if form.is_valid():
@ -35,4 +34,4 @@ def view(request):
},
)
else:
return render(request, "upload.html")
return render(request, "upload.html", {"form": form})

Binary file not shown.

View File

@ -14,12 +14,13 @@
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="side_menu">
{% if not img_saved%}
{% if img_saved is None %}
<p id="description">Choose image for analysis</p>
<input type="submit" id="submit" value="Submit">
{% else %}
<p id="description">Photo saved</p>
<button onclick="location.href='{% url 'detect' %}'" id='submit' method="GET" type='button'>Submit again</button>
{%endif%}
<input type="submit" id="submit" value="Submit">
</div>
<div class="upload_field">
<label for="id_image", class="upload_button">

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB