blog

Add first template and fix bug of names in views.py

Author
Maarten 'Vngngdn' Vangeneugden
Date
July 11, 2017, 1:51 a.m.
Hash
54432e23a61348fb4b5b3785069ce2c9d807758c
Parent
5011887dcc386db02032e35d44b01027dd073a7a
Modified files
templates/blog/index.html
views.py

templates/blog/index.html

41 additions and 0 deletions.

View changes Hide changes
+
1
{% load i18n %}
+
2
+
3
{% block title %}{% trans "Maarten's blog" %}{% endblock title %}
+
4
+
5
{% block description %}{% blocktrans %}The always coherently put together, yet
+
6
fuzzy blog of whatever sprouts in my mind.{% endblocktrans %}
+
7
{% endblock description %}
+
8
+
9
{% block main %}
+
10
{% with color="brown", accent_color="yellow" %}
+
11
<div class="section {{ color }} z-depth-3">
+
12
    <div class="container">
+
13
        <div class="white-text">
+
14
            <h3>{% trans "Blog" %}</h3>
+
15
            <p>
+
16
                {% blocktrans %}Welcome to my blog. Here, I write
+
17
                about things that interest me. Politics, coding,
+
18
                studying, life, or anything else I fancy rambling
+
19
                about. If you're in luck, I may've written it in a
+
20
                language that you understand better than English.
+
21
                {% endblocktrans %}
+
22
            </p>
+
23
        </div>
+
24
    </div>
+
25
</div>
+
26
+
27
<div class="container">
+
28
    {% for title, date, description, link in post_links %}
+
29
        <h2 class="{{ color}}-text">{{ title }}</h2>
+
30
        {# FIXME: Date is in all languages of the same format. Fix for each language #}
+
31
        <span class="grey-text">{{ date|date "l j F Y" }}</span>
+
32
        <p class="hide-on-small-only">{{ description }}</p>
+
33
        <a class="btn {{accent_color}} accent-3" href="{{link}}">
+
34
            {% trans "Read on"%}
+
35
        </a>
+
36
        <hr />
+
37
    {% endfor %}
+
38
</div>
+
39
{% endwith %}
+
40
{% endblock main %}
+
41

views.py

2 additions and 2 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.file_name
67
-
        date = post.published
+
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(english_file=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