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)