blog

Few more changes to make it work =P

Author
Maarten Vangeneugden
Date
April 11, 2018, 9:49 p.m.
Hash
32199815ef6c8946572a2d2af8bf876bb963defe
Parent
822ac1493a49d83d8ea46ae148399b2f4df133b3
Modified files
templates/blog/feed.rss
views.py

templates/blog/feed.rss

3 additions and 1 deletion.

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
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
        <pubDate>{{ item.added|date:'r' }}</pubDate>
+
12
        <pubDate>{{ item.added|date:'r' }}</pubDate>
12
13
    </item>
+
14
    </item>
13
15
    {% endfor %}
14
16
    </channel>
15
17
</rss>
16
18

views.py

10 additions and 4 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
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
    return subprocess.check_output(["pandoc", "--from=org", "--to=html", "/srv/django/website/media/"+file_path])
33
33
34
34
def get_available_post_languages(post):
35
35
    """ Returns the language codes for which a blog post exists. This function
36
36
    always returns English (because that field mustn't be empty).
37
37
    So say a blog post has an English, Dutch and French version (which means
38
38
    english_file, french_file and dutch_file aren't empty), the function will return {"en",
39
39
    "fr", "nl"}. """
40
40
    available_languages = {ENGLISH}
41
41
    if post.german_file != "":
42
42
        available_languages.add(GERMAN)
43
43
    if post.spanish_file != "":
44
44
        available_languages.add(SPANISH)
45
45
    if post.french_file != "":
46
46
        available_languages.add(FRENCH)
47
47
    if post.dutch_file != "":
48
48
        available_languages.add(DUTCH)
49
49
    return available_languages
50
50
51
51
def index(request):
52
52
    template = "blog/index.html"
53
53
    posts = Post.objects.all()
54
54
    language = translation.get_language()
55
55
56
56
    post_links = []
57
57
    for post in posts:
58
58
        blog_file = post.text_file()
59
-
        blog_text = org_to_html(blog_file.name)
+
59
        blog_text = org_to_html(blog_file.name)
60
60
        # TODO: The link can possibly be reversed in the DTL using the title, which is actually
61
61
        # a cleaner way to do it. Investigate.
62
62
        link = reverse("blog-post-language", args=[language, post.slug()])
63
-
        post_links.append([post.title(), post.published, blog_text, link])
64
-
+
63
        post_links.append([post.title(language), post.published, blog_text, link])
+
64
65
65
    context = {
66
66
            'posts': post_links,
67
67
            'materialDesign_color': "brown",
68
68
            'materialDesign_accentColor': "yellow",
69
69
            'navbar_title': _("Notepad from a student"),
70
70
            'navbar_backArrow': True,
71
71
            'footer_links': footer_links,
72
72
            'footer_description': footer_description,
73
73
            }
74
74
    if not request.session.get("feed-fab-introduction-seen", default=False):
75
75
        context['introduce_feed'] = True
76
76
        request.session['feed-fab-introduction-seen'] = True
77
77
    return render(request, template, context)
78
78
79
79
def post(request, post_slug, language=None):
80
80
    if request.method == "POST":  # Handling a reply if one is sent
81
81
        form = CommentForm(request.POST)
82
82
        if form.is_valid():
+
83
            if post.slug(language) == post_slug:
+
84
                form.post = post
+
85
            else:
+
86
                print(post.slug(language))
+
87
        if form.is_valid():
83
88
            form.save()
84
89
+
90
85
91
    if language is not None:
86
92
        if translation.check_for_language(language):
87
93
            translation.activate(language)
88
94
            request.session[translation.LANGUAGE_SESSION_KEY] = language
89
95
            #return post(request, post_slug)
90
96
    else:
91
97
        language = translation.get_language()
92
98
93
99
    template = "blog/post.html"
94
100
    posts = Post.objects.all()
95
101
    #comments = Comment.objects.filter(post
96
102
    for post in posts:
97
103
        if post.slug(language) == post_slug:
98
104
            comments = Comment.objects.filter(post=post)
99
105
            form = CommentForm(initial={'post': post})
100
-
            post_file = post.text_file(language)
+
106
            post_file = post.text_file(language)
101
107
            post_text = org_to_html(post_file.name)
102
108
            context = {
103
109
                'comments': comments,
104
110
                'form' : form,
105
111
                'human_post_title': post.title(language),
106
112
                'materialDesign_color': "brown",
107
113
                'materialDesign_accentColor': "yellow",
108
114
                'article': post_text,
109
115
                'title': post.title(language),
110
116
                'navbar_title': post.title(language),
111
117
                'navbar_backArrow': False,
112
118
                'post_slug': post_slug,
113
119
                'footer_links': footer_links,
114
120
                'footer_description': footer_description,
115
121
                }
116
122
117
123
            # Getting all available article links
118
124
            available = get_available_post_languages(post)
119
125
            if ENGLISH in available:
120
126
                context['english_link'] = reverse("blog-post-language", args=[ENGLISH, post.slug(ENGLISH)])
121
127
            if DUTCH in available:
122
128
                context['dutch_link'] = reverse("blog-post-language", args=[DUTCH, post.slug(DUTCH)])
123
129
124
130
            if FRENCH in available:
125
131
                context['french_link'] = reverse("blog-post-language", args=[FRENCH, post.slug(FRENCH)])
126
132
127
133
            if SPANISH in available:
128
134
                context['spanish_link'] = reverse("blog-post-language", args=[SPANISH, post.slug(SPANISH)])
129
135
130
136
            if GERMAN in available:
131
137
                context['german_link'] = reverse("blog-post-language", args=[GERMAN, post.slug(GERMAN)])
132
138
133
139
            return render(request, template, context)
134
140
135
141
def rss(request):
136
142
    template = "blog/feed.rss"
137
143
    context = {
138
144
        'items': FeedItem.objects.all(),
139
145
        }
140
146
    return render(request, template, context)
141
147