Coverage for src/django_resume/plugins/cover.py: 82%

26 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-13 13:17 +0200

1from django import forms 

2from django.http import HttpRequest 

3 

4from .base import SimplePlugin, SimpleTemplates, ContextDict 

5from ..markdown import ( 

6 markdown_to_html, 

7 textarea_input_to_markdown, 

8 markdown_to_textarea_input, 

9) 

10 

11 

12class CoverForm(forms.Form): 

13 title = forms.CharField( 

14 label="Cover Letter Title", 

15 max_length=256, 

16 initial="Cover Title", 

17 ) 

18 text = forms.CharField( 

19 label="Cover Letter Text", 

20 max_length=1024, 

21 initial="Some cover letter text...", 

22 widget=forms.Textarea(), 

23 ) 

24 

25 def __init__(self, *args, **kwargs): 

26 super().__init__(*args, **kwargs) 

27 # Transform initial text from markdown to textarea input. 

28 self.initial["text"] = markdown_to_textarea_input(self.initial.get("text", "")) 

29 

30 def clean_text(self): 

31 text = self.cleaned_data["text"] 

32 text = textarea_input_to_markdown(text) 

33 return text 

34 

35 

36class CoverPlugin(SimplePlugin): 

37 name: str = "cover" 

38 verbose_name: str = "Cover Letter" 

39 templates = SimpleTemplates( 

40 main="django_resume/cover/plain/content.html", 

41 form="django_resume/cover/plain/form.html", 

42 ) 

43 admin_form_class = inline_form_class = CoverForm 

44 

45 def get_context( 

46 self, 

47 _request: HttpRequest, 

48 plugin_data: dict, 

49 resume_pk: int, 

50 *, 

51 context: ContextDict, 

52 edit: bool = False, 

53 ) -> ContextDict: 

54 context = super().get_context( 

55 _request, plugin_data, resume_pk, context=context, edit=edit 

56 ) 

57 cover_text = plugin_data.get("text") 

58 print("cover_text", cover_text) 

59 if cover_text is not None: 59 ↛ 60line 59 didn't jump to line 60 because the condition on line 59 was never true

60 context["text"] = markdown_to_html(cover_text) 

61 return context