Skip to content

Commit

Permalink
Unify Migrate view
Browse files Browse the repository at this point in the history
* Common base class
* Fewer locales strings
* Fixed migrate template
  • Loading branch information
pehala committed Dec 25, 2024
1 parent 8fa4113 commit 75fe192
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 189 deletions.
3 changes: 2 additions & 1 deletion category/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.utils.translation import gettext_lazy as _

from category.models import Category
from tenants.models import Tenant


@admin.register(Category)
Expand All @@ -18,7 +19,7 @@ class CategoryAdmin(admin.ModelAdmin):
# list_display_links = ["tenant_name"]
actions = ["move_tenant"]

@admin.display(description=_("Tenant"))
@admin.display(description=Tenant._meta.verbose_name)
def tenant_name(self, obj):
"""Shows Tenant name"""
link = reverse("admin:tenants_tenant_change", args=[obj.tenant.id])
Expand Down
6 changes: 0 additions & 6 deletions category/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,3 @@ class NameForm(Form):

pk = IntegerField(widget=HiddenInput())
name = CharField(disabled=True, required=False)


class ChooseTenantForm(Form):
"""Form to choose Tenant"""

tenant = ModelChoiceField(queryset=Tenant.objects.all(), label=_("Tenant"))
24 changes: 5 additions & 19 deletions category/locale/cs/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-12-22 22:15+0000\n"
"POT-Creation-Date: 2024-12-25 17:43+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -19,11 +19,7 @@ msgstr ""
"Plural-Forms: nplurals=4; plural=(n == 1 && n % 1 == 0) ? 0 : (n >= 2 && n "
"<= 4 && n % 1 == 0) ? 1: (n % 1 != 0 ) ? 2 : 3;\n"

#: category/admin.py:21 category/forms.py:43
msgid "Tenant"
msgstr "Do Zpěvníku"

#: category/admin.py:27
#: category/admin.py:28
msgid "Move Category to a different Tenant"
msgstr "Přesunout Kategorie do jiného Zpěvníku"

Expand All @@ -48,28 +44,18 @@ msgstr ""
msgid "Category"
msgstr "Kategorie"

#: category/models.py:26 category/templates/admin/category/migrate.html:6
#: category/templates/category/list.html:4
#: category/models.py:26 category/templates/category/list.html:4
#: category/templates/category/list.html:5
msgid "Categories"
msgstr "Kategorie"

#: category/templates/admin/category/migrate.html:4
#: category/templates/admin/category/migrate.html:5
msgid "Migrating categories to Tenant"
msgstr "Přesun kategorii do jiného Zpěvníku"

#: category/templates/admin/category/migrate.html:7
msgid "To Tenant"
msgstr "Do Zpěvníku"

#: category/templates/category/list.html:14
msgid "Language"
msgstr "Jazyk"

#: category/templates/category/list.html:15
msgid "Actions"
msgstr ""
msgstr "Akce"

#: category/templates/category/list.html:23
msgid "Yes"
Expand All @@ -85,7 +71,7 @@ msgstr "Upravit"

#: category/templates/category/list.html:29
msgid "Toggle Dropdown"
msgstr ""
msgstr "Rozbalit"

#: category/templates/category/list.html:34
msgid "Already in queue"
Expand Down
7 changes: 0 additions & 7 deletions category/templates/admin/category/migrate.html

This file was deleted.

55 changes: 10 additions & 45 deletions category/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
from django.contrib import messages
from django.core.cache import cache
from django.db import transaction
from django.forms import formset_factory
from django.http import Http404
from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views import View
from django.views.generic import ListView, TemplateView
from django.views.generic import ListView
from django.views.generic.detail import SingleObjectMixin

from analytics.views import AnalyticsMixin
from backend.generic import UniversalDeleteView, UniversalUpdateView, UniversalCreateView
from backend.mixins import RegenerateViewMixin, LocalAdminRequired, SuperAdminRequired
from backend.mixins import RegenerateViewMixin, LocalAdminRequired
from backend.models import Song
from backend.views import BaseSongListView
from category.forms import CategoryForm, NameForm, ChooseTenantForm
from category.forms import CategoryForm, NameForm
from category.models import Category
from pdf.models.request import PDFRequest, RequestType, Status
from pdf.utils import request_pdf_regeneration
from tenants.views import AdminMoveView


class CategorySongsListView(BaseSongListView, AnalyticsMixin):
Expand Down Expand Up @@ -121,46 +121,22 @@ def post(self, request, *args, **kwargs):
return response


class CategoryMoveView(SuperAdminRequired, TemplateView):
class CategoryMoveView(AdminMoveView):
"""Moves Categories to a different Tenant"""

template_name = "admin/category/migrate.html"
form_class = ChooseTenantForm
formset_class = formset_factory(NameForm, extra=0)

def initial(self, pks):
"""Initial Form population"""
query = Category.objects.filter(id__in=pks)

form = self.form_class()
initial = []
for category in query.values_list("id", "name"):
initial.append({"pk": category[0], "name": category[1]})

formset = self.formset_class(initial=initial)
return form, formset

def get_context_data(self, **kwargs):
"""Appends Form and Formset"""
context = super().get_context_data(**kwargs)

pks = self.request.GET.getlist("pk")
form, formset = self.initial(pks)

context["form"] = form
context["formset"] = formset
return context
formset_form = NameForm
model = Category

