blog

Add temporary Latin description to views.py

Author
Maarten 'Vngngdn' Vangeneugden
Date
July 10, 2017, 11:53 p.m.
Hash
7763f91b71fcbc0115d5b18bc8291c30fc0b14df
Parent
54432e23a61348fb4b5b3785069ce2c9d807758c
Modified file
views.py

views.py

1 addition and 0 deletions.

View changes Hide changes
1
1
2
2
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.
3
3
from django.http import HttpResponseRedirect, HttpResponse
4
4
from django.core.urlresolvers import reverse # Why?
5
5
from django.template import loader # This allows to actually load the template.
6
6
from django.contrib.auth.decorators import login_required
7
7
from django.contrib.auth import authenticate, login
8
8
from .models import Post
9
9
from django.core.exceptions import ObjectDoesNotExist
10
10
from markdown import markdown
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 get_available_post_languages(post):
26
26
    """ Returns the language codes for which a blog post exists. This function
27
27
    always returns English (because that field mustn't be empty).
28
28
    So say a blog post has an English, Dutch and French version (which means
29
29
    english_file, french_file and dutch_file aren't empty), the function will return {"en",
30
30
    "fr", "nl"}. """
31
31
    available_languages = {"en"}
32
32
    if post.german_file is not none:
33
33
        available_languages.add("de")
34
34
    if post.spanish_file is not None:
35
35
        available_languages.add("es")
36
36
    if post.french_file is not None:
37
37
        available_languages.add("fr")
38
38
    if post.dutch_file is not None:
39
39
        available_languages.add("nl")
40
40
    return available_languages
41
41
42
42
def get_preferred_post_language(post, language):
43
43
    """ Returns the post language file that best suits the given language. This
44
44
    is handy if you know what language the user prefers, but aren't sure whether
45
45
    you can provide that language. This function will try to provide the file
46
46
    for that language, or return English if that's not possible. """
47
47
    if language == "de" and post.german_file is not None:
48
48
        return post.german_file
49
49
    if language == "es" and post.spanish_file is not None:
50
50
        return post.spanish_file
51
51
    if language == "fr" and post.french_file is not None:
52
52
        return post.french_file
53
53
    if language == "nl" and post.dutch_file is not None:
54
54
        return post.dutch_file
55
55
    return post.english_file  # Returned if all other choices wouldn't be satisfactory, or the requested language is English.
56
56
    
57
57
58
58
def index(request):
59
59
    template = "blog/index.html"
60
60
    posts = Post.objects.all()
61
61
    language = translation.get_language()
62
62
63
63
    post_links = []
64
64
    for post in posts:
65
65
        blog_file = get_preferred_post_language(post, language)
66
66
        title = blog_file.name
67
67
        date = post.published
68
68
        # TODO: The link can possibly be reversed in the DTL using the title, which is actually
+
69
        # TODO: The link can possibly be reversed in the DTL using the title, which is actually
69
70
        # a cleaner way to do it. Investigate.
70
71
        link = reverse("blog-post", args=[str(post)])
71
72
        post_links.append([title, date, description, link])
72
73
73
74
    context = {
74
75
            'post_links': post_links,
75
76
            }
76
77
    return render(request, template, context)
77
78
78
79
def post(request, title):
79
80
    template = "blog/post.html"
80
81
    posts = Post.objects.get(english_file=title)
81
82
    language = translation.get_language()
82
83
    blog_file = get_preferred_post_language(post, language)
83
84
    blog_text = markdown(blog_file)
84
85
85
86
    context = {
86
87
        'article': blog_text,
87
88
        'title': blog_file.name,
88
89
        }
89
90
    return render(request, template, context)
90
91