author | amit |
Wed, 27 Oct 2010 13:59:11 +0530 | |
changeset 3 | de4a2ed2f34b |
parent 2 | f5e18f8ed036 |
permissions | -rw-r--r-- |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
1 |
# Create your views here. |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
2 |
from django.http import HttpResponse |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
3 |
from django.utils.simplejson import dumps |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
4 |
from django.contrib.csrf.middleware import csrf_exempt |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
5 |
import django.forms as forms |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
6 |
from django.db import connection |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
7 |
from django.http import HttpResponse,Http404 |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
8 |
from sphinxcomment.models import Comment, Element |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
9 |
from django.shortcuts import get_object_or_404, render_to_response |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
10 |
from django.template import Context |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
11 |
from django.template.loader import get_template |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
12 |
from django.utils.simplejson import dumps |
3 | 13 |
from django.conf import settings |
14 |
from os.path import join, splitext |
|
15 |
from BeautifulSoup import BeautifulSoup as bss, Tag |
|
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
16 |
|
3 | 17 |
#placeholder_string = open('paragraph_id.py').read() |
18 |
#exec placeholder_string |
|
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
19 |
|
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
20 |
def dump_queries(): |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
21 |
# requires settings.DEBUG to be set to True in order to work |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
22 |
if len(connection.queries) == 1: |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
23 |
print connection.queries |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
24 |
else: |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
25 |
qs = {} |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
26 |
for q in connection.queries: |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
27 |
qs[q['sql']] = qs.setdefault(q['sql'], 0) + 1 |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
28 |
for q in sorted(qs.items(), key=lambda x: x[1], reverse=True): |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
29 |
print q |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
30 |
print len(connection.queries) |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
31 |
|
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
32 |
class CommentForm(forms.Form): |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
33 |
name = forms.CharField(max_length=64) |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
34 |
url = forms.URLField(max_length=128, required=False) |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
35 |
comment = forms.CharField(widget=forms.Textarea(attrs={ |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
36 |
'rows': 8, 'cols': 60 |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
37 |
})) |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
38 |
remember = forms.BooleanField(initial=True, required=False) |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
39 |
|
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
40 |
def comments_by_chapter(chapter): |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
41 |
objs = {} |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
42 |
|
3 | 43 |
for c in Comment.objects.filter(element__chapter_name=chapter).order_by('date'): |
44 |
objs.setdefault(c.element.paragraph_id, []).append(c) |
|
45 |
||
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
46 |
|
3 | 47 |
|
48 |
||
49 |
return objs |
|
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
50 |
|
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
51 |
|
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
52 |
|
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
53 |
|
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
54 |
|
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
55 |
|
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
56 |
|
3 | 57 |
# def chapter(request): |
58 |
# template = get_template('comment.html') |
|
59 |
# resp = {} |
|
60 |
# for elt, comments in comments_by_chapter(chapter).iteritems(): |
|
61 |
# print elt ,comments |
|
62 |
# form = CommentForm(initial={ |
|
63 |
# 'paragraph_id': elt, |
|
64 |
# 'name': name |
|
65 |
# }) |
|
66 |
# resp[elt] = template.render(Context({ |
|
67 |
# 'paragraph_id': elt, |
|
68 |
# 'form': form, |
|
69 |
# 'length': len(comments), |
|
70 |
# 'query': comments, |
|
71 |
# })) |
|
72 |
# return HttpResponse(dumps(resp), mimetype='application/json') |
|
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
73 |
|
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
74 |
def chapter_count(request,chapter_name): |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
75 |
print chapter_name |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
76 |
chapter_name=chapter_name.split('.')[0] |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
77 |
comment_objs = comments_by_chapter(chapter_name) |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
78 |
resp={} |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
79 |
temp_dict={} |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
80 |
for elt, comments in comment_objs.iteritems(): |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
81 |
temp_dict[elt]=len(comments) |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
82 |
|
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
83 |
resp['count']=temp_dict |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
84 |
|
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
85 |
|
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
86 |
print resp |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
87 |
return HttpResponse(dumps(resp), mimetype='application/json') |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
88 |
|
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
89 |
def single(request,paragraph_id, form=None, newid=None): |
3 | 90 |
if paragraph_id[-1]=='/': |
91 |
paragraph_id=paragraph_id[:-1] |
|
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
92 |
queryset = Comment.objects.filter(element=paragraph_id) |
3 | 93 |
print len(queryset) |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
94 |
if form is None: |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
95 |
form = CommentForm(initial={ |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
96 |
'paragraph_id': paragraph_id, |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
97 |
'name': request.session.get('name', ''), |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
98 |
}) |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
99 |
try: |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
100 |
error = form.errors[0] |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
101 |
except: |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
102 |
error = '' |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
103 |
print form.errors |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
104 |
return render_to_response('comment.html', { |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
105 |
'paragraph_id': paragraph_id, |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
106 |
'form': form, |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
107 |
'length': len(queryset), |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
108 |
'query': queryset, |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
109 |
'newid': newid or True, |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
110 |
'error': error, |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
111 |
}) |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
112 |
|
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
113 |
|
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
114 |
|
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
115 |
|
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
116 |
def submit(request, paragraph_id): |
3 | 117 |
|
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
118 |
try: |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
119 |
element = get_object_or_404(Element, paragraph_id=paragraph_id) |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
120 |
except Http404: |
3 | 121 |
#creating chapter name from paragraph_id surely there is a better way using the context but i do not know as yet |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
122 |
chapter_id='_'.join(paragraph_id.split('_')[0:-1]) |
3 | 123 |
chapter_name=chapter_id[-1::-1].replace('_','/',1)[-1::-1] |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
124 |
element=Element(chapter_name=chapter_name,paragraph_id=paragraph_id) |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
125 |
element.save() |
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
126 |
print element.chapter_name |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
127 |
form = None |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
128 |
newid = None |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
129 |
|
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
130 |
if request.method == 'POST': |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
131 |
form = CommentForm(request.POST) |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
132 |
print form.errors |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
133 |
if form.is_valid(): |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
134 |
print form.cleaned_data |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
135 |
data = form.cleaned_data |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
136 |
if data.get('remember'): |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
137 |
request.session['name'] = data['name'] |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
138 |
request.session['url'] = data['url'] |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
139 |
else: |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
140 |
request.session.pop('name', None) |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
141 |
request.session.pop('url', None) |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
142 |
c = Comment(element=element, |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
143 |
comment=data['comment'], |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
144 |
submitter_name=data['name'], |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
145 |
submitter_url=data['url'], |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
146 |
ip=request.META.get('REMOTE_ADDR')) |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
147 |
print c |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
148 |
c.save() |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
149 |
|
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
150 |
form = None |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
151 |
return single(request, paragraph_id, form,) |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
152 |
|
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
153 |
def test(request): |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
154 |
print request |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
155 |
string="<p>test comment</p>" |
2
f5e18f8ed036
Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name
amit
parents:
0
diff
changeset
|
156 |
return HttpResponse(string,mimetype="text/plain") |
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
157 |
|
3 | 158 |
def page(req, path): |
159 |
if splitext(path)[1] == '.html': |
|
160 |
soup = bss(open(join(settings.SPHINX_PROJECT, path)).read()) |
|
161 |
head = soup.find('head') |
|
162 |
first_script = Tag(soup, 'script') |
|
163 |
first_script['src'] = "_static/simplecomment.js" |
|
164 |
first_script['type'] = "text/javascript" |
|
165 |
second_script = Tag(soup, 'script') |
|
166 |
second_script['src'] = "_static/jquery.form.js" |
|
167 |
second_script['type'] = "text/javascript" |
|
168 |
head.insert(-1, first_script) |
|
169 |
head.insert(-1, second_script) |
|
170 |
counter = 0 |
|
171 |
page_identity = path.split('.')[0].replace('/', '_') |
|
172 |
for p in soup.findAll('p'): |
|
173 |
p['id'] = '%s_%s' %(page_identity, counter) |
|
174 |
counter += 1 |
|
175 |
return HttpResponse(str(soup)) |
|
176 |
else: |
|
177 |
return HttpResponse(open(join(settings.SPHINX_PROJECT, path)).read()) |
|
178 |
||
0
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
179 |
#return HttpResponse(dumps(string),mimetype="text/plain") |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
180 |
#test= csrf_exempt(test) |
54f784230511
Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff
changeset
|
181 |