diff --git a/.gitignore b/.gitignore index 726c6a3fd7d8a5920961c646fabed51e0363a1d3..b9d9cb3c54d93f3eca735a006887b75ae04e1bb9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ #SOURCE: https://www.codingforentrepreneurs.com/blog/django-virtualenv-python-gitignore-file/ +# Project related +/media + # Virtualenv related bin/ include/ diff --git a/unisportomat/quiz/fixtures/images/test_image.png b/unisportomat/quiz/fixtures/images/test_image.png new file mode 100644 index 0000000000000000000000000000000000000000..6bc0d74c8d2d1dcc063ab7e782a42dbcf466664d Binary files /dev/null and b/unisportomat/quiz/fixtures/images/test_image.png differ diff --git a/unisportomat/quiz/migrations/0002_auto_20210601_1537.py b/unisportomat/quiz/migrations/0002_auto_20210601_1537.py new file mode 100644 index 0000000000000000000000000000000000000000..95041780a411d9bdb80e3a18d857c4e903b6e79f --- /dev/null +++ b/unisportomat/quiz/migrations/0002_auto_20210601_1537.py @@ -0,0 +1,38 @@ +# Generated by Django 3.2 on 2021-06-01 15:37 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("quiz", "0001_refactor_sport_and_add_criteria"), + ] + + operations = [ + migrations.CreateModel( + name="CallToMove", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ], + ), + migrations.AlterField( + model_name="criterionrating", + name="rating", + field=models.IntegerField( + validators=[ + django.core.validators.MaxValueValidator(10), + django.core.validators.MinValueValidator(1), + ] + ), + ), + ] diff --git a/unisportomat/quiz/migrations/0003_auto_20210601_1549.py b/unisportomat/quiz/migrations/0003_auto_20210601_1549.py new file mode 100644 index 0000000000000000000000000000000000000000..a1c8659fe2609ddd584d94c8019ffc5d4c5bf936 --- /dev/null +++ b/unisportomat/quiz/migrations/0003_auto_20210601_1549.py @@ -0,0 +1,24 @@ +# Generated by Django 3.2 on 2021-06-01 15:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("quiz", "0002_auto_20210601_1537"), + ] + + operations = [ + migrations.AddField( + model_name="calltomove", + name="image", + field=models.ImageField(null=True, upload_to=""), + ), + migrations.AddField( + model_name="calltomove", + name="text", + field=models.TextField(default=""), + preserve_default=False, + ), + ] diff --git a/unisportomat/quiz/models.py b/unisportomat/quiz/models.py index a3b25fde270a944af9ecd5d8a89ad8a129c2c66e..c68fdad11e1d2a30df1a9ac1f67dcc44cc96f0b6 100644 --- a/unisportomat/quiz/models.py +++ b/unisportomat/quiz/models.py @@ -54,3 +54,8 @@ class Criterion(models.Model): """ name = models.TextField() + + +class CallToMove(models.Model): + text = models.TextField() + image = models.ImageField(null=True, max_length=200) diff --git a/unisportomat/quiz/tests.py b/unisportomat/quiz/tests.py index 3c8a3f087dfcd217ae3297e625c2355ec692c3b7..5f1d5c524e2212f12b0ab6de0c1ac1d7a137798b 100644 --- a/unisportomat/quiz/tests.py +++ b/unisportomat/quiz/tests.py @@ -1,7 +1,14 @@ """ This module tests all our quiz models""" -from django.test import TestCase -from .models import Sport, Criterion +import os +import shutil +import tempfile + +from django.core.files import File +from django.core.files.uploadedfile import SimpleUploadedFile +from django.test import TestCase, override_settings +from .models import Sport, Criterion, CallToMove +from django.conf import settings class SportModelTest(TestCase): @@ -74,3 +81,41 @@ class CriterionModelTest(TestCase): Criterion(name=name).save() test_criterion = Criterion.objects.first() self.assertEqual(test_criterion.name, name) + + +FIXTURE_IMAGES = os.path.join(settings.BASE_DIR, "quiz", "fixtures", "images") +MEDIA_ROOT = tempfile.mkdtemp(suffix="testing") # Create a temp directory for files created during tests + + +@override_settings(MEDIA_ROOT=MEDIA_ROOT) +class InfoScreenTest(TestCase): + """Tests the Model for the info screen (Call To Move or Knowledge Snack)""" + + def setUp(self): + tempfile.mkdtemp(suffix="testing") # recreate tmp folder before each test + + self.text = "Kreise deine Arme vor der nächsten Frage 3x nach hinten" + self.image_name = "test_image.png" + self.image_path = os.path.join(FIXTURE_IMAGES, self.image_name) + self.image = SimpleUploadedFile( + name=self.image_name, + content=open(self.image_path, "rb").read(), + content_type="image/png", + ) + self.call_to_move = CallToMove(text=self.text, image=self.image) + self.call_to_move.save() + + def tearDown(self) -> None: + """ Delete the temp dir after each test """ + shutil.rmtree(MEDIA_ROOT, ignore_errors=True) + + def test_can_create_call_to_move(self): + """ A call to move can be correctly created """ + self.assertEqual(self.call_to_move.text, self.text) + self.assertEqual(self.call_to_move.image.name, self.image.name) + + def test_can_save_and_load_call_to_move(self): + """ A saved Call to Move can be loaded """ + call_to_move = CallToMove.objects.first() + self.assertEqual(call_to_move.text, self.text) + self.assertEqual(call_to_move.image.name, self.image.name) diff --git a/unisportomat/unisportomat/settings.py b/unisportomat/unisportomat/settings.py index 23f0cbdb1ed78fc8b2dfd21a2e5c4e27ac3eca39..626b566fd3345803ba07040f260987ee5529a3d9 100644 --- a/unisportomat/unisportomat/settings.py +++ b/unisportomat/unisportomat/settings.py @@ -9,12 +9,14 @@ https://docs.djangoproject.com/en/3.2/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.2/ref/settings/ """ - +import os from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent +# Where user uploaded files like images are stored +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ @@ -27,7 +29,6 @@ DEBUG = True ALLOWED_HOSTS = [] - # Application definition INSTALLED_APPS = [ @@ -70,7 +71,6 @@ TEMPLATES = [ WSGI_APPLICATION = 'unisportomat.wsgi.application' - # Database # https://docs.djangoproject.com/en/3.2/ref/settings/#databases @@ -81,7 +81,6 @@ DATABASES = { } } - # Password validation # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators @@ -100,7 +99,6 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] - # Internationalization # https://docs.djangoproject.com/en/3.2/topics/i18n/ @@ -114,7 +112,6 @@ USE_L10N = True USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.2/howto/static-files/