blog

Some unstashed changes

Author
Maarten Vangeneugden
Date
Feb. 11, 2019, 10:03 p.m.
Hash
aacefb32db6817ce8965d86886a70c9be75150cd
Parent
cccb93bb4bb11732c326f6216f59a45e10d21f1c
Modified files
forms.py
templates/blog/post.html
views.py

forms.py

0 additions and 4 deletions.

View changes Hide changes
1
1
from django.forms import ModelForm
2
2
from . import models
3
3
4
4
class CommentForm(ModelForm):
5
5
    class Meta:
6
6
        model = models.Comment
7
7
        fields = ['name',
8
8
                  'text',
9
9
                  'post',
10
-
                  ]
11
10
        widgets = {
12
-
            'post': None,
13
-
        }
14
-

templates/blog/post.html

1 addition and 1 deletion.

View changes Hide changes
1
1
{% load humanize %}
2
2
{% load i18n %}
3
3
{% load static %}
4
4
5
5
{% block stylesheets %}
6
6
{{ block.super }}
7
7
<link href="{% static "blog/stylesheet.css" %}" rel="stylesheet" media="screen, projection" />
8
8
{% endblock %}
9
9
10
10
{% block description %}
11
11
{{ article|safe|truncatewords_html:20 }}
12
-
{% endblock description %}
+
12
{% endblock description %}
13
13
{% block title %}📚 {{ navbar_title }}{% endblock title %}
14
14
15
15
16
16
{% block main %}
17
17
{% with color="brown" accent_color="yellow" %}
18
18
<div class="section {{ color }} lighten-1 z-depth-3">
19
19
    <div class="container">
20
20
        <article style="font-family:serif;">
21
21
            {{ article|safe }}
22
22
        </article>
23
23
24
24
25
25
<h5 class="white-text">{% trans "This article in other languages" %}</h5>
26
26
27
27
{% get_language_info for 'nl' as LANG %}
28
28
<a {% if dutch_link %} href="{{dutch_link}}" {% endif %}
29
29
   class="btn {{accent_color}} accent-4 black-text waves-effect
30
30
   {% if not dutch_link %}disabled{% endif %}">
31
31
    🇧🇪 {{ LANG.name_translated}} 🇳🇱
32
32
</a>
33
33
{% get_current_language as lang %}
34
34
{% get_language_info for 'fr' as LANG %}
35
35
<a {% if french_link %} href="{{french_link}}" {% endif %}
36
36
   class="btn {{accent_color}} accent-4 black-text waves-effect
37
37
   {% if not french_link %}disabled{% endif %}">
38
38
    🇧🇪 {{ LANG.name_translated}} 🇫🇷
39
39
</a>
40
40
{% get_language_info for 'en' as LANG %}
41
41
<a {% if english_link %} href="{{english_link}}" {% endif %}
42
42
   class="btn {{accent_color}} accent-4 black-text waves-effect
43
43
   {% if not english_link %}disabled{% endif %}">
44
44
    🇬🇧 {{ LANG.name_translated}} 🇺🇸
45
45
</a>
46
46
{% get_language_info for 'de' as LANG %}
47
47
<a {% if german_link %} href="{{german_link}}" {% endif %}
48
48
   class="btn {{accent_color}} accent-4 black-text waves-effect
49
49
   {% if not german_link %}disabled{% endif %}">
50
50
    🇧🇪 {{ LANG.name_translated}} 🇩🇪
51
51
</a>
52
52
{% get_language_info for 'es' as LANG %}
53
53
<a {% if spanish_link %} href="{{spanish_link}}" {% endif %}
54
54
   class="btn {{accent_color}} accent-4 black-text waves-effect
55
55
   {% if not spanish_link %}disabled{% endif %}">
56
56
    🇪🇸 {{ LANG.name_translated}} 🇲🇽
