blog

Give RSS feed correct MIME type

Author
Maarten Vangeneugden
Date
Feb. 22, 2021, 1:59 p.m.
Hash
b51541052ba69b896a0e8c5286e8088bddd02c5d
Parent
db1a4fb4f28386760048a994257a5109405d7e64
Modified files
templates/blog/feed.rss
views.py

templates/blog/feed.rss

2 additions and 2 deletions.

View changes Hide changes
1
-
<rss version="2.0">
+
1
<rss version="2.0">
2
2
    <channel>
3
3
    <title>RSS feed for maartenv.be</title>
4
-
    <link>https://maartenv.be</link>
+
4
    <link>https://maartenv.be</link>
5
5
    <description>The official RSS feed for the personal website of Maarten V.</description>
6
6
    {% for item in items %}
7
7
    <item>
8
8
        <title>{{ item.title }}</title>
9
9
        <link>{{ item.link }}</link>
10
10
        <description>{{ item.description }}</description>
11
11
        {% language 'en' %}
12
12
        <pubDate>{{ item.added|date:'r' }}</pubDate>
13
13
        {% endlanguage %}
14
14
    </item>
15
15
    {% endfor %}
16
16
    </channel>
17
17
</rss>
18
18

views.py

1 addition and 1 deletion.

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.urls import reverse
7
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.decode("utf-8").replace(".jpgPANDOCBUG", ".jpg")
46
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.djhtml"
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': "blue",
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
            'stylesheet_name': "blog",
89
89
            }
90
90
    if not request.session.get("feed-fab-introduction-seen", default=False):
91
91
        context['introduce_feed'] = True
92
92
        request.session['feed-fab-introduction-seen'] = True
93
93
    return render(request, template, context)
94
94
95
95
def post(request, post_slug, language=None):
96
96
    if request.method == "POST":  # Handling a reply if one is sent
97
97
        form = CommentForm(request.POST)
98
98
        for post in Post.objects.all():
99
99
            if post.slug(language) == post_slug:
100
100
                form.post = post
101
101
                break
102
102
        if form.is_valid():
103
103
            new_comment = form.save(commit=False)
104
104
            for post in Post.objects.all():
105
105
                if post.slug(language) == post_slug:
106
106
                    new_comment.post = post
107
107
                    new_comment.save()
108
108
    if language is not None:
109
109
        if translation.check_for_language(language):
110
110
            translation.activate(language)
111
111
            request.session[translation.LANGUAGE_SESSION_KEY] = language
112
112
            #return post(request, post_slug)
113
113
    else:
114
114
        language = translation.get_language()
115
115
116
116
    template = "blog/post.djhtml"
117
117
    posts = Post.objects.all()
118
118
    #comments = Comment.objects.filter(post
119
119
    for post in posts:
120
120
        if post.slug(language) == post_slug:
121
121
            comments = Comment.objects.filter(post=post)
122
122
            form = CommentForm()
123
123
            post_file = post.text_file(language)
124
124
            post_text = org_to_html(post_file.name)
125
125
            context = {
126
126
                'comments': comments,
127
127
                'form' : form,
128
128
                'human_post_title': post.title(language),
129
129
                'materialDesign_color': "brown",
130
130
                'materialDesign_accentColor': "blue",
131
131
                'article': post_text,
132
132
                'title': post.title(language),
133
133
                'navbar_title': post.title(language),
134
134
                'navbar_backArrow': False,
135
135
                'post_slug': post_slug,
136
136
                'footer_links': footer_links,
137
137
                'footer_description': footer_description,
138
138
                'stylesheet_name': "blog",
139
139
                }
140
140
141
141
            # Getting all available article links
142
142
            available = get_available_post_languages(post)
143
143
            if ENGLISH in available:
144
144
                context['english_link'] = reverse("blog-post-language", args=[ENGLISH, post.slug(ENGLISH)])
145
145
            if DUTCH in available:
146
146
                context['dutch_link'] = reverse("blog-post-language", args=[DUTCH, post.slug(DUTCH)])
147
147
148
148
            if FRENCH in available:
149
149
                context['french_link'] = reverse("blog-post-language", args=[FRENCH, post.slug(FRENCH)])
150
150
151
151
            if SPANISH in available:
152
152
                context['spanish_link'] = reverse("blog-post-language", args=[SPANISH, post.slug(SPANISH)])
153
153
154
154
            if GERMAN in available:
155
155
                context['german_link'] = reverse("blog-post-language", args=[GERMAN, post.slug(GERMAN)])
156
156
157
157
            return render(request, template, context)
158
158
159
159
def rss(request):
160
160
    template = "blog/feed.rss"
161
161
    context = {
162
162
        'items': FeedItem.objects.all(),
163
163
        }
164
164
    return render(request, template, context)
165
-
+
165
166
166
167
167
def monthly_archive(request):
168
168
    template = "blog/monthly_archive.djhtml"
169
169
    language = translation.get_language()
170
170
171
171
    file_2017 = org_to_html("blog/weekly/2017.org")
172
172
    file_2018 = org_to_html("blog/weekly/2018.org")
173
173
    file_2019 = org_to_html("blog/weekly/2019.org")
174
174
175
175
176
176
177
177
    context = {
178
178
        't2017': file_2017,
179
179
        't2018': file_2018,
180
180
        't2019': file_2019,
181
181
            'materialDesign_color': "brown",
182
182
            'materialDesign_accentColor': "blue",
183
183
            'navbar_title': _("Weekly-archief"),
184
184
            'navbar_backArrow': True,
185
185
            'footer_links': footer_links,
186
186
            'footer_description': footer_description,
187
187
            'stylesheet_name': "blog",
188
188
            }
189
189
    return render(request, template, context)
190
190