def action(self, tenant, ids):
def action(self, target, ids):
"""What should happen on POST with data from forms"""
categories = Category.objects.filter(id__in=ids)
songs = Song.objects.filter(categories__id__in=ids).distinct()
requests = PDFRequest.objects.filter(category_id__in=ids).distinct()
with transaction.atomic():
for category in categories:
category.tenant = tenant
category.tenant = target
for request in requests:
request.tenant = tenant
request.tenant = target
Category.objects.bulk_update(categories, ["tenant"])
PDFRequest.objects.bulk_update(requests, ["tenant"])
for song in songs:
Expand All @@ -178,14 +154,3 @@ def action(self, tenant, ids):
song.save()
song.categories.set(to_remove)
song.save()

def post(self, request, *args, **kwargs):
"""POST request"""
form = self.form_class(request.POST)
formset = self.formset_class(request.POST)
if all([form.is_valid(), formset.is_valid()]):
tenant = form.cleaned_data["tenant"]
ids = [inline_form.cleaned_data["pk"] for inline_form in formset]
self.action(tenant, ids)
return redirect("admin:index")
return self.render_to_response({"form": form, "formset": formset})
11 changes: 5 additions & 6 deletions frontend/locale/cs/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-12-22 21:56+0000\n"
"POT-Creation-Date: 2024-12-25 17:32+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -48,15 +48,14 @@ msgstr "Zpátky domů"
msgid "URL %(request_path)s does not exist."
msgstr "URL %(request_path)s neexistuje."

#: frontend/templates/admin/base/migrate.html:29
#: frontend/templates/base/form.html:9
msgid "Submit"
msgstr "Odeslat"

#: frontend/templates/base/error.html:8
msgid "Not found"
msgstr "Nenalezeno"

#: frontend/templates/base/form.html:9
msgid "Submit"
msgstr "Odeslat"

#: frontend/templates/base/index.html:38
msgid "Logo"
msgstr ""
Expand Down
32 changes: 0 additions & 32 deletions frontend/templates/admin/base/migrate.html

This file was deleted.

37 changes: 11 additions & 26 deletions pdf/locale/cs/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-12-20 12:45+0000\n"
"POT-Creation-Date: 2024-12-25 17:41+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -140,27 +140,18 @@ msgstr "Hotovo"
msgid "Failed"
msgstr "Selhalo"

#: pdf/models/request.py:76 pdf/templates/admin/pdf/migrate.html:6
#: pdf/models/request.py:80
msgid "PDFRequest"
msgstr "PDF požadavek"

#: pdf/models/request.py:77
#: pdf/models/request.py:81
msgid "PDFRequests"
msgstr "PDF požadavky"

#: pdf/models/request.py:93
#: pdf/models/request.py:97
msgid "Song number"
msgstr "Číslo písničky"

#: pdf/templates/admin/pdf/migrate.html:4
#: pdf/templates/admin/pdf/migrate.html:5
msgid "Migrating PDFRequests to Tenant"
msgstr "Přesouvání PDF požadavků do Tenant"

#: pdf/templates/admin/pdf/migrate.html:7
msgid "To Tenant"
msgstr "Do Tenanta"

#: pdf/templates/pdf/index.html:45
msgid "Table of Contents"
msgstr "Obsah"
Expand Down Expand Up @@ -303,41 +294,35 @@ msgstr "Postup"
msgid "songbook"
msgstr "zpevnik"

#: pdf/views.py:45
#: pdf/views.py:47
#, python-format
msgid "Request %(id)s is already in queue"
msgstr "Požadavek %(id)s je již ve frontě"

#: pdf/views.py:51
#: pdf/views.py:53
#, python-format
msgid "Request %(id)s was scheduled for regeneration"
msgstr "Požadavek %(id)s byl označen k přegenerování"

#: pdf/views.py:67
#: pdf/views.py:69
#, python-format
msgid "Unable to remove file from request %(id)s that doesn't have one"
msgstr "Nepodařilo se odstranit soubor z požadavku %(id)s, protože žádný nemá"

#: pdf/views.py:74
#: pdf/views.py:76
#, python-format
msgid "File %(name)s was successfully deleted"
msgstr "Soubor %(name)s byl úspěšně odstraněn"

#: pdf/views.py:111
#: pdf/views.py:113
msgid "Required parameters not found"
msgstr "Požadované parametry nenalezeny"

#: pdf/views.py:116
#: pdf/views.py:118
msgid "You need to select at least one song"
msgstr "Musíte vybrat alespoň jednu písničku"

#: pdf/views.py:141
#: pdf/views.py:143
#, python-format
msgid "PDF Request with id %(id)s was successfully created"
msgstr "PDF požadavek se id %(id)s byl úspěšně vytvořen"

#~ msgid "Search"
#~ msgstr "Hledat"

#~ msgid "Insert query"
#~ msgstr "Vložte text"
6 changes: 6 additions & 0 deletions pdf/models/request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Models for PDF module"""

import os
from datetime import datetime
from typing import List

Expand Down Expand Up @@ -68,6 +69,11 @@ class PDFRequest(PDFOptions):
category = ForeignKey(Category, null=True, on_delete=SET_NULL)
scheduled_at = DateTimeField(null=True)

@property
def name(self):
"""Returns displayable name"""
return os.path.basename(self.file.name)

def get_songs(self) -> List[Song]:
"""Returns all songs for request"""
return [transform_song(pdf_song) for pdf_song in PDFSong.objects.filter(request=self)]
Expand Down
7 changes: 0 additions & 7 deletions pdf/templates/admin/pdf/migrate.html

This file was deleted.

Loading

0 comments on commit 75fe192

Please sign in to comment.