|
1 import sys |
|
2 import django.forms as forms |
|
3 from django.db import connection |
|
4 from django.http import HttpResponse |
|
5 from hgbook.comments.models import Comment, Element |
|
6 from django.shortcuts import get_object_or_404, render_to_response |
|
7 from django.template import Context ,RequestContext |
|
8 from django.template.loader import get_template |
|
9 from django.utils.simplejson import dumps |
|
10 from p_list import * |
|
11 from BeautifulSoup import BeautifulSoup |
|
12 import glob |
|
13 html_folder='/home/amit/SEES-hacks/web/html' |
|
14 |
|
15 def sort_dict(dict): |
|
16 new_dict = {} |
|
17 sorted_keys = dict.keys() |
|
18 sorted_keys.sort() |
|
19 print >> sys.stderr , sorted_keys |
|
20 for key in sorted_keys: |
|
21 new_dict[key] = dict[key] |
|
22 |
|
23 return new_dict |
|
24 |
|
25 def dump_queries(): |
|
26 # requires settings.DEBUG to be set to True in order to work |
|
27 if len(connection.queries) == 1: |
|
28 print connection.queries |
|
29 else: |
|
30 qs = {} |
|
31 for q in connection.queries: |
|
32 qs[q['sql']] = qs.setdefault(q['sql'], 0) + 1 |
|
33 for q in sorted(qs.items(), key=lambda x: x[1], reverse=True): |
|
34 print q |
|
35 print len(connection.queries) |
|
36 |
|
37 class CommentForm(forms.Form): |
|
38 id = forms.CharField(widget=forms.HiddenInput) |
|
39 name = forms.CharField(max_length=64) |
|
40 url = forms.URLField(max_length=128, required=False) |
|
41 comment = forms.CharField(widget=forms.Textarea(attrs={ |
|
42 'rows': 8, 'cols': 60 |
|
43 })) |
|
44 remember = forms.BooleanField(initial=True, required=False) |
|
45 |
|
46 def search(request): |
|
47 print request |
|
48 |
|
49 |
|
50 def index(request): |
|
51 html_files = glob.glob(html_folder+'ch*.html') |
|
52 print >> sys.stderr ,html_files |
|
53 print >> sys.stderr ,"just checking whether i got here" |
|
54 print >> sys.stderr , html_files |
|
55 html_files.sort() |
|
56 link_list=['/review/html/'+a.split('/')[-1] for a in html_files] |
|
57 # print >> sys.stderr , html_files |
|
58 title_list=[] |
|
59 for html_file in html_files: |
|
60 file_str=open(html_file,'r').read() |
|
61 soup_obj=BeautifulSoup(''.join(file_str)) |
|
62 title_str=soup_obj.html.head.title.string |
|
63 title_str=unicode(title_str) |
|
64 title_str.encode('ascii','ignore') |
|
65 title_list.append(title_str) |
|
66 |
|
67 resp_dict=zip(link_list,title_list) |
|
68 print >>sys.stderr ,resp_dict |
|
69 return render_to_response('index.html', {'resp_dict': resp_dict }) |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 def comments_by_chapter(id): |
|
78 objs = {} |
|
79 for c in Comment.objects.filter(element=id, hidden=False).order_by('date'): |
|
80 objs.setdefault(c.element_id, []).append(c) |
|
81 return objs |
|
82 |
|
83 def chapter(request, id): |
|
84 template = get_template('comment.html') |
|
85 resp = {} |
|
86 for elt, comments in comments_by_chapter(id).iteritems(): |
|
87 |
|
88 form = CommentForm(initial={ |
|
89 'id': elt, |
|
90 'name': request.session.get('name', ''), |
|
91 }) |
|
92 |
|
93 |
|
94 |
|
95 resp[elt] = template.render(RequestContext({ |
|
96 'id': elt, |
|
97 'form': form, |
|
98 'length': len(comments), |
|
99 'query': comments, |
|
100 })) |
|
101 return HttpResponse(dumps(resp), mimetype='application/json') |
|
102 |
|
103 # queryset = Comment.objects.filter(element=id, hidden=False).order_by('date') |
|
104 # if form is None: |
|
105 # form = CommentForm(initial={ |
|
106 # 'id': id, |
|
107 # 'name': request.session.get('name', ''), |
|
108 # }) |
|
109 # try: |
|
110 # error = form.errors[0] |
|
111 # except: |
|
112 # error = '' |
|
113 # return render_to_response('comment.html', { |
|
114 # 'id': id, |
|
115 # 'form': form, |
|
116 # 'length': len(queryset), |
|
117 # 'query': queryset, |
|
118 # 'newid': newid or True, |
|
119 # 'error': error, |
|
120 # }) |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 def single_com(request,id): |
|
127 |
|
128 # template = get_template('comment.html') |
|
129 resp = {} |
|
130 print >> sys.stderr ,id |
|
131 for i in p_list[id]: |
|
132 # form=None |
|
133 queryset = Comment.objects.filter(element=i, hidden=False).order_by('date') |
|
134 |
|
135 # if form is None: |
|
136 # form = CommentForm(initial={ |
|
137 # 'id': id, |
|
138 # 'name': request.session.get('name', ''), |
|
139 # }) |
|
140 if len(queryset)==0: |
|
141 resp[i] = "No" |
|
142 elif len(queryset)==1: |
|
143 resp[i]="One" |
|
144 else: |
|
145 resp[i] = len(queryset) |
|
146 # 'id': i, |
|
147 # 'form': form, |
|
148 # 'length': len(queryset), |
|
149 # 'query': queryset, |
|
150 |
|
151 return HttpResponse(dumps(resp), mimetype='application/json') |
|
152 # template = get_template('comment.html') |
|
153 # for i in p_list[id]: |
|
154 # form=None |
|
155 # queryset = Comment.objects.filter(element=i, hidden=False).order_by('date') |
|
156 # print queryset |
|
157 # if form is None: |
|
158 # form = CommentForm(initial={ |
|
159 # 'id': id, |
|
160 # 'name': request.session.get('name', ''), |
|
161 # }) |
|
162 # try: |
|
163 # error = form.errors[0] |
|
164 # except: |
|
165 # error = '' |
|
166 |
|
167 # return HttpResponse(response_list) |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 def chapter_count(request, id): |
|
175 |
|
176 resp = comments_by_chapter(id) |
|
177 |
|
178 for elt, comments in resp.iteritems(): |
|
179 |
|
180 resp[elt] = len(comments) |
|
181 |
|
182 return HttpResponse(dumps(resp), mimetype='application/json') |
|
183 |
|
184 def single(request, id, form=None, newid=None ): |
|
185 queryset = Comment.objects.filter(element=id, hidden=False).order_by('date') |
|
186 if form is None: |
|
187 form = CommentForm(initial={ |
|
188 'id': id, |
|
189 'name': request.session.get('name', ''), |
|
190 }) |
|
191 try: |
|
192 error = form.errors[0] |
|
193 except: |
|
194 error = '' |
|
195 return render_to_response('comment.html', { |
|
196 'id': id, |
|
197 'form': form, |
|
198 'length': len(queryset), |
|
199 'query': queryset, |
|
200 'newid': newid or True, |
|
201 'error': error, |
|
202 }) |
|
203 |
|
204 def submit(request, id): |
|
205 # print request |
|
206 # print id |
|
207 try : |
|
208 element=Element.objects.get(id=id,chapter='chap_intro') |
|
209 except Element.DoesNotExist: |
|
210 element=Element(id=id) |
|
211 element.save() |
|
212 |
|
213 |
|
214 if request.method == 'POST': |
|
215 |
|
216 form = CommentForm(request.POST) |
|
217 |
|
218 if form.is_valid(): |
|
219 data = form.cleaned_data |
|
220 |
|
221 if data.get('remember'): |
|
222 |
|
223 request.session['name'] = data['name'] |
|
224 request.session['url'] = data['url'] |
|
225 else: |
|
226 request.session.pop('name', None) |
|
227 request.session.pop('url', None) |
|
228 c = Comment(element=element, |
|
229 comment=data['comment'], |
|
230 submitter_name=data['name'], |
|
231 submitter_url=data['url'], |
|
232 ip=request.META.get('REMOTE_ADDR')) |
|
233 |
|
234 |
|
235 c.save() |
|
236 newid=c.id |
|
237 form = None |
|
238 return single(request, id, form, newid) |
|
239 |