blog

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