Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added seeder for Album #668

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
61 changes: 61 additions & 0 deletions nablapps/core/management/commands/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

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

from faker import Factory

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
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -729,6 +789,7 @@ def delete(cls) -> None:
PodcastSeeder,
NabladSeeder,
GameSeeder,
AlbumSeeder,
)


Expand Down
Loading