blog

Change variables in views.py to match the post model

Author
Maarten 'Vngngdn' Vangeneugden
Date
July 11, 2017, 12:56 a.m.
Hash
5011887dcc386db02032e35d44b01027dd073a7a
Parent
7c717d97cecae78d3ce135554725a3ab4522df6f
Modified files
dependencies.txt
views.py

dependencies.txt

0 additions and 3 deletions.

View changes Hide changes
1
-
	- Pillow (https://pillow.readthedocs.org/en/latest/): For image parsing in Python
2
-
	- Materialize (http://materializecss.com/): The layout framework for the website's view
3
-

views.py

15 additions and 15 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
    file_en, file_fr and file_nl aren't empty), the function will return {"en",
30
-
    "fr", "nl"}. """
+
30
    "fr", "nl"}. """
31
31
    available_languages = {"en"}
32
32
    if post.file_de is not none:
33
-
        available_languages.add("de")
+
33
        available_languages.add("de")
34
34
    if post.file_es is not None:
35
-
        available_languages.add("es")
+
35
        available_languages.add("es")
36
36
    if post.file_fr is not None:
37
-
        available_languages.add("fr")
+
37
        available_languages.add("fr")
38
38
    if post.file_nl is not None:
39
-
        available_languages.add("nl")
+
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
-
        return post.file_de
49
-
    if language == "es" and post.file_es is not None:
50
-
        return post.file_es
51
-
    if language == "fr" and post.file_fr is not None:
52
-
        return post.file_fr
53
-
    if language == "nl" and post.file_nl is not None:
54
-
        return post.file_nl
55
-
    return post.file_en  # Returned if all other choices wouldn't be satisfactory, or the requested language is English.
56
-
    
+
48
        return post.german_file
+
49
    if language == "es" and post.spanish_file is not None:
+
50
        return post.spanish_file
+
51
    if language == "fr" and post.french_file is not None:
+
52
        return post.french_file
+
53
    if language == "nl" and post.dutch_file is not None:
+
54
        return post.dutch_file
+
55
    return post.english_file  # Returned if all other choices wouldn't be satisfactory, or the requested language is English.
+
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
-
    language = translation.get_language()
+
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