From 57ddbdd7873a218b7248669c6380ab4fed0bb72f Mon Sep 17 00:00:00 2001 From: Kajrakso Date: Thu, 7 Mar 2024 19:55:31 +0100 Subject: [PATCH] added seeder for Album --- .pre-commit-config.yaml | 8 +-- nablapps/core/management/commands/seed.py | 61 +++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 85248d8f..fef9cf0f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,21 +1,21 @@ exclude: 'static/.*' repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: check-yaml - id: end-of-file-fixer - id: check-merge-conflict - id: check-added-large-files - repo: https://github.com/psf/black - rev: 23.3.0 + rev: 24.2.0 hooks: - id: black - repo: https://github.com/PyCQA/isort - rev: 5.12.0 + rev: 5.13.2 hooks: - id: isort - repo: https://github.com/PyCQA/flake8 - rev: 6.0.0 + rev: 7.0.0 hooks: - id: flake8 diff --git a/nablapps/core/management/commands/seed.py b/nablapps/core/management/commands/seed.py index 3a70cb2e..153732cb 100644 --- a/nablapps/core/management/commands/seed.py +++ b/nablapps/core/management/commands/seed.py @@ -17,6 +17,7 @@ from django.conf import settings from django.core.files.base import ContentFile +from django.core.files.uploadedfile import UploadedFile from django.core.management.base import BaseCommand from django.db import transaction @@ -24,6 +25,7 @@ from nablapps.accounts.models import FysmatClass, NablaGroup from nablapps.accounts.models import NablaUser as User +from nablapps.album.models import Album, AlbumForm, AlbumImage from nablapps.events.models import Event from nablapps.interactive.models.code_golf import CodeTask, Result from nablapps.interactive.models.games import Game @@ -685,6 +687,64 @@ def delete(cls) -> None: Nablad.objects.all().delete() +class AlbumSeeder: + amount_albums = 10 + amount_child_albums = 3 + amount_images_per_album = 5 + + description = f"{amount_albums} Albums with {amount_child_albums} child Albums. {amount_images_per_album} images per album." + short_description = "Albums" + + @classmethod + def exists(cls) -> bool: + return Album.objects.exists() + + @classmethod + def create(cls) -> None: + for i in range(cls.amount_albums): + parent_album = Album.objects.create( + title=f"Parent Album: {i+1}", visibility="p" if i % 2 else "u" + ) + + sub_albums = [ + Album.objects.create( + title=f"Child Album: {j+1}", + visibility="p" if j % 2 else "u", + parent=parent_album, + ) + for j in range(cls.amount_child_albums) + ] + + all_albums = [parent_album] + sub_albums + + for album in all_albums: + # Create form + form = AlbumForm() + + # "upload" the images + form.files.setlist( + "photos", + [ + UploadedFile(file=polygon_picture()) + for i in range(cls.amount_images_per_album) + ], + ) + + form.save_photos(album) + + # Add numbering and description to each image + images = AlbumImage.objects.filter(album=album) + for j, img in enumerate(images): + img.num = j + img.description = random_text() + img.save() + + @classmethod + def delete(cls) -> None: + Album.objects.all().delete() + AlbumImage.objects.all().delete() + + class GameSeeder: description = short_description = "Games" @@ -729,6 +789,7 @@ def delete(cls) -> None: PodcastSeeder, NabladSeeder, GameSeeder, + AlbumSeeder, )