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