Coverage for website/announcements/admin.py: 88.57%

35 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2025-08-14 10:31 +0000

1from django.contrib import admin 

2from django.template.defaultfilters import striptags 

3 

4from events.admin.event import EventAdmin as BaseEventAdmin 

5from events.models import Event 

6from thaliawebsite.templatetags.bleach_tags import bleach 

7 

8from .models import Announcement, FrontpageArticle, Slide 

9 

10 

11@admin.register(Announcement) 

12class AnnouncementAdmin(admin.ModelAdmin): 

13 """Manage the admin pages for the announcements.""" 

14 

15 #: show these fields in the admin overview list 

16 #: see :py:method:content_html for the 'content_html' field 

17 #: see :py:method:visible for the visible field 

18 list_display = ("content_html", "since", "until", "visible") 

19 

20 def content_html(self, obj): 

21 """Get the content of the object as html. 

22 

23 :param obj: the object to render for 

24 :return: the stripped html 

25 """ 

26 # Both bleach and striptags. 

27 # First to convert HTML entities and second to strip all HTML 

28 return bleach(striptags(obj.content)) 

29 

30 def visible(self, obj): 

31 """Is the object visible.""" 

32 return obj.is_visible 

33 

34 visible.boolean = True 

35 

36 

37@admin.register(FrontpageArticle) 

38class FrontpageArticleAdmin(admin.ModelAdmin): 

39 """Manage front page articles.""" 

40 

41 #: available fields in the admin overview list 

42 list_display = ("title", "since", "until", "visible") 

43 

44 def visible(self, obj): 

45 """Is the object visible.""" 

46 return obj.is_visible 

47 

48 visible.boolean = True 

49 

50 

51@admin.register(Slide) 

52class SlideAdmin(admin.ModelAdmin): 

53 """Manage the admin pages for the slides.""" 

54 

55 #: show these fields in the admin overview list 

56 #: see :py:method:visible for the visible field 

57 list_display = ("title", "since", "until", "visible") 

58 

59 def visible(self, obj): 

60 """Is the object visible.""" 

61 return obj.is_visible 

62 

63 visible.boolean = True 

64 

65 

66class SlideInline(admin.StackedInline): 

67 model = Slide 

68 extra = 0 

69 classes = ("collapse",) 

70 

71 

72class EventAdmin(BaseEventAdmin): 

73 def get_inlines(self, request, obj=None): 

74 return [*super().get_inlines(request, obj), SlideInline] 

75 

76 

77# Unregister the original EventAdmin and register the new one with SlideInline. 

78admin.site.unregister(Event) 

79admin.site.register(Event, EventAdmin)