blog

Add introduction for RSS feed to blog

Author
Maarten 'Vngngdn' Vangeneugden
Date
April 9, 2018, 9:07 p.m.
Hash
85720e66aa3de8133be48767b11418963ccb1dcc
Parent
919accec757efdc4ed16df3fb73c7ce442b8be3b
Modified files
templates/blog/index.html
views.py

templates/blog/index.html

18 additions and 1 deletion.

View changes Hide changes
1
1
{% load i18n %}
2
2
{% load static %}
3
3
4
4
{% block stylesheets %}
5
5
{{ block.super }}
6
6
{#<link href="{% static "blog/stylesheet.css" %}" rel="stylesheet" media="screen, projection" />#}
7
7
{% endblock %}
8
8
9
9
{% block title %}{% trans "Maarten's blog" %}{% endblock title %}
10
10
11
11
{% block description %}{% blocktrans %}The always coherently put together, yet
12
12
fuzzy blog of whatever sprouts in my mind.{% endblocktrans %}
13
13
{% endblock description %}
14
14
15
15
{% block main %}
16
16
{% with color="brown" accent_color="yellow" %}
17
17
<div class="section {{ color }} z-depth-3">
18
18
    <div class="container">
19
19
        <div class="white-text">
20
20
            <h3>{% trans "Blog" %}</h3>
21
21
            <p>
22
22
                {% blocktrans %}Welcome to my blog. Here, I write
23
23
                about things that interest me. Politics, coding,
24
24
                studying, life, or anything else I fancy rambling
25
25
                about. If you're in luck, I may've written it in a
26
26
                language that you understand better than English.
27
27
                {% endblocktrans %}
28
28
            </p>
29
29
        </div>
30
30
    </div>
31
31
</div>
32
32
33
33
<div class="fixed-action-btn">
34
34
    <a href="{% url 'blog-feed' %}" class="btn-floating btn-large orange accent-4">
35
-
    <i class="large material-icons">rss_feed</i>
+
35
    <i class="large material-icons">rss_feed</i>
36
36
    </a>
37
37
</div>
38
38
39
39
40
40
<div class="container row">
41
41
    <div class="col s12 m6">
42
42
        <h1 id="weekly" class="{{ color }}-text">Weekly</h1>
43
43
        {% include "blog/weekly.html" %}
44
44
    </div>
45
45
    <div class="container col s12 m6">
46
46
        {% for title, date, blog_text, link in posts %}
47
47
            <h1 class="{{ color}}-text">{{ title }}</h2>
48
48
            <span class="grey-text">{{ date|date:"DATE_FORMAT" }}</span>
49
49
            <p class="hide-on-small-only">{{ blog_text|safe|truncatewords_html:100 }}</p>
50
50
            <a class="btn black-text waves-effect {{accent_color}} accent-4" href="{{link}}">
51
51
                📚 {% trans "Read on"%}
52
52
            </a>
53
53
            <hr />
54
54
        {% endfor %}
55
55
    </div>
56
56
</div>
57
57
{% endwith %}
+
58
{% if introduce-feed %}
+
59
    <div class="tap-target" data-target="feed-fab">
+
60
        <div class="tap-target-content">
+
61
            <h5>{% trans "Never miss an update again." %}</h5>
+
62
            <p>{% blocktrans %}
+
63
                Getting info on updates to blog posts, major website changes, and other important
+
64
                news items is now easy and convenient with RSS.
+
65
                {% endblocktrans %}</p>
+
66
        </div>
+
67
    </div>
+
68
    <script>
+
69
    var elem = document.querySelector('.tap-target');
+
70
    var instance = M.TapTarget.init(elem, options);
+
71
    </script>
+
72
{% endif %}
+
73
+
74
{% endwith %}
58
75
{% endblock main %}
59
76

views.py

6 additions and 0 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
        [_("Blog 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
# FIXME: Remove this template trash. THIS IS A VIEW, NOT A FUCKING TEMPLATE FFS
26
26
context = {
27
27
    'materialDesign_color': "green",
28
28
    'materialDesign_accentColor': "purple",
29
29
    'navbar_title': "Blog",
30
30
    'navbar_fixed': True,
31
31
    'navbar_backArrow': True,
32
32
    #'footer_title': "Maarten's blog",
33
33
    #'footer_description': "My personal scribbly notepad.",
34
34
    #'footer_links': footer_links,
35
35
    }
36
36
37
37
def org_to_html(file_path):
38
38
    """ Converts the given org formatted file to HTML.
39
39
    This function directly returns the resulting HTML code. This function uses
40
40
    the amazing Haskell library Pandoc to convert the file (and takes care
41
41
    of header id's and all that stuff).
42
42
    """
43
43
    # FIXME: Remove hardcoded link to media. Replace with media tag!
44
44
    return subprocess.check_output(["pandoc", "--from=org", "--to=html", "/srv/django/website/media/"+file_path])
45
45
46
46
def get_available_post_languages(post):
47
47
    """ Returns the language codes for which a blog post exists. This function
48
48
    always returns English (because that field mustn't be empty).
49
49
    So say a blog post has an English, Dutch and French version (which means
50
50
    english_file, french_file and dutch_file aren't empty), the function will return {"en",
51
51
    "fr", "nl"}. """
52
52
    available_languages = {ENGLISH}
53
53
    if post.german_file != "":
54
54
        available_languages.add(GERMAN)
55
55
    if post.spanish_file != "":
56
56
        available_languages.add(SPANISH)
57
57
    if post.french_file != "":
58
58
        available_languages.add(FRENCH)
59
59
    if post.dutch_file != "":
60
60
        available_languages.add(DUTCH)
61
61
    return available_languages
62
62
63
63
def get_preferred_post_language(post, language):
64
64
    """ Returns the post language file that best suits the given language. This
65
65
    is handy if you know what language the user prefers, but aren't sure whether
66
66
    you can provide that language. This function will try to provide the file
67
67
    for that language, or return English if that's not possible. """
68
68
    if language == GERMAN and post.german_file is not None:
69
69
        return post.german_file
70
70
    if language == SPANISH and post.spanish_file is not None:
71
71
        return post.spanish_file
72
72
    if language == FRENCH and post.french_file is not None:
73
73
        return post.french_file
74
74
    if language == DUTCH and post.dutch_file is not None:
75
75
        return post.dutch_file
76
76
    return post.english_file  # Returned if all other choices wouldn't be satisfactory, or the requested language is English.
77
77
78
78
def index(request):
79
79
    template = "blog/index.html"
80
80
    posts = Post.objects.all()
81
81
    language = translation.get_language()
82
82
83
83
    post_links = []
84
84
    for post in posts:
85
85
        blog_file = get_preferred_post_language(post, language)
86
86
        # TODO: Find a cleaner way to determine the title. First and foremost:
87
87
        # If the language differs from English, the other language file needs to
88
88
        # be loaded. Plus: look for a built in function to remove the full path
89
89
        # and only return the file name.
90
90
        title = (blog_file.name.rpartition("/")[2]).rpartition(".")[0]
91
91
        human_title = title.replace("_"," ")
92
92
        date = post.published
93
93
        blog_text = org_to_html(blog_file.name)
94
94
        # TODO: The link can possibly be reversed in the DTL using the title, which is actually
95
95
        # a cleaner way to do it. Investigate.
96
96
        link = reverse("blog-post-language", args=[language, post.slug(language)])
97
97
        post_links.append([human_title, date, blog_text, link])
98
98
99
99
    context = {
100
100
            'posts': post_links,
101
101
            'materialDesign_color': "brown",
102
102
            'materialDesign_accentColor': "yellow",
103
103
            'navbar_title': _("Notepad from a student"),
104
104
            'navbar_backArrow': True,
105
105
            'footer_links': footer_links,
106
106
            'footer_description': footer_description,
107
107
            }
108
108
    return render(request, template, context)
+
109
        context['introduce-feed'] = True
+
110
        request.session['feed-fab-introduction-seen'] = True
+
111
    else:
+
112
        context['introduce-feed'] = False
+
113
+
114
    return render(request, template, context)
109
115
110
116
def post(request, post_slug, language=None):
111
117
    if request.method == "POST":  # Handling a reply if one is sent
112
118
        form = CommentForm(request.POST)
113
119
        if form.is_valid():
114
120
            form.save()
115
121
116
122
    if language is not None:
117
123
        if translation.check_for_language(language):
118
124
            translation.activate(language)
119
125
            request.session[translation.LANGUAGE_SESSION_KEY] = language
120
126
            #return post(request, post_slug)
121
127
    else:
122
128
        language = translation.get_language()
123
129
124
130
    template = "blog/post.html"
125
131
    posts = Post.objects.all()
126
132
    #comments = Comment.objects.filter(post
127
133
    for post in posts:
128
134
        if post.slug(language) == post_slug:
129
135
            comments = Comment.objects.filter(post=post)
130
136
            form = CommentForm(initial={'post': post})
131
137
            blog_file = get_preferred_post_language(post, language)
132
138
            blog_text = org_to_html(blog_file.name)
133
139
            context = {
134
140
                'comments': comments,
135
141
                'form' : form,
136
142
                'human_post_title': blog_file.name.replace("_"," "),
137
143
                'materialDesign_color': "brown",
138
144
                'materialDesign_accentColor': "yellow",
139
145
                'article': blog_text,
140
146
                'title': str(blog_file),
141
147
                'navbar_title': ((blog_file.name.rpartition("/")[2]).rpartition(".")[0]).replace("_"," "),
142
148
                'navbar_backArrow': False,
143
149
                'post_slug': post_slug,
144
150
                'footer_links': footer_links,
145
151
                'footer_description': footer_description,
146
152
                }
147
153
148
154
            # Getting all available article links
149
155
            available = get_available_post_languages(post)
150
156
            if ENGLISH in available:
151
157
                context['english_link'] = reverse("blog-post-language", args=[ENGLISH, post.slug(ENGLISH)])
152
158
            if DUTCH in available:
153
159
                context['dutch_link'] = reverse("blog-post-language", args=[DUTCH, post.slug(DUTCH)])
154
160
155
161
            if FRENCH in available:
156
162
                context['french_link'] = reverse("blog-post-language", args=[FRENCH, post.slug(FRENCH)])
157
163
158
164
            if SPANISH in available:
159
165
                context['spanish_link'] = reverse("blog-post-language", args=[SPANISH, post.slug(SPANISH)])
160
166
161
167
            if GERMAN in available:
162
168
                context['german_link'] = reverse("blog-post-language", args=[GERMAN, post.slug(GERMAN)])
163
169
164
170
            return render(request, template, context)
165
171
166
172
def rss(request):
167
173
    template = "blog/feed.rss"
168
174
    context = {
169
175
        'items': FeedItem.objects.all(),
170
176
        }
171
177
    return render(request, template, context)
172
178