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