changeset 207:d534881629ff

Updated snippet's template and views files to fix rating code collision which prevented posting new snippets. Updated snippet's models file so that now secret id of each snippet will be unique. It also means that the no two or more than two snippets will have same secret id.
author Ahsan Ali Shahid <ahsan.ali.shahid@gmail.com>
date Mon, 19 Aug 2013 20:30:19 +0500
parents 1f1e867cb37f
children a22259c9862e
files apps/snippet/forms.py apps/snippet/models.py apps/snippet/views.py templates/snippet/snippet_details.djhtml
diffstat 4 files changed, 15 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/apps/snippet/forms.py	Fri Jul 19 22:54:18 2013 +0500
+++ b/apps/snippet/forms.py	Mon Aug 19 20:30:19 2013 +0500
@@ -35,8 +35,6 @@
     )
 
 
-    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')
--- a/apps/snippet/models.py	Fri Jul 19 22:54:18 2013 +0500
+++ b/apps/snippet/models.py	Mon Aug 19 20:30:19 2013 +0500
@@ -12,7 +12,10 @@
 
 t = 'abcdefghijkmnopqrstuvwwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ1234567890'
 def generate_secret_id(length=4):
-    return ''.join([random.choice(t) for i in range(length)])
+    unique_id = ''.join([random.choice(t) for i in range(length)])
+    while Snippet.objects.filter(secret_id = unique_id ).count()!= 0:
+        unique_id = ''.join([random.choice(t) for i in range(length)])    
+    return unique_id
 
 class SnippetManager(models.Manager):
     def public(self):
--- a/apps/snippet/views.py	Fri Jul 19 22:54:18 2013 +0500
+++ b/apps/snippet/views.py	Mon Aug 19 20:30:19 2013 +0500
@@ -108,9 +108,11 @@
         default_pygments_style = PygmentsStyle.objects.get(pk=1)
 
 
+    r = snippet.rating.votes
+    if snippet.rating.votes == 0:
+        r = 1
 
-    score = (snippet.rating.score/snippet.rating.votes)
-
+    score = snippet.rating.score/(r)
 
     context = {
         'snippet_form': snippet_form,
--- a/templates/snippet/snippet_details.djhtml	Fri Jul 19 22:54:18 2013 +0500
+++ b/templates/snippet/snippet_details.djhtml	Mon Aug 19 20:30:19 2013 +0500
@@ -91,7 +91,13 @@
               <form action = "rating/process/"  method = "POST" id="rate_form">
               {% csrf_token%}
               <h3 id ="rate_label">Rate this Snippet:</h3>
-              {{ snippet_form.Rate }}
+              <ul>
+<li><label for="id_Rate_0"><input name="Rate" onclick="document.getElementById('rate_form').submit();" type="radio" class="rate_radio" value="1" id="id_Rate_0"> 1</label></li>
+<li><label for="id_Rate_1"><input name="Rate" onclick="document.getElementById('rate_form').submit();" type="radio" class="rate_radio" value="2" id="id_Rate_1"> 2</label></li>
+<li><label for="id_Rate_2"><input name="Rate" onclick="document.getElementById('rate_form').submit();" type="radio" class="rate_radio" value="3" id="id_Rate_2"> 3</label></li>
+<li><label for="id_Rate_3"><input name="Rate" onclick="document.getElementById('rate_form').submit();" type="radio" class="rate_radio" value="4" id="id_Rate_3"> 4</label></li>
+<li><label for="id_Rate_4"><input name="Rate" onclick="document.getElementById('rate_form').submit();" type="radio" class="rate_radio" value="5" id="id_Rate_4"> 5</label></li>
+</ul>
               <input type="hidden" name="next" value="{{ request.get_full_path }}" />
               </form>
     </div>