From e45210cfd928b45848fe80d9ea6af01da068b4b2 Mon Sep 17 00:00:00 2001 From: Piotr Szkudlarek Date: Tue, 28 Nov 2023 18:32:10 +0100 Subject: [PATCH] Add tests for file extension, add return button to detect page --- .../0002_alter_detectimage_image.py | 25 ++++++++++++++++++ PlanktonDetector/DetectionApp/models.py | 7 ++++- PlanktonDetector/DetectionApp/tests.py | 23 +++++++++++++++- PlanktonDetector/DetectionApp/views.py | 5 ++-- PlanktonDetector/db.sqlite3 | Bin 135168 -> 143360 bytes PlanktonDetector/templates/upload.html | 5 ++-- .../11/20/Screenshot_2023-11-19_173550.png | Bin 10457 -> 0 bytes .../uploaded_media/test_image.jpg | Bin 0 -> 43258 bytes 8 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 PlanktonDetector/DetectionApp/migrations/0002_alter_detectimage_image.py delete mode 100644 PlanktonDetector/uploaded_media/plankton/2023/11/20/Screenshot_2023-11-19_173550.png create mode 100755 PlanktonDetector/uploaded_media/test_image.jpg diff --git a/PlanktonDetector/DetectionApp/migrations/0002_alter_detectimage_image.py b/PlanktonDetector/DetectionApp/migrations/0002_alter_detectimage_image.py new file mode 100644 index 0000000..e84b9ba --- /dev/null +++ b/PlanktonDetector/DetectionApp/migrations/0002_alter_detectimage_image.py @@ -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"] + ) + ], + ), + ), + ] diff --git a/PlanktonDetector/DetectionApp/models.py b/PlanktonDetector/DetectionApp/models.py index 453945f..371f9f5 100644 --- a/PlanktonDetector/DetectionApp/models.py +++ b/PlanktonDetector/DetectionApp/models.py @@ -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/") \ No newline at end of file + image = models.ImageField( + upload_to="plankton/%Y/%m/%d/", + validators=[FileExtensionValidator(allowed_extensions=["jpg", "png"])], + ) diff --git a/PlanktonDetector/DetectionApp/tests.py b/PlanktonDetector/DetectionApp/tests.py index 7ce503c..b022e39 100644 --- a/PlanktonDetector/DetectionApp/tests.py +++ b/PlanktonDetector/DetectionApp/tests.py @@ -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()) diff --git a/PlanktonDetector/DetectionApp/views.py b/PlanktonDetector/DetectionApp/views.py index df67824..b92e99e 100644 --- a/PlanktonDetector/DetectionApp/views.py +++ b/PlanktonDetector/DetectionApp/views.py @@ -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}) diff --git a/PlanktonDetector/db.sqlite3 b/PlanktonDetector/db.sqlite3 index 8e90e390844864a034949ab02f9d527c560e3036..ffceb28c5051c3d185231cb138e59bcb5eeffb06 100644 GIT binary patch delta 515 zcmZozz|ru4V}dkmIRgWO@vStuBqSs9sH85!xB zTbLS|8EqDqjuT-FoLr{g$tcofu*`t*0uL+DYDWIwK$1auV?iJP#0e>!N}jwxt|!Bu zsq9SM9E=5w<_!NB_DpP4Vsh}DzL1Mal*!0q`bQ=vDM51s^Z3euywH&BP`!e@baRjC z2AoWyj26=mGBSxw=VM~xV|0fJfi!ReHGmbmp@^Cqnt?@K!J^v_FfuvtNr2qI!6(AN zf1JOM--n-z?*v~zpD&-t#>PLqOy+?w1AsOuGC2gm-37CmlP|u?J;JZRq6BQTKazS0 zZWAM8V`GrGgC91H-idw{KKWH(1-{r6l;-4m=Z3q075KmvB(gBc^2J948io{xMS*Sg VhS>^~Qe-msf^mUiA {% csrf_token %}
- {% if not img_saved%} + {% if img_saved is None %}

Choose image for analysis

+ {% else %}

Photo saved

+ {%endif%} -