blog

Add function to derive the right title name for display and use lorem ispum for testing

Author
Maarten 'Vngngdn' Vangeneugden
Date
July 11, 2017, 2:05 a.m.
Hash
754c93cf11faf4bde462aebfb371b00319fa3258
Parent
b2cfc78e1e2d66fa54242752e4b378fd90911bee
Modified files
templates/blog/index.html
views.py

templates/blog/index.html

2 additions and 1 deletion.

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

views.py

5 additions 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
    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
-
        date = post.published
+
67
        # If the language differs from English, the other language file needs to
+
68
        # be loaded. Plus: look for a built in function to remove the full path
+
69
        # and only return the file name.
+
70
        title = (blog_file.name.rpartition("/")[2]).rpartition(".")[0]
+
71
        date = post.published
68
72
        description = "Lorem ipsum"
69
73
        # TODO: The link can possibly be reversed in the DTL using the title, which is actually
70
74
        # a cleaner way to do it. Investigate.
71
75
        link = reverse("blog-post", args=[str(post)])
72
76
        post_links.append([title, date, description, link])
73
77
74
78
    context = {
75
79
            'post_links': post_links,
76
80
            }
77
81
    return render(request, template, context)
78
82
79
83
def post(request, title):
80
84
    template = "blog/post.html"
81
85
    posts = Post.objects.get(english_file=title)
82
86
    language = translation.get_language()
83
87
    blog_file = get_preferred_post_language(post, language)
84
88
    blog_text = markdown(blog_file)
85
89
86
90
    context = {
87
91
        'article': blog_text,
88
92
        'title': blog_file.name,
89
93
        }
90
94
    return render(request, template, context)
91
95