Fix bugs regarding RSS feed rendering
Some of the ending tags were wrong. Those were corrected. The links used to be soft-coded, so I could make use of the {% url %} tag to create the links. I have removed that for now, but I can add it later. For now all links are hardcoded. The model has been updated accordingly. There was also a typo regarding the URL and how it was used in views.
- Author
- Maarten Vangeneugden
- Date
- April 9, 2018, 8:47 p.m.
- Hash
- 919accec757efdc4ed16df3fb73c7ce442b8be3b
- Parent
- 409038fe4f555d356da53ed03a3d9b4532689745
- Modified files
- migrations/0004_feeditem.py
- migrations/0005_auto_20180409_1845.py
- models.py
- templates/blog/feed.rss
- urls.py
- views.py
migrations/0004_feeditem.py ¶
26 additions and 0 deletions.
View changes Hide changes
+ |
1 |
# Generated by Django 1.11.10 on 2018-04-09 18:30 |
+ |
2 |
from __future__ import unicode_literals |
+ |
3 |
|
+ |
4 |
from django.db import migrations, models |
+ |
5 |
|
+ |
6 |
|
+ |
7 |
class Migration(migrations.Migration): |
+ |
8 |
|
+ |
9 |
dependencies = [ |
+ |
10 |
('blog', '0003_comment'), |
+ |
11 |
] |
+ |
12 |
|
+ |
13 |
operations = [ |
+ |
14 |
migrations.CreateModel( |
+ |
15 |
name='FeedItem', |
+ |
16 |
fields=[ |
+ |
17 |
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
+ |
18 |
('title', models.CharField(max_length=64)), |
+ |
19 |
('added', models.DateTimeField(auto_now_add=True)), |
+ |
20 |
('description', models.CharField(max_length=400)), |
+ |
21 |
('link', models.CharField(max_length=64)), |
+ |
22 |
('soft_url', models.BooleanField(default=True)), |
+ |
23 |
], |
+ |
24 |
), |
+ |
25 |
] |
+ |
26 |
migrations/0005_auto_20180409_1845.py ¶
24 additions and 0 deletions.
View changes Hide changes
+ |
1 |
# Generated by Django 1.11.10 on 2018-04-09 18:45 |
+ |
2 |
from __future__ import unicode_literals |
+ |
3 |
|
+ |
4 |
from django.db import migrations, models |
+ |
5 |
|
+ |
6 |
|
+ |
7 |
class Migration(migrations.Migration): |
+ |
8 |
|
+ |
9 |
dependencies = [ |
+ |
10 |
('blog', '0004_feeditem'), |
+ |
11 |
] |
+ |
12 |
|
+ |
13 |
operations = [ |
+ |
14 |
migrations.RemoveField( |
+ |
15 |
model_name='feeditem', |
+ |
16 |
name='soft_url', |
+ |
17 |
), |
+ |
18 |
migrations.AlterField( |
+ |
19 |
model_name='feeditem', |
+ |
20 |
name='link', |
+ |
21 |
field=models.URLField(), |
+ |
22 |
), |
+ |
23 |
] |
+ |
24 |
models.py ¶
1 addition and 2 deletions.
View changes Hide changes
1 |
1 |
from django.utils import translation |
2 |
2 |
from django.template.defaultfilters import slugify |
3 |
3 |
from django.db import models |
4 |
4 |
import datetime |
5 |
5 |
import os |
6 |
6 |
|
7 |
7 |
def post_title_directory(instance, filename): |
8 |
8 |
""" Files will be uploaded to MEDIA_ROOT/blog/<year of publishing>/<blog |
9 |
9 |
title> |
10 |
10 |
The blog title is determined by the text before the first period (".") in |
11 |
11 |
the filename. So if the file has the name "Trains are bæ.en.md", the file |
12 |
12 |
will be stored in "blog/<this year>/Trains are bæ". Name your files |
13 |
13 |
properly! |
14 |
14 |
It should also be noted that all files are stored in the same folder if they |
15 |
15 |
belong to the same blogpost, regardless of language. The titles that are |
16 |
16 |
displayed to the user however, should be the titles of the files themselves, |
17 |
17 |
which should be in the native language. So if a blog post is titled |
18 |
18 |
"Universities of Belgium", its Dutch counterpart should be titled |
19 |
19 |
"Universiteiten van België", so the correct title can be derived from the |
20 |
20 |
filename. |
21 |
21 |
|
22 |
22 |
Recommended way to name the uploaded file: "<name of blog post in language |
23 |
23 |
it's written>.org". This removes the maximum amount of redundancy (e.g. the |
24 |
24 |
language of the file can be derived from the title, no ".fr.org" or something |
25 |
25 |
like that necessary), and can directly be used for the end user (the title |
26 |
26 |
is what should be displayed). |
27 |
27 |
""" |
28 |
28 |
english_file_name = os.path.basename(instance.english_file.name) # TODO: Test if this returns the file name! |
29 |
29 |
english_title = english_file_name.rpartition(".")[0] |
30 |
30 |
year = datetime.date.today().year |
31 |
31 |
|
32 |
32 |
return "blog/{0}/{1}/{2}".format(year, english_title, filename) |
33 |
33 |
|
34 |
34 |
class Post(models.Model): |
35 |
35 |
""" Represents a blog post. The title of the blog post is determnined by the name |
36 |
36 |
of the files. |
37 |
37 |
A blog post can be in 5 different languages: German, Spanish, English, French, |
38 |
38 |
and Dutch. For all these languages, a seperate field exists. Thus, a |
39 |
39 |
translated blog post has a seperate file for each translation, and is |
40 |
40 |
seperated from Django's internationalization/localization system. |
41 |
41 |
Only the English field is mandatory. The others may contain a value if a |
42 |
42 |
translated version exists, which will be displayed accordingly. |
43 |
43 |
""" |
44 |
44 |
published = models.DateTimeField(auto_now_add=True) |
45 |
45 |
english_file = models.FileField(upload_to=post_title_directory, unique=True, blank=False) |
46 |
46 |
dutch_file = models.FileField(upload_to=post_title_directory, blank=True) |
47 |
47 |
french_file = models.FileField(upload_to=post_title_directory, blank=True) |
48 |
48 |
german_file = models.FileField(upload_to=post_title_directory, blank=True) |
49 |
49 |
spanish_file = models.FileField(upload_to=post_title_directory, blank=True) |
50 |
50 |
# Only the English file can be unique, because apparantly, there can't be |
51 |
51 |
# two blank fields in a unique column. Okay then. |
52 |
52 |
|
53 |
53 |
def __str__(self): |
54 |
54 |
return self.slug("en") |
55 |
55 |
|
56 |
56 |
def slug(self, language_code=translation.get_language()): |
57 |
57 |
""" Returns a slug of the requested language, or None if no version exists in that language. """ |
58 |
58 |
possibilities = { |
59 |
59 |
"en" : self.english_file, |
60 |
60 |
"de" : self.german_file, |
61 |
61 |
"nl" : self.dutch_file, |
62 |
62 |
"fr" : self.french_file, |
63 |
63 |
"es" : self.spanish_file, |
64 |
64 |
} |
65 |
65 |
if possibilities[language_code]: |
66 |
66 |
return slugify(os.path.basename(possibilities[language_code].name).rpartition(".")[0]) |
67 |
67 |
else: |
68 |
68 |
return None |
69 |
69 |
|
70 |
70 |
class Comment(models.Model): |
71 |
71 |
""" Represents a comment on a blog post. |
72 |
72 |
Comments are not filtered by language; a |
73 |
73 |
comment made by someone reading the article in Dutch, that's written in |
74 |
74 |
Dutch, will show up (unedited) for somebody whom's reading the Spanish |
75 |
75 |
version. |
76 |
76 |
""" |
77 |
77 |
date = models.DateTimeField(auto_now_add=True) |
78 |
78 |
name = models.CharField(max_length=64) |
79 |
79 |
text = models.TextField(max_length=1000) # Should be more than enough. |
80 |
80 |
post = models.ForeignKey( |
81 |
81 |
Post, |
82 |
82 |
on_delete=models.CASCADE, |
83 |
83 |
null=False, |
84 |
84 |
) |
85 |
85 |
class meta: |
86 |
86 |
ordering = ['date'] # When printed, prints the oldest comment first. |
87 |
87 |
|
88 |
88 |
class FeedItem(models.Model): |
89 |
89 |
""" An item that shows up in the RSS feed.""" |
90 |
90 |
title = models.CharField(max_length=64) |
91 |
91 |
added = models.DateTimeField(auto_now_add=True) |
92 |
92 |
description = models.CharField(max_length=400) |
93 |
93 |
link = models.CharField(max_length=64) |
94 |
- | soft_url = models.BooleanField(default=True) |
95 |
- | |
+ |
94 |
templates/blog/feed.rss ¶
3 additions and 7 deletions.
View changes Hide changes
1 |
1 |
<rss version="2.0"> |
2 |
2 |
<channel> |
3 |
3 |
<title>RSS feed for maartenv.be</title> |
4 |
4 |
<link>https://maartenv.be</link> |
5 |
5 |
<description>The official RSS feed for the personal website of Maarten V.</description> |
6 |
6 |
{% for item in items %} |
7 |
7 |
<item> |
8 |
8 |
<title>{{ item.title }}</title> |
9 |
9 |
{% if item.soft_url %} |
10 |
- | <link>{% url item.link %}</title> |
11 |
- | {% else %} |
12 |
- | <link>{{ item.link }}</title> |
13 |
- | {% endif %} |
14 |
- | <description>{{ item.description }}</description> |
+ |
10 |
<description>{{ item.description }}</description> |
15 |
11 |
<pubDate>{{ item.added|time:'r' }}</pubDate> |
16 |
- | </item> |
+ |
12 |
</item> |
17 |
13 |
{% endfor %} |
18 |
14 |
</channel> |
19 |
15 |
</rss> |
20 |
- | |
+ |
16 |
urls.py ¶
1 addition and 1 deletion.
View changes Hide changes
1 |
1 |
|
2 |
2 |
from . import views # Imports the views from the same directory (which is views.py). |
3 |
3 |
|
4 |
4 |
urlpatterns = [ |
5 |
5 |
url(r'^$', views.index, name='blog-index'), |
6 |
6 |
url(r'^(?P<language>(.){2})/(?P<post_slug>(.)+)$', views.post, name='blog-post-language'), |
+ |
7 |
url(r'^(?P<language>(.){2})/(?P<post_slug>(.)+)$', views.post, name='blog-post-language'), |
7 |
8 |
url(r'^(?P<post_slug>(.)+)$', views.post, name='blog-post'), |
8 |
9 |
url(r'^feed.rss', views.rss, name='blog-rss'), |
9 |
- | ] |
10 |
10 |
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 |
[_("Blog 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 |
# FIXME: Remove this template trash. THIS IS A VIEW, NOT A FUCKING TEMPLATE FFS |
26 |
26 |
context = { |
27 |
27 |
'materialDesign_color': "green", |
28 |
28 |
'materialDesign_accentColor': "purple", |
29 |
29 |
'navbar_title': "Blog", |
30 |
30 |
'navbar_fixed': True, |
31 |
31 |
'navbar_backArrow': True, |
32 |
32 |
#'footer_title': "Maarten's blog", |
33 |
33 |
#'footer_description': "My personal scribbly notepad.", |
34 |
34 |
#'footer_links': footer_links, |
35 |
35 |
} |
36 |
36 |
|
37 |
37 |
def org_to_html(file_path): |
38 |
38 |
""" Converts the given org formatted file to HTML. |
39 |
39 |
This function directly returns the resulting HTML code. This function uses |
40 |
40 |
the amazing Haskell library Pandoc to convert the file (and takes care |
41 |
41 |
of header id's and all that stuff). |
42 |
42 |
""" |
43 |
43 |
# FIXME: Remove hardcoded link to media. Replace with media tag! |
44 |
44 |
return subprocess.check_output(["pandoc", "--from=org", "--to=html", "/srv/django/website/media/"+file_path]) |
45 |
45 |
|
46 |
46 |
def get_available_post_languages(post): |
47 |
47 |
""" Returns the language codes for which a blog post exists. This function |
48 |
48 |
always returns English (because that field mustn't be empty). |
49 |
49 |
So say a blog post has an English, Dutch and French version (which means |
50 |
50 |
english_file, french_file and dutch_file aren't empty), the function will return {"en", |
51 |
51 |
"fr", "nl"}. """ |
52 |
52 |
available_languages = {ENGLISH} |
53 |
53 |
if post.german_file != "": |
54 |
54 |
available_languages.add(GERMAN) |
55 |
55 |
if post.spanish_file != "": |
56 |
56 |
available_languages.add(SPANISH) |
57 |
57 |
if post.french_file != "": |
58 |
58 |
available_languages.add(FRENCH) |
59 |
59 |
if post.dutch_file != "": |
60 |
60 |
available_languages.add(DUTCH) |
61 |
61 |
return available_languages |
62 |
62 |
|
63 |
63 |
def get_preferred_post_language(post, language): |
64 |
64 |
""" Returns the post language file that best suits the given language. This |
65 |
65 |
is handy if you know what language the user prefers, but aren't sure whether |
66 |
66 |
you can provide that language. This function will try to provide the file |
67 |
67 |
for that language, or return English if that's not possible. """ |
68 |
68 |
if language == GERMAN and post.german_file is not None: |
69 |
69 |
return post.german_file |
70 |
70 |
if language == SPANISH and post.spanish_file is not None: |
71 |
71 |
return post.spanish_file |
72 |
72 |
if language == FRENCH and post.french_file is not None: |
73 |
73 |
return post.french_file |
74 |
74 |
if language == DUTCH and post.dutch_file is not None: |
75 |
75 |
return post.dutch_file |
76 |
76 |
return post.english_file # Returned if all other choices wouldn't be satisfactory, or the requested language is English. |
77 |
77 |
|
78 |
78 |
def index(request): |
79 |
79 |
template = "blog/index.html" |
80 |
80 |
posts = Post.objects.all() |
81 |
81 |
language = translation.get_language() |
82 |
82 |
|
83 |
83 |
post_links = [] |
84 |
84 |
for post in posts: |
85 |
85 |
blog_file = get_preferred_post_language(post, language) |
86 |
86 |
# TODO: Find a cleaner way to determine the title. First and foremost: |
87 |
87 |
# If the language differs from English, the other language file needs to |
88 |
88 |
# be loaded. Plus: look for a built in function to remove the full path |
89 |
89 |
# and only return the file name. |
90 |
90 |
title = (blog_file.name.rpartition("/")[2]).rpartition(".")[0] |
91 |
91 |
human_title = title.replace("_"," ") |
92 |
92 |
date = post.published |
93 |
93 |
blog_text = org_to_html(blog_file.name) |
94 |
94 |
# TODO: The link can possibly be reversed in the DTL using the title, which is actually |
95 |
95 |
# a cleaner way to do it. Investigate. |
96 |
96 |
link = reverse("blog-post-language", args=[language, post.slug(language)]) |
97 |
97 |
post_links.append([human_title, date, blog_text, link]) |
98 |
98 |
|
99 |
99 |
context = { |
100 |
100 |
'posts': post_links, |
101 |
101 |
'materialDesign_color': "brown", |
102 |
102 |
'materialDesign_accentColor': "yellow", |
103 |
103 |
'navbar_title': _("Notepad from a student"), |
104 |
104 |
'navbar_backArrow': True, |
105 |
105 |
'footer_links': footer_links, |
106 |
106 |
'footer_description': footer_description, |
107 |
107 |
} |
108 |
108 |
return render(request, template, context) |
109 |
109 |
|
110 |
110 |
def post(request, post_slug, language=None): |
111 |
111 |
if request.method == "POST": # Handling a reply if one is sent |
112 |
112 |
form = CommentForm(request.POST) |
113 |
113 |
if form.is_valid(): |
114 |
114 |
form.save() |
115 |
115 |
|
116 |
116 |
if language is not None: |
117 |
117 |
if translation.check_for_language(language): |
118 |
118 |
translation.activate(language) |
119 |
119 |
request.session[translation.LANGUAGE_SESSION_KEY] = language |
120 |
120 |
#return post(request, post_slug) |
121 |
121 |
else: |
122 |
122 |
language = translation.get_language() |
123 |
123 |
|
124 |
124 |
template = "blog/post.html" |
125 |
125 |
posts = Post.objects.all() |
126 |
126 |
#comments = Comment.objects.filter(post |
127 |
127 |
for post in posts: |
128 |
128 |
if post.slug(language) == post_slug: |
129 |
129 |
comments = Comment.objects.filter(post=post) |
130 |
130 |
form = CommentForm(initial={'post': post}) |
131 |
131 |
blog_file = get_preferred_post_language(post, language) |
132 |
132 |
blog_text = org_to_html(blog_file.name) |
133 |
133 |
context = { |
134 |
134 |
'comments': comments, |
135 |
135 |
'form' : form, |
136 |
136 |
'human_post_title': blog_file.name.replace("_"," "), |
137 |
137 |
'materialDesign_color': "brown", |
138 |
138 |
'materialDesign_accentColor': "yellow", |
139 |
139 |
'article': blog_text, |
140 |
140 |
'title': str(blog_file), |
141 |
141 |
'navbar_title': ((blog_file.name.rpartition("/")[2]).rpartition(".")[0]).replace("_"," "), |
142 |
142 |
'navbar_backArrow': False, |
143 |
143 |
'post_slug': post_slug, |
144 |
144 |
'footer_links': footer_links, |
145 |
145 |
'footer_description': footer_description, |
146 |
146 |
} |
147 |
147 |
|
148 |
148 |
# Getting all available article links |
149 |
149 |
available = get_available_post_languages(post) |
150 |
150 |
if ENGLISH in available: |
151 |
151 |
context['english_link'] = reverse("blog-post-language", args=[ENGLISH, post.slug(ENGLISH)]) |
152 |
152 |
if DUTCH in available: |
153 |
153 |
context['dutch_link'] = reverse("blog-post-language", args=[DUTCH, post.slug(DUTCH)]) |
154 |
154 |
|
155 |
155 |
if FRENCH in available: |
156 |
156 |
context['french_link'] = reverse("blog-post-language", args=[FRENCH, post.slug(FRENCH)]) |
157 |
157 |
|
158 |
158 |
if SPANISH in available: |
159 |
159 |
context['spanish_link'] = reverse("blog-post-language", args=[SPANISH, post.slug(SPANISH)]) |
160 |
160 |
|
161 |
161 |
if GERMAN in available: |
162 |
162 |
context['german_link'] = reverse("blog-post-language", args=[GERMAN, post.slug(GERMAN)]) |
163 |
163 |
|
164 |
164 |
return render(request, template, context) |
165 |
165 |
|
166 |
166 |
def rss(request): |
167 |
167 |
template = "blog/feed.rss" |
168 |
168 |
context = { |
169 |
169 |
'items': ItemFeed.objects.all(), |
170 |
- | } |
+ |
170 |
} |
171 |
171 |
return render(request, template, context) |
172 |
172 |