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