# Path: bakery/socialize/managers.py
# class VoteManager(Manager):
# 
#     use_for_related_fields = True
# 
#     def get_query_set(self):
#         return VoteQuerySet(self.model)
# 
#     def get_for_cookie(self, cookie):
#         return self.get_query_set().get_for_cookie(cookie)
# 
#     def get_for_user(self, user):
#         return self.get_query_set().get_for_user(user)
# 
#     def get_for_user_and_cookie(self, user, cookie):
#         return self.get_query_set().get_for_user_and_cookie(user, cookie)
# 
#     def has_voted(self, user, cookie):
#         return self.get_query_set().has_voted(user, cookie)


import random
from django.db import models, transaction
from django.utils.translation import ugettext_lazy as _
from bakery.socialize.managers import VoteManager
# -*- coding: utf-8 -*-





def do_vote(user, cookie):
    assert user and cookie
    if not Vote.objects.has_voted(user, cookie):
        with transaction.commit_on_success():
            vote = Vote.objects.create(cookie=cookie, user=user)
            candy_type = random.choice(CANDIES)
            Candy.objects.create(candy_type=candy_type[0], user=user, vote=vote)


def do_unvote(user, cookie):
    assert user and cookie
    Vote.objects.get_for_user_and_cookie(user, cookie).delete()


class Vote(models.Model):
    cookie = models.ForeignKey('cookies.Cookie')
    user = models.ForeignKey('auth.BakeryUser')

    datetime = models.DateTimeField(auto_now_add=True)


# Path: bakery/socialize/managers.py
# class VoteManager(Manager):
# 
#     use_for_related_fields = True
# 
#     def get_query_set(self):
#         return VoteQuerySet(self.model)
# 
#     def get_for_cookie(self, cookie):
#         return self.get_query_set().get_for_cookie(cookie)
# 
#     def get_for_user(self, user):
#         return self.get_query_set().get_for_user(user)
# 
#     def get_for_user_and_cookie(self, user, cookie):
#         return self.get_query_set().get_for_user_and_cookie(user, cookie)
# 
#     def has_voted(self, user, cookie):
#         return self.get_query_set().has_voted(user, cookie)


# Path: bakery/socialize/models.py
import random
from django.db import models, transaction
from django.utils.translation import ugettext_lazy as _
from bakery.socialize.managers import VoteManager

# -*- coding: utf-8 -*-





def do_vote(user, cookie):
    assert user and cookie
    if not Vote.objects.has_voted(user, cookie):
        with transaction.commit_on_success():
            vote = Vote.objects.create(cookie=cookie, user=user)
            candy_type = random.choice(CANDIES)
            Candy.objects.create(candy_type=candy_type[0], user=user, vote=vote)


def do_unvote(user, cookie):
    assert user and cookie
    Vote.objects.get_for_user_and_cookie(user, cookie).delete()


class Vote(models.Model):
    cookie = models.ForeignKey('cookies.Cookie')
    user = models.ForeignKey('auth.BakeryUser')

    datetime = models.DateTimeField(auto_now_add=True)


    objects = VoteManager()