blog

Change accent color to blue

Author
Maarten Vangeneugden
Date
Feb. 9, 2019, 3:46 p.m.
Hash
968e3805bde59aabd0e4cb1ba9a25d9f342f395d
Parent
face3a0fcd24330d1e428e6e283a827c72390ec6
Modified file
views.py

views.py

2 additions and 2 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
    # 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
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()
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()])
78
78
        post_links.append([post.title(), 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
-
            'navbar_title': _("Notepad from a student"),
+
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
        if form.is_valid():
98
98
            new_comment = form.save(commit=False)
99
99
            for post in posts:
100
100
                if post.slug(language) == post_slug:
101
101
                    new_comment.post = post
102
102
                    new_comment.save()
103
103
104
104
105
105
106
106
    if language is not None:
107
107
        if translation.check_for_language(language):
108
108
            translation.activate(language)
109
109
            request.session[translation.LANGUAGE_SESSION_KEY] = language
110
110
            #return post(request, post_slug)
111
111
    else:
112
112
        language = translation.get_language()
113
113
114
114
    template = "blog/post.html"
115
115
    posts = Post.objects.all()
116
116
    #comments = Comment.objects.filter(post
117
117
    for post in posts:
118
118
        if post.slug(language) == post_slug:
119
119
            comments = Comment.objects.filter(post=post)
120
120
            form = CommentForm()
121
121
            post_file = post.text_file(language)
122
122
            post_text = org_to_html(post_file.name)
123
123
            context = {
124
124
                'comments': comments,
125
125
                'form' : form,
126
126
                'human_post_title': post.title(language),
127
127
                'materialDesign_color': "brown",
128
128
                'materialDesign_accentColor': "yellow",
129
-
                'article': post_text,
+
129
                'article': post_text,
130
130
                'title': post.title(language),
131
131
                'navbar_title': post.title(language),
132
132
                'navbar_backArrow': False,
133
133
                'post_slug': post_slug,
134
134
                'footer_links': footer_links,
135
135
                'footer_description': footer_description,
136
136
                }
137
137
138
138
            # Getting all available article links
139
139
            available = get_available_post_languages(post)
140
140
            if ENGLISH in available:
141
141
                context['english_link'] = reverse("blog-post-language", args=[ENGLISH, post.slug(ENGLISH)])
142
142
            if DUTCH in available:
143
143
                context['dutch_link'] = reverse("blog-post-language", args=[DUTCH, post.slug(DUTCH)])
144
144
145
145
            if FRENCH in available:
146
146
                context['french_link'] = reverse("blog-post-language", args=[FRENCH, post.slug(FRENCH)])
147
147
148
148
            if SPANISH in available:
149
149
                context['spanish_link'] = reverse("blog-post-language", args=[SPANISH, post.slug(SPANISH)])
150
150
151
151
            if GERMAN in available:
152
152
                context['german_link'] = reverse("blog-post-language", args=[GERMAN, post.slug(GERMAN)])
153
153
154
154
            return render(request, template, context)
155
155
156
156
def rss(request):
157
157
    template = "blog/feed.rss"
158
158
    context = {
159
159
        'items': FeedItem.objects.all(),
160
160
        }
161
161
    return render(request, template, context)
162
162