blog

Add new view function for specific languages

Author
Maarten 'Vngngdn' Vangeneugden
Date
Jan. 4, 2018, 11:42 p.m.
Hash
ad92bb1cd2593778402803c2cf8fbcd743f2eab0
Parent
1f838f93bc8a322bb126311c62c927a8a515ee29
Modified file
views.py

views.py

5 additions and 0 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
7
from django.template import loader # This allows to actually load the template.
8
8
from django.contrib.auth.decorators import login_required
9
9
from django.contrib.auth import authenticate, login
10
10
from .models import Post
11
11
from django.core.exceptions import ObjectDoesNotExist
12
12
from django.utils import translation
13
13
14
14
GERMAN = "de"
15
15
SPANISH = "es"
16
16
FRENCH = "fr"
17
17
DUTCH = "nl"
18
18
ENGLISH = "en"
19
19
20
20
# FIXME: Remove this template trash. THIS IS A VIEW, NOT A FUCKING TEMPLATE FFS
21
21
context = {
22
22
    'materialDesign_color': "green",
23
23
    'materialDesign_accentColor': "purple",
24
24
    'navbar_title': "Blog",
25
25
    'navbar_fixed': True,
26
26
    'navbar_backArrow': True,
27
27
    #'footer_title': "Maarten's blog",
28
28
    #'footer_description': "My personal scribbly notepad.",
29
29
    #'footer_links': footer_links,
30
30
    }
31
31
32
32
def markdown_to_html(file_path):
33
33
    """ Converts the given Markdown formatted file to HTML.
34
34
    This function directly returns the resulting HTML code. This function uses
35
35
    the amazing Haskell library Pandoc to convert the file (and takes care
36
36
    of header id's and all that stuff).
37
37
    """
38
38
    # FIXME: Remove hardcoded link to media. Replace with media tag!
39
39
    return subprocess.check_output(["pandoc", "--from=org", "--to=html", "/srv/django/website/media/"+file_path])
40
40
41
41
def get_available_post_languages(post):
42
42
    """ Returns the language codes for which a blog post exists. This function
43
43
    always returns English (because that field mustn't be empty).
44
44
    So say a blog post has an English, Dutch and French version (which means
45
45
    english_file, french_file and dutch_file aren't empty), the function will return {"en",
46
46
    "fr", "nl"}. """
47
47
    available_languages = {ENGLISH}
48
48
    if post.german_file is not None:
49
49
        available_languages.add(GERMAN)
50
50
    if post.spanish_file is not None:
51
51
        available_languages.add(SPANISH)
52
52
    if post.french_file is not None:
53
53
        available_languages.add(FRENCH)
54
54
    if post.dutch_file is not None:
55
55
        available_languages.add(DUTCH)
56
56
    return available_languages
57
57
58
58
def get_preferred_post_language(post, language):
59
59
    """ Returns the post language file that best suits the given language. This
60
60
    is handy if you know what language the user prefers, but aren't sure whether
61
61
    you can provide that language. This function will try to provide the file
62
62
    for that language, or return English if that's not possible. """
63
63
    if language == GERMAN and post.german_file is not None:
64
64
        return post.german_file
65
65
    if language == SPANISH and post.spanish_file is not None:
66
66
        return post.spanish_file
67
67
    if language == FRENCH and post.french_file is not None:
68
68
        return post.french_file
69
69
    if language == DUTCH and post.dutch_file is not None:
70
70
        return post.dutch_file
71
71
    return post.english_file  # Returned if all other choices wouldn't be satisfactory, or the requested language is English.
72
72
73
73
def index(request):
74
74
    template = "blog/index.html"
75
75
    posts = Post.objects.all()
76
76
    language = translation.get_language()
77
77
78
78
    post_links = []
79
79
    for post in posts:
80
80
        blog_file = get_preferred_post_language(post, language)
81
81
        # TODO: Find a cleaner way to determine the title. First and foremost:
82
82
        # If the language differs from English, the other language file needs to
83
83
        # be loaded. Plus: look for a built in function to remove the full path
84
84
        # and only return the file name.
85
85
        title = (blog_file.name.rpartition("/")[2]).rpartition(".")[0]
86
86
        date = post.published
87
87
        blog_text = markdown_to_html(blog_file.name)
88
88
        # TODO: The link can possibly be reversed in the DTL using the title, which is actually
89
89
        # a cleaner way to do it. Investigate.
90
90
        link = reverse("blog-post", args=[str(post)])
91
91
        post_links.append([title, date, description, link])
92
92
93
93
    context = {
94
94
            'post_links': post_links,
95
95
            'materialDesign_color': "brown",
96
96
            'materialDesign_accentColor': "yellow",
97
97
            'navbar_title': _("Notepad from a student"),
98
98
            'navbar_backArrow': True,
99
99
            }
100
100
    return render(request, template, context)
101
101
102
102
def post(request, title):
103
103
    template = "blog/post.html"
104
104
    posts = Post.objects.all()
105
105
    for post in posts:
106
106
        if str(post)==title:
107
107
            language = translation.get_language()
108
108
            blog_file = get_preferred_post_language(post, language)
109
109
            blog_text = markdown_to_html(blog_file.name)
110
110
            context = {
111
111
                'materialDesign_color': "brown",
112
112
                'materialDesign_accentColor': "yellow",
113
113
                'article': blog_text,
114
114
                'title': blog_file.name,
115
115
                'navbar_title': (blog_file.name.rpartition("/")[2]).rpartition(".")[0],
116
116
                'navbar_backArrow': True,
117
117
                }
118
118
            return render(request, template, context)
119
119
+
120
def post_lang(request, language, title):
+
121
    translation.activate(language)
+
122
    request.session[translation.LANGUAGE_SESSION_KEY] = language
+
123
    return post(request, title)
+
124