Change view context for new stylesheet
- Author
- Maarten Vangeneugden
- Date
- Sept. 20, 2020, 11:10 p.m.
- Hash
- b949e8b1ddcecce670b0f373e96605ae542c9f9d
- Parent
- d6b7f80b59eed008c3474f60d00e23452f843101
- Modified file
- views.py
views.py ¶
3 additions and 0 deletions.
View changes Hide changes
1 |
1 |
import subprocess |
2 |
2 |
|
3 |
3 |
from django.utils.translation import ugettext as _ |
4 |
4 |
from django.shortcuts import get_object_or_404, render # This allows to render the template with the view here. It's pretty cool and important. |
5 |
5 |
from django.http import HttpResponseRedirect, HttpResponse |
6 |
6 |
from django.urls import reverse |
7 |
7 |
from django.template import loader # This allows to actually load the template. |
8 |
8 |
from .models import * |
9 |
9 |
from .forms import CommentForm |
10 |
10 |
from django.core.exceptions import ObjectDoesNotExist |
11 |
11 |
from django.utils import translation |
12 |
12 |
|
13 |
13 |
GERMAN = "de" |
14 |
14 |
SPANISH = "es" |
15 |
15 |
FRENCH = "fr" |
16 |
16 |
DUTCH = "nl" |
17 |
17 |
ENGLISH = "en" |
18 |
18 |
|
19 |
19 |
footer_links = [ |
20 |
20 |
[_("Back to main page"), "/blog"], |
21 |
21 |
[_("Contact"), "mailto:maarten.vangeneugden@student.uhasselt.be"], |
22 |
22 |
] |
23 |
23 |
footer_description = _("Maarten's personal blog, with sprinkles and a dollop of healthy bugs.") |
24 |
24 |
|
25 |
25 |
def org_to_html(file_path): |
26 |
26 |
""" Converts the given org formatted file to HTML. |
27 |
27 |
This function directly returns the resulting HTML code. This function uses |
28 |
28 |
the amazing Haskell library Pandoc to convert the file (and takes care |
29 |
29 |
of header id's and all that stuff). |
30 |
30 |
""" |
31 |
31 |
# FIXME: Remove hardcoded link to media. Replace with media tag! |
32 |
32 |
# XXX: The reason I'm first converting all occurences of .jpg][ and .png][ |
33 |
33 |
# to .jpgPANDOCBUG][ and .pngPANDOCBUG][, is because of a Pandoc bug that |
34 |
34 |
# removes the text links for images. It is afterwards converted back, no |
35 |
35 |
# worries. |
36 |
36 |
file = open("/srv/django/website/media/"+file_path, "r", encoding="utf-8") |
37 |
37 |
text = file.read() |
38 |
38 |
file.close() |
39 |
39 |
text = text.replace(".jpg][", ".jpgPANDOCBUG][") |
40 |
40 |
text = text.replace(".png][", ".pngPANDOCBUG][") |
41 |
41 |
file = open("/tmp/blog-file.org", "w", encoding="utf-8") |
42 |
42 |
file.write(text) |
43 |
43 |
file.close() |
44 |
44 |
html_text = subprocess.check_output(["pandoc", "--from=org", "--to=html","/tmp/blog-file.org"]) |
45 |
45 |
html_text = html_text.decode("utf-8").replace(".jpgPANDOCBUG", ".jpg") |
46 |
46 |
html_text = html_text.replace(".pngPANDOCBUG", ".png") |
47 |
47 |
return html_text |
48 |
48 |
|
49 |
49 |
def get_available_post_languages(post): |
50 |
50 |
""" Returns the language codes for which a blog post exists. This function |
51 |
51 |
always returns English (because that field mustn't be empty). |
52 |
52 |
So say a blog post has an English, Dutch and French version (which means |
53 |
53 |
english_file, french_file and dutch_file aren't empty), the function will return {"en", |
54 |
54 |
"fr", "nl"}. """ |
55 |
55 |
available_languages = {ENGLISH} |
56 |
56 |
if post.german_file != "": |
57 |
57 |
available_languages.add(GERMAN) |
58 |
58 |
if post.spanish_file != "": |
59 |
59 |
available_languages.add(SPANISH) |
60 |
60 |
if post.french_file != "": |
61 |
61 |
available_languages.add(FRENCH) |
62 |
62 |
if post.dutch_file != "": |
63 |
63 |
available_languages.add(DUTCH) |
64 |
64 |
return available_languages |
65 |
65 |
|
66 |
66 |
def index(request): |
67 |
67 |
template = "blog/index.djhtml" |
68 |
68 |
posts = Post.objects.all() |
69 |
69 |
language = translation.get_language() |
70 |
70 |
|
71 |
71 |
post_links = [] |
72 |
72 |
for post in posts: |
73 |
73 |
blog_file = post.text_file(language) |
74 |
74 |
blog_text = org_to_html(blog_file.name) |
75 |
75 |
# TODO: The link can possibly be reversed in the DTL using the title, which is actually |
76 |
76 |
# a cleaner way to do it. Investigate. |
77 |
77 |
link = reverse("blog-post-language", args=[language, post.slug(language)]) |
78 |
78 |
post_links.append([post.title(language), post.published, blog_text, link]) |
79 |
79 |
|
80 |
80 |
context = { |
81 |
81 |
'posts': post_links, |
82 |
82 |
'materialDesign_color': "brown", |
83 |
83 |
'materialDesign_accentColor': "blue", |
84 |
84 |
'navbar_title': _("Notepad from a student"), |
85 |
85 |
'navbar_backArrow': True, |
86 |
86 |
'footer_links': footer_links, |
87 |
87 |
'footer_description': footer_description, |
88 |
88 |
} |
+ |
89 |
} |
89 |
90 |
if not request.session.get("feed-fab-introduction-seen", default=False): |
90 |
91 |
context['introduce_feed'] = True |
91 |
92 |
request.session['feed-fab-introduction-seen'] = True |
92 |
93 |
return render(request, template, context) |
93 |
94 |
|
94 |
95 |
def post(request, post_slug, language=None): |
95 |
96 |
if request.method == "POST": # Handling a reply if one is sent |
96 |
97 |
form = CommentForm(request.POST) |
97 |
98 |
for post in Post.objects.all(): |
98 |
99 |
if post.slug(language) == post_slug: |
99 |
100 |
form.post = post |
100 |
101 |
break |
101 |
102 |
if form.is_valid(): |
102 |
103 |
new_comment = form.save(commit=False) |
103 |
104 |
for post in Post.objects.all(): |
104 |
105 |
if post.slug(language) == post_slug: |
105 |
106 |
new_comment.post = post |
106 |
107 |
new_comment.save() |
107 |
108 |
if language is not None: |
108 |
109 |
if translation.check_for_language(language): |
109 |
110 |
translation.activate(language) |
110 |
111 |
request.session[translation.LANGUAGE_SESSION_KEY] = language |
111 |
112 |
#return post(request, post_slug) |
112 |
113 |
else: |
113 |
114 |
language = translation.get_language() |
114 |
115 |
|
115 |
116 |
template = "blog/post.djhtml" |
116 |
117 |
posts = Post.objects.all() |
117 |
118 |
#comments = Comment.objects.filter(post |
118 |
119 |
for post in posts: |
119 |
120 |
if post.slug(language) == post_slug: |
120 |
121 |
comments = Comment.objects.filter(post=post) |
121 |
122 |
form = CommentForm() |
122 |
123 |
post_file = post.text_file(language) |
123 |
124 |
post_text = org_to_html(post_file.name) |
124 |
125 |
context = { |
125 |
126 |
'comments': comments, |
126 |
127 |
'form' : form, |
127 |
128 |
'human_post_title': post.title(language), |
128 |
129 |
'materialDesign_color': "brown", |
129 |
130 |
'materialDesign_accentColor': "blue", |
130 |
131 |
'article': post_text, |
131 |
132 |
'title': post.title(language), |
132 |
133 |
'navbar_title': post.title(language), |
133 |
134 |
'navbar_backArrow': False, |
134 |
135 |
'post_slug': post_slug, |
135 |
136 |
'footer_links': footer_links, |
136 |
137 |
'footer_description': footer_description, |
137 |
138 |
} |
+ |
139 |
} |
138 |
140 |
|
139 |
141 |
# Getting all available article links |
140 |
142 |
available = get_available_post_languages(post) |
141 |
143 |
if ENGLISH in available: |
142 |
144 |
context['english_link'] = reverse("blog-post-language", args=[ENGLISH, post.slug(ENGLISH)]) |
143 |
145 |
if DUTCH in available: |
144 |
146 |
context['dutch_link'] = reverse("blog-post-language", args=[DUTCH, post.slug(DUTCH)]) |
145 |
147 |
|
146 |
148 |
if FRENCH in available: |
147 |
149 |
context['french_link'] = reverse("blog-post-language", args=[FRENCH, post.slug(FRENCH)]) |
148 |
150 |
|
149 |
151 |
if SPANISH in available: |
150 |
152 |
context['spanish_link'] = reverse("blog-post-language", args=[SPANISH, post.slug(SPANISH)]) |
151 |
153 |
|
152 |
154 |
if GERMAN in available: |
153 |
155 |
context['german_link'] = reverse("blog-post-language", args=[GERMAN, post.slug(GERMAN)]) |
154 |
156 |
|
155 |
157 |
return render(request, template, context) |
156 |
158 |
|
157 |
159 |
def rss(request): |
158 |
160 |
template = "blog/feed.rss" |
159 |
161 |
context = { |
160 |
162 |
'items': FeedItem.objects.all(), |
161 |
163 |
} |
162 |
164 |
return render(request, template, context) |
163 |
165 |
|
164 |
166 |
|
165 |
167 |
def monthly_archive(request): |
166 |
168 |
template = "blog/monthly_archive.djhtml" |
167 |
169 |
language = translation.get_language() |
168 |
170 |
|
169 |
171 |
file_2017 = org_to_html("blog/weekly/2017.org") |
170 |
172 |
file_2018 = org_to_html("blog/weekly/2018.org") |
171 |
173 |
file_2019 = org_to_html("blog/weekly/2019.org") |
172 |
174 |
|
173 |
175 |
|
174 |
176 |
|
175 |
177 |
context = { |
176 |
178 |
't2017': file_2017, |
177 |
179 |
't2018': file_2018, |
178 |
180 |
't2019': file_2019, |
179 |
181 |
'materialDesign_color': "brown", |
180 |
182 |
'materialDesign_accentColor': "blue", |
181 |
183 |
'navbar_title': _("Weekly-archief"), |
182 |
184 |
'navbar_backArrow': True, |
183 |
185 |
'footer_links': footer_links, |
184 |
186 |
'footer_description': footer_description, |
185 |
187 |
} |
+ |
188 |
} |
186 |
189 |
return render(request, template, context) |
187 |
190 |