Skip to content

Commit

Permalink
Add new models for Files
Browse files Browse the repository at this point in the history
- On this commit, website is not yet fully functional
- Add ManualPDFTemplate
- PDFFile are now separed from configuration to create them to ManualPDFTemplate
- PDFTemplate as common ancestor of Category and ManualPDFTemplate
  • Loading branch information
pehala committed Dec 26, 2024
1 parent b67190e commit 37b8711
Show file tree
Hide file tree
Showing 18 changed files with 688 additions and 90 deletions.
19 changes: 19 additions & 0 deletions backend/migrations/0013_alter_song_categories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.1.4 on 2024-12-20 18:42

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("backend", "0012_rename_prerendered_pdf_song_prerendered_and_more"),
("category", "0013_category2"),
]

operations = [
migrations.AlterField(
model_name="song",
name="categories",
field=models.ManyToManyField(to="category.category2", verbose_name="Categories"),
),
]
2 changes: 1 addition & 1 deletion backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from django.utils.translation import gettext_lazy as _
from markdownx.models import MarkdownxField

from chords.markdown import RENDERER
from category.models import Category
from chords.markdown import RENDERER


class Song(Model):
Expand Down
77 changes: 77 additions & 0 deletions category/migrations/0013_category2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Generated by Django 5.1.4 on 2024-12-20 18:29

import django.db.models.deletion
from django.db import migrations, models


def recreate_categories(apps, schema_editor):
Category = apps.get_model("category", "Category")
NewCategory = apps.get_model("category", "Category2")
ContentType = apps.get_model("contenttypes", "ContentType")
db_alias = schema_editor.connection.alias

for category in Category.objects.using(db_alias).all():
new_category = NewCategory()
new_category.polymorphic_ctype_id = ContentType.objects.get_for_model(Category).pk
for field in [
"pk",
"filename",
"public",
"locale",
"title",
"show_date",
"image",
"margin",
"link",
"generate_pdf",
"tenant_id",
"slug",
"name",
]:
setattr(new_category, field, getattr(category, field))
new_category.save()


class Migration(migrations.Migration):

dependencies = [
("category", "0012_alter_category_link"),
("pdf", "0026_pdftemplate_pdffile_alter_pdfrequest_options_and_more"),
("tenants", "0005_link_tenant_links"),
]

operations = [
migrations.CreateModel(
name="Category2",
fields=[
(
"pdftemplate_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="pdf.pdftemplate",
),
),
("name", models.CharField(max_length=100, verbose_name="Name")),
("slug", models.SlugField(max_length=25, verbose_name="URL pattern")),
(
"generate_pdf",
models.BooleanField(
help_text="Should the PDF file be automatically generated when a song changes?",
verbose_name="PDF generation",
),
),
("tenant", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="tenants.tenant")),
],
options={
"verbose_name": "Category",
"verbose_name_plural": "Categories",
"unique_together": {("tenant", "name"), ("tenant", "slug")},
},
bases=("pdf.pdftemplate",),
),
migrations.RunPython(recreate_categories),
]
17 changes: 17 additions & 0 deletions category/migrations/0014_delete_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1.4 on 2024-12-20 18:51

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("backend", "0013_alter_song_categories"),
("category", "0013_category2"),
]

operations = [
migrations.DeleteModel(
name="Category",
),
]
20 changes: 20 additions & 0 deletions category/migrations/0015_rename_category2_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.1.4 on 2024-12-20 18:51

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("backend", "0013_alter_song_categories"),
("category", "0014_delete_category"),
("pdf", "0026_pdftemplate_pdffile_alter_pdfrequest_options_and_more"),
("tenants", "0005_link_tenant_links"),
]

operations = [
migrations.RenameModel(
old_name="Category2",
new_name="Category",
),
]
16 changes: 12 additions & 4 deletions category/models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
"""Models"""

from django.db.models import CharField, SlugField, BooleanField, ForeignKey, CASCADE
from typing import Iterable, Tuple, TYPE_CHECKING

from django.db.models import CharField, SlugField, BooleanField, ForeignKey, CASCADE, Model
from django.utils.translation import gettext_lazy as _

from pdf.models import PDFOptions
from pdf.models import PDFTemplate
from tenants.models import Tenant

if TYPE_CHECKING:
from backend.models import Song


class Category(PDFOptions):
"""Represents set of songs"""
class Category(PDFTemplate):
"""Song Category"""

tenant = ForeignKey(Tenant, on_delete=CASCADE)
name = CharField(verbose_name=_("Name"), max_length=100)
Expand All @@ -18,6 +23,9 @@ class Category(PDFOptions):
help_text=_("Should the PDF file be automatically generated when a song changes?"),
)

def get_songs(self) -> Iterable[Tuple[int, "Song"]]:
return list(enumerate(self.song_set.filter(archived=False)))

def __str__(self):
return self.name

Expand Down
19 changes: 19 additions & 0 deletions docs/FILES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# File handling in Song Book

## Models

### PDFTemplate
* Represents set of configuration options that are used for generating a PDFFile
* No way of recreating exactly the same file, PDFTemplates can change and every regeneration will be based on the new configuration

#### ManualPDFTemplate
#### Category
* Category is a subclass of PDFTemplate and provides songs

### PDFFile
* Represents single file that is either generated or scheduled for generation

### NumberedSong
* Song + fixed song number, used in PDFTemplate to change order of songs in PDF

## Workflow
38 changes: 19 additions & 19 deletions pdf/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
from pdf.models.request import PDFRequest


@admin.register(PDFRequest)
class RequestAdmin(admin.ModelAdmin):
"""Request Model Admin"""

list_display = ["id", "title", "update_date", "type", "file", "status", "tenant_name"]
actions = ["move_tenant"]

@admin.display(description=_("Tenant"))
def tenant_name(self, obj):
"""Shows Tenant name"""
link = reverse("admin:tenants_tenant_change", args=[obj.tenant.id])
return format_html('<a href="{}">{}</a>', link, obj.tenant.name)

@admin.action(description=_("Move Request to a different Tenant"))
def move_tenant(self, request, queryset):
"""Move Request to a different Tenant"""
ids = queryset.values_list("id", flat=True)
url = f"{reverse_lazy('pdf:move')}?{urlencode({'pk': list(ids)}, True)}"
return HttpResponseRedirect(url)
# @admin.register(PDFRequest)
# class RequestAdmin(admin.ModelAdmin):
# """Request Model Admin"""
#
# list_display = ["id", "title", "update_date", "type", "file", "status", "tenant_name"]
# actions = ["move_tenant"]
#
# @admin.display(description=_("Tenant"))
# def tenant_name(self, obj):
# """Shows Tenant name"""
# link = reverse("admin:tenants_tenant_change", args=[obj.tenant.id])
# return format_html('<a href="{}">{}</a>', link, obj.tenant.name)
#
# @admin.action(description=_("Move Request to a different Tenant"))
# def move_tenant(self, request, queryset):
# """Move Request to a different Tenant"""
# ids = queryset.values_list("id", flat=True)
# url = f"{reverse_lazy('pdf:move')}?{urlencode({'pk': list(ids)}, True)}"
# return HttpResponseRedirect(url)
Loading

0 comments on commit 37b8711

Please sign in to comment.