changeset 202:499e5f47f278

Add necessary code to views and forms of snippet for Ratings
author Ahsan Ali Shahid <ahsan.ali.shahid@gmail.com>
date Fri, 19 Jul 2013 22:09:29 +0500
parents c2a1c8950db2
children c5087ce140c9
files apps/snippet/forms.py apps/snippet/views.py static/css/agora.less
diffstat 3 files changed, 56 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/apps/snippet/forms.py	Fri Jul 19 21:55:04 2013 +0500
+++ b/apps/snippet/forms.py	Fri Jul 19 22:09:29 2013 +0500
@@ -34,6 +34,10 @@
         label=_(u'Expires'),
     )
 
+
+    form_choices = ( (x,x) for x in range(1,6) )
+    Rate = forms.ChoiceField(form_choices,widget=forms.RadioSelect(attrs={'onclick' :"document.getElementById('rate_form').submit();","class":"rate_radio"}))
+
     def __init__(self, *args, **kwargs):
         request = kwargs.pop('request')
         super(SnippetForm, self).__init__(*args, **kwargs)
--- a/apps/snippet/views.py	Fri Jul 19 21:55:04 2013 +0500
+++ b/apps/snippet/views.py	Fri Jul 19 22:09:29 2013 +0500
@@ -107,6 +107,11 @@
     else:
         default_pygments_style = PygmentsStyle.objects.get(pk=1)
 
+
+
+    score = (snippet.rating.score/snippet.rating.votes)
+
+
     context = {
         'snippet_form': snippet_form,
         'snippet': snippet,
@@ -117,6 +122,7 @@
         'default_style': default_pygments_style,
         'no_descendants': len(tree) == 1,
         'show': show,
+        'score' : score,
     }
 
     response = render(request, template_name, context)
@@ -228,3 +234,24 @@
     code_string = request.GET.get('codestring', False)
     response = simplejson.dumps({'lexer': guess_code_lexer(code_string)})
     return HttpResponse(response)
+
+
+
+#Rating Processing
+def processrating(request,snippet_id):
+    from djangoratings.views import AddRatingView
+    import django.db.models
+    from django.contrib.contenttypes.models import ContentType
+
+
+    content_type = ContentType.objects.get(model='snippet').id
+    if request.method == 'POST':
+        params = {
+            'content_type_id': content_type,
+            'object_id': get_object_or_404(Snippet,secret_id=snippet_id).id,
+            'field_name': 'rating', # Must be the same as the name declared in the model of snippet
+            'score': int( request.POST['Rate']) , # the score value sent
+            }
+        response = AddRatingView()(request, **params)
+        if response.status_code == 200:
+            return redirect( request.POST['next'] )
\ No newline at end of file
--- a/static/css/agora.less	Fri Jul 19 21:55:04 2013 +0500
+++ b/static/css/agora.less	Fri Jul 19 22:09:29 2013 +0500
@@ -488,4 +488,29 @@
 .comment-body
 {
     padding-left: 15px;
+}
+#radio_votes
+{
+    margin:5px;
+}
+#radio_votes ul
+{
+    
+    list-style: none;
+    display:block;
+}
+#radio_votes ul li
+{
+    float:left;
+    font-weight: bold;
+    margin: 2px;
+}
+#rate_label
+{
+    float:left;
+    margin-right:3px;
+}
+.rate_radio
+{
+    margin:2px;
 }
\ No newline at end of file