57
57
</a>
58
58
59
59
<h5 class="white-text">{% trans "Comments" %}</h5>
60
60
{% for comment in comments %} {# Whoops =P #}
61
61
    <span class="white-text">{{ comment.name|title }} | </span>
62
62
    <time class="grey-text" datetime="{{ comment.date|date:'c' }}">{{ comment.date|naturaltime }}</time>
63
63
    <br />
64
64
    <p class="white-text">{{ comment.text|urlize }}</p>
65
65
    <hr />
66
66
{% endfor %}
67
67
    {# Form for new comment #}
68
68
    <form action="" method="POST">
69
69
        {% csrf_token %}
70
70
        {{ form.name.label_tag }}
71
71
        <input class="white-text browser-default" type="text" id="id_name" name="name" maxlength="64" required />
72
72
        {{ form.text.label_tag }}
73
73
        <textarea id="id_text" class="white-text" name="text" maxlength="1000" required></textarea>
74
74
        <input type="submit" value="{% trans "Submit" %}" />
75
75
    </form>
76
76
77
77
78
78
{% comment %}
79
79
<a href="{% url 'blog-post' post_slug %}" class="btn {{accent_color}} accent-4 black-text 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>
80
80
    {# TODO: Change to rainbow flag when possible #}
81
81
{% endcomment %}
82
82
    </div>
83
83
84
84
</div>
85
85
<div class="container">
86
86
    {% for title, date, description, link in post_links %}
87
87
        <h2 class="{{ color}}-text">{{ title }}</h2>
88
88
        {# FIXME: Date is in all languages of the same format. Fix for each language #}
89
89
        <span class="grey-text">{{ date|date:"l j F Y" }}</span>
90
90
        {#<p class="hide-on-small-only">{{ description }}</p>#}
91
91
        <p class="hide-on-small-only">{% lorem %}</p>
92
92
        <a class="btn {{accent_color}} accent-3" href="{{link}}">
93
93
            {% trans "Read on"%}
94
94
        </a>
95
95
        <hr />
96
96
    {% endfor %}
97
97
</div>
98
98
{% endwith %}
99
99
{% endblock main %}
100
100

views.py

4 additions and 5 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
7
-
from django.template import loader # This allows to actually load the template.
+
7
from django.template import loader # This allows to actually load the template.
8
8
from .models import *
9
9
from .forms import CommentForm
10
10
from django.core.exceptions import ObjectDoesNotExist
11
11
from django.utils import translation
12
12
13
13
GERMAN = "de"
14
14
SPANISH = "es"
15
15
FRENCH = "fr"
16
16
DUTCH = "nl"
17
17
ENGLISH = "en"
18
18
19
19
footer_links = [
20
20
        [_("Back to main page"), "/blog"],
21
21
        [_("Contact"), "mailto:maarten.vangeneugden@student.uhasselt.be"],
22
22
        ]
23
23
footer_description = _("Maarten's personal blog, with sprinkles and a dollop of healthy bugs.")
24
24
25
25
def org_to_html(file_path):
26
26
    """ Converts the given org formatted file to HTML.
27
27
    This function directly returns the resulting HTML code. This function uses
28
28
    the amazing Haskell library Pandoc to convert the file (and takes care
29
29
    of header id's and all that stuff).
30
30
    """
31
31
    # FIXME: Remove hardcoded link to media. Replace with media tag!
32
32
    # XXX: The reason I'm first converting all occurences of .jpg][ and .png][
33
33
    # to .jpgPANDOCBUG][ and .pngPANDOCBUG][, is because of a Pandoc bug that
34
34
    # removes the text links for images. It is afterwards converted back, no
35
35
    # worries.
36
36
    file = open("/srv/django/website/media/"+file_path, "r", encoding="utf-8")
37
37
    text = file.read()
38
38
    file.close()
39
39
    text = text.replace(".jpg][", ".jpgPANDOCBUG][")
40
40
    text = text.replace(".png][", ".pngPANDOCBUG][")
41
41
    file = open("/tmp/blog-file.org", "w", encoding="utf-8")
42
42
    file.write(text)
43
43
    file.close()
44
44
    html_text = subprocess.check_output(["pandoc", "--from=org", "--to=html","/tmp/blog-file.org"])
45
45
    html_text = html_text.replace(".jpgPANDOCBUG", ".jpg")
46
-
    html_text = html_text.replace(".pngPANDOCBUG", ".png")
+
46
    html_text = html_text.replace(".pngPANDOCBUG", ".png")
47
47
    return html_text
48
48
49
49
def get_available_post_languages(post):
50
50
    """ Returns the language codes for which a blog post exists. This function
51
51
    always returns English (because that field mustn't be empty).
52
52
    So say a blog post has an English, Dutch and French version (which means
53
53
    english_file, french_file and dutch_file aren't empty), the function will return {"en",
54
54
    "fr", "nl"}. """
55
55
    available_languages = {ENGLISH}
56
56
    if post.german_file != "":
57
57
        available_languages.add(GERMAN)
58
58
    if post.spanish_file != "":
59
59
        available_languages.add(SPANISH)
60
60
    if post.french_file != "":
61
61
        available_languages.add(FRENCH)
62
62
    if post.dutch_file != "":
63
63
        available_languages.add(DUTCH)
64
64
    return available_languages
65
65
66
66
def index(request):
67
67
    template = "blog/index.html"
68
68
    posts = Post.objects.all()
69
69
    language = translation.get_language()
70
70
71
71
    post_links = []
72
72
    for post in posts:
73
73
        blog_file = post.text_file(language)
74
74
        blog_text = org_to_html(blog_file.name)
75
75
        # TODO: The link can possibly be reversed in the DTL using the title, which is actually
76
76
        # a cleaner way to do it. Investigate.
77
77
        link = reverse("blog-post-language", args=[language, post.slug(language)])
78
78
        post_links.append([post.title(language), post.published, blog_text, link])
79
79
80
80
    context = {
81
81
            'posts': post_links,
82
82
            'materialDesign_color': "brown",
83
83
            'materialDesign_accentColor': "yellow",
84
84
            'navbar_title': _("Notepad from a student"),
85
85
            'navbar_backArrow': True,
86
86
            'footer_links': footer_links,
87
87
            'footer_description': footer_description,
88
88
            }
89
89
    if not request.session.get("feed-fab-introduction-seen", default=False):
90
90
        context['introduce_feed'] = True
91
91
        request.session['feed-fab-introduction-seen'] = True
92
92
    return render(request, template, context)
93
93
94
94
def post(request, post_slug, language=None):
95
95
    if request.method == "POST":  # Handling a reply if one is sent
96
96
        form = CommentForm(request.POST)
97
97
        for post in Post.objects.all():
98
98
            if post.slug(language) == post_slug:
99
99
                form.post = post
100
100
            else:
101
-
                print(post.slug(language))
102
-
        if form.is_valid():
+
101
        if form.is_valid():
103
102
            new_comment = form.save(commit=False)
104
103
            for post in posts:
105
-
                if post.slug(language) == post_slug:
+
104
                if post.slug(language) == post_slug:
106
105
                    new_comment.post = post
107
106
                    new_comment.save()
108
107
    if language is not None:
109
108
        if translation.check_for_language(language):
110
109
            translation.activate(language)
111
110
            request.session[translation.LANGUAGE_SESSION_KEY] = language
112
111
            #return post(request, post_slug)
113
112
    else:
114
113
        language = translation.get_language()
115
114
116
115
    template = "blog/post.html"
117
116
    posts = Post.objects.all()
118
117
    #comments = Comment.objects.filter(post
119
118
    for post in posts:
120
119
        if post.slug(language) == post_slug:
121
120
            comments = Comment.objects.filter(post=post)
122
121
            form = CommentForm()
123
122
            post_file = post.text_file(language)
124
123
            post_text = org_to_html(post_file.name)
125
124
            context = {
126
125
                'comments': comments,
127
126
                'form' : form,
128
127
                'human_post_title': post.title(language),
129
128
                'materialDesign_color': "brown",
130
129
                'materialDesign_accentColor': "yellow",
131
130
                'article': post_text,
132
131
                'title': post.title(language),
133
132
                'navbar_title': post.title(language),
134
133
                'navbar_backArrow': False,
135
134
                'post_slug': post_slug,
136
135
                'footer_links': footer_links,
137
136
                'footer_description': footer_description,
138
137
                }
139
138
140
139
            # Getting all available article links
141
140
            available = get_available_post_languages(post)
142
141
            if ENGLISH in available:
143
142
                context['english_link'] = reverse("blog-post-language", args=[ENGLISH, post.slug(ENGLISH)])
144
143
            if DUTCH in available:
145
144
                context['dutch_link'] = reverse("blog-post-language", args=[DUTCH, post.slug(DUTCH)])
146
145
147
146
            if FRENCH in available:
148
147
                context['french_link'] = reverse("blog-post-language", args=[FRENCH, post.slug(FRENCH)])
149
148
150
149
            if SPANISH in available:
151
150
                context['spanish_link'] = reverse("blog-post-language", args=[SPANISH, post.slug(SPANISH)])
152
151
153
152
            if GERMAN in available:
154
153
                context['german_link'] = reverse("blog-post-language", args=[GERMAN, post.slug(GERMAN)])
155
154
156
155
            return render(request, template, context)
157
156
158
157
def rss(request):
159
158
    template = "blog/feed.rss"
160
159
    context = {
161
160
        'items': FeedItem.objects.all(),
162
161
        }
163
162
    return render(request, template, context)
164
163