Commits

Ian Lewis committed 05ca970

Pollに投稿したら、UserVoteができるようにしました

  • Participants
  • Parent commits 66a5d9b
  • Branches google_login

Comments (0)

Files changed (4)

File handson/polls/admin.py

-from polls.models import Poll, Choice
+from polls.models import Poll, Choice,UserVote
 from django.contrib import admin
 
 class ChoiceInline(admin.TabularInline):
     search_fields = ['question']
     date_hierarchy = 'pub_date'
 
+class UserVoteAdmin(admin.ModelAdmin):
+    fieldsets = [
+        (None,               {'fields': ['user','poll','choice']}),
+        ('Date information', {'fields': ['vote_date'], 'classes': ['collapse']}),
+    ]
+    list_display = ('user', 'poll', 'choice')
+    list_filter = ['poll']
+
 admin.site.register(Poll, PollAdmin)
 admin.site.register(Choice)
+admin.site.register(UserVote, UserVoteAdmin)

File handson/polls/models.py

 # -*- coding: utf-8 -*-
 from django.utils.translation import ugettext_lazy as _
+from django.contrib.auth.models import User
 from google.appengine.ext import db
 import datetime
 
                                default=0)
     def __unicode__(self):
         return self.choice
+
+class UserVote(db.Model):
+    poll = db.ReferenceProperty(Poll)
+    choice = db.ReferenceProperty(Choice) # == parent
+    user = db.ReferenceProperty(User, verbose_name='user')
+    vote_date = db.DateTimeProperty(verbose_name='date voted',
+                                    auto_now_add=True)
+
+    def __unicode__(self):
+        return self.user.__unicode__()

File handson/polls/views.py

 from django.http import HttpResponse, HttpResponseRedirect
 from django.utils.translation import ugettext as _
 from django.shortcuts import render_to_response
-from polls.models import Poll, Choice
+from polls.models import Poll, Choice, UserVote
 from django.template import Context, loader
-from ragendja.dbutils import get_object_or_404
+from ragendja.dbutils import get_object_or_404,db_create,transaction
 from django.core.urlresolvers import reverse
 from google.appengine.ext import db
 
             'poll': p,
             'error_message': "You didn't select a choice.",
         })
+    import logging
+    logging.debug(request.user.__class__)
+    @transaction
     def countup_vote(key):
       c = Choice.get(key)
       c.votes += 1
       c.put()
-    db.run_in_transaction(countup_vote, selected_choice.key())
+      db_create(UserVote,
+        parent = c,
+        poll = p,
+        choice = c,
+        user = request.user,
+        non_transactional = True, # already in a transaction
+      )
+
+    countup_vote(selected_choice.key())
+
     return HttpResponseRedirect(reverse('polls.views.results',
                                         args=(p.key().id(),)))
 

File handson/settings.py

 AUTH_USER_MODULE = 'ragendja.auth.google_models'
 AUTH_ADMIN_MODULE = 'ragendja.auth.google_admin'
 # Hybrid Django/Google authentication
-#AUTH_USER_MODULE = 'ragendja.auth.hybrid_models'
+AUTH_USER_MODULE = 'ragendja.auth.hybrid_models'
 
 GLOBALTAGS = (
     'ragendja.templatetags.ragendjatags',