[Oi-commits] r1492 - in trunk: forum poll templates/forum

oi-commits at pardus.org.tr oi-commits at pardus.org.tr
Wed Jun 18 20:03:09 EEST 2008


Author: jnmbk
Date: Wed Jun 18 20:03:05 2008
New Revision: 1492

Modified:
   trunk/forum/forms.py
   trunk/forum/views.py
   trunk/poll/models.py
   trunk/templates/forum/topic.html
Log:
end_date checking and allow_multiple_choices, it has some bugs to fix...

Modified: trunk/forum/forms.py
=================================================================
--- trunk/forum/forms.py	(original)
+++ trunk/forum/forms.py	Wed Jun 18 20:03:05 2008
@@ -75,8 +75,8 @@
     def clean_end_date(self):
         field_data = self.cleaned_data['end_date']
 
-        # it must be bigger than today
-        if field_data != None and self.cleaned_data["date_limit"] and field_data <= date.today():
-            raise forms.ValidationError("Oylama bitiş tarihi bugünden küçük olamaz.")
+        # it must be filled if date_limit is on
+        if self.cleaned_data["date_limit"] and field_data == None:
+            raise forms.ValidationError("Oylama bitiş tarihini belirlemelisiniz.")
 
         return field_data

Modified: trunk/forum/views.py
=================================================================
--- trunk/forum/views.py	(original)
+++ trunk/forum/views.py	Wed Jun 18 20:03:05 2008
@@ -171,11 +171,13 @@
     if request.user.has_perm("forum.can_change_abusereport"):
         abuse_count = AbuseReport.objects.count()
 
-    # polloptions if topic has a poll
-    poll_options = None
+    # create polloption percents and poll_enabled information if topic has a poll
+    poll_options = poll_enabled = False
     try:
-        poll_options = topic.poll.polloption_set.all()
-        total_vote_count = topic.poll.pollvote_set.count() * 1.0
+        # calculate percents
+        poll = topic.poll
+        poll_options = poll.polloption_set.all()
+        total_vote_count = poll.pollvote_set.count() * 1.0
         for option in poll_options:
             if total_vote_count < 1:
                 option.percent = 0
@@ -183,6 +185,8 @@
                 option.percent = int(option.vote_count / total_vote_count * 100)
             if option.percent < 80:
                 option.percent_out = True
+        # now let's see if we'll enable voting for this user
+        poll_enabled = request.user.is_authenticated() and poll.date_limit and poll.end_date > datetime.now()
     except: #DoesNotExist
         pass
 
@@ -196,6 +200,7 @@
                            "watching": watching,
                            "abuse_count": abuse_count,
                            "poll_options": poll_options,
+                           "poll_enabled": poll_enabled,
                            },
                        paginate_by = POSTS_PER_PAGE,
                        allow_empty = True)
@@ -745,6 +750,7 @@
             poll = Poll(
                     question = form.cleaned_data["question"],
                     allow_changing_vote = form.cleaned_data["allow_changing_vote"],
+                    allow_multiple_choices = form.cleaned_data["allow_multiple_choices"],
                     date_limit = form.cleaned_data["date_limit"],
                     end_date = form.cleaned_data["end_date"],
                     )
@@ -789,9 +795,15 @@
     if request.method == 'POST':
         form = PollForm(request.POST.copy())
         if form.is_valid():
+            # we must delete existing votes in this case
+            #FIXME: Doesn't work because form.cleaned_data["allow_changing_vote"] always return True
+            print poll.allow_multiple_choices, form.cleaned_data["allow_changing_vote"], poll.allow_multiple_choices and not form.cleaned_data["allow_changing_vote"]
+            if poll.allow_multiple_choices and not form.cleaned_data["allow_changing_vote"]:
+                poll.pollvote_set.delete()
             # change the poll
             poll.question = form.cleaned_data["question"]
             poll.allow_changing_vote = form.cleaned_data["allow_changing_vote"]
+            poll.allow_multiple_choices = form.cleaned_data["allow_multiple_choices"]
             poll.date_limit = form.cleaned_data["date_limit"]
             poll.end_date = form.cleaned_data["end_date"]
             poll.save()
@@ -826,15 +838,13 @@
     else:
         # convert returned value "day/month/year"
         if poll.end_date:
-            get = str(poll.end_date)
-            get = get.split("-")
-
-            end_date = "%s/%s/%s" % (get[2], get[1], get[0])
+            end_date = poll.end_date.strftime("%d/%m/%Y")
         else:
-            end_date=None
+            end_date = None
         initial = {
                 "question": poll.question,
                 "allow_changing_vote": poll.allow_changing_vote,
+                "allow_multiple_choices": poll.allow_multiple_choices,
                 "date_limit": poll.date_limit,
                 "end_date": end_date,
                 }
@@ -869,15 +879,48 @@
 
     # check locks
     if forum.locked or topic.locked:
-        return HttpResponse("Forum ya da başlık kilitli")
+        return HttpResponse("Forum ya da başlık kilitlidir.")
 
