blog

Fix bug in views.py with footer_links

Author
Maarten 'Vngngdn' Vangeneugden
Date
July 10, 2017, 4:38 p.m.
Hash
dcd54c2df39a90f9ec5c861b4e2bc865e7dd44b6
Parent
671b4e2bab070095cc86e4f98a338230504c0bd0
Modified file
views.py

views.py

1 addition and 1 deletion.

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
    file_en, file_fr and file_nl aren't empty), the function will return {"en",
30
30
    "fr", "nl"}. """
31
31
    available_languages = {"en"}
32
32
    if post.file_de is not none:
33
33
        available_languages.add("de")
34
34
    if post.file_es is not None:
35
35
        available_languages.add("es")
36
36
    if post.file_fr is not None:
37
37
        available_languages.add("fr")
38
38
    if post.file_nl 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.file_de is not None:
48
48
        return post.file_de
49
49
    if language == "es" and post.file_es is not None:
50
50
        return post.file_es
51
51
    if language == "fr" and post.file_fr is not None:
52
52
        return post.file_fr
53
53
    if language == "nl" and post.file_nl is not None:
54
54
        return post.file_nl
55
55
    return post.file_en  # 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.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
69
        # a cleaner way to do it. Investigate.
70
70
        link = reverse("blog-post", args=[str(post)])
71
71
        post_links.append([title, date, description, link])
72
72
73
73
    context = {
74
74
            'post_links': post_links,
75
75
            }
76
76
    return render(request, template, context)
77
77
78
78
def post(request, title):
79
79
    template = "blog/post.html"
80
80
    posts = Post.objects.get(file_en=title)
81
81
    language = translation.get_language()
82
82
    blog_file = get_preferred_post_language(post, language)
83
83
    blog_text = markdown(blog_file)
84
84
85
85
    context = {
86
86
        'article': blog_text,
87
87
        'title': blog_file.file_name,
88
88
        }
89
89
    return render(request, template, context)
90
90