refactor: Add staticmethod to get a number of texts

This commit is contained in:
moanos [he/him] 2024-10-05 11:02:26 +02:00
parent e6428965c4
commit 83a219df0c
2 changed files with 15 additions and 6 deletions

View File

@ -516,6 +516,18 @@ class Text(models.Model):
def __str__(self): def __str__(self):
return f"{self.title} ({self.language})" 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): class Announcement(Text):
""" """

View File

@ -46,12 +46,9 @@ def index(request):
lang = Language.objects.get(languagecode=language_code) lang = Language.objects.get(languagecode=language_code)
active_announcements = Announcement.get_active_announcements(lang) active_announcements = Announcement.get_active_announcements(lang)
context = {"adoption_notices": active_adoptions[:5], "adoption_notices_map": active_adoptions, "announcements": active_announcements} context = {"adoption_notices": active_adoptions[:5], "adoption_notices_map": active_adoptions,
for text_code in ["how_to", "introduction"]: "announcements": active_announcements}
try: Text.get_texts(["how_to", "introduction"], lang, context)
context[text_code] = Text.objects.get(text_code=text_code, language=lang, )
except Text.DoesNotExist:
context[text_code] = None
return render(request, 'fellchensammlung/index.html', context=context) return render(request, 'fellchensammlung/index.html', context=context)