From 83a219df0c0eab39f43b33633336ec14c1dab869 Mon Sep 17 00:00:00 2001 From: moanos Date: Sat, 5 Oct 2024 11:02:26 +0200 Subject: [PATCH] refactor: Add staticmethod to get a number of texts --- src/fellchensammlung/models.py | 12 ++++++++++++ src/fellchensammlung/views.py | 9 +++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index f3123c0..7c8c577 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -516,6 +516,18 @@ class Text(models.Model): def __str__(self): return f"{self.title} ({self.language})" + @staticmethod + def get_texts(text_codes, language, expandable_dict=None): + if expandable_dict is None: + expandable_dict = {} + for text_code in text_codes: + try: + expandable_dict[text_code] = Text.objects.get(text_code=text_code, language=language, ) + except Text.DoesNotExist: + expandable_dict[text_code] = None + return expandable_dict + + class Announcement(Text): """ diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index ab2020b..86542f4 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -46,12 +46,9 @@ def index(request): lang = Language.objects.get(languagecode=language_code) active_announcements = Announcement.get_active_announcements(lang) - context = {"adoption_notices": active_adoptions[:5], "adoption_notices_map": active_adoptions, "announcements": active_announcements} - for text_code in ["how_to", "introduction"]: - try: - context[text_code] = Text.objects.get(text_code=text_code, language=lang, ) - except Text.DoesNotExist: - context[text_code] = None + context = {"adoption_notices": active_adoptions[:5], "adoption_notices_map": active_adoptions, + "announcements": active_announcements} + Text.get_texts(["how_to", "introduction"], lang, context) return render(request, 'fellchensammlung/index.html', context=context)