blog

Fix bug and fix template tags

A reference to an unexisting class was fixed, and the tags have been adjusted so that links to static files now use the appropriate Django tag.

Author
Maarten 'Vngngdn' Vangeneugden
Date
Jan. 23, 2018, 1:45 a.m.
Hash
a63d7baae9a651b3327347bc7a48dae00c2e6b39
Parent
363b92495b64816b589200b794a8b42d529680c7
Modified files
models.py
templates/blog/index.html
templates/blog/post.html

models.py

1 addition and 1 deletion.

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

templates/blog/index.html

6 additions and 0 deletions.

View changes Hide changes
1
1
{% load i18n %}
2
2
+
3
+
4
{% block stylesheets %}
+
5
{{ block.super }}
+
6
<link href="{% static "blog/stylesheet.css" %}" rel="stylesheet" media="screen, projection" />
+
7
{% endblock %}
+
8
3
9
{% block title %}{% trans "Maarten's blog" %}{% endblock title %}
4
10
5
11
{% block description %}{% blocktrans %}The always coherently put together, yet
6
12
fuzzy blog of whatever sprouts in my mind.{% endblocktrans %}
7
13
{% endblock description %}
8
14
9
15
{% block main %}
10
16
{% with color="brown" accent_color="yellow" %}
11
17
<div class="section {{ color }} z-depth-3">
12
18
    <div class="container">
13
19
        <div class="white-text">
14
20
            <h3>{% trans "Blog" %}</h3>
15
21
            <p>
16
22
                {% blocktrans %}Welcome to my blog. Here, I write
17
23
                about things that interest me. Politics, coding,
18
24
                studying, life, or anything else I fancy rambling
19
25
                about. If you're in luck, I may've written it in a
20
26
                language that you understand better than English.
21
27
                {% endblocktrans %}
22
28
            </p>
23
29
        </div>
24
30
    </div>
25
31
</div>
26
32
27
33
<div class="container" style=" font-size:16px; font-family: Merriweather;">
28
34
    <h1 id="Weekboek" class="{{ color }}-text">Weekboek</h1>
29
35
    {% include "blog/weekly.html" %}
30
36
</div>
31
37
<div class="container">
32
38
    {% for title, date, blog_text, link in posts %}
33
39
        <h2 class="{{ color}}-text">{{ title }}</h2>
34
40
        <span class="grey-text">{{ date|date:"DATE_FORMAT" }}</span>
35
41
        <p class="hide-on-small-only">{{ blog_text|safe|truncatewords_html:100 }}</p>
36
42
        <a class="btn {{accent_color}} accent-4" href="{{link}}">
37
43
            📚 {% trans "Read on"%}
38
44
        </a>
39
45
        <hr />
40
46
    {% endfor %}
41
47
</div>
42
48
{% endwith %}
43
49
{% endblock main %}
44
50

templates/blog/post.html

6 additions and 0 deletions.

View changes Hide changes
1
1
{% load i18n %}
2
2
+
3
+
4
{% block stylesheets %}
+
5
{{ block.super }}
+
6
<link href="{% static "blog/stylesheet.css" %}" rel="stylesheet" media="screen, projection" />
+
7
{% endblock %}
+
8
3
9
{% block title %}📚 {{ post_title }}{% endblock title %}
4
10
5
11
{% block description %}{{ article|safe|truncatewords_html:100 }}{% endblock description %}
6
12
7
13
{% block main %}
8
14
{% with color="brown" accent_color="yellow" %}
9
15
<div class="section {{ color }} lighten-1 z-depth-3">
10
16
    <div class="container">
11
17
        <div class="white-text flow-text" style="font-family:serif;">
12
18
            {{ article|safe }}
13
19
        </div>
14
20
    </div>
15
21
</div>
16
22
17
23
<hr />
18
24
19
25
<h5>{% trans "This article in other languages" %}</h5>
20
26
{% for slug, language in localized_links %}
21
27
    {% get_language_info for language as lang %}
22
28
    <a href="{% url 'blog-post-language' language slug %}" class="btn">
23
29
        {% if lang.code == "en" %}
24
30
        🇬🇧 {{ lang.name_translated}} 🇺🇸
25
31
        {% elif lang.code == "es" %}
26
32
        🇪🇸 {{ lang.name_translated}} 🇲🇽
27
33
        {% elif lang.code == "nl" %}
28
34
        🇧🇪 {{ lang.name_translated}} 🇳🇱
29
35
        {% elif lang.code == "fr" %}
30
36
        🇧🇪 {{ lang.name_translated}} 🇫🇷
31
37
        {% elif lang.code == "de" %}
32
38
        🇧🇪 {{ lang.name_translated }} 🇩🇪
33
39
        {% endif %}
34
40
    </a>
35
41
{% endfor %}
36
42
37
43
    <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
44
39
45
<div class="container">
40
46
    {% for title, date, description, link in post_links %}
41
47
        <h2 class="{{ color}}-text">{{ title }}</h2>
42
48
        {# FIXME: Date is in all languages of the same format. Fix for each language #}
43
49
        <span class="grey-text">{{ date|date:"l j F Y" }}</span>
44
50
        {#<p class="hide-on-small-only">{{ description }}</p>#}
45
51
        <p class="hide-on-small-only">{% lorem %}</p>
46
52
        <a class="btn {{accent_color}} accent-3" href="{{link}}">
47
53
            {% trans "Read on"%}
48
54
        </a>
49
55
        <hr />
50
56
    {% endfor %}
51
57
</div>
52
58
{% endcomment %}
53
59
{% endwith %}
54
60
{% endblock main %}
55
61