-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathviews.py
178 lines (147 loc) · 5.43 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from datetime import datetime
from django.db.models import F
from django.http import Http404, HttpResponseForbidden
from django.shortcuts import render, redirect, get_object_or_404
from django.utils.translation import gettext_lazy as _, get_language
from comments.models import Comment
from posts.forms import PostEditForm
from posts.models import Post
from posts.renderers import render_list, render_list_all, render_post
from vas3k_blog.posts import INDEX_PAGE_BEST_POSTS, POST_TYPES
def index(request):
# select top post
top_post = Post.visible_objects()\
.filter(is_visible_on_home_page=True)\
.order_by("-published_at")\
.first()
# latest posts
latest_posts = Post.visible_objects()\
.filter(type__in=["blog", "world"], is_visible_on_home_page=True)\
.exclude(id=top_post.id if top_post else None)\
.order_by("-published_at")[:6]
# travel posts
latest_world_posts = Post.visible_objects()\
.filter(type="world", is_visible_on_home_page=True)\
.exclude(id__in=[post.id for post in latest_posts] if latest_posts else [])\
.order_by("-published_at")[:3]
top_world_posts = Post.visible_objects()\
.filter(type="world", is_visible_on_home_page=True)\
.exclude(id__in=[
top_post.id if top_post else None,
*[post.id for post in latest_posts],
*[post.id for post in latest_world_posts],
])\
.order_by("-view_count")[:5]
world_posts = list(latest_world_posts) + list(top_world_posts)
# featured posts
best_posts = Post.visible_objects()\
.filter(slug__in=INDEX_PAGE_BEST_POSTS)\
.order_by("-published_at")[:10]
# notes
notes_posts = Post.visible_objects()\
.filter(type="notes", is_visible_on_home_page=True)\
.order_by("-published_at")[:11]
return render(request, "index.html", {
"blocks": [
{
"template": "index/main.html",
"post": top_post
},
{
"title": "",
"template": "index/posts3.html",
"posts": latest_posts
},
{
"title": _("Обо мне"),
"template": "index/about.html",
"posts": []
},
{
"title": _("Заметки"),
"url": "/notes/",
"template": "index/posts4.html",
"posts": notes_posts
},
{
"title": _("Отвратительные путешествия"),
"template": "index/posts3.html",
"url": "/world/",
"posts": world_posts
} if get_language() == "ru" else {},
{
"title": _("Нетленки"),
"template": "index/posts2.html",
"posts": best_posts
} if get_language() == "ru" else {},
{
"title": _("Проекты"),
"template": "index/projects.html",
"posts": []
} if get_language() == "ru" else {},
]
})
def list_posts(request, post_type="all"):
posts = Post.visible_objects().select_related()
if post_type and post_type != "all":
if post_type not in POST_TYPES:
raise Http404()
posts = posts.filter(type=post_type)
if not posts:
raise Http404()
return render_list(request, post_type, posts)
else:
return render_list_all(request, posts)
def show_post(request, post_type, post_slug):
post = get_object_or_404(Post, slug=post_slug, lang=get_language())
# post_type can be changed
if post.type != post_type:
return redirect("show_post", post.type, post.slug)
# post_type can be removed
if post_type not in POST_TYPES:
raise Http404()
# drafts are visible only with a flag
if not post.is_visible and not request.GET.get("preview"):
raise Http404()
Post.objects.filter(id=post.id)\
.update(view_count=F("view_count") + 1)
# don't show private posts into public
if post.is_members_only:
if not request.user.is_authenticated:
return render(request, "users/post_access_denied.html", {
"post": post
})
if post.url:
return redirect(post.url)
comments = Comment.visible_objects()\
.filter(post=post)\
.order_by("created_at")
translations = Post.objects.filter(
type=post.type,
slug=post.slug,
is_visible=True,
published_at__lte=datetime.utcnow(),
).exclude(
lang=get_language()
).order_by("lang")
return render_post(request, post, {
"post": post,
"comments": comments,
"translations": translations,
})
def edit_post(request, post_type, post_slug):
if not request.user.is_authenticated:
return redirect("login")
if not request.user.is_superuser:
return HttpResponseForbidden()
post = get_object_or_404(Post, type=post_type, slug=post_slug, lang=get_language())
if request.method == "POST":
form = PostEditForm(request.POST, instance=post)
if form.is_valid():
form.save()
return redirect("show_post", post_type=post.type, post_slug=post.slug)
else:
form = PostEditForm(instance=post)
return render(request, "posts/edit.html", {
"form": form,
})