Fix bug with loading a blog post
- Author
- Maarten 'Vngngdn' Vangeneugden
- Date
- Oct. 19, 2017, 6:44 a.m.
- Hash
- c9288536c0a0c31e9f9c8fc39522b5696f7fb230
- Parent
- 157c956b2575553e2a52ceaea98ed5fcee36e858
- Modified files
- urls.py
- views.py
urls.py ¶
1 addition and 1 deletion.
views.py ¶
11 additions and 10 deletions.
View changes Hide changes
1 |
1 |
import subprocess |
2 |
2 |
|
3 |
3 |
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. |
4 |
4 |
from django.http import HttpResponseRedirect, HttpResponse |
5 |
5 |
from django.core.urlresolvers import reverse # Why? |
6 |
6 |
from django.template import loader # This allows to actually load the template. |
7 |
7 |
from django.contrib.auth.decorators import login_required |
8 |
8 |
from django.contrib.auth import authenticate, login |
9 |
9 |
from .models import Post |
10 |
10 |
from django.core.exceptions import ObjectDoesNotExist |
11 |
11 |
from django.utils import translation |
12 |
12 |
|
13 |
13 |
# FIXME: Remove this template trash. THIS IS A VIEW, NOT A FUCKING TEMPLATE FFS |
14 |
14 |
context = { |
15 |
15 |
'materialDesign_color': "green", |
16 |
16 |
'materialDesign_accentColor': "purple", |
17 |
17 |
'navbar_title': "Blog", |
18 |
18 |
'navbar_fixed': True, |
19 |
19 |
'navbar_backArrow': True, |
20 |
20 |
#'footer_title': "Maarten's blog", |
21 |
21 |
#'footer_description': "My personal scribbly notepad.", |
22 |
22 |
#'footer_links': footer_links, |
23 |
23 |
} |
24 |
24 |
|
25 |
25 |
def markdown_to_html(file_path): |
26 |
26 |
""" Converts the given Markdown 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 |
return subprocess.check_output(["pandoc", "--from=markdown", "--to=html", file_path]) |
32 |
32 |
|
33 |
33 |
def get_available_post_languages(post): |
34 |
34 |
""" Returns the language codes for which a blog post exists. This function |
35 |
35 |
always returns English (because that field mustn't be empty). |
36 |
36 |
So say a blog post has an English, Dutch and French version (which means |
37 |
37 |
english_file, french_file and dutch_file aren't empty), the function will return {"en", |
38 |
38 |
"fr", "nl"}. """ |
39 |
39 |
available_languages = {"en"} |
40 |
40 |
if post.german_file is not None: |
41 |
41 |
available_languages.add("de") |
42 |
42 |
if post.spanish_file is not None: |
43 |
43 |
available_languages.add("es") |
44 |
44 |
if post.french_file is not None: |
45 |
45 |
available_languages.add("fr") |
46 |
46 |
if post.dutch_file is not None: |
47 |
47 |
available_languages.add("nl") |
48 |
48 |
return available_languages |
49 |
49 |
|
50 |
50 |
def get_preferred_post_language(post, language): |
51 |
51 |
""" Returns the post language file that best suits the given language. This |
52 |
52 |
is handy if you know what language the user prefers, but aren't sure whether |
53 |
53 |
you can provide that language. This function will try to provide the file |
54 |
54 |
for that language, or return English if that's not possible. """ |
55 |
55 |
if language == "de" and post.german_file is not None: |
56 |
56 |
return post.german_file |
57 |
57 |
if language == "es" and post.spanish_file is not None: |
58 |
58 |
return post.spanish_file |
59 |
59 |
if language == "fr" and post.french_file is not None: |
60 |
60 |
return post.french_file |
61 |
61 |
if language == "nl" and post.dutch_file is not None: |
62 |
62 |
return post.dutch_file |
63 |
63 |
return post.english_file # Returned if all other choices wouldn't be satisfactory, or the requested language is English. |
64 |
64 |
|
65 |
65 |
def index(request): |
66 |
66 |
template = "blog/index.html" |
67 |
67 |
posts = Post.objects.all() |
68 |
68 |
language = translation.get_language() |
69 |
69 |
|
70 |
70 |
post_links = [] |
71 |
71 |
for post in posts: |
72 |
72 |
blog_file = get_preferred_post_language(post, language) |
73 |
73 |
# TODO: Find a cleaner way to determine the title. First and foremost: |
74 |
74 |
# If the language differs from English, the other language file needs to |
75 |
75 |
# be loaded. Plus: look for a built in function to remove the full path |
76 |
76 |
# and only return the file name. |
77 |
77 |
title = (blog_file.name.rpartition("/")[2]).rpartition(".")[0] |
78 |
78 |
date = post.published |
79 |
79 |
description = "Lorem ipsum" |
80 |
80 |
# TODO: The link can possibly be reversed in the DTL using the title, which is actually |
81 |
81 |
# a cleaner way to do it. Investigate. |
82 |
82 |
link = reverse("blog-post", args=[str(post)]) |
83 |
83 |
post_links.append([title, date, description, link]) |
84 |
84 |
|
85 |
85 |
context = { |
86 |
86 |
'post_links': post_links, |
87 |
87 |
} |
88 |
88 |
return render(request, template, context) |
89 |
89 |
|
90 |
90 |
def post(request, title): |
91 |
91 |
template = "blog/post.html" |
92 |
92 |
posts = Post.objects.get(english_file=title) |
93 |
- | language = translation.get_language() |
94 |
- | blog_file = get_preferred_post_language(post, language) |
95 |
- | blog_text = markdown(blog_file) |
96 |
- | |
97 |
- | context = { |
98 |
- | 'article': blog_text, |
99 |
- | 'title': blog_file.name, |
100 |
- | } |
101 |
- | return render(request, template, context) |
102 |
- | |
+ |
93 |
for post in posts: |
+ |
94 |
if str(post)==title: |
+ |
95 |
language = translation.get_language() |
+ |
96 |
blog_file = get_preferred_post_language(post, language) |
+ |
97 |
blog_text = markdown_to_html(blog_file) |
+ |
98 |
context = { |
+ |
99 |
'article': blog_text, |
+ |
100 |
'title': blog_file.name, |
+ |
101 |
} |
+ |
102 |
return render(request, template, context) |
+ |
103 |