Fix some bugs related to database changes
The migration has also been included in this commit. There was a typo regarding the context, but it has been fixed as well. All titles and slugs are now properly stored and displayed to the user.
- Author
- Maarten Vangeneugden
- Date
- April 9, 2018, 9:34 p.m.
- Hash
- 822ac1493a49d83d8ea46ae148399b2f4df133b3
- Parent
- fbff300ceba7e4f07548d25bdec66c24b29e2fae
- Modified files
- migrations/0006_auto_20180409_2128.py
- views.py
migrations/0006_auto_20180409_2128.py ¶
80 additions and 0 deletions.
View changes Hide changes
+ |
1 |
# Generated by Django 1.11.10 on 2018-04-09 21:28 |
+ |
2 |
from __future__ import unicode_literals |
+ |
3 |
|
+ |
4 |
import blog.models |
+ |
5 |
from django.db import migrations, models |
+ |
6 |
|
+ |
7 |
|
+ |
8 |
class Migration(migrations.Migration): |
+ |
9 |
|
+ |
10 |
dependencies = [ |
+ |
11 |
('blog', '0005_auto_20180409_1845'), |
+ |
12 |
] |
+ |
13 |
|
+ |
14 |
operations = [ |
+ |
15 |
migrations.AddField( |
+ |
16 |
model_name='post', |
+ |
17 |
name='slug_de', |
+ |
18 |
field=models.SlugField(allow_unicode=True, blank=True), |
+ |
19 |
), |
+ |
20 |
migrations.AddField( |
+ |
21 |
model_name='post', |
+ |
22 |
name='slug_en', |
+ |
23 |
field=models.SlugField(allow_unicode=True, default='alol', unique=True), |
+ |
24 |
preserve_default=False, |
+ |
25 |
), |
+ |
26 |
migrations.AddField( |
+ |
27 |
model_name='post', |
+ |
28 |
name='slug_es', |
+ |
29 |
field=models.SlugField(allow_unicode=True, blank=True), |
+ |
30 |
), |
+ |
31 |
migrations.AddField( |
+ |
32 |
model_name='post', |
+ |
33 |
name='slug_fr', |
+ |
34 |
field=models.SlugField(allow_unicode=True, blank=True), |
+ |
35 |
), |
+ |
36 |
migrations.AddField( |
+ |
37 |
model_name='post', |
+ |
38 |
name='slug_nl', |
+ |
39 |
field=models.SlugField(allow_unicode=True, default='eee', unique=True), |
+ |
40 |
preserve_default=False, |
+ |
41 |
), |
+ |
42 |
migrations.AddField( |
+ |
43 |
model_name='post', |
+ |
44 |
name='title_de', |
+ |
45 |
field=models.CharField(blank=True, max_length=64), |
+ |
46 |
), |
+ |
47 |
migrations.AddField( |
+ |
48 |
model_name='post', |
+ |
49 |
name='title_en', |
+ |
50 |
field=models.CharField(default='ETEE', max_length=64, unique=True), |
+ |
51 |
preserve_default=False, |
+ |
52 |
), |
+ |
53 |
migrations.AddField( |
+ |
54 |
model_name='post', |
+ |
55 |
name='title_es', |
+ |
56 |
field=models.CharField(blank=True, max_length=64), |
+ |
57 |
), |
+ |
58 |
migrations.AddField( |
+ |
59 |
model_name='post', |
+ |
60 |
name='title_fr', |
+ |
61 |
field=models.CharField(blank=True, max_length=64), |
+ |
62 |
), |
+ |
63 |
migrations.AddField( |
+ |
64 |
model_name='post', |
+ |
65 |
name='title_nl', |
+ |
66 |
field=models.CharField(default='TEEE', max_length=64, unique=True), |
+ |
67 |
preserve_default=False, |
+ |
68 |
), |
+ |
69 |
migrations.AlterField( |
+ |
70 |
model_name='post', |
+ |
71 |
name='dutch_file', |
+ |
72 |
field=models.FileField(upload_to=blog.models.post_title_directory), |
+ |
73 |
), |
+ |
74 |
migrations.AlterField( |
+ |
75 |
model_name='post', |
+ |
76 |
name='english_file', |
+ |
77 |
field=models.FileField(upload_to=blog.models.post_title_directory), |
+ |
78 |
), |
+ |
79 |
] |
+ |
80 |
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 |
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 |
return subprocess.check_output(["pandoc", "--from=org", "--to=html", "/srv/django/website/media/"+file_path]) |
33 |
33 |
|
34 |
34 |
def get_available_post_languages(post): |
35 |
35 |
""" Returns the language codes for which a blog post exists. This function |
36 |
36 |
always returns English (because that field mustn't be empty). |
37 |
37 |
So say a blog post has an English, Dutch and French version (which means |
38 |
38 |
english_file, french_file and dutch_file aren't empty), the function will return {"en", |
39 |
39 |
"fr", "nl"}. """ |
40 |
40 |
available_languages = {ENGLISH} |
41 |
41 |
if post.german_file != "": |
42 |
42 |
available_languages.add(GERMAN) |
43 |
43 |
if post.spanish_file != "": |
44 |
44 |
available_languages.add(SPANISH) |
45 |
45 |
if post.french_file != "": |
46 |
46 |
available_languages.add(FRENCH) |
47 |
47 |
if post.dutch_file != "": |
48 |
48 |
available_languages.add(DUTCH) |
49 |
49 |
return available_languages |
50 |
50 |
|
51 |
51 |
def index(request): |
52 |
52 |
template = "blog/index.html" |
53 |
53 |
posts = Post.objects.all() |
54 |
54 |
language = translation.get_language() |
55 |
55 |
|
56 |
56 |
post_links = [] |
57 |
57 |
for post in posts: |
58 |
58 |
blog_file = post.text_file() |
59 |
59 |
blog_text = org_to_html(blog_file.name) |
60 |
60 |
# TODO: The link can possibly be reversed in the DTL using the title, which is actually |
61 |
61 |
# a cleaner way to do it. Investigate. |
62 |
62 |
link = reverse("blog-post-language", args=[language, post.slug()]) |
63 |
63 |
post_links.append([post.title(), post.published, blog_text, link]) |
64 |
64 |
|
65 |
65 |
context = { |
66 |
66 |
'posts': post_links, |
67 |
67 |
'materialDesign_color': "brown", |
68 |
68 |
'materialDesign_accentColor': "yellow", |
69 |
69 |
'navbar_title': _("Notepad from a student"), |
70 |
70 |
'navbar_backArrow': True, |
71 |
71 |
'footer_links': footer_links, |
72 |
72 |
'footer_description': footer_description, |
73 |
73 |
} |
74 |
74 |
if not request.session.get("feed-fab-introduction-seen", default=False): |
75 |
75 |
context['introduce_feed'] = True |
76 |
76 |
request.session['feed-fab-introduction-seen'] = True |
77 |
77 |
return render(request, template, context) |
78 |
78 |
|
79 |
79 |
def post(request, post_slug, language=None): |
80 |
80 |
if request.method == "POST": # Handling a reply if one is sent |
81 |
81 |
form = CommentForm(request.POST) |
82 |
82 |
if form.is_valid(): |
83 |
83 |
form.save() |
84 |
84 |
|
85 |
85 |
if language is not None: |
86 |
86 |
if translation.check_for_language(language): |
87 |
87 |
translation.activate(language) |
88 |
88 |
request.session[translation.LANGUAGE_SESSION_KEY] = language |
89 |
89 |
#return post(request, post_slug) |
90 |
90 |
else: |
91 |
91 |
language = translation.get_language() |
92 |
92 |
|
93 |
93 |
template = "blog/post.html" |
94 |
94 |
posts = Post.objects.all() |
95 |
95 |
#comments = Comment.objects.filter(post |
96 |
96 |
for post in posts: |
97 |
97 |
if post.slug(language) == post_slug: |
98 |
98 |
comments = Comment.objects.filter(post=post) |
99 |
99 |
form = CommentForm(initial={'post': post}) |
100 |
100 |
post_file = post.text_file(language) |
101 |
101 |
post_text = org_to_html(post_file.name) |
102 |
102 |
context = { |
103 |
103 |
'comments': comments, |
104 |
104 |
'form' : form, |
105 |
105 |
'human_post_title': post.title(language), |
106 |
106 |
'materialDesign_color': "brown", |
107 |
107 |
'materialDesign_accentColor': "yellow", |
108 |
108 |
'article': post_text, |
109 |
109 |
'title': post.title(language), |
110 |
110 |
'navbar_title': blog.title(language), |
111 |
- | 'navbar_backArrow': False, |
+ |
111 |
'navbar_backArrow': False, |
112 |
112 |
'post_slug': post_slug, |
113 |
113 |
'footer_links': footer_links, |
114 |
114 |
'footer_description': footer_description, |
115 |
115 |
} |
116 |
116 |
|
117 |
117 |
# Getting all available article links |
118 |
118 |
available = get_available_post_languages(post) |
119 |
119 |
if ENGLISH in available: |
120 |
120 |
context['english_link'] = reverse("blog-post-language", args=[ENGLISH, post.slug(ENGLISH)]) |
121 |
121 |
if DUTCH in available: |
122 |
122 |
context['dutch_link'] = reverse("blog-post-language", args=[DUTCH, post.slug(DUTCH)]) |
123 |
123 |
|
124 |
124 |
if FRENCH in available: |
125 |
125 |
context['french_link'] = reverse("blog-post-language", args=[FRENCH, post.slug(FRENCH)]) |
126 |
126 |
|
127 |
127 |
if SPANISH in available: |
128 |
128 |
context['spanish_link'] = reverse("blog-post-language", args=[SPANISH, post.slug(SPANISH)]) |
129 |
129 |
|
130 |
130 |
if GERMAN in available: |
131 |
131 |
context['german_link'] = reverse("blog-post-language", args=[GERMAN, post.slug(GERMAN)]) |
132 |
132 |
|
133 |
133 |
return render(request, template, context) |
134 |
134 |
|
135 |
135 |
def rss(request): |
136 |
136 |
template = "blog/feed.rss" |
137 |
137 |
context = { |
138 |
138 |
'items': FeedItem.objects.all(), |
139 |
139 |
} |
140 |
140 |
return render(request, template, context) |
141 |
141 |