# HG changeset patch # User amit # Date 1287138568 -19800 # Node ID f5e18f8ed036af570d31366c281cd4254299cab1 # Parent 5574cfc2b28d590e63488db1e0851a98f96af96f Changes to account for a new way of keeping ids ... Uses the whole path rather than just file name diff -r 5574cfc2b28d -r f5e18f8ed036 hsbook_back.js --- a/hsbook_back.js Thu Sep 30 15:59:32 2010 +0530 +++ b/hsbook_back.js Fri Oct 15 15:59:28 2010 +0530 @@ -3,43 +3,52 @@ } function beforeComment(formData, jqForm, options) { - var form = jqForm[0]; - if (!form.comment.value) { - $(options.target + " span.comment_error").empty().append( - "Your comment is empty"); + var form=jqForm[0]; + + if (form.comment.textLength.toString()=='0') { + $("span.comment_error").empty().append( + " Your comment is empty"); return false; } - if (!form.name.value) { - $(options.target + " span.comment_error").empty().append( - "Please provide a name"); + if (form.name.textLength.toString()=='0') { + $("span.comment_error").empty().append( + " Please provide a name"); return false; } $(options.target + " span.comment_error").empty().after( ""); - $(options.target + " input[@name=submit]").attr("disabled", true); + $("input[@name=submit]").attr("disabled", true); } function ajaxifyForm(id) { - var q = qid(id); + +// $('#form_func_1').replaceWith('something'); - $("#form_" + q).ajaxForm({ beforeSubmit: beforeComment, - success: function() { ajaxifyForm(id); }, - target: "#comments_" + q }); -} + var substring=id.substr(9); + $('#form_'+substring).ajaxForm({beforeSubmit: beforeComment, + success: function(){ loadComments(id);} + + });} + function toggleComment(id) { $("#toggle_" + qid(id)).nextAll().toggle(); return false; } -function loadComments(id) { - $("#comments_" + qid(id)).load(location.protocol + "//" + location.host + - "/comments/single/" + id + "/", function() { - ajaxifyForm(id); - }); - return false; + +function loadComments(id) +{ + + var substring=id.substr(9); + $('#comments_'+substring).load("http://127.0.0.1:8000/single/"+ substring +'/',function() { ajaxifyForm(id);} + ); + } + + + function loadAllComments() { $("a.commenttoggle").each(function() { var id = $(this).attr("pid"); @@ -61,20 +70,37 @@ $("p[@id]").each(function() { $(this).append(loading($(this).attr("id"))); }); -$("p[@id]").each(function() { - var url_string=window.location.pathname; - var temp = new Array(); - temp = url_string.split('/'); - var chap_name=temp[temp.length-1].split('.')[0]; + + +var url_string=window.location.pathname; +var temp = new Array(); +temp = url_string.split('/'); +var chap_name=temp[temp.length-1].split('.')[0]; + +jQuery.getJSON("http://127.0.0.1:8000/count/"+chap_name, function(data) { - $.getJSON("http://127.0.0.1:8000/count/"+chap_name, function(data) { - $.each(data , function(id) { - $("span.commenttoggle").replaceWith("" + data.count +" "+"comments" + ""); - }); + $("span.comment").each(function(data_val) { + var id = $(this).attr("id"); + var substring=id.substr(9); + + $(this).replaceWith("" + data.count[substring] +' comments'+ ""); + + }); + + + }); + + }); - }); - }); + + + + - }); + + + + + diff -r 5574cfc2b28d -r f5e18f8ed036 sphinx_django/settings.py --- a/sphinx_django/settings.py Thu Sep 30 15:59:32 2010 +0530 +++ b/sphinx_django/settings.py Fri Oct 15 15:59:28 2010 +0530 @@ -65,6 +65,9 @@ ROOT_URLCONF = 'sphinx_django.urls' + + + TEMPLATE_DIRS = ( "/home/amit/review/sphinx_django/templates/" diff -r 5574cfc2b28d -r f5e18f8ed036 sphinx_django/settings.pyc Binary file sphinx_django/settings.pyc has changed diff -r 5574cfc2b28d -r f5e18f8ed036 sphinx_django/settings.py~ --- a/sphinx_django/settings.py~ Thu Sep 30 15:59:32 2010 +0530 +++ b/sphinx_django/settings.py~ Fri Oct 15 15:59:28 2010 +0530 @@ -4,7 +4,7 @@ TEMPLATE_DEBUG = DEBUG ADMINS = ( - # ('Your Name', 'your_email@domain.com'), + ('Amit Sethi', 'amit.pureenergy@gmail.com'), ) MANAGERS = ADMINS @@ -67,15 +67,17 @@ TEMPLATE_DIRS = ( "/home/amit/review/sphinx_django/templates/" - + # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. ) INSTALLED_APPS = ( + 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', + 'sphinx_django.sphinxcomment', ) diff -r 5574cfc2b28d -r f5e18f8ed036 sphinx_django/sphinxcomment/models.py --- a/sphinx_django/sphinxcomment/models.py Thu Sep 30 15:59:32 2010 +0530 +++ b/sphinx_django/sphinxcomment/models.py Fri Oct 15 15:59:28 2010 +0530 @@ -7,12 +7,12 @@ mutable = True class Element(models.Model): - chapter_name = models.CharField('Chapter name', max_length=100, editable=False, - db_index=True) - + paragraph_id = models.CharField('Paragraph Id', max_length=100, editable=False, primary_key=True) + chapter_name = models.CharField('Chapter name', max_length=100, editable=False,db_index=True) + def __unicode__(self): - return self.chapter_name + return self.paragraph_id class Comment(models.Model): element = models.ForeignKey(Element, diff -r 5574cfc2b28d -r f5e18f8ed036 sphinx_django/sphinxcomment/models.pyc Binary file sphinx_django/sphinxcomment/models.pyc has changed diff -r 5574cfc2b28d -r f5e18f8ed036 sphinx_django/sphinxcomment/models.py~ --- a/sphinx_django/sphinxcomment/models.py~ Thu Sep 30 15:59:32 2010 +0530 +++ b/sphinx_django/sphinxcomment/models.py~ Fri Oct 15 15:59:28 2010 +0530 @@ -2,19 +2,17 @@ # Create your models here. from django.db import models -import sha + mutable = True -class Chapter(models.Model): - id = models.CharField('ID attribute', max_length=64, editable=False, - primary_key=True) - chapter = models.CharField('Chapter ID', max_length=100, editable=False, - db_index=True) - +class Element(models.Model): + paragraph_id = models.CharField('Paragraph Id', max_length=100, editable=False, primary_key=True) + chapter_name = models.CharField('Chapter name', max_length=100, editable=False,db_index=True) + def __unicode__(self): - return self.id + return self.paragraph_id class Comment(models.Model): element = models.ForeignKey(Element, @@ -29,19 +27,8 @@ help_text='IP address from which comment was submitted') date = models.DateTimeField('date submitted', auto_now=True, auto_now_add=True) - reviewed = models.BooleanField(default=False, db_index=True, - help_text='Has this comment been reviewed by an author?') - hidden = models.BooleanField(default=False, db_index=True, - help_text='Has this comment been hidden from public display?') def __unicode__(self): return self.comment[:32] - def get_absolute_url(self): - s = sha.new() - s.update(repr(self.comment)) - s.update(repr(self.submitter_name)) - s.update(str(self.date)) - return '/read/%s.html#%s?comment=%s&uuid=%s' % ( - self.element.chapter, self.element.id, self.id, s.hexdigest()[:20] - ) + diff -r 5574cfc2b28d -r f5e18f8ed036 sphinx_django/sphinxcomment/views.py --- a/sphinx_django/sphinxcomment/views.py Thu Sep 30 15:59:32 2010 +0530 +++ b/sphinx_django/sphinxcomment/views.py Fri Oct 15 15:59:28 2010 +0530 @@ -4,13 +4,17 @@ from django.contrib.csrf.middleware import csrf_exempt import django.forms as forms from django.db import connection -from django.http import HttpResponse +from django.http import HttpResponse,Http404 from sphinxcomment.models import Comment, Element from django.shortcuts import get_object_or_404, render_to_response from django.template import Context from django.template.loader import get_template from django.utils.simplejson import dumps + +placeholder_string = open('paragraph_id.py').read() +exec placeholder_string + def dump_queries(): # requires settings.DEBUG to be set to True in order to work if len(connection.queries) == 1: @@ -24,7 +28,6 @@ print len(connection.queries) class CommentForm(forms.Form): - id = forms.CharField(widget=forms.HiddenInput) name = forms.CharField(max_length=64) url = forms.URLField(max_length=128, required=False) comment = forms.CharField(widget=forms.Textarea(attrs={ @@ -33,48 +36,81 @@ remember = forms.BooleanField(initial=True, required=False) def comments_by_chapter(chapter): - objs = [] - for c in Comment.objects.filter(element__chapter_name=chapter).order_by('date'): - objs.append(c) - return objs + objs = {} + + try: + para_list=p_list[chapter] + + for paragraph_id in para_list: + objs[paragraph_id]=[] + + except: + para_list=[] + + for paragraph_id in para_list: + print chapter ,paragraph_id + for c in Comment.objects.filter(element__chapter_name=chapter,element__paragraph_id=paragraph_id).order_by('date'): + + objs[paragraph_id].append(c) + + + + return objs + + + + + def chapter(request): -# template = get_template('comment.html') -# resp = {} - # for elt, comments in comments_by_chapter(id).iteritems(): - form = CommentForm(initial={ - 'id': 'elt', - 'name': 'name' + template = get_template('comment.html') + resp = {} + for elt, comments in comments_by_chapter(chapter).iteritems(): + print elt ,comments + form = CommentForm(initial={ + 'paragraph_id': elt, + 'name': name }) - resp['elt'] = template.render(Context({ - 'id': 'elt', + resp[elt] = template.render(Context({ + 'paragraph_id': elt, 'form': form, - 'length': 5, - 'query': 'abcde', + 'length': len(comments), + 'query': comments, })) return HttpResponse(dumps(resp), mimetype='application/json') def chapter_count(request,chapter_name): - comment_count = comments_by_chapter(chapter_name) + print chapter_name + chapter_name=chapter_name.split('.')[0] + comment_objs = comments_by_chapter(chapter_name) resp={} - resp['count']=len(comment_count) + temp_dict={} + for elt, comments in comment_objs.iteritems(): + temp_dict[elt]=len(comments) - + resp['count']=temp_dict + + + print resp return HttpResponse(dumps(resp), mimetype='application/json') -def single(request, id, form=None, newid=None): - queryset = Comment.objects.filter(element=id, hidden=False).order_by('date') +def single(request,paragraph_id, form=None, newid=None): + if paragraph_id[-1]=='/': + paragraph_id=paragraph_id[:-1] + queryset = Comment.objects.filter(element=paragraph_id) + print paragraph_id if form is None: form = CommentForm(initial={ - 'id': id, + 'paragraph_id': paragraph_id, 'name': request.session.get('name', ''), }) try: error = form.errors[0] except: error = '' + print form.errors return render_to_response('comment.html', { - 'id': id, + 'paragraph_id': paragraph_id, 'form': form, 'length': len(queryset), 'query': queryset, @@ -82,13 +118,28 @@ 'error': error, }) -def submit(request, id): - element = get_object_or_404(Element, id=id) + + + +def submit(request, paragraph_id): + + try: + element = get_object_or_404(Element, paragraph_id=paragraph_id) + except Http404: + #creating chapter name from paragraph_id surely there is a better way using the context but i do not know as yet + chapter_id='_'.join(paragraph_id.split('_')[0:-1]) + chapter_name=chapter_id[-1::-1].replace('_','/',1)[-1::-1] + element=Element(chapter_name=chapter_name,paragraph_id=paragraph_id) + element.save() + print element.chapter_name form = None newid = None + if request.method == 'POST': form = CommentForm(request.POST) + print form.errors if form.is_valid(): + print form.cleaned_data data = form.cleaned_data if data.get('remember'): request.session['name'] = data['name'] @@ -101,15 +152,16 @@ submitter_name=data['name'], submitter_url=data['url'], ip=request.META.get('REMOTE_ADDR')) + print c c.save() - newid = c.id + form = None - return single(request, id, form, newid) + return single(request, paragraph_id, form,) def test(request): print request string="

