blog

Fix bug with loading blog posts

Author
Maarten Vangeneugden
Date
Oct. 19, 2017, 6:45 a.m.
Hash
e0f666cebed6fa6b160a057ccc49f174d75821d5
Parent
64957dedb35f24df5d3712ced37d0422efab8f23
Modified file
views.py

views.py

4 additions and 4 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
        blog_text = markdown(blog_file.read())
81
-
        blog_text = blog_text.replace("\\n\\n", "</p><p>")
82
-
        blog_text = blog_text.replace("\\n", " ")
83
-
        description = blog_text
+
81
        #blog_text = blog_text.replace("\\n\\n", "</p><p>")
+
82
        #blog_text = blog_text.replace("\\n", " ")
+
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.get(english_file=title)
97
97
    language = translation.get_language()
98
98
    blog_file = get_preferred_post_language(post, language)
99
99
    blog_text = markdown(blog_file)
100
100
101
101
    context = {
102
102
        'article': blog_text,
103
103
        'title': blog_file.name,
104
104
        }
105
105
    return render(request, template, context)
106
106