views.py

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