Mercurial > hgbook
annotate web/hgbook/comments/views.py @ 673:ad304b606163
Initial cut at web comment system import
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Tue, 10 Mar 2009 21:42:19 -0700 |
parents | |
children | 61a3f7dacf2f |
rev | line source |
---|---|
673
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
1 import django.newforms as forms |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
2 from django.db import connection |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
3 from django.http import HttpResponse |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
4 from hgbook.comments.models import Comment, Element |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
5 from django.shortcuts import get_object_or_404, render_to_response |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
6 from django.template import Context |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
7 from django.template.loader import get_template |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
8 from django.utils.simplejson import dumps |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
9 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
10 def dump_queries(): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
11 # requires settings.DEBUG to be set to True in order to work |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
12 if len(connection.queries) == 1: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
13 print connection.queries |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
14 else: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
15 qs = {} |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
16 for q in connection.queries: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 qs[q['sql']] = qs.setdefault(q['sql'], 0) + 1 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
18 for q in sorted(qs.items(), key=lambda x: x[1], reverse=True): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
19 print q |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
20 print len(connection.queries) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
21 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
22 class CommentForm(forms.Form): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
23 id = forms.CharField(widget=forms.HiddenInput) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
24 name = forms.CharField(max_length=64) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
25 url = forms.URLField(max_length=128, required=False) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
26 comment = forms.CharField(widget=forms.Textarea(attrs={ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
27 'rows': 8, 'cols': 60 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
28 })) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
29 remember = forms.BooleanField(initial=True, required=False) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
30 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
31 def comments_by_chapter(id): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
32 objs = {} |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
33 for c in Comment.objects.filter(element__chapter=id, hidden=False).order_by('date'): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
34 objs.setdefault(c.element_id, []).append(c) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
35 return objs |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
36 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
37 def chapter(request, id): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
38 template = get_template('comment.html') |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
39 resp = {} |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
40 for elt, comments in comments_by_chapter(id).iteritems(): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
41 form = CommentForm(initial={ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
42 'id': elt, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
43 'name': request.session.get('name', ''), |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
44 }) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
45 resp[elt] = template.render(Context({ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
46 'id': elt, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
47 'form': form, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
48 'length': len(comments), |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
49 'query': comments, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
50 })) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
51 return HttpResponse(dumps(resp), mimetype='application/json') |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
52 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
53 def chapter_count(request, id): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
54 resp = comments_by_chapter(id) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
55 for elt, comments in resp.iteritems(): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
56 resp[elt] = len(comments) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
57 return HttpResponse(dumps(resp), mimetype='application/json') |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
58 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
59 def single(request, id, form=None, newid=None): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
60 queryset = Comment.objects.filter(element=id, hidden=False).order_by('date') |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
61 if form is None: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
62 form = CommentForm(initial={ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
63 'id': id, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
64 'name': request.session.get('name', ''), |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
65 }) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
66 try: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
67 error = form.errors[0] |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
68 except: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
69 error = '' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
70 return render_to_response('comment.html', { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
71 'id': id, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
72 'form': form, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
73 'length': len(queryset), |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
74 'query': queryset, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
75 'newid': newid or True, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
76 'error': error, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
77 }) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
78 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
79 def submit(request, id): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
80 element = get_object_or_404(Element, id=id) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
81 form = None |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
82 newid = None |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
83 if request.method == 'POST': |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
84 form = CommentForm(request.POST) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
85 if form.is_valid(): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
86 data = form.cleaned_data |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
87 if data.get('remember'): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
88 request.session['name'] = data['name'] |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
89 request.session['url'] = data['url'] |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
90 else: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
91 request.session.pop('name', None) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
92 request.session.pop('url', None) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
93 c = Comment(element=element, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
94 comment=data['comment'], |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
95 submitter_name=data['name'], |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
96 submitter_url=data['url'], |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
97 ip=request.META.get('REMOTE_ADDR')) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
98 c.save() |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
99 newid = c.id |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
100 form = None |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
101 return single(request, id, form, newid) |