From 16da452fa914e394adb6ed82ff875e0aa4af040d Mon Sep 17 00:00:00 2001 From: FuzzyGrim Date: Tue, 27 Dec 2022 00:12:28 +0100 Subject: [PATCH] store images for tracked media --- .gitignore | 3 +- Yamtarr/settings.py | 6 ++ app/apps.py | 5 ++ app/models.py | 2 +- app/templates/app/include/list-section.html | 8 +-- app/templates/app/search_mal.html | 7 ++- app/tests/test_models.py | 17 +++++ app/urls.py | 4 +- app/utils/database.py | 69 ++++++++++++++------- 9 files changed, 90 insertions(+), 31 deletions(-) create mode 100644 app/apps.py diff --git a/.gitignore b/.gitignore index d9e41b22..7ac9a353 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ __pycache__ *.pyc .env db.sqlite3 -./static/css \ No newline at end of file +/static/ +/media/ \ No newline at end of file diff --git a/Yamtarr/settings.py b/Yamtarr/settings.py index 3aa33e2b..cb4ad724 100644 --- a/Yamtarr/settings.py +++ b/Yamtarr/settings.py @@ -70,6 +70,7 @@ TEMPLATES = [ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', + 'django.template.context_processors.media', ], }, }, @@ -125,6 +126,11 @@ USE_TZ = True STATIC_URL = 'static/' +# Media files +MEDIA_URL = 'media/' +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') + + # Default primary key field type # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field diff --git a/app/apps.py b/app/apps.py new file mode 100644 index 00000000..906b8320 --- /dev/null +++ b/app/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + +class AppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'app' \ No newline at end of file diff --git a/app/models.py b/app/models.py index bde48a2f..f02441aa 100644 --- a/app/models.py +++ b/app/models.py @@ -6,7 +6,7 @@ from django.conf import settings class Media(models.Model): media_id = models.IntegerField() title = models.CharField(max_length=100) - image = models.TextField() + image = models.ImageField(upload_to="images", null=True) media_type = models.CharField(max_length=100) seasons_score = models.JSONField(default=dict) score = models.FloatField(null=True) diff --git a/app/templates/app/include/list-section.html b/app/templates/app/include/list-section.html index 7ac0acc3..db8a9df3 100644 --- a/app/templates/app/include/list-section.html +++ b/app/templates/app/include/list-section.html @@ -1,13 +1,9 @@
- {% if media.image == "None" or media.image == ""%} + {% if not media.image %}
{% else %} - {% if media.api_origin == "tmdb" %} - - {% elif media.api_origin == "mal" %} - - {% endif %} + {% endif %}
diff --git a/app/templates/app/search_mal.html b/app/templates/app/search_mal.html index 95a09472..abc8d48f 100644 --- a/app/templates/app/search_mal.html +++ b/app/templates/app/search_mal.html @@ -28,7 +28,12 @@
- + {% if query.node.main_picture.large == None %} +
+
+ {% else %} + + {% endif %}
diff --git a/app/tests/test_models.py b/app/tests/test_models.py index fc5eacf8..ecaf656b 100644 --- a/app/tests/test_models.py +++ b/app/tests/test_models.py @@ -1,10 +1,13 @@ from django.test import TestCase from django.urls import reverse from django.contrib import auth +from django.test import override_settings +import shutil from app.models import User +TEST_DIR = 'test_data' class RegisterLoginUser(TestCase): def test_create_user(self): @@ -53,6 +56,7 @@ class CreateMedia(TestCase): self.user = User.objects.create_user(**self.credentials) self.client.login(**self.credentials) + @override_settings(MEDIA_ROOT=(TEST_DIR + '/media')) def test_create_tmdb(self): response = self.client.post( reverse("search", args=["tmdb", "flcl"]), @@ -71,8 +75,15 @@ class CreateMedia(TestCase): response = self.client.get(reverse("home")) self.assertContains(response, "FLCL") + def tearDownClass(): + try: + shutil.rmtree(TEST_DIR) + except OSError: + pass + class EditMedia(TestCase): + @override_settings(MEDIA_ROOT=(TEST_DIR + '/media')) def setUp(self): self.credentials = {"username": "test", "password": "12345"} self.user = User.objects.create_user(**self.credentials) @@ -125,3 +136,9 @@ class EditMedia(TestCase): response = self.client.get(reverse("home")) self.assertContains(response, "Watching") + + def tearDownClass(): + try: + shutil.rmtree(TEST_DIR) + except OSError: + pass \ No newline at end of file diff --git a/app/urls.py b/app/urls.py index 3703cee2..97cf38d7 100644 --- a/app/urls.py +++ b/app/urls.py @@ -1,5 +1,7 @@ from django.urls import path from django.contrib.auth import views as auth_views +from django.conf import settings +from django.conf.urls.static import static from . import views @@ -10,4 +12,4 @@ urlpatterns = [ path("profile/", views.profile, name = "profile"), path("login/", views.login, name = "login"), path("logout/", auth_views.LogoutView.as_view(), name = "logout"), -] \ No newline at end of file +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \ No newline at end of file diff --git a/app/utils/database.py b/app/utils/database.py index 9d0330f5..59c0be79 100644 --- a/app/utils/database.py +++ b/app/utils/database.py @@ -1,34 +1,61 @@ from app.models import Media from app.utils import helpers +import requests +from django.core.files import File +from django.core.files.temp import NamedTemporaryFile def add_media(request): + media = Media() + if request.POST["score"] == "": - request.POST["score"] = None + media.score = None else: - score = float(request.POST["score"]) + media.score = float(request.POST["score"]) - api_origin = request.POST["api_origin"] - if api_origin == "mal": - media_type = helpers.convert_mal_media_type(request.POST["media_type"]) - else: - media_type = request.POST["media_type"] - - media = Media( - media_id=request.POST["media_id"], - title=request.POST["title"], - image=request.POST["image"], - media_type=media_type, - score=score, - user=request.user, - status=request.POST["status"], - num_seasons=request.POST.get("num_seasons"), - api_origin=api_origin, - ) + media.media_id=request.POST["media_id"] + media.title=request.POST["title"] + media.user=request.user + media.status=request.POST["status"] + media.num_seasons=request.POST.get("num_seasons") if "season" in request.POST: media.seasons_score={request.POST["season"]: request.POST["score"]} - media.save() + img_temp = NamedTemporaryFile(delete=True) + media.api_origin = request.POST["api_origin"] + if media.api_origin == "mal": + media.media_type = helpers.convert_mal_media_type(request.POST["media_type"]) + + if request.POST['image'] == "": + media.image = None + media.save() + return + else: + r = requests.get(request.POST['image']) + + img_temp.write(r.content) + img_temp.flush() + + if media.media_type == "anime": + media.image.save(f"anime-{request.POST['image'].rsplit('/', 1)[-1]}", File(img_temp), save=True) + + elif media.media_type == "manga": + media.image.save(f"manga-{request.POST['image'].rsplit('/', 1)[-1]}", File(img_temp), save=True) + + else: + media.media_type = request.POST["media_type"] + + if request.POST['image'] == "": + media.image = None + media.save() + return + else: + r = requests.get(f"https://image.tmdb.org/t/p/w154{request.POST['image']}") + + img_temp.write(r.content) + img_temp.flush() + + media.image.save(f"tmdb-{request.POST['image'].rsplit('/', 1)[-1]}", File(img_temp), save=True) def edit_media(request): @@ -43,7 +70,7 @@ def edit_media(request): api_origin=request.POST["api_origin"], ) - if request.POST["season"]: + if "season" in request.POST: # if media didn't have seasons before, add the previous score as season 1 if not media.seasons_score: media.seasons_score["1"] = media.score