This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
views.py
136 lines (110 loc) Β· 3.78 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
import datetime
from annoying.functions import get_object_or_None
from django.shortcuts import get_object_or_404, render
from snipts.models import Snipt
THEME_CHOICES = {"D": "blogs/themes/default/", "A": "blogs/themes/pro-adams/"}
def blog_list(request, username_or_custom_slug=None):
if username_or_custom_slug:
return blog_post(request, username_or_custom_slug)
snipts = (
Snipt.objects.filter(
user=request.blog_user,
blog_post=True,
public=True,
publish_date__lte=datetime.datetime.now(),
)
.order_by("-publish_date")
.exclude(title__iexact="Homepage")
.exclude(title__iexact="Work")
)
normal_snipts = Snipt.objects.filter(
blog_post=False, user=request.blog_user, public=True
).order_by("-created")
normal_snipts = normal_snipts.exclude(title__in=[""])
normal_snipts = normal_snipts.exclude(tags__name__in=["tmp"])
normal_snipts = normal_snipts[:3]
sidebar = get_object_or_None(
Snipt, user=request.blog_user, title="Sidebar", blog_post=True
)
header = get_object_or_None(
Snipt, user=request.blog_user, title="Header", blog_post=True
)
custom_css = get_object_or_None(
Snipt, user=request.blog_user, title="CSS", lexer="css", blog_post=True
)
context = {
"blog_user": request.blog_user,
"custom_css": custom_css,
"has_snipts": True,
"header": header,
"normal_snipts": normal_snipts,
"public": True,
"sidebar": sidebar,
"snipts": snipts,
}
if "rss" in request.GET:
context["snipts"] = context["snipts"][:20]
return rss(request, context)
template = THEME_CHOICES[request.blog_user.profile.blog_theme]
template = "{}/list.html".format(template)
return render(request, template, context)
def blog_post(request, username_or_custom_slug):
snipt = get_object_or_404(
Snipt,
user=request.blog_user,
blog_post=True,
public=True,
publish_date__lte=datetime.datetime.now(),
slug=username_or_custom_slug,
)
snipts = (
Snipt.objects.filter(
user=request.blog_user,
blog_post=True,
public=True,
publish_date__lte=datetime.datetime.now(),
)
.order_by("-publish_date")
.exclude(title__iexact="Homepage")
.exclude(title__iexact="Work")
)
sidebar = get_object_or_None(
Snipt, user=request.blog_user, title="Sidebar", blog_post=True
)
header = get_object_or_None(
Snipt, user=request.blog_user, title="Header", blog_post=True
)
custom_css = get_object_or_None(
Snipt, user=request.blog_user, title="CSS", lexer="css", blog_post=True
)
normal_snipts = Snipt.objects.filter(
blog_post=False, user=request.blog_user, public=True
).order_by("-created")
normal_snipts = normal_snipts.exclude(title__in=[""])
normal_snipts = normal_snipts.exclude(tags__name__in=["tmp"])
normal_snipts = normal_snipts[:3]
if snipt.user != request.user:
snipt.views = snipt.views + 1
snipt.save()
context = {
"blog_user": request.blog_user,
"custom_css": custom_css,
"detail": True,
"has_snipts": True,
"header": header,
"normal_snipts": normal_snipts,
"public": True,
"sidebar": sidebar,
"snipt": snipt,
"snipts": snipts,
}
template = THEME_CHOICES[request.blog_user.profile.blog_theme]
template = "{}/post.html".format(template)
return render(request, template, context)
def rss(request, context):
return render(
request,
"blogs/themes/default/rss.xml",
context,
content_type="application/rss+xml",
)