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