[Oi-commits] r1479 - in trunk: forum templates/forum

oi-commits at pardus.org.tr oi-commits at pardus.org.tr
Tue Jun 17 19:53:07 EEST 2008


Author: jnmbk
Date: Tue Jun 17 19:53:07 2008
New Revision: 1479

Modified:
   trunk/forum/urls.py
   trunk/forum/views.py
   trunk/templates/forum/topic.html
Log:
voting to polls

Modified: trunk/forum/urls.py
=================================================================
--- trunk/forum/urls.py	(original)
+++ trunk/forum/urls.py	Tue Jun 17 19:53:07 2008
@@ -46,6 +46,7 @@
     (r'^(?P<forum_slug>.*)/(?P<topic_id>\d+)/edit/$', 'oi.forum.views.edit_topic'),
     (r'^(?P<forum_slug>.*)/(?P<topic_id>\d+)/poll/create/$', 'oi.forum.views.create_poll'),
     (r'^(?P<forum_slug>.*)/(?P<topic_id>\d+)/poll/change/$', 'oi.forum.views.change_poll'),
+    (r'^(?P<forum_slug>.*)/(?P<topic_id>\d+)/poll/vote/(?P<option_id>\d+)/$', 'oi.forum.views.vote_poll'),
     (r'^(?P<forum_slug>.*)/(?P<topic_id>\d+)/$', 'oi.forum.views.topic'),
     (r'^(?P<forum_slug>.*)/$', 'oi.forum.views.forum'),
 )

Modified: trunk/forum/views.py
=================================================================
--- trunk/forum/views.py	(original)
+++ trunk/forum/views.py	Tue Jun 17 19:53:07 2008
@@ -25,7 +25,7 @@
 from django.core.exceptions import ObjectDoesNotExist
 from django.core.mail import send_mail
 from oi.st.models import Tag, News
-from oi.poll.models import Poll, PollOption
+from oi.poll.models import Poll, PollOption, PollVote
 
 # import our function for sending e-mails and setting
 from oi.st.wrappers import send_mail_with_header
@@ -711,11 +711,11 @@
     if forum.slug != forum_slug:
         return HttpResponseRedirect(topic.get_create_poll_url())
 
-    # Check locks
+    # check locks
     if forum.locked or topic.locked:
         return HttpResponse('Forum or topic is locked')
 
-    # Check if it already has a poll
+    # check if it already has a poll
     try:
         topic.poll
         return HttpResponse('Bu konuya zaten anket eklenmiş')
@@ -766,7 +766,7 @@
     except: #DoesNotExist
         return HttpResponseRedirect(topic.get_create_poll_url())
 
-    # Check locks
+    # check locks
     if forum.locked or topic.locked:
         return HttpResponse('Forum or topic is locked')
 
@@ -823,3 +823,55 @@
         form = PollForm(initial=initial)
 
     return render_response(request, "forum/change_poll.html", locals())
+
+ at login_required
+def vote_poll(request,forum_slug,topic_id,option_id):
+    topic = get_object_or_404(Topic, pk=topic_id)
+    option = get_object_or_404(PollOption, pk=option_id)
+    forum = topic.forum
+    if forum.slug != forum_slug:
+        return HttpResponseRedirect(topic.get_absolute_url())
+
+    # check poll
+    try:
+        poll = topic.poll
+    except: #DoesNotExist
+        return HttpResponseRedirect(topic.get_absolute_url())
+
+    # check if this option belongs to the poll
+    if option.poll != poll:
+        return HttpResponseRedirect(topic.get_absolute_url())
+
+    # check locks
+    if forum.locked or topic.locked:
+        return HttpResponse("Forum ya da başlık kilitli")
+
+    # create or change vote
+    try:
+        vote = PollVote.objects.get(poll=poll, voter=request.user)
+        if poll.allow_changing_vote:
+            vote.option.vote_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 += 1
+            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/templates/forum/topic.html
=================================================================
--- trunk/templates/forum/topic.html	(original)
+++ trunk/templates/forum/topic.html	Tue Jun 17 19:53:07 2008
@@ -22,11 +22,15 @@
 <br />
 
 {% if topic.poll %}
+<div class="poll">
 {{ topic.poll.question }}<br />
+<ul>
 {% for option in topic.poll.polloption_set.all %}
-{{ option.text }}<br />
+<li><a href="{{ forum.get_absolute_url }}{{ topic.id }}/poll/vote/{{ option.id }}/" title="oy ver">{{ option.text }} ({{ option.vote_count }})</a></li>
 {% endfor %}
+</ul>
 {% if perms.forum.can_change_poll %}<a href="{{ topic.get_change_poll_url }}">change</a> - delete{% endif %}
+</div>
 {% endif %}
 
 {% for post in post_list %}


More information about the Oi-commits mailing list