Add featurediscovery to RSS FAB
- Author
- Maarten Vangeneugden
- Date
- April 9, 2018, 7:58 p.m.
- Hash
- 9ff64bdd36d325c58b51a1f8df40de78d89d7511
- Parent
- dc7275db17af203b70cb3d4ad30f0c651e348bf9
- Modified files
- templates/blog/index.html
- views.py
templates/blog/index.html ¶
12 additions and 4 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 |
{% endblock %} |
+ |
8 |
|
+ |
9 |
<!-- Compiled and minified JavaScript --> |
+ |
10 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script> |
+ |
11 |
{% endblock %} |
8 |
12 |
|
9 |
13 |
{% block title %}{% trans "Maarten's blog" %}{% endblock title %} |
10 |
14 |
|
11 |
15 |
{% block description %}{% blocktrans %}The always coherently put together, yet |
12 |
16 |
fuzzy blog of whatever sprouts in my mind.{% endblocktrans %} |
13 |
17 |
{% endblock description %} |
14 |
18 |
|
15 |
19 |
{% block main %} |
16 |
20 |
{% with color="brown" accent_color="yellow" %} |
17 |
21 |
<div class="section {{ color }} z-depth-3"> |
18 |
22 |
<div class="container"> |
19 |
23 |
<div class="white-text"> |
20 |
24 |
<h3>{% trans "Blog" %}</h3> |
21 |
25 |
<p> |
22 |
26 |
{% blocktrans %}Welcome to my blog. Here, I write |
23 |
27 |
about things that interest me. Politics, coding, |
24 |
28 |
studying, life, or anything else I fancy rambling |
25 |
29 |
about. If you're in luck, I may've written it in a |
26 |
30 |
language that you understand better than English. |
27 |
31 |
{% endblocktrans %} |
28 |
32 |
</p> |
29 |
33 |
</div> |
30 |
34 |
</div> |
31 |
35 |
</div> |
32 |
36 |
|
33 |
37 |
<div class="fixed-action-btn"> |
34 |
38 |
<a href="{% url 'blog-feed' %}" id="feed-fab" class="btn-floating waves-effect waves-light btn-large orange accent-4"> |
35 |
39 |
<i class="large material-icons">rss_feed</i> |
36 |
40 |
</a> |
37 |
41 |
</div> |
38 |
42 |
|
39 |
43 |
|
40 |
44 |
<div class="container row"> |
41 |
45 |
<div class="col s12 m6"> |
42 |
46 |
<h1 id="weekly" class="{{ color }}-text">Weekly</h1> |
43 |
47 |
{% include "blog/weekly.html" %} |
44 |
48 |
</div> |
45 |
49 |
<div class="container col s12 m6"> |
46 |
50 |
{% for title, date, blog_text, link in posts %} |
47 |
51 |
<h1 class="{{ color}}-text">{{ title }}</h2> |
48 |
52 |
<span class="grey-text">{{ date|date:"DATE_FORMAT" }}</span> |
49 |
53 |
<p class="hide-on-small-only">{{ blog_text|safe|truncatewords_html:100 }}</p> |
50 |
54 |
<a class="btn black-text waves-effect {{accent_color}} accent-4" href="{{link}}"> |
51 |
55 |
📚 {% trans "Read on"%} |
52 |
56 |
</a> |
53 |
57 |
<hr /> |
54 |
58 |
{% endfor %} |
55 |
59 |
</div> |
56 |
60 |
</div> |
57 |
61 |
|
58 |
62 |
{% if introduce_feed %} |
59 |
63 |
<div class="tap-target" data-target="feed-fab"> |
60 |
- | <div class="tap-target-content"> |
61 |
- | <h5>{% trans "Never miss an update again." %}</h5> |
+ |
64 |
<div class="white-text tap-target-content"> |
+ |
65 |
<h5>{% trans "Never miss an update again." %}</h5> |
62 |
66 |
<p>{% blocktrans %} |
63 |
67 |
Getting info on updates to blog posts, major website changes, and other important |
64 |
68 |
news items is now easy and convenient with RSS. |
65 |
69 |
{% endblocktrans %}</p> |
66 |
70 |
</div> |
67 |
71 |
</div> |
68 |
72 |
<script> |
69 |
73 |
var elem = document.querySelector('.tap-target'); |
70 |
- | var instance = M.TapTarget.init(elem, options); |
71 |
- | </script> |
+ |
74 |
console.log(elem); |
+ |
75 |
var instance = M.TapTarget.init(elem, null); |
+ |
76 |
console.log(instance); |
+ |
77 |
instance.open(); |
+ |
78 |
</script> |
72 |
79 |
{% endif %} |
73 |
80 |
|
+ |
81 |
|
74 |
82 |
{% endwith %} |
75 |
83 |
{% endblock main %} |
76 |
84 |
views.py ¶
1 addition 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 |
[_("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 |
if not request.session.get("feed-fab-introduction-seen", default=False): |
109 |
109 |
context['introduce_feed'] = True |
110 |
110 |
request.session['feed-fab-introduction-seen'] = True |
111 |
111 |
else: |
112 |
112 |
context['introduce_feed'] = False |
113 |
113 |
|
114 |
114 |
return render(request, template, context) |
+ |
115 |
return render(request, template, context) |
115 |
116 |
|
116 |
117 |
def post(request, post_slug, language=None): |
117 |
118 |
if request.method == "POST": # Handling a reply if one is sent |
118 |
119 |
form = CommentForm(request.POST) |
119 |
120 |
if form.is_valid(): |
120 |
121 |
form.save() |
121 |
122 |
|
122 |
123 |
if language is not None: |
123 |
124 |
if translation.check_for_language(language): |
124 |
125 |
translation.activate(language) |
125 |
126 |
request.session[translation.LANGUAGE_SESSION_KEY] = language |
126 |
127 |
#return post(request, post_slug) |
127 |
128 |
else: |
128 |
129 |
language = translation.get_language() |
129 |
130 |
|
130 |
131 |
template = "blog/post.html" |
131 |
132 |
posts = Post.objects.all() |
132 |
133 |
#comments = Comment.objects.filter(post |
133 |
134 |
for post in posts: |
134 |
135 |
if post.slug(language) == post_slug: |
135 |
136 |
comments = Comment.objects.filter(post=post) |
136 |
137 |
form = CommentForm(initial={'post': post}) |
137 |
138 |
blog_file = get_preferred_post_language(post, language) |
138 |
139 |
blog_text = org_to_html(blog_file.name) |
139 |
140 |
context = { |
140 |
141 |
'comments': comments, |
141 |
142 |
'form' : form, |
142 |
143 |
'human_post_title': blog_file.name.replace("_"," "), |
143 |
144 |
'materialDesign_color': "brown", |
144 |
145 |
'materialDesign_accentColor': "yellow", |
145 |
146 |
'article': blog_text, |
146 |
147 |
'title': str(blog_file), |
147 |
148 |
'navbar_title': ((blog_file.name.rpartition("/")[2]).rpartition(".")[0]).replace("_"," "), |
148 |
149 |
'navbar_backArrow': False, |
149 |
150 |
'post_slug': post_slug, |
150 |
151 |
'footer_links': footer_links, |
151 |
152 |
'footer_description': footer_description, |
152 |
153 |
} |
153 |
154 |
|
154 |
155 |
# Getting all available article links |
155 |
156 |
available = get_available_post_languages(post) |
156 |
157 |
if ENGLISH in available: |
157 |
158 |
context['english_link'] = reverse("blog-post-language", args=[ENGLISH, post.slug(ENGLISH)]) |
158 |
159 |
if DUTCH in available: |
159 |
160 |
context['dutch_link'] = reverse("blog-post-language", args=[DUTCH, post.slug(DUTCH)]) |
160 |
161 |
|
161 |
162 |
if FRENCH in available: |
162 |
163 |
context['french_link'] = reverse("blog-post-language", args=[FRENCH, post.slug(FRENCH)]) |
163 |
164 |
|
164 |
165 |
if SPANISH in available: |
165 |
166 |
context['spanish_link'] = reverse("blog-post-language", args=[SPANISH, post.slug(SPANISH)]) |
166 |
167 |
|
167 |
168 |
if GERMAN in available: |
168 |
169 |
context['german_link'] = reverse("blog-post-language", args=[GERMAN, post.slug(GERMAN)]) |
169 |
170 |
|
170 |
171 |
return render(request, template, context) |
171 |
172 |
|
172 |
173 |
def rss(request): |
173 |
174 |
template = "blog/feed.rss" |
174 |
175 |
context = { |
175 |
176 |
'items': FeedItem.objects.all(), |
176 |
177 |
} |
177 |
178 |
return render(request, template, context) |
178 |
179 |