sphinx_django/sphinxcomment/views.py~
author amit
Thu, 30 Sep 2010 11:36:30 +0530
changeset 0 54f784230511
child 2 f5e18f8ed036
permissions -rw-r--r--
Initial commit of django based commenting system for sphinx. Already has most of the boiler plate code.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
     7
from django.http import HttpResponse
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 
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    13
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    14
# def dump_queries():
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    15
#     # requires settings.DEBUG to be set to True in order to work
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    16
#     if len(connection.queries) == 1:
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    17
#         print connection.queries
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    18
#     else:
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    19
#         qs = {}
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    20
#         for q in connection.queries:
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    21
#             qs[q['sql']] = qs.setdefault(q['sql'], 0) + 1
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    22
#         for q in sorted(qs.items(), key=lambda x: x[1], reverse=True):
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    23
#             print q
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    24
#         print len(connection.queries)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    25
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    26
class CommentForm(forms.Form):
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    27
    id = forms.CharField(widget=forms.HiddenInput)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    28
    name = forms.CharField(max_length=64)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    29
    url = forms.URLField(max_length=128, required=False)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    30
    comment = forms.CharField(widget=forms.Textarea(attrs={
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    31
        'rows': 8, 'cols': 60
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    32
        }))
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    33
    remember = forms.BooleanField(initial=True, required=False)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    34
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    35
def comments_by_chapter(chapter):
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    36
    objs = {}
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    37
    for c in Comment.objects.filter(element__chapter=id, hidden=False).order_by('date'):
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    38
        objs.setdefault(c.element_id, []).append(c)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    39
    return objs
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    40
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    41
def chapter(request):
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    42
#    template = get_template('comment.html')
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    43
#    resp = {}
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    44
    # for elt, comments in comments_by_chapter(id).iteritems():
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    45
    form = CommentForm(initial={
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    46
            'id': 'elt',
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    47
            'name': 'name'
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    48
            })
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    49
    resp['elt'] = template.render(Context({
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    50
            'id': 'elt',
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    51
            'form': form,
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    52
            'length': 5,
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    53
            'query': 'abcde',
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    54
            }))
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    55
    return HttpResponse(dumps(resp), mimetype='application/json')
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    56
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    57
def chapter_count(request):
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    58
#     resp = comments_by_chapter(chapter_name)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    59
# #   for elt, comments in resp.iteritems():
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    60
# #        resp[elt] = len(comments)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    61
    resp={}
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    62
    resp['elt']=5
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    63
    return HttpResponse(dumps(resp), mimetype='application/json')
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    64
    
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    65
def single(request, id, form=None, newid=None):
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    66
    queryset = Comment.objects.filter(element=id, hidden=False).order_by('date')
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    67
    if form is None:
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    68
        form = CommentForm(initial={
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    69
            'id': id,
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    70
            'name': request.session.get('name', ''),
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    71
            })
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    72
    try:
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    73
        error = form.errors[0]
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    74
    except:
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    75
        error = ''
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    76
    return render_to_response('comment.html', {
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    77
        'id': id,
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    78
        'form': form,
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    79
        'length': len(queryset),
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    80
        'query': queryset,
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    81
        'newid': newid or True,
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    82
        'error': error,
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    83
        })
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    84
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    85
def submit(request, id):
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    86
    element = get_object_or_404(Element, id=id)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    87
    form = None
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    88
    newid = None
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    89
    if request.method == 'POST':
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    90
        form = CommentForm(request.POST)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    91
        if form.is_valid():
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    92
            data = form.cleaned_data
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    93
            if data.get('remember'):
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    94
                request.session['name'] = data['name']
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    95
                request.session['url'] = data['url']
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    96
            else:
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    97
                request.session.pop('name', None)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    98
                request.session.pop('url', None)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
    99
            c = Comment(element=element,
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   100
                        comment=data['comment'],
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   101
                        submitter_name=data['name'],
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   102
                        submitter_url=data['url'],
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   103
                        ip=request.META.get('REMOTE_ADDR'))
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   104
            c.save()
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   105
            newid = c.id
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   106
            form = None
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   107
    return single(request, id, form, newid)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   108
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   109
def test(request):
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   110
    print request
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   111
    string="<p>test comment</p>"
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   112
    return HttpResponse(dumps(string),mimetype="application/json")
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   113
    
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   114
       
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   115
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   116
#return HttpResponse(dumps(string),mimetype="text/plain")
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   117
#test= csrf_exempt(test)
54f784230511 Initial commit of django based commenting system for sphinx. Already has
amit
parents:
diff changeset
   118