test comment

" - return HttpResponse(dumps(string),mimetype="application/json") + return HttpResponse(string,mimetype="text/plain") diff -r 5574cfc2b28d -r f5e18f8ed036 sphinx_django/sphinxcomment/views.pyc Binary file sphinx_django/sphinxcomment/views.pyc has changed diff -r 5574cfc2b28d -r f5e18f8ed036 sphinx_django/sphinxcomment/views.py~ --- a/sphinx_django/sphinxcomment/views.py~ Thu Sep 30 15:59:32 2010 +0530 +++ b/sphinx_django/sphinxcomment/views.py~ Fri Oct 15 15:59:28 2010 +0530 @@ -4,27 +4,30 @@ from django.contrib.csrf.middleware import csrf_exempt import django.forms as forms from django.db import connection -from django.http import HttpResponse +from django.http import HttpResponse,Http404 from sphinxcomment.models import Comment, Element from django.shortcuts import get_object_or_404, render_to_response from django.template import Context from django.template.loader import get_template from django.utils.simplejson import dumps -# def dump_queries(): -# # requires settings.DEBUG to be set to True in order to work -# if len(connection.queries) == 1: -# print connection.queries -# else: -# qs = {} -# for q in connection.queries: -# qs[q['sql']] = qs.setdefault(q['sql'], 0) + 1 -# for q in sorted(qs.items(), key=lambda x: x[1], reverse=True): -# print q -# print len(connection.queries) + +placeholder_string = open('paragraph_id.py').read() +exec placeholder_string + +def dump_queries(): + # requires settings.DEBUG to be set to True in order to work + if len(connection.queries) == 1: + print connection.queries + else: + qs = {} + for q in connection.queries: + qs[q['sql']] = qs.setdefault(q['sql'], 0) + 1 + for q in sorted(qs.items(), key=lambda x: x[1], reverse=True): + print q + print len(connection.queries) class CommentForm(forms.Form): - id = forms.CharField(widget=forms.HiddenInput) name = forms.CharField(max_length=64) url = forms.URLField(max_length=128, required=False) comment = forms.CharField(widget=forms.Textarea(attrs={ @@ -33,48 +36,78 @@ remember = forms.BooleanField(initial=True, required=False) def comments_by_chapter(chapter): - objs = {} - for c in Comment.objects.filter(element__chapter=id, hidden=False).order_by('date'): - objs.setdefault(c.element_id, []).append(c) - return objs + objs = {} + + try: + para_list=p_list[chapter] + + for paragraph_id in para_list: + objs[paragraph_id]=[] + + except: + para_list=[] + + for paragraph_id in para_list: + + for c in Comment.objects.filter(element__chapter_name=chapter,element__paragraph_id=paragraph_id).order_by('date'): + + objs[paragraph_id].append(c) + + return objs + + + + + def chapter(request): -# template = get_template('comment.html') -# resp = {} - # for elt, comments in comments_by_chapter(id).iteritems(): - form = CommentForm(initial={ - 'id': 'elt', - 'name': 'name' + template = get_template('comment.html') + resp = {} + for elt, comments in comments_by_chapter(chapter).iteritems(): + print elt ,comments + form = CommentForm(initial={ + 'paragraph_id': elt, + 'name': name }) - resp['elt'] = template.render(Context({ - 'id': 'elt', + resp[elt] = template.render(Context({ + 'paragraph_id': elt, 'form': form, - 'length': 5, - 'query': 'abcde', + 'length': len(comments), + 'query': comments, })) return HttpResponse(dumps(resp), mimetype='application/json') -def chapter_count(request): -# resp = comments_by_chapter(chapter_name) -# # for elt, comments in resp.iteritems(): -# # resp[elt] = len(comments) +def chapter_count(request,chapter_name): + print chapter_name + chapter_name=chapter_name.split('.')[0] + comment_objs = comments_by_chapter(chapter_name) resp={} - resp['elt']=5 + temp_dict={} + for elt, comments in comment_objs.iteritems(): + temp_dict[elt]=len(comments) + + resp['count']=temp_dict + + + print resp return HttpResponse(dumps(resp), mimetype='application/json') -def single(request, id, form=None, newid=None): - queryset = Comment.objects.filter(element=id, hidden=False).order_by('date') +def single(request,paragraph_id, form=None, newid=None): + paragraph_id=paragraph_id[:-1] + queryset = Comment.objects.filter(element=paragraph_id) + print paragraph_id if form is None: form = CommentForm(initial={ - 'id': id, + 'paragraph_id': paragraph_id, 'name': request.session.get('name', ''), }) try: error = form.errors[0] except: error = '' + print form.errors return render_to_response('comment.html', { - 'id': id, + 'paragraph_id': paragraph_id, 'form': form, 'length': len(queryset), 'query': queryset, @@ -82,13 +115,26 @@ 'error': error, }) -def submit(request, id): - element = get_object_or_404(Element, id=id) + + + +def submit(request, paragraph_id): + try: + element = get_object_or_404(Element, paragraph_id=paragraph_id) + except Http404: + chapter_id='_'.join(paragraph_id.split('_')[0:-1]) + chapter_name=chapter_id.replace('_','/') + element=Element(chapter_name=chapter_name,paragraph_id=paragraph_id) + element.save() + print element.chapter_name form = None newid = None + if request.method == 'POST': form = CommentForm(request.POST) + print form.errors if form.is_valid(): + print form.cleaned_data data = form.cleaned_data if data.get('remember'): request.session['name'] = data['name'] @@ -101,15 +147,16 @@ submitter_name=data['name'], submitter_url=data['url'], ip=request.META.get('REMOTE_ADDR')) + print c c.save() - newid = c.id + form = None - return single(request, id, form, newid) + return single(request, paragraph_id, form,) def test(request): print request string="

test comment

" - return HttpResponse(dumps(string),mimetype="application/json") + return HttpResponse(string,mimetype="text/plain") diff -r 5574cfc2b28d -r f5e18f8ed036 sphinx_django/templates/comment.html --- a/sphinx_django/templates/comment.html Thu Sep 30 15:59:32 2010 +0530 +++ b/sphinx_django/templates/comment.html Fri Oct 15 15:59:28 2010 +0530 @@ -1,7 +1,54 @@ + + {% ifequal length 1 %} One comment + href="comments: show / hide">{{ length }} comments {% else %} {% if length %} No comments - + href="comments: show / hide">0 comments + {% endif %} {% endifequal %} {% for c in query %} - + + +
{{ forloop.counter }} {{ c.submitter_name }} {{ c.comment|escape|linebreaks }}
+
{% endfor %} -