views.py
1 |
|
2 |
import requests |
3 |
|
4 |
|
5 |
from django.utils.translation import gettext as _ |
6 |
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. |
7 |
from django.http import HttpResponseRedirect, HttpResponse |
8 |
from django.urls import reverse |
9 |
from django.template import loader # This allows to actually load the template. |
10 |
|
11 |
from django.conf import settings # For the archive 20XX.org files |
12 |
|
13 |
from .models import * |
14 |
from .forms import CommentForm |
15 |
from django.core.exceptions import ObjectDoesNotExist |
16 |
from django.utils import translation |
17 |
|
18 |
GERMAN = "de" |
19 |
SPANISH = "es" |
20 |
FRENCH = "fr" |
21 |
DUTCH = "nl" |
22 |
ENGLISH = "en" |
23 |
|
24 |
def index(request): |
25 |
template = "blog/index.djhtml" |
26 |
posts = Post.objects.exclude(visible=False) |
27 |
|
28 |
|
29 |
context = { |
30 |
'posts': posts, |
31 |
'navbar_title': _("Notepad from a student"), |
32 |
'navbar_backArrow': True, |
33 |
'stylesheet_name': "blog", |
34 |
} |
35 |
return render(request, template, context) |
36 |
|
37 |
def post(request, language_code, post_slug): |
38 |
context = dict() |
39 |
if request.method == "POST": # Handling a reply if one is sent |
40 |
form = CommentForm(request.POST) |
41 |
|
42 |
form.post = Post.objects.get(id=request.POST['post']) |
43 |
if form.is_valid(): |
44 |
new_comment = form.save(commit=False) |
45 |
if request.POST['reaction_to'] != "": |
46 |
new_comment.reaction_to = Comment.objects.get(id=request.POST['reaction_to']) |
47 |
new_comment.save() |
48 |
context["comment_response"] = _("Comment succesfully submitted!") |
49 |
else: |
50 |
print("ERROR") |
51 |
print(form.errors) |
52 |
context["contact_response"] = _("An error occured while trying to submit your comment. Please try again later.") |
53 |
|
54 |
template = "blog/post.djhtml" |
55 |
article = Article.objects.get(slug=post_slug, language_code=language_code) |
56 |
root_comments = Comment.objects.filter(post=article.post, reaction_to=None).order_by('-date') |
57 |
context = context | { |
58 |
'djhtml_file': "/tmp/blog-file-"+article.slug+".djhtml", |
59 |
'article': article, |
60 |
'root_comments': root_comments, |
61 |
'title': article.post.title(), |
62 |
'navbar_title': article.post.title(), |
63 |
'navbar_backArrow': False, |
64 |
'stylesheet_name': "blog"} |
65 |
|
66 |
return render(request, template, context) |
67 |
|
68 |
def rss(request): |
69 |
template = "blog/feed.rss" |
70 |
context = { |
71 |
'items': FeedItem.objects.all(), |
72 |
} |
73 |
return render(request, template, context, content_type="application/rss+xml") |
74 |
|
75 |
|
76 |
def archive(request): |
77 |
template = "blog/monthly_archive.djhtml" |
78 |
language = translation.get_language() |
79 |
|
80 |
file_2017 = org_to_html(settings.MEDIA_ROOT + "blog/weekly/2017.org", slug="arkivo-sizsig-2017") |
81 |
file_2018 = org_to_html(settings.MEDIA_ROOT + "blog/weekly/2018.org", slug="arkivo-sizsig-2018") |
82 |
file_2019 = org_to_html(settings.MEDIA_ROOT + "blog/weekly/2019.org", slug="arkivo-sizsig-2019") |
83 |
file_2020 = org_to_html(settings.MEDIA_ROOT + "blog/weekly/2020.org", slug="arkivo-sizsig-2020") |
84 |
file_2021 = org_to_html(settings.MEDIA_ROOT + "blog/weekly/2021.org", slug="arkivo-sizsig-2021") |
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
context = { |
91 |
't2017': file_2017, |
92 |
't2018': file_2018, |
93 |
't2019': file_2019, |
94 |
't2020': file_2020, |
95 |
't2021': file_2021, |
96 |
'navbar_title': _("Weekly-archief"), |
97 |
'navbar_backArrow': True, |
98 |
'stylesheet_name': "blog", |
99 |
} |
100 |
return render(request, template, context) |
101 |