blog

General internationalization and finishing parts

I'm currently reworking the internationalization system so it properly accepts and works with multiple languages. In the changes are also included: An updated URL system and an improvement of the views. The templates are also more focused on handling the blog posts well.

Author
Maarten 'Vngngdn' Vangeneugden
Date
Jan. 22, 2018, 11:16 p.m.
Hash
da2cafd56c1da50b508409b32f742587296e1b11
Parent
ad92bb1cd2593778402803c2cf8fbcd743f2eab0
Modified files
models.py
templates/blog/index.html
templates/blog/post.html
urls.py
views.py

models.py

25 additions and 9 deletions.

View changes Hide changes
1
1
from django.db import models
+
2
from django.template.defaultfilters import slugify
+
3
from django.db import models
2
4
import datetime
3
5
import os
4
6
5
7
def post_title_directory(instance, filename):
6
8
    """ Files will be uploaded to MEDIA_ROOT/blog/<year of publishing>/<blog
7
9
    title>
8
10
    The blog title is determined by the text before the first period (".") in
9
11
    the filename. So if the file has the name "Trains are bæ.en.md", the file
10
12
    will be stored in "blog/<this year>/Trains are bæ". Name your files
11
13
    properly!
12
14
    It should also be noted that all files are stored in the same folder if they
13
15
    belong to the same blogpost, regardless of language. The titles that are
14
16
    displayed to the user however, should be the titles of the files themselves,
15
17
    which should be in the native language. So if a blog post is titled
16
18
    "Universities of Belgium", its Dutch counterpart should be titled
17
19
    "Universiteiten van België", so the correct title can be derived from the
18
20
    filename.
19
21
20
22
    Recommended way to name the uploaded file: "<name of blog post in language
21
23
    it's written>.md". This removes the maximum amount of redundancy (e.g. the
22
-
    language of the file can be derived from the title, no ".fr.md" or something
23
-
    like that necessary), and can directly be used for the end user (the title
+
24
    language of the file can be derived from the title, no ".fr.org" or something
+
25
    like that necessary), and can directly be used for the end user (the title
24
26
    is what should be displayed).
25
27
    """
26
28
    english_file_name = os.path.basename(instance.english_file.name) # TODO: Test if this returns the file name!
27
29
    english_title = english_file_name.rpartition(".")[0] 
28
-
    year = datetime.date.today().year
+
30
    year = datetime.date.today().year
29
31
30
32
    return "blog/{0}/{1}/{2}".format(year, english_title, filename)
31
33
32
34
class Post(models.Model):
33
35
    """ Represents a blog post. The title of the blog post is determnined by the name
34
36
    of the files.
35
37
    A blog post can be in 5 different languages: German, Spanish, English, French,
36
38
    and Dutch. For all these languages, a seperate field exists. Thus, a
37
39
    translated blog post has a seperate file for each translation, and is
38
40
    seperated from Django's internationalization/localization system.
39
41
    Only the English field is mandatory. The others may contain a value if a
40
42
    translated version exists, which will be displayed accordingly.
41
43
    """
42
44
    published = models.DateTimeField(auto_now_add=True)
43
45
    english_file = models.FileField(upload_to=post_title_directory, unique=True, blank=False)
44
46
    dutch_file = models.FileField(upload_to=post_title_directory, blank=True)
45
47
    french_file = models.FileField(upload_to=post_title_directory, blank=True)
46
48
    german_file = models.FileField(upload_to=post_title_directory, blank=True)
47
49
    spanish_file = models.FileField(upload_to=post_title_directory, blank=True)
48
50
    # Only the English file can be unique, because apparantly, there can't be
49
51
    # two blank fields in a unique column. Okay then.
50
52
51
53
    def __str__(self):
52
54
        return os.path.basename(self.english_file.name).rpartition(".")[0]
53
-
+
55
54
56
#class Comment(models.model):
55
-
    """ Represents a comment on a blog post.
+
57
        """ Returns a slug of the requested language, or None if no version exists in that language. """
+
58
        possibilities = {
+
59
            "en" : self.english_file,
+
60
            "de" : self.german_file,
+
61
            "nl" : self.dutch_file,
+
62
            "fr" : self.french_file,
+
63
            "es" : self.spanish_file,
+
64
            }
+
65
        if possibilities[language_code]:
+
66
            return slugify(os.path.basename(possibilities[language].name).rpartition(".")[0])
+
67
        else:
+
68
            return None
+
69
+
70
class Comment(models.model):
+
71
    """ Represents a comment on a blog post.
