blog

Fix type error

Author
Maarten 'Vngngdn' Vangeneugden
Date
April 9, 2018, 5:24 p.m.
Hash
0a38e021df7fcd5f3fe9ea3c92680a613096d94c
Parent
68a29033971c193978769df0dddffb6ba9006731
Modified file
views.py

views.py

1 addition and 1 deletion.

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