Add comment form to render context
- Author
- Maarten 'Vngngdn' Vangeneugden
- Date
- April 9, 2018, 5:51 p.m.
- Hash
- ce775c486d88b3dc9babe1b24b00b5437333dd51
- Parent
- b40673fe82c25edf6f3530d7cf2ca0a8cd4d9ce5
- Modified file
- views.py
views.py ¶
2 additions and 3 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.core.urlresolvers import reverse # Why? |
7 |
- | from django.template import loader # This allows to actually load the template. |
+ |
7 |
from django.template import loader # This allows to actually load the template. |
8 |
8 |
from django.contrib.auth.decorators import login_required |
9 |
- | from django.contrib.auth import authenticate, login |
10 |
- | from .models import Post, Comment |
11 |
9 |
from .forms import CommentForm |
12 |
10 |
from django.core.exceptions import ObjectDoesNotExist |
13 |
11 |
from django.utils import translation |
14 |
12 |
|
15 |
13 |
GERMAN = "de" |
16 |
14 |
SPANISH = "es" |
17 |
15 |
FRENCH = "fr" |
18 |
16 |
DUTCH = "nl" |
19 |
17 |
ENGLISH = "en" |
20 |
18 |
|
21 |
19 |
footer_links = [ |
22 |
20 |
[_("Blog main page"), "/blog"], |
23 |
21 |
[_("Contact"), "mailto:maarten.vangeneugden@student.uhasselt.be"], |
24 |
22 |
] |
25 |
23 |
footer_description = _("Maarten's personal blog, with sprinkles and a dollop of healthy bugs.") |
26 |
24 |
|
27 |
25 |
# FIXME: Remove this template trash. THIS IS A VIEW, NOT A FUCKING TEMPLATE FFS |
28 |
26 |
context = { |
29 |
27 |
'materialDesign_color': "green", |
30 |
28 |
'materialDesign_accentColor': "purple", |
31 |
29 |
'navbar_title': "Blog", |
32 |
30 |
'navbar_fixed': True, |
33 |
31 |
'navbar_backArrow': True, |
34 |
32 |
#'footer_title': "Maarten's blog", |
35 |
33 |
#'footer_description': "My personal scribbly notepad.", |
36 |
34 |
#'footer_links': footer_links, |
37 |
35 |
} |
38 |
36 |
|
39 |
37 |
def org_to_html(file_path): |
40 |
38 |
""" Converts the given org formatted file to HTML. |
41 |
39 |
This function directly returns the resulting HTML code. This function uses |
42 |
40 |
the amazing Haskell library Pandoc to convert the file (and takes care |
43 |
41 |
of header id's and all that stuff). |
44 |
42 |
""" |
45 |
43 |
# FIXME: Remove hardcoded link to media. Replace with media tag! |
46 |
44 |
return subprocess.check_output(["pandoc", "--from=org", "--to=html", "/srv/django/website/media/"+file_path]) |
47 |
45 |
|
48 |
46 |
def get_available_post_languages(post): |
49 |
47 |
""" Returns the language codes for which a blog post exists. This function |
50 |
48 |
always returns English (because that field mustn't be empty). |
51 |
49 |
So say a blog post has an English, Dutch and French version (which means |
52 |
50 |
english_file, french_file and dutch_file aren't empty), the function will return {"en", |
53 |
51 |
"fr", "nl"}. """ |
54 |
52 |
available_languages = {ENGLISH} |
55 |
53 |
if post.german_file != "": |
56 |
54 |
available_languages.add(GERMAN) |
57 |
55 |
if post.spanish_file != "": |
58 |
56 |
available_languages.add(SPANISH) |
59 |
57 |
if post.french_file != "": |
60 |
58 |
available_languages.add(FRENCH) |
61 |
59 |
if post.dutch_file != "": |
62 |
60 |
available_languages.add(DUTCH) |
63 |
61 |
return available_languages |
64 |
62 |
|
65 |
63 |
def get_preferred_post_language(post, language): |
66 |
64 |
""" Returns the post language file that best suits the given language. This |
67 |
65 |
is handy if you know what language the user prefers, but aren't sure whether |
68 |
66 |
you can provide that language. This function will try to provide the file |
69 |
67 |
for that language, or return English if that's not possible. """ |
70 |
68 |
if language == GERMAN and post.german_file is not None: |
71 |
69 |
return post.german_file |
72 |
70 |
if language == SPANISH and post.spanish_file is not None: |
73 |
71 |
return post.spanish_file |
74 |
72 |
if language == FRENCH and post.french_file is not None: |
75 |
73 |
return post.french_file |
76 |
74 |
if language == DUTCH and post.dutch_file is not None: |
77 |
75 |
return post.dutch_file |
78 |
76 |
return post.english_file # Returned if all other choices wouldn't be satisfactory, or the requested language is English. |
79 |
77 |
|
80 |
78 |
def index(request): |
81 |
79 |
template = "blog/index.html" |
82 |
80 |
posts = Post.objects.all() |
83 |
81 |
language = translation.get_language() |
84 |
82 |
|
85 |
83 |
post_links = [] |
86 |
84 |
for post in posts: |
87 |
85 |
blog_file = get_preferred_post_language(post, language) |
88 |
86 |
# TODO: Find a cleaner way to determine the title. First and foremost: |
89 |
87 |
# If the language differs from English, the other language file needs to |
90 |
88 |
# be loaded. Plus: look for a built in function to remove the full path |
91 |
89 |
# and only return the file name. |
92 |
90 |
title = (blog_file.name.rpartition("/")[2]).rpartition(".")[0] |
93 |
91 |
human_title = title.replace("_"," ") |
94 |
92 |
date = post.published |
95 |
93 |
blog_text = org_to_html(blog_file.name) |
96 |
94 |
# TODO: The link can possibly be reversed in the DTL using the title, which is actually |
97 |
95 |
# a cleaner way to do it. Investigate. |
98 |
96 |
link = reverse("blog-post-language", args=[language, post.slug(language)]) |
99 |
97 |
post_links.append([human_title, date, blog_text, link]) |
100 |
98 |
|
101 |
99 |
context = { |
102 |
100 |
'posts': post_links, |
103 |
101 |
'materialDesign_color': "brown", |
104 |
102 |
'materialDesign_accentColor': "yellow", |
105 |
103 |
'navbar_title': _("Notepad from a student"), |
106 |
104 |
'navbar_backArrow': True, |
107 |
105 |
'footer_links': footer_links, |
108 |
106 |
'footer_description': footer_description, |
109 |
107 |
} |
110 |
108 |
return render(request, template, context) |
111 |
109 |
|
112 |
110 |
def post(request, post_slug, language=None): |
113 |
111 |
if request.method == "POST": # Handling a reply if one is sent |
114 |
112 |
form = CommentForm(request.POST) |
115 |
113 |
if form.is_valid(): |
116 |
114 |
form.save() |
117 |
115 |
|
118 |
116 |
if language is not None: |
119 |
117 |
if translation.check_for_language(language): |
120 |
118 |
translation.activate(language) |
121 |
119 |
request.session[translation.LANGUAGE_SESSION_KEY] = language |
122 |
120 |
#return post(request, post_slug) |
123 |
121 |
else: |
124 |
122 |
language = translation.get_language() |
125 |
123 |
|
126 |
124 |
template = "blog/post.html" |
127 |
125 |
posts = Post.objects.all() |
128 |
126 |
#comments = Comment.objects.filter(post |
129 |
127 |
for post in posts: |
130 |
128 |
if post.slug(language) == post_slug: |
131 |
129 |
comments = Comment.objects.filter(post=post) |
132 |
130 |
form = CommentForm(initial={'post': post}) |
133 |
131 |
blog_file = get_preferred_post_language(post, language) |
134 |
132 |
blog_text = org_to_html(blog_file.name) |
135 |
133 |
context = { |
136 |
134 |
'comments': comments, |
137 |
135 |
'human_post_title': blog_file.name.replace("_"," "), |
+ |
136 |
'human_post_title': blog_file.name.replace("_"," "), |
138 |
137 |
'materialDesign_color': "brown", |
139 |
138 |
'materialDesign_accentColor': "yellow", |
140 |
139 |
'article': blog_text, |
141 |
140 |
'title': str(blog_file), |
142 |
141 |
'navbar_title': ((blog_file.name.rpartition("/")[2]).rpartition(".")[0]).replace("_"," "), |
143 |
142 |
'navbar_backArrow': False, |
144 |
143 |
'post_slug': post_slug, |
145 |
144 |
'footer_links': footer_links, |
146 |
145 |
'footer_description': footer_description, |
147 |
146 |
} |
148 |
147 |
|
149 |
148 |
# Getting all available article links |
150 |
149 |
available = get_available_post_languages(post) |
151 |
150 |
if ENGLISH in available: |
152 |
151 |
context['english_link'] = reverse("blog-post-language", args=[ENGLISH, post.slug(ENGLISH)]) |
153 |
152 |
if DUTCH in available: |
154 |
153 |
context['dutch_link'] = reverse("blog-post-language", args=[DUTCH, post.slug(DUTCH)]) |
155 |
154 |
|
156 |
155 |
if FRENCH in available: |
157 |
156 |
context['french_link'] = reverse("blog-post-language", args=[FRENCH, post.slug(FRENCH)]) |
158 |
157 |
|
159 |
158 |
if SPANISH in available: |
160 |
159 |
context['spanish_link'] = reverse("blog-post-language", args=[SPANISH, post.slug(SPANISH)]) |
161 |
160 |
|
162 |
161 |
if GERMAN in available: |
163 |
162 |
context['german_link'] = reverse("blog-post-language", args=[GERMAN, post.slug(GERMAN)]) |
164 |
163 |
|
165 |
164 |
return render(request, template, context) |
166 |
165 |