-
Notifications
You must be signed in to change notification settings - Fork 62
/
views.py
executable file
·364 lines (309 loc) · 13.6 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.template import RequestContext
from django.core.paginator import Paginator
from django.core.urlresolvers import reverse
from django.views.generic.date_based import archive_month
from django.http import Http404
from django.core.exceptions import ObjectDoesNotExist
from blogango.models import Blog, BlogEntry, Comment, BlogRoll, Reaction
from blogango import forms as bforms
from blogango.conf.settings import AKISMET_COMMENT, AKISMET_API_KEY
def welcome(request):
return render_to_response('mainpage.html', {})
def handle404(view_function):
def wrapper(*args, **kwargs):
try:
return view_function(*args, **kwargs)
except ObjectDoesNotExist:
raise Http404
return wrapper
def index(request, page = 1):
if not _is_blog_installed():
return HttpResponseRedirect(reverse('blogango_install'))
page = int(page)
blog = Blog.objects.all()[0]
entries = BlogEntry.objects.filter(is_page=False, is_published=True).order_by('-created_on')
paginator = Paginator(entries, blog.entries_per_page)
page_ = paginator.page(page)
entries = page_.object_list
has_next = page_.has_next()
has_prev = page_.has_previous()
next = page + 1
prev = page - 1
payload = locals()
return render('blogango/mainpage.html', request, payload)
def check_comment_spam(request, comment):
from blogango.akismet import Akismet, APIKeyError
api = Akismet(AKISMET_API_KEY, 'http://%s' % (request.get_host()), request.META['HTTP_USER_AGENT'])
if api.verify_key():
akismet_data = {'user_ip': request.META['REMOTE_ADDR'],
'user_agent': request.META['HTTP_USER_AGENT'],
'comment_author': comment.user_name,
'comment_author_email': comment.email_id,
'comment_author_url': comment.user_url,
'comment_type': 'comment'}
return api.comment_check(comment.text, akismet_data)
raise APIKeyError("Akismet API key is invalid.")
@handle404
def details(request, year, month, slug):
if not _is_blog_installed():
return HttpResponseRedirect(reverse('blogango_install'))
entry = BlogEntry.objects.get(created_on__year=year,
created_on__month=month,
slug=slug,
is_published=True)
if not entry.is_published:
raise Http404
if request.method == 'POST':
comment_f = bforms.CommentForm(request.POST)
if comment_f.is_valid():
comment_by = request.user if request.user.is_authenticated() else None
comment = Comment(text=comment_f.cleaned_data['text'],
created_by=comment_by,
comment_for=entry,
user_name=comment_f.cleaned_data['name'],
user_url=comment_f.cleaned_data['url'],
email_id=comment_f.cleaned_data['email'])
if AKISMET_COMMENT:
comment.is_spam = check_comment_spam(request, comment)
comment.save()
return HttpResponseRedirect('.')
else:
comment_f = bforms.CommentForm()
comments = Comment.objects.filter(comment_for=entry, is_spam=False)
reactions = Reaction.objects.filter(comment_for=entry)
# tags = Tag.objects.filter(tag_for=entry)
payload = {'entry': entry, 'comments': comments, 'reactions': reactions, 'comment_form': comment_f}
return render('blogango/details.html', request, payload)
@handle404
def comment_details(request, comment_id):
comment = Comment.objects.get(id=comment_id)
payload = locals()
return render('blogango/comment.html', request, payload)
def tag_details(request, tag_slug):
from taggit.models import Tag
if Tag.objects.filter(slug=tag_slug).count() == 0:
raise Http404
tag = Tag.objects.get(slug=tag_slug)
entries = BlogEntry.objects.filter(is_published=True, tags__in=[tag])
feed_url = getattr(settings, 'FEED_URL', reverse('blogango_feed', args=['tag']) + tag.slug + '/')
payload = {'tag': tag, 'entries': entries, 'feed_url': feed_url}
return render('blogango/tag_details.html', request, payload)
@login_required
def create_entry(request):
if request.method == 'GET':
create = bforms.EntryForm()
elif request.method == 'POST':
create = bforms.EntryForm(request.POST)
if create.is_valid():
if request.POST.has_key('save'):
publish = False
elif request.POST.has_key('post'):
publish = True
entry = BlogEntry(created_by=request.user,
text=create.cleaned_data['text'],
title=create.cleaned_data['title'],
slug=create.cleaned_data['slug'],
is_page=create.cleaned_data['is_page'],
is_published=publish,
is_rte=create.cleaned_data['is_rte'])
entry.save()
tags = create.cleaned_data['tags']
tag_list = tags.split()
for tag in tag_list:
tag_, created = Tag.objects.get_or_create(tag_txt=tag.strip())
tag_.save()
entry.tag_set.add(tag_)
if request.POST.has_key('save'):
return HttpResponseRedirect('.')
elif request.POST.has_key('post'):
return HttpResponseRedirect(entry.get_absolute_url())
payload = {'create_form': create,}
return render('blogango/create.html', request, payload)
@login_required
def edit_entry(request, entry_id):
if request.method == 'GET':
entry = BlogEntry.objects.filter(id=entry_id).values()[0]
entry_ = BlogEntry.objects.get(id=entry_id)
tags = Tag.objects.filter(tag_for=entry_)
tags = [tag.tag_txt for tag in tags]
tag_ = " ".join(tags)
entry['tags'] = tag_
create = bforms.EntryForm(entry)
elif request.method == 'POST':
create = bforms.EntryForm(request.POST)
if create.is_valid():
entry = BlogEntry.objects.get(id=entry_id)
entry.text = create.cleaned_data['text']
entry.title = create.cleaned_data['title']
entry.slug = create.cleaned_data['slug']
entry.is_page = create.cleaned_data['is_page']
entry.comments_allowed = create.cleaned_data['comments_allowed']
if request.POST.has_key('save'):
publish = False
elif request.POST.has_key('post'):
publish = True
entry.is_published = publish
entry.save()
tags = Tag.objects.filter(tag_for=entry)
for tag in tags:
entry.tag_set.remove(tag)
tags_data = create.cleaned_data['tags']
tag_list = tags_data.split(' ')
for tag in tag_list:
tag_, created = Tag.objects.get_or_create(tag_txt=tag.strip())
tag_.save()
entry.tag_set.add(tag_)
return HttpResponseRedirect(entry.get_absolute_url())
payload = {'create_form': create,}
return render('blogango/create.html', request, payload)
@login_required
def mod_entries(request):
if request.method == 'GET':
entries = BlogEntry.objects.all()
payload = locals()
return render('blogango/manage_entries.html', request, payload)
if request.method == 'POST':
if request.POST.has_key("unpublish"):
entry_ids = request.POST['entries']
entries = BlogEntry.objects.filter(id__in=entry_ids)
for entry in entries:
entry.is_published = False
entry.save()
elif request.POST.has_key("del"):
entry_ids = request.POST['entries']
# print request.POST
# print entry_ids
BlogEntry.objects.filter(id__in=entry_ids).delete()
return HttpResponseRedirect('.')
@login_required
def moderate_comments(request):
if request.method == 'GET':
comments = Comment.objects.filter()
payload = {"comments": comments}
return render('blogango/mod_comment.html', request, payload)
elif request.method == 'POST':
if request.POST.has_key('spam'):
spammeds = request.POST['spam']
else:
spammeds = {}
for spammed in spammeds:
comment = Comment.objects.get(id = spammed)
comment.is_spam = True
comment.save()
if request.POST.has_key('delete'):
deleteds = request.POST['delete']
else:
deleteds = {}
for deleted in deleteds:
Comment.objects.get(id = deleted).delete()
return HttpResponseRedirect('.')
@login_required
def install_blog(request):
if _is_blog_installed():
return HttpResponseRedirect(reverse('blogango_index'))
if request.method == 'GET':
install_form = bforms.InstallForm()
if request.method == 'POST':
install_form = bforms.InstallForm(request.POST)
if install_form.is_valid():
install_form.save()
return HttpResponseRedirect(reverse('blogango_index'))
payload = {"install_form": install_form}
return render('blogango/install.html', request, payload)
@login_required
def create_blogroll(request):
if request.method == 'GET':
blogroll_form = bforms.BlogForm()
if request.method == 'POST':
blogroll_form = bforms.BlogForm(request.POST)
if blogroll_form.is_valid():
blogroll_form.save()
return HttpResponseRedirect('.')
payload = {"blogroll_form": blogroll_form}
return render('blogango/blogroll.html', request, payload)
@login_required
def edit_preferences(request):
if request.method == 'GET':
prefs_form = bforms.PreferencesForm(Blog.objects.all().values()[0])
if request.method == 'POST':
prefs_form = bforms.PreferencesForm(request.POST)
if prefs_form.is_valid():
blog = Blog.objects.all()[0]
# print blog.id
blog.entries_per_page = prefs_form.cleaned_data['entries_per_page']
blog.recents = prefs_form.cleaned_data['recents']
blog.recent_comments = prefs_form.cleaned_data['recents']
blog.save()
return HttpResponseRedirect('.')
payload = {"install_form": prefs_form}
return render('blogango/edit_preferences.html', request, payload)
@login_required
def manage(request):
return render('blogango/manage.html', request, {})
def author(request, username):
author = get_object_or_404(User, username=username)
return render('blogango/author.html', request, {'author': author})
def monthly_view(request, year, month):
# print year, month
queryset = BlogEntry.objects.filter(is_page=False, is_published=True)
return archive_month(request=request,
template_name='blogango/archive_view.html',
year=year,
month=month,
queryset=queryset,
date_field='created_on',
extra_context=_get_sidebar_objects(request))
#Helper methods.
def _is_blog_installed():
if Blog.objects.count() == 0:
return False
return True
def render (template, request, payload):
"""Wrapper on render_to_response.
Adds sidebar objects. Adds RequestContext"""
payload.update(_get_sidebar_objects(request))
return render_to_response(template, payload, context_instance=RequestContext(request),)
def _get_sidebar_objects (request):
"""Gets the objects which are always displayed in the sidebar"""
try:
blog = Blog.objects.all()[0]
except:
return {}
recents = BlogEntry.objects.filter(is_page = False, is_published = True).order_by('-created_on')[:blog.recents]
blogroll = BlogRoll.objects.filter(is_published=True)
# pages = BlogEntry.objects.filter(is_page = True, is_published = True)
# recent_comments = Comment.objects.all().order_by('-created_on')[:blog.recent_comments]
# date_list = _get_archive_months()
return {'blog':blog,
'recents':recents,
'blogroll':blogroll,
'canonical_url': request.build_absolute_uri(),
'pingback_xmlrpc_url': request.build_absolute_uri(reverse('xmlrpc')),}
def _get_archive_months():
"""Get the months for which at least one entry exists"""
dates = BlogEntry.objects.dates('created_on', 'month', order='DESC')
# print dates
# date_list = []
# for date in dates:
# date_list.append((date.strftime('%Y/%b'), date.strftime('%B %y')))
return dates
def _generic_form_display(request, form_class):
if request.method == 'GET':
form_inst = form_class()
if request.method == 'POST':
form_inst = form_class(request.POST)
if form_inst.is_valid():
form_inst.save()
return HttpResponseRedirect('.')
payload = {"install_form": form_inst}
return render('blogango/install.html', request, payload)
def generic(request): # A generic form processor.
if request.method == 'GET':
pass
if request.method == 'POST':
pass