-    # create or change vote
-    try:
-        vote = PollVote.objects.get(poll=poll, voter=request.user)
-        if poll.allow_changing_vote:
-            vote.option.vote_count = vote.option.pollvote_set.count() - 1
-            vote.option.save()
-            vote.delete()
+    # check date
+    if poll.date_limit and datetime.now() > poll.end_date:
+        return HttpResponse("Oylama süresi dolmuştur.")
+
+    if poll.allow_multiple_choices:
+        # select/unselect option
+        try:
+            vote = PollVote.objects.get(option=option, voter=request.user)
+            if poll.allow_changing_vote:
+                vote.option.vote_count = vote.option.pollvote_set.count() - 1
+                vote.option.save()
+                vote.delete()
+        except ObjectDoesNotExist:
+            vote = PollVote(
+                    poll=poll,
+                    option=option,
+                    voter=request.user,
+                    voter_ip=request.META.get('REMOTE_ADDR', None),
+                    )
+            vote.save()
+            option.vote_count = option.pollvote_set.count()
+            option.save()
+    else:
+        # create or change vote
+        try:
+            vote = PollVote.objects.get(poll=poll, voter=request.user)
+            if poll.allow_changing_vote:
+                vote.option.vote_count = vote.option.pollvote_set.count() - 1
+                vote.option.save()
+                vote.delete()
+                vote = PollVote(
+                        poll=poll,
+                        option=option,
+                        voter=request.user,
+                        voter_ip=request.META.get('REMOTE_ADDR', None),
+                        )
+                vote.save()
+                option.vote_count = option.pollvote_set.count()
+                option.save()
+        except ObjectDoesNotExist:
             vote = PollVote(
                     poll=poll,
                     option=option,
@@ -887,17 +930,6 @@
             vote.save()
             option.vote_count = option.pollvote_set.count()
             option.save()
-        #TODO: else: say that no changes allowed
-    except ObjectDoesNotExist:
-        vote = PollVote(
-                poll=poll,
-                option=option,
-                voter=request.user,
-                voter_ip=request.META.get('REMOTE_ADDR', None),
-                )
-        vote.save()
-        option.vote_count += 1
-        option.save()
 
     return HttpResponseRedirect(topic.get_absolute_url())
 

Modified: trunk/poll/models.py
=================================================================
--- trunk/poll/models.py	(original)
+++ trunk/poll/models.py	Wed Jun 18 20:03:05 2008
@@ -50,8 +50,8 @@
 
 class Poll(models.Model):
     question = models.CharField("Soru", max_length=128, help_text="Buraya anketin sorusunu yazın.")
-    allow_changing_vote = models.BooleanField("Oy Değiştirmek İzinli", default=False, help_text="Kullanılan oyların sonradan değiştirilebilmesini istiyorsanız bunu işaretleyin.")
-    allow_multiple_choices = models.BooleanField("Çok Seçmeli Oylama", default=False, help_text="Bir kişinin birden fazla seçenekte oy kullanabilmesini isiyorsanız bunu seçin.")
+    allow_changing_vote = models.BooleanField("Oy Değiştirmek İzinli", default=False, blank=True, help_text="Kullanılan oyların sonradan değiştirilebilmesini istiyorsanız bunu işaretleyin.")
+    allow_multiple_choices = models.BooleanField("Çok Seçmeli Oylama", default=False, blank=True, help_text="Bir kişinin birden fazla seçenekte oy kullanabilmesini isiyorsanız bunu seçin. Uyarı: Eğer seçiliyse ve sonradan kaldırılırsa o ana kadar verilen bütün oylar silinecektir.")
     date_limit = models.BooleanField("Süreli", help_text="Oylamada süre sınırı olmasını istiyorsanız bunu işaretleyin.")
     end_date = models.DateTimeField("Bitiş Tarihi", blank=True, null=True, help_text="Oylamanın ne zaman biteceğini belirleyin. 30/8/2008 gibi.")
 

Modified: trunk/templates/forum/topic.html
=================================================================
--- trunk/templates/forum/topic.html	(original)
+++ trunk/templates/forum/topic.html	Wed Jun 18 20:03:05 2008
@@ -26,10 +26,11 @@
 {{ topic.poll.question }}<br />
 <ul>
 {% for option in poll_options %}
-<li><a href="{{ forum.get_absolute_url }}{{ topic.id }}/poll/vote/{{ option.id }}/" title="oy vermek için tıklayın">{{ option.text }}</a><br /><div class="topic_poll"><div style="width:{{ option.percent }}%;background-color:#51798E;">{% if option.percent_out %}&nbsp;</div><span>%{{ option.percent }} ({{ option.vote_count }} oy)</span>{% else %}<span style="float:right;color:white;">%{{ option.percent }} ({{ option.vote_count }} oy)</span></div>{% endif %}</div></li>
+<li>{% if poll_enabled %}<a href="{{ forum.get_absolute_url }}{{ topic.id }}/poll/vote/{{ option.id }}/" title="oy vermek için tıklayın">{{ option.text }}</a>{% else %}{{ option.text }}{% endif %}<br /><div class="topic_poll"><div style="width:{{ option.percent }}%;background-color:#51798E;">{% if option.percent_out %}&nbsp;</div><span>%{{ option.percent }} ({{ option.vote_count }} oy)</span>{% else %}<span style="float:right;color:white;">%{{ option.percent }} ({{ option.vote_count }} oy)</span></div>{% endif %}</div></li>
 {% endfor %}
 </ul>
 {% if perms.forum.can_change_poll %}<a href="{{ topic.get_change_poll_url }}">change</a> - <a href="{{ topic.get_delete_poll_url }}">delete</a>{% endif %}
+{% if topic.poll.date_limit %}<br />Oylama bitiş tarihi: {{ topic.poll.end_date|date:"j F Y l" }}{% endif %}
 </div>
 {% endif %}
 


More information about the Oi-commits mailing list