-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathviews.py
139 lines (115 loc) · 4.48 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
"""
Views.py
Author: Will Larson
Contact: lethain@gmail.com
Contains one custom view for displaying articles.
Mostly necessary to presort the articles in order
of descending size.
"""
import datetime, time, random, cgi, md5
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.http import Http404, HttpResponseRedirect
from django.conf import settings
from django.core.paginator import QuerySetPaginator
from lifeflow.models import Series, Flow, Entry, Comment
from lifeflow.forms import CommentForm
from django.core.cache import cache
from django.http import HttpRequest
from django.utils.cache import get_cache_key
def expire_page(path):
'http://www.djangosnippets.org/snippets/936/'
request = HttpRequest()
request.path = path
key = get_cache_key(request)
if cache.has_key(key):
cache.delete(key)
def server_error(request):
return render_to_response('500.html',{},RequestContext(request,{}))
def articles(request):
object_list = Series.objects.all()
return render_to_response('lifeflow/articles.html', {'object_list' : object_list},RequestContext(request, {}))
def comments(request, entry_id=None, parent_id=None):
def make_identifier(id, time):
secret = getattr(settings, 'SECRET_KEY')
time = time[:-4]
data = "%s%s%s%s" % ("lifeflow", id, time, secret)
return md5.md5(data).hexdigest()
# if an entry ID has been posted, use that
if request.POST.has_key('entry_id'):
id = int(request.POST['entry_id'])
# otherwise use the parameter
elif entry_id is None:
return render_to_response('lifeflow/invalid_comment.html',{},RequestContext(request, {}))
else:
id = int(entry_id)
# TODO: validate ID, throw 500 otherwise
entry = Entry.objects.get(pk=id)
if request.POST.has_key('parent_id') and request.POST['parent_id'] != u"":
parent_id = int(request.POST['parent_id'])
parent = Comment.objects.get(pk=parent_id)
elif parent_id is None:
parent = None
else:
parent_id = int(parent_id)
parent = Comment.objects.get(pk=parent_id)
# add an identifier to the post, part of the
# anti-spam implementation
if request.POST.has_key('identifier') is False:
now = unicode(time.time()).split('.')[0]
identifier = make_identifier(id, now)
# or make a new identifier
else:
identifier = request.POST['identifier']
now = request.POST['time']
form = CommentForm(request.POST)
form.is_valid()
# Initial submission from entry_detail.html
if request.POST.has_key('submit'):
for i in xrange(5,8):
name = u"honey%s" % i
value = request.POST[name]
if value != u"":
raise Http404
if time.time() - int(now) > 3600:
raise Http404
if identifier != make_identifier(id, now):
raise Http404
name = form.cleaned_data['name']
email = form.cleaned_data['email']
webpage = form.cleaned_data['webpage']
html = form.cleaned_data['html']
body = form.cleaned_data['body']
c = Comment(entry=entry,parent=parent,name=name,email=email,
webpage=webpage,body=body,html=html)
c.save()
url = u"%s#comment_%s" % (entry.get_absolute_url(), c.pk)
expire_page(entry.get_absolute_url())
return HttpResponseRedirect(url)
return render_to_response(
'lifeflow/comment.html',
{'object':entry,'parent':parent,'identifier':identifier,'time':now,'form':form},
RequestContext(request, {}))
def flow(request, slug):
try:
flow = Flow.objects.get(slug=slug)
except Flow.DoesNotExist:
raise Http404
try:
page = int(request.GET["page"])
except:
page = 1
page = QuerySetPaginator(Flow.objects.get(slug=slug).latest(), 5).page(page)
return render_to_response('lifeflow/flow_detail.html',
{'object' : flow, 'page' : page,},
RequestContext(request, {}))
def front(request):
try:
page = int(request.GET["page"])
except:
page = 1
page = QuerySetPaginator(Entry.current.all(), 5).page(page)
return render_to_response('lifeflow/front.html', {'page':page}, RequestContext(request, {}))
def rss(request):
flows = Flow.objects.all()
return render_to_response('lifeflow/meta_rss.html', {'flows' : flows }, RequestContext(request, {}))