Coverage for website/photos/templatetags/photos_cards.py: 76.60%

41 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2025-10-29 17:07 +0000

1from django import template 

2from django.template.defaultfilters import date 

3from django.urls import reverse 

4 

5from thaliawebsite.templatetags.grid_item import grid_item 

6from utils.media.services import get_media_url 

7from django.utils.html import format_html 

8 

9register = template.Library() 

10 

11 

12@register.inclusion_tag("includes/grid_item.html") 

13def album_card(album): 

14 """Create a card to show on a page of multiple albums.""" 

15 class_name = "album-card" 

16 image_url = "" 

17 

18 if album.cover: 18 ↛ 19line 18 didn't jump to line 19 because the condition on line 18 was never true

19 image_url = album.cover.file.thumbnails.medium.url 

20 

21 url = album.get_absolute_url 

22 if not album.accessible: 22 ↛ 23line 22 didn't jump to line 23 because the condition on line 22 was never true

23 class_name += " grayscale" 

24 url = None 

25 

26 return grid_item( 

27 title=album.title, 

28 meta_text=f'<p>{date(album.date, "Y-m-d")}</p>', 

29 url=url, 

30 image_url=image_url, 

31 class_name=class_name, 

32 ) 

33 

34 

35@register.inclusion_tag("includes/grid_item.html") 

36def photo_card(photo): 

37 """Create a card of a photo to show on an album page.""" 

38 class_name = "photo-card" 

39 anchor_attrs = 'data-fancybox="gallery"' 

40 

41 if photo.album.shareable: 

42 url = reverse( 

43 "photos:shared-download", 

44 args=[photo.album.slug, photo.album.access_token, photo], 

45 ) 

46 anchor_attrs += f" data-download-src={url}" 

47 else: 

48 url = reverse("photos:download", args=[photo.album.slug, photo]) 

49 anchor_attrs += f" data-download-src={url}" 

50 

51 anchor_attrs += f" data-numLikes='{photo.num_likes}'" 

52 anchor_attrs += ( 

53 f" data-likeUrl={reverse('api:v2:photos:photo-like', args=[photo.pk])}" 

54 ) 

55 

56 image_url = get_media_url(photo.file.thumbnails.medium) 

57 

58 return grid_item( 

59 title="", 

60 meta_text=f"<p><i class='fas fa-heart me-2'></i>{photo.num_likes}</p>", 

61 url=photo.file.url, 

62 image_url=image_url, 

63 class_name=class_name, 

64 anchor_attrs=anchor_attrs, 

65 album_title=photo.album.title, 

66 ) 

67 

68 

69@register.inclusion_tag("includes/grid_item.html") 

70def liked_photo_card(photo): 

71 card = photo_card(photo) 

72 card.update({"meta_text": ""}) 

73 return card 

74 

75 

76@register.inclusion_tag("includes/grid_item.html") 

77def mostliked_photo_card(photo): 

78 card = photo_card(photo) 

79 card["meta_text"] = format_html( 

80 "<h5 class='px-2'>{title}</h5><p><i class='fas fa-heart me-2'></i>{likes}</p>", 

81 title=photo.album.title, 

82 likes=photo.num_likes, 

83 ) 

84 return card