56
72
57
73
    Comments are not linked to an account or anything, I'm trusting the
58
74
    commenter that he is honest with his credentials. That being said:
59
75
    XXX: Remember to put up a notification that comments are not checked for
60
76
    identity, and, unless verified by a trustworthy source, cannot be seen as
61
77
    being an actual statement from the commenter.
62
78
    Comments are linked to a blogpost, and are not filtered by language. (So a
63
79
    comment made by someone reading the article in Dutch, that's written in
64
80
    Dutch, will show up (unedited) for somebody whom's reading the Spanish
65
81
    version.
66
82
    XXX: Remember to notify (tiny footnote or something) that comments showing
67
83
    up in a foreign language is by design, and not a bug.
68
84
    """
69
85
#    date = models.DateTimeField(auto_now_add=True)
70
-
    #name = models.TextField()
71
-
    #mail = models.EmailField()
72
-
    #post = models.ForeignKey(Post) # TODO: Finish this class and the shit...
73
-
+
86
    name = models.CharField(max_length=64)
+
87
    text = models.TextField()
+
88
    post = models.ForeignKey(Post) # TODO: Finish this class and the shit...
+
89

templates/blog/index.html

3 additions and 3 deletions.

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 in post_links %}
29
-
        <h2 class="{{ color}}-text">{{ title }}</h2>
+
29
        <h2 class="{{ color}}-text">{{ title }}</h2>
30
30
        <span class="grey-text">{{ date|date:"DATE_FORMAT" }}</span>
31
31
        <p class="hide-on-small-only">{{ description|safe|truncatewords_html:100 }}</p>
32
-
        <a class="btn {{accent_color}} accent-4" href="{% url "blog-post" title %}">
33
-
            📚 {% trans "Read on"%}
+
32
        <a class="btn {{accent_color}} accent-4" href="{{link}}">
+
33
            📚 {% trans "Read on"%}
34
34
        </a>
35
35
        <hr />
36
36
    {% endfor %}
37
37
</div>
38
38
{% endwith %}
39
39
{% endblock main %}
40
40

templates/blog/post.html

22 additions and 9 deletions.

View changes Hide changes
1
1
{% load i18n %}
2
2
3
3
{% block title %}📚 {{ post_title }}{% endblock title %}
4
4
5
5
{% block description %}{{ article|safe|truncatewords_html:100 }}{% endblock description %}
6
6
7
7
{% block main %}
8
8
{% with color="brown" accent_color="yellow" %}
9
9
<div class="section {{ color }} lighten-1 z-depth-3">
10
10
    <div class="container">
11
11
        <div class="white-text flow-text" style="font-family:serif;">
12
12
            {{ article|safe }}
13
13
        </div>
14
14
    </div>
15
15
</div>
16
16
{% comment %}
17
-
{% get_language_info for "en" as lang %}
18
-
    <a href="{% url 'blog_post' 'en' post_url %}" class="btn tooltipped"
19
-
    data-position="bottom" data-delay="50" data-tooltip="{% trans 'Share the English version' %}">{{lang.name_translated}}</a>
20
-
    <a href="{% url 'blog_post' 'nl' post %}" class="btn tooltipped" data-position="bottom" data-delay="50" data-tooltip="{% trans ">Hover me!</a>
21
-
    <a href="{% url 'blog_post' 'de' post %}" class="btn tooltipped" data-position="bottom" data-delay="50" data-tooltip="{% trans ">Hover me!</a>
22
-
    <a href="{% url 'blog_post' 'fr' post %}"class="btn tooltipped" data-position="bottom" data-delay="50" data-tooltip="{% trans ">Hover me!</a>
23
-
    <a href="{% url 'blog_post' 'es' post %}"class="btn tooltipped" data-position="bottom" data-delay="50" data-tooltip="{% trans ">Hover me!</a>
24
-
    <a href="{% url 'blog_post' post_url %}" class="btn tooltipped" data-position="bottom" data-delay="50" data-tooltip="{% trans 'Multilingual link. Links to the version in the viewer's preferred language.' %}">🏳️‍🌈 {% trans "All possible languages" %}</a>
25
-
+
17
<hr />
+
18
+
19
<h5>{% trans "This article in other languages" %}</h5>
+
20
{% for slug, language in localized_links %}
+
21
    {% get_language_info for language as lang %}
+
22
    <a href="{% url 'blog-post-language' language slug %}" class="btn">
+
23
        {% if lang.code == "en" %}
+
24
        🇬🇧 {{ lang.name_translated}} 🇺🇸
+
25
        {% elif lang.code == "es" %}
+
26
        🇪🇸 {{ lang.name_translated}} 🇲🇽
+
27
        {% elif lang.code == "nl" %}
+
28
        🇧🇪 {{ lang.name_translated}} 🇳🇱
+
29
        {% elif lang.code == "fr" %}
+
30
        🇧🇪 {{ lang.name_translated}} 🇫🇷
+
31
        {% elif lang.code == "de" %}
+
32
        🇧🇪 {{ lang.name_translated }} 🇩🇪
+
33
        {% endif %}
+
34
    </a>
+
35
{% endfor %}
+
36
+
37
    <a href="{% url 'blog-post' post-slug %}" class="btn tooltipped" data-position="bottom" data-delay="50" data-tooltip="{% trans 'Multilingual link. Links to the version in the viewer's preferred language.' %}">🏳️‍🌈 {% trans "All available languages" %}</a>
+
38
26
39
<div class="container">
27
40
    {% for title, date, description, link in post_links %}
28
41
        <h2 class="{{ color}}-text">{{ title }}</h2>
29
42
        {# FIXME: Date is in all languages of the same format. Fix for each language #}
30
43
        <span class="grey-text">{{ date|date:"l j F Y" }}</span>
31
44
        {#<p class="hide-on-small-only">{{ description }}</p>#}
32
45
        <p class="hide-on-small-only">{% lorem %}</p>
33
46
        <a class="btn {{accent_color}} accent-3" href="{{link}}">
34
47
            {% trans "Read on"%}
35
48
        </a>
36
49
        <hr />
37
50
    {% endfor %}
38
51
</div>
39
52
{% endcomment %}
40
53
{% endwith %}
41
54
{% endblock main %}
42
55

urls.py

2 additions and 2 deletions.

View changes Hide changes
1
1
2
2
from . import views # Imports the views from the same directory (which is views.py).
3
3
4
4
urlpatterns = [
5
5
    url(r'^$', views.index, name='blog-index'),
6
6
    url(r'^(?P<language>(.)+)/(?P<title>(.)+)$', views.post_lang, name='blog-post-language'),
7
-
    url(r'^(?P<title>(.)+)$', views.post, name='blog-post'),
8
-
        ]
+
7
    url(r'^(?P<post_slug>(.)+)$', views.post, name='blog-post'),
+
8
        ]
9
9

views.py

17 additions and 15 deletions.

View changes Hide changes
1
1
import subprocess
2
2
3
3
from django.utils.translation import ugettext as _
4
4
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.
5
5
from django.http import HttpResponseRedirect, HttpResponse
6
6
from django.core.urlresolvers import reverse # Why?
7
7
from django.template import loader # This allows to actually load the template.
8
8
from django.contrib.auth.decorators import login_required
9
9
from django.contrib.auth import authenticate, login
10
10
from .models import Post
11
11
from django.core.exceptions import ObjectDoesNotExist
12
12
from django.utils import translation
13
13
14
14
GERMAN = "de"
15
15
SPANISH = "es"
16
16
FRENCH = "fr"
17
17
DUTCH = "nl"
18
18
ENGLISH = "en"
19
19
20
20
# FIXME: Remove this template trash. THIS IS A VIEW, NOT A FUCKING TEMPLATE FFS
21
21
context = {
22
22
    'materialDesign_color': "green",
23
23
    'materialDesign_accentColor': "purple",
24
24
    'navbar_title': "Blog",
25
25
    'navbar_fixed': True,
26
26
    'navbar_backArrow': True,
27
27
    #'footer_title': "Maarten's blog",
28
28
    #'footer_description': "My personal scribbly notepad.",
29
29
    #'footer_links': footer_links,
30
30
    }
31
31
32
32
def markdown_to_html(file_path):
33
-
    """ Converts the given Markdown formatted file to HTML.
34
-
    This function directly returns the resulting HTML code. This function uses
+
33
    """ Converts the given org formatted file to HTML.
+
34
    This function directly returns the resulting HTML code. This function uses
35
35
    the amazing Haskell library Pandoc to convert the file (and takes care
36
36
    of header id's and all that stuff).
37
37
    """
38
38
    # FIXME: Remove hardcoded link to media. Replace with media tag!
39
39
    return subprocess.check_output(["pandoc", "--from=org", "--to=html", "/srv/django/website/media/"+file_path])
40
40
41
41
def get_available_post_languages(post):
42
42
    """ Returns the language codes for which a blog post exists. This function
43
43
    always returns English (because that field mustn't be empty).
44
44
    So say a blog post has an English, Dutch and French version (which means
45
45
    english_file, french_file and dutch_file aren't empty), the function will return {"en",
46
46
    "fr", "nl"}. """
47
47
    available_languages = {ENGLISH}
48
48
    if post.german_file is not None:
49
49
        available_languages.add(GERMAN)
50
50
    if post.spanish_file is not None:
51
51
        available_languages.add(SPANISH)
52
52
    if post.french_file is not None:
53
53
        available_languages.add(FRENCH)
54
54
    if post.dutch_file is not None:
55
55
        available_languages.add(DUTCH)
56
56
    return available_languages
57
57
58
58
def get_preferred_post_language(post, language):
59
59
    """ Returns the post language file that best suits the given language. This
60
60
    is handy if you know what language the user prefers, but aren't sure whether
61
61
    you can provide that language. This function will try to provide the file
62
62
    for that language, or return English if that's not possible. """
63
63
    if language == GERMAN and post.german_file is not None:
64
64
        return post.german_file
65
65
    if language == SPANISH and post.spanish_file is not None:
66
66
        return post.spanish_file
67
67
    if language == FRENCH and post.french_file is not None:
68
68
        return post.french_file
69
69
    if language == DUTCH and post.dutch_file is not None:
70
70
        return post.dutch_file
71
71
    return post.english_file  # Returned if all other choices wouldn't be satisfactory, or the requested language is English.
72
72
73
73
def index(request):
74
74
    template = "blog/index.html"
75
75
    posts = Post.objects.all()
76
76
    language = translation.get_language()
77
77
78
78
    post_links = []
79
79
    for post in posts:
80
80
        blog_file = get_preferred_post_language(post, language)
81
81
        # TODO: Find a cleaner way to determine the title. First and foremost:
82
82
        # If the language differs from English, the other language file needs to
83
83
        # be loaded. Plus: look for a built in function to remove the full path
84
84
        # and only return the file name.
85
85
        title = (blog_file.name.rpartition("/")[2]).rpartition(".")[0]
86
86
        date = post.published
87
87
        blog_text = markdown_to_html(blog_file.name)
88
-
        # TODO: The link can possibly be reversed in the DTL using the title, which is actually
+
88
        # TODO: The link can possibly be reversed in the DTL using the title, which is actually
89
89
        # a cleaner way to do it. Investigate.
90
90
        link = reverse("blog-post", args=[str(post)])
91
-
        post_links.append([title, date, description, link])
92
-
+
91
        post_links.append([title, date, blog_text, link])
+
92
93
93
    context = {
94
94
            'post_links': post_links,
95
-
            'materialDesign_color': "brown",
+
95
            'materialDesign_color': "brown",
96
96
            'materialDesign_accentColor': "yellow",
97
97
            'navbar_title': _("Notepad from a student"),
98
98
            'navbar_backArrow': True,
99
99
            }
100
100
    return render(request, template, context)
101
101
102
102
def post(request, title):
103
-
    template = "blog/post.html"
+
103
    if language is not None:
+
104
        if translation.check_for_language(language):
+
105
            translation.activate(language)
+
106
            request.session[translation.LANGUAGE_SESSION_KEY] = language
+
107
        return post(request, post_slug)
+
108
    else:
+
109
        language = translation.get_language()
+
110
+
111
    template = "blog/post.html"
104
112
    posts = Post.objects.all()
105
113
    for post in posts:
106
114
        if str(post)==title:
107
-
            language = translation.get_language()
108
-
            blog_file = get_preferred_post_language(post, language)
+
115
            blog_file = get_preferred_post_language(post, language)
109
116
            blog_text = markdown_to_html(blog_file.name)
110
-
            context = {
+
117
            context = {
111
118
                'materialDesign_color': "brown",
112
119
                'materialDesign_accentColor': "yellow",
113
120
                'article': blog_text,
114
121
                'title': blog_file.name,
115
122
                'navbar_title': (blog_file.name.rpartition("/")[2]).rpartition(".")[0],
116
123
                'navbar_backArrow': True,
117
124
                }
118
125
            return render(request, template, context)
119
126
120
-
def post_lang(request, language, title):
121
-
    translation.activate(language)
122
-
    request.session[translation.LANGUAGE_SESSION_KEY] = language
123
-
    return post(request, title)
124
-