Add changes for a weekly archive system
- Author
- Maarten Vangeneugden
- Date
- Feb. 11, 2019, 9:03 p.m.
- Hash
- bc08565cbb1feec368c4511597b6efb5d84da969
- Parent
- 968e3805bde59aabd0e4cb1ba9a25d9f342f395d
- Modified files
- templates/blog/index.html
- urls.py
- views.py
templates/blog/index.html ¶
4 additions and 0 deletions.
View changes Hide changes
1 |
1 |
{% load i18n %} |
2 |
2 |
{% load static %} |
3 |
3 |
|
4 |
4 |
{% block stylesheets %} |
5 |
5 |
{{ block.super }} |
6 |
6 |
{#<link href="{% static "blog/stylesheet.css" %}" rel="stylesheet" media="screen, projection" />#} |
7 |
7 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css"> |
8 |
8 |
|
9 |
9 |
<!-- Compiled and minified JavaScript --> |
10 |
10 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script> |
11 |
11 |
{% endblock %} |
12 |
12 |
|
13 |
13 |
{% block title %}{% trans "Maarten's blog" %}{% endblock title %} |
14 |
14 |
|
15 |
15 |
{% block description %}{% blocktrans %}The always coherently put together, yet |
16 |
16 |
fuzzy blog of whatever sprouts in my mind.{% endblocktrans %} |
17 |
17 |
{% endblock description %} |
18 |
18 |
|
19 |
19 |
{% block main %} |
20 |
20 |
{% with color="brown" accent_color="yellow" %} |
21 |
21 |
<div class="section {{ color }} z-depth-3"> |
22 |
22 |
<div class="container"> |
23 |
23 |
<div class="white-text"> |
24 |
24 |
<h3>{% trans "Blog" %}</h3> |
25 |
25 |
<p> |
26 |
26 |
{% blocktrans %}Welcome to my blog. Here, I write |
27 |
27 |
about things that interest me. Politics, coding, |
28 |
28 |
studying, life, or anything else I fancy rambling |
29 |
29 |
about. If you're in luck, I may've written it in a |
30 |
30 |
language that you understand better than English. |
31 |
31 |
{% endblocktrans %} |
32 |
32 |
</p> |
33 |
33 |
</div> |
34 |
34 |
</div> |
35 |
35 |
</div> |
36 |
36 |
|
37 |
37 |
<div class="fixed-action-btn"> |
38 |
38 |
<a href="{% url 'blog-feed' %}" id="feed-fab" class="btn-floating waves-effect waves-light btn-large orange accent-4"> |
39 |
39 |
<i class="large material-icons">rss_feed</i> |
40 |
40 |
</a> |
41 |
41 |
</div> |
42 |
42 |
|
43 |
43 |
|
44 |
44 |
<div class="container row"> |
45 |
45 |
<div class="col s12 m6"> |
46 |
46 |
<h1 id="weekly" class="{{ color }}-text">Weekly</h1> |
47 |
47 |
{% include "blog/weekly.html" %} |
+ |
48 |
href="{% url "weekly-archive" %}"> |
+ |
49 |
Open archief |
+ |
50 |
</a><hr /> |
+ |
51 |
{% include "blog/weekly.html" %} |
48 |
52 |
</div> |
49 |
53 |
<div class="container col s12 m6"> |
50 |
54 |
{% for title, date, blog_text, link in posts %} |
51 |
55 |
<h1 class="{{ color}}-text">{{ title }}</h2> |
52 |
56 |
<span class="grey-text">{{ date|date:"DATE_FORMAT" }}</span> |
53 |
57 |
<p class="hide-on-small-only">{{ blog_text|safe|truncatewords_html:100 }}</p> |
54 |
58 |
<a class="btn black-text waves-effect {{accent_color}} accent-4" href="{{link}}"> |
55 |
59 |
📚 {% trans "Read on"%} |
56 |
60 |
</a> |
57 |
61 |
<hr /> |
58 |
62 |
{% endfor %} |
59 |
63 |
</div> |
60 |
64 |
</div> |
61 |
65 |
|
62 |
66 |
{% if introduce_feed %} |
63 |
67 |
<div class="orange accent-4 tap-target" data-target="feed-fab"> |
64 |
68 |
<div class="white-text tap-target-content"> |
65 |
69 |
<h5>{% trans "Never miss an update again." %}</h5> |
66 |
70 |
<p>{% blocktrans %}Getting info on updates to blog posts, major website changes, and other important |
67 |
71 |
news items is now easy and convenient with RSS. |
68 |
72 |
{% endblocktrans %}</p> |
69 |
73 |
</div> |
70 |
74 |
</div> |
71 |
75 |
<script> |
72 |
76 |
var elem = document.querySelector('.tap-target'); |
73 |
77 |
console.log(elem); |
74 |
78 |
var instance = M.TapTarget.init(elem, null); |
75 |
79 |
console.log(instance); |
76 |
80 |
instance.open(); |
77 |
81 |
</script> |
78 |
82 |
{% endif %} |
79 |
83 |
|
80 |
84 |
{% endwith %} |
81 |
85 |
{% endblock main %} |
82 |
86 |
urls.py ¶
1 addition and 0 deletions.
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'^feed.rss$', views.rss, name='blog-feed'), |
7 |
7 |
url(r'^(?P<language>(.){2})/(?P<post_slug>(.)+)$', views.post, name='blog-post-language'), |
8 |
8 |
url(r'^(?P<post_slug>(.)+)$', views.post, name='blog-post'), |
9 |
9 |
] |
+ |
10 |
] |
10 |
11 |
views.py ¶
23 additions and 0 deletions.
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 |
# XXX: The reason I'm first converting all occurences of .jpg][ and .png][ |
33 |
33 |
# to .jpgPANDOCBUG][ and .pngPANDOCBUG][, is because of a Pandoc bug that |
34 |
34 |
# removes the text links for images. It is afterwards converted back, no |
35 |
35 |
# worries. |
36 |
36 |
file = open("/srv/django/website/media/"+file_path, "r", encoding="utf-8") |
37 |
37 |
text = file.read() |
38 |
38 |
file.close() |
39 |
39 |
text = text.replace(".jpg][", ".jpgPANDOCBUG][") |
40 |
40 |
text = text.replace(".png][", ".pngPANDOCBUG][") |
41 |
41 |
file = open("/tmp/blog-file.org", "w", encoding="utf-8") |
42 |
42 |
file.write(text) |
43 |
43 |
file.close() |
44 |
44 |
html_text = subprocess.check_output(["pandoc", "--from=org", "--to=html","/tmp/blog-file.org"]) |
45 |
45 |
html_text = html_text.replace(".jpgPANDOCBUG", ".jpg") |
46 |
46 |
html_text = html_text.replace(".pngPANDOCBUG", ".png") |
47 |
47 |
return html_text |
48 |
48 |
|
49 |
49 |
def get_available_post_languages(post): |
50 |
50 |
""" Returns the language codes for which a blog post exists. This function |
51 |
51 |
always returns English (because that field mustn't be empty). |
52 |
52 |
So say a blog post has an English, Dutch and French version (which means |
53 |
53 |
english_file, french_file and dutch_file aren't empty), the function will return {"en", |
54 |
54 |
"fr", "nl"}. """ |
55 |
55 |
available_languages = {ENGLISH} |
56 |
56 |
if post.german_file != "": |
57 |
57 |
available_languages.add(GERMAN) |
58 |
58 |
if post.spanish_file != "": |
59 |
59 |
available_languages.add(SPANISH) |
60 |
60 |
if post.french_file != "": |
61 |
61 |
available_languages.add(FRENCH) |
62 |
62 |
if post.dutch_file != "": |
63 |
63 |
available_languages.add(DUTCH) |
64 |
64 |
return available_languages |
65 |
65 |
|
66 |
66 |
def index(request): |
67 |
67 |
template = "blog/index.html" |
68 |
68 |
posts = Post.objects.all() |
69 |
69 |
language = translation.get_language() |
70 |
70 |
|
71 |
71 |
post_links = [] |
72 |
72 |
for post in posts: |
73 |
73 |
blog_file = post.text_file() |
74 |
74 |
blog_text = org_to_html(blog_file.name) |
75 |
75 |
# TODO: The link can possibly be reversed in the DTL using the title, which is actually |
76 |
76 |
# a cleaner way to do it. Investigate. |
77 |
77 |
link = reverse("blog-post-language", args=[language, post.slug()]) |
78 |
78 |
post_links.append([post.title(), post.published, blog_text, link]) |
79 |
79 |
|
80 |
80 |
context = { |
81 |
81 |
'posts': post_links, |
82 |
82 |
'materialDesign_color': "brown", |
83 |
83 |
'materialDesign_accentColor': "blue", |
84 |
84 |
'navbar_title': _("Notepad from a student"), |
85 |
85 |
'navbar_backArrow': True, |
86 |
86 |
'footer_links': footer_links, |
87 |
87 |
'footer_description': footer_description, |
88 |
88 |
} |
89 |
89 |
if not request.session.get("feed-fab-introduction-seen", default=False): |
90 |
90 |
context['introduce_feed'] = True |
91 |
91 |
request.session['feed-fab-introduction-seen'] = True |
92 |
92 |
return render(request, template, context) |
93 |
93 |
|
94 |
94 |
def post(request, post_slug, language=None): |
95 |
95 |
if request.method == "POST": # Handling a reply if one is sent |
96 |
96 |
form = CommentForm(request.POST) |
97 |
97 |
if form.is_valid(): |
98 |
98 |
new_comment = form.save(commit=False) |
99 |
99 |
for post in posts: |
100 |
100 |
if post.slug(language) == post_slug: |
101 |
101 |
new_comment.post = post |
102 |
102 |
new_comment.save() |
103 |
103 |
|
104 |
104 |
|
105 |
105 |
|
106 |
106 |
if language is not None: |
107 |
107 |
if translation.check_for_language(language): |
108 |
108 |
translation.activate(language) |
109 |
109 |
request.session[translation.LANGUAGE_SESSION_KEY] = language |
110 |
110 |
#return post(request, post_slug) |
111 |
111 |
else: |
112 |
112 |
language = translation.get_language() |
113 |
113 |
|
114 |
114 |
template = "blog/post.html" |
115 |
115 |
posts = Post.objects.all() |
116 |
116 |
#comments = Comment.objects.filter(post |
117 |
117 |
for post in posts: |
118 |
118 |
if post.slug(language) == post_slug: |
119 |
119 |
comments = Comment.objects.filter(post=post) |
120 |
120 |
form = CommentForm() |
121 |
121 |
post_file = post.text_file(language) |
122 |
122 |
post_text = org_to_html(post_file.name) |
123 |
123 |
context = { |
124 |
124 |
'comments': comments, |
125 |
125 |
'form' : form, |
126 |
126 |
'human_post_title': post.title(language), |
127 |
127 |
'materialDesign_color': "brown", |
128 |
128 |
'materialDesign_accentColor': "blue", |
129 |
129 |
'article': post_text, |
130 |
130 |
'title': post.title(language), |
131 |
131 |
'navbar_title': post.title(language), |
132 |
132 |
'navbar_backArrow': False, |
133 |
133 |
'post_slug': post_slug, |
134 |
134 |
'footer_links': footer_links, |
135 |
135 |
'footer_description': footer_description, |
136 |
136 |
} |
137 |
137 |
|
138 |
138 |
# Getting all available article links |
139 |
139 |
available = get_available_post_languages(post) |
140 |
140 |
if ENGLISH in available: |
141 |
141 |
context['english_link'] = reverse("blog-post-language", args=[ENGLISH, post.slug(ENGLISH)]) |
142 |
142 |
if DUTCH in available: |
143 |
143 |
context['dutch_link'] = reverse("blog-post-language", args=[DUTCH, post.slug(DUTCH)]) |
144 |
144 |
|
145 |
145 |
if FRENCH in available: |
146 |
146 |
context['french_link'] = reverse("blog-post-language", args=[FRENCH, post.slug(FRENCH)]) |
147 |
147 |
|
148 |
148 |
if SPANISH in available: |
149 |
149 |
context['spanish_link'] = reverse("blog-post-language", args=[SPANISH, post.slug(SPANISH)]) |
150 |
150 |
|
151 |
151 |
if GERMAN in available: |
152 |
152 |
context['german_link'] = reverse("blog-post-language", args=[GERMAN, post.slug(GERMAN)]) |
153 |
153 |
|
154 |
154 |
return render(request, template, context) |
155 |
155 |
|
156 |
156 |
def rss(request): |
157 |
157 |
template = "blog/feed.rss" |
158 |
158 |
context = { |
159 |
159 |
'items': FeedItem.objects.all(), |
160 |
160 |
} |
161 |
161 |
return render(request, template, context) |
162 |
162 |
|
163 |
163 |
|
+ |
164 |
def weekly_archive(request): |
+ |
165 |
template = "blog/weekly_archive.html" |
+ |
166 |
language = translation.get_language() |
+ |
167 |
|
+ |
168 |
file_2017 = org_to_html("blog/weekly/2017.org") |
+ |
169 |
file_2018 = org_to_html("blog/weekly/2018.org") |
+ |
170 |
file_2019 = org_to_html("blog/weekly/2019.org") |
+ |
171 |
|
+ |
172 |
|
+ |
173 |
|
+ |
174 |
context = { |
+ |
175 |
't2017': file_2017, |
+ |
176 |
't2018': file_2018, |
+ |
177 |
't2019': file_2019, |
+ |
178 |
'materialDesign_color': "brown", |
+ |
179 |
'materialDesign_accentColor': "blue", |
+ |
180 |
'navbar_title': _("Weekly-archief"), |
+ |
181 |
'navbar_backArrow': True, |
+ |
182 |
'footer_links': footer_links, |
+ |
183 |
'footer_description': footer_description, |
+ |
184 |
} |
+ |
185 |
return render(request, template, context) |
+ |
186 |