Coverage for cookbook/tables.py: 92%

60 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2023-12-29 00:47 +0100

1import django_tables2 as tables 

2from django.utils.html import format_html 

3from django.utils.translation import gettext as _ 

4from django_tables2.utils import A 

5 

6from .models import CookLog, InviteLink, RecipeImport, Storage, Sync, SyncLog, ViewLog 

7 

8 

9class StorageTable(tables.Table): 

10 id = tables.LinkColumn('edit_storage', args=[A('id')]) 

11 

12 class Meta: 

13 model = Storage 

14 template_name = 'generic/table_template.html' 

15 fields = ('id', 'name', 'method') 

16 

17 

18class ImportLogTable(tables.Table): 

19 sync_id = tables.LinkColumn('edit_sync', args=[A('sync_id')]) 

20 

21 @staticmethod 

22 def render_status(value): 

23 if value == 'SUCCESS': 

24 return format_html( 

25 '<span class="badge badge-success">%s</span>' % value 

26 ) 

27 else: 

28 return format_html( 

29 '<span class="badge badge-danger">%s</span>' % value 

30 ) 

31 

32 class Meta: 

33 model = SyncLog 

34 template_name = 'generic/table_template.html' 

35 fields = ('status', 'msg', 'sync_id', 'created_at') 

36 

37 

38class SyncTable(tables.Table): 

39 id = tables.LinkColumn('edit_sync', args=[A('id')]) 

40 

41 @staticmethod 

42 def render_path(value): 

43 return format_html('<code>%s</code>' % value) 

44 

45 @staticmethod 

46 def render_storage(value): 

47 return format_html( 

48 '<span class="badge badge-success">%s</span>' % value 

49 ) 

50 

51 class Meta: 

52 model = Sync 

53 template_name = 'generic/table_template.html' 

54 fields = ('id', 'path', 'storage', 'last_checked') 

55 

56 

57class RecipeImportTable(tables.Table): 

58 id = tables.LinkColumn('new_recipe_import', args=[A('id')]) 

59 delete = tables.TemplateColumn( 

60 "<a href='{% url 'delete_recipe_import' record.id %}' >" + _('Delete') + "</a>" # noqa: E501 

61 ) 

62 

63 class Meta: 

64 model = RecipeImport 

65 template_name = 'generic/table_template.html' 

66 fields = ('id', 'name', 'file_path') 

67 

68 

69class InviteLinkTable(tables.Table): 

70 link = tables.TemplateColumn( 

71 "<input value='{{ request.scheme }}://{{ request.get_host }}{% url 'view_invite' record.uuid %}' class='form-control' />" 

72 ) 

73 delete_link = tables.TemplateColumn( 

74 "<a href='{% url 'delete_invite_link' record.pk %}' >" + _('Delete') + "</a>", verbose_name=_('Delete') 

75 ) 

76 

77 class Meta: 

78 model = InviteLink 

79 template_name = 'generic/table_template.html' 

80 fields = ( 

81 'username', 'group', 'valid_until', 

82 ) 

83 

84 

85class ViewLogTable(tables.Table): 

86 recipe = tables.LinkColumn('view_recipe', args=[A('recipe_id')]) 

87 

88 class Meta: 

89 model = ViewLog 

90 template_name = 'generic/table_template.html' 

91 fields = ('recipe', 'created_at') 

92 

93 

94class CookLogTable(tables.Table): 

95 recipe = tables.LinkColumn('view_recipe', args=[A('recipe_id')]) 

96 

97 class Meta: 

98 model = CookLog 

99 template_name = 'generic/table_template.html' 

100 fields = ('recipe', 'rating', 'serving', 'created_at')