1
+ from wagtail .models import Page
2
+ from wagtail .fields import RichTextField
3
+ from wagtail .admin .panels import FieldPanel
4
+ from django .core .exceptions import ObjectDoesNotExist
1
5
from django .db import models
6
+ from django .apps import apps
7
+ import httpx
8
+ import markdown
2
9
3
- # Create your models here.
10
+
11
+ class PKBasePage (Page ):
12
+ class Meta :
13
+ abstract = True
14
+
15
+ def get_context (self , request , * args , ** kwargs ):
16
+ "common context for PK"
17
+ context = super ().get_context (request , * args , ** kwargs )
18
+
19
+ SponsorPageModel = apps .get_model ("pythonkr" , "PKSponsors" )
20
+ try :
21
+ sponsor_page = SponsorPageModel .objects .get ()
22
+ except ObjectDoesNotExist :
23
+ return context
24
+
25
+ context ["looking_for_sponsors" ] = sponsor_page .is_looking_for_sponsors
26
+ context ["current_sponsors" ] = []
27
+
28
+ return context
29
+
30
+ class PKSponsors (PKBasePage ):
31
+ content = RichTextField ()
32
+ is_looking_for_sponsors = models .BooleanField (default = False )
33
+
34
+ parent_page_types = ["pythonkr.PKHomePage" ]
35
+
36
+ content_panels = Page .content_panels + [
37
+ FieldPanel ("content" ),
38
+ FieldPanel ("is_looking_for_sponsors" ),
39
+ ]
40
+
41
+ class PKPage (PKBasePage ):
42
+ template = "pythonkr/pk_page.html"
43
+ content = RichTextField (blank = True )
44
+
45
+ parent_page_types = ["pythonkr.PKHomePage" ]
46
+
47
+ content_panels = Page .content_panels + [
48
+ FieldPanel ("content" ),
49
+ ]
50
+
51
+
52
+ class PKDocPage (PKBasePage ):
53
+ template = "pythonkr/pk_markdown_doc.html"
54
+
55
+ content = RichTextField (blank = True )
56
+ markdown_url = models .URLField (blank = True , null = True )
57
+ parent_page_types = ["pythonkr.PKHomePage" ]
58
+
59
+ content_panels = Page .content_panels + [
60
+ FieldPanel ("content" ),
61
+ FieldPanel ("markdown_url" ),
62
+ ]
63
+
64
+ def _render_markdown (self , markdown_text ):
65
+ return markdown .markdown (markdown_text )
66
+
67
+ def get_rendered_content (self ):
68
+ if self .markdown_url :
69
+ response = httpx .get (self .markdown_url )
70
+ if response .status_code == 200 :
71
+ return self ._render_markdown (response .text )
72
+ return self .content
73
+
74
+ def save (self , * args , ** kwargs ):
75
+ if self .markdown_url :
76
+ response = httpx .get (self .markdown_url )
77
+ if response .status_code == 200 :
78
+ self .content = self ._render_markdown (response .text )
79
+ super ().save (* args , ** kwargs )
80
+
81
+
82
+ class PKHomePage (PKBasePage ):
83
+ template = "pythonkr/pk_home.html"
84
+ body = RichTextField (blank = True )
85
+
86
+ subpage_types = [
87
+ PKPage ,
88
+ PKDocPage ,
89
+ ]
90
+
91
+ content_panels = Page .content_panels + [
92
+ FieldPanel ("body" ),
93
+ ]
94
+
95
+ def get_context (self , request , * args , ** kwargs ):
96
+ context = super ().get_context (request , * args , ** kwargs )
97
+ return context
0 commit comments