blog

Fix bug about post loading and rendering

Author
Maarten Vangeneugden
Date
Oct. 19, 2017, 6:48 a.m.
Hash
471cd0b503cd7f13b06121119bc8f65cd378c7f1
Parent
c847712754e521e95976dadebcde679c565e45c9
Modified files
urls.py
views.py

urls.py

1 addition and 1 deletion.

View changes Hide changes
1
1
2
2
from . import views # Imports the views from the same directory (which is views.py).
3
3
4
4
urlpatterns = [
5
5
        url(r'^$', views.index, name='blog-index'),
6
6
        url(r'^(?P<title>[.]+)$', views.post, name='blog-post'),
7
-
        ]
+
7
        ]
8
8

views.py

1 addition and 1 deletion.

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", "/srv/django/website/media/"+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
        blog_text = markdown_to_html(blog_file.name)
81
81
        #blog_text = blog_text.replace("\\n\\n", "</p><p>")
82
82
        #blog_text = blog_text.replace("\\n", " ")
83
83
        description = blog_text
84
84
        # TODO: The link can possibly be reversed in the DTL using the title, which is actually
85
85
        # a cleaner way to do it. Investigate.
86
86
        link = reverse("blog-post", args=[str(post)])
87
87
        post_links.append([title, date, description, link])
88
88
89
89
    context = {
90
90
            'post_links': post_links,
91
91
            }
92
92
    return render(request, template, context)
93
93
94
94
def post(request, title):
95
95
    template = "blog/post.html"
96
96
    posts = Post.objects.all()
97
97
    for post in posts:
98
98
        if str(post)==title:
99
99
            language = translation.get_language()
100
100
            blog_file = get_preferred_post_language(post, language)
101
101
            blog_text = markdown_to_html(blog_file)
102
-
            context = {
+
102
            context = {
103
103
                'article': blog_text,
104
104
                'title': blog_file.name,
105
105
                }
106
106
            return render(request, template, context)
107
107