# Path: django_ratings/models.py
# class TotalRate(models.Model):
#     """
#     save all rating for individual object.
#     """
#     target_ct = models.ForeignKey(ContentType, db_index=True)
#     target_id = models.PositiveIntegerField(_('Object ID'))
#     target = generic.GenericForeignKey('target_ct', 'target_id')
#     amount = models.DecimalField(_('Amount'), max_digits=10, decimal_places=2)
# 
#     objects = TotalRateManager()
# 
#     def __unicode__(self):
#         return u'%s points for %s' % (self.amount, self.target)
# 
#     class Meta:
#         verbose_name = _('Total rate')
#         verbose_name_plural = _('Total rates')
#         unique_together = (('target_ct', 'target_id',),)
# 
# Path: django_ratings/forms.py
# class RateForm(forms.Form):
#     content_type = forms.IntegerField(widget=forms.HiddenInput)
#     target = forms.IntegerField(widget=forms.HiddenInput)
# 
#     def clean_content_type(self):
#         try:
#             value = ContentType.objects.get(pk=self.cleaned_data['content_type'])
#         except models.ObjectDoesNotExist:
#             raise forms.ValidationError, _('The given ContentType object does not exist.')
#         return value
# 
#     def clean_target(self):
#         try:
#             value = self.cleaned_data['content_type'].get_object_for_this_type(pk=self.cleaned_data['content_type'])
#         except models.ObjectDoesNotExist:
#             raise forms.ValidationError, _('The given target object does not exist.')
#         return value
# 
# Path: django_ratings/views.py
# def get_was_rated(request, ct, target):
#     """
#     Returns whether object was rated by current user
# 
#     Rating can fail later on db query, this checks user cookies
#     """
#     if isinstance(ct, ContentType):
#         ct = ct.id
#     if isinstance(target, models.Model):
#         target = target.pk
#     return '%s:%s' % (ct, target) in _get_cookie(request)


from decimal import Decimal
from django import template
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.template.defaultfilters import slugify
from django_ratings.models import TotalRate
from django_ratings.forms import RateForm
from django_ratings.views import get_was_rated
from django.utils.translation import ugettext as _
from recepty import settings
    Examples::

        {% rating for object as object_rating %}
        object {{object}} has rating of {{object_rating}}

        {% rating for object max 1 step 0.5 as object_rating %}
        object {{object}} has rating of {{object_rating}} from (-1, -0.5, 0, 0.5, 1)

    """
    bits = token.split_contents()
    if len(bits) == 5 and bits[1] == 'for' and bits[3] == 'as':
        return RatingNode(bits[2], bits[4])
    if len(bits) == 9 and bits[1] == 'for' and bits[3] == 'max' \
            and bits[5] == 'step' and bits[7] == 'as':
        return RatingNode(bits[2], bits[8], bits[4], bits[6])
    if len(bits) == 11 and bits[1] == 'for' and bits[3] == 'min' \
            and bits[5] == 'max' and bits[7] == 'step' and bits[9] == 'as':
        return RatingNode(bits[2], bits[10], bits[6], bits[8], bits[4])

    raise template.TemplateSyntaxError, \
        "{% rating for OBJ as VAR %} or {% rating for OBJ max X step Y as VAR %}"

class WasRatedNode(template.Node):

    def __init__(self, object, name):
        self.object, self.name = object, name

    def render(self, context):
        object = template.Variable(self.object).resolve(context)
        ct = ContentType.objects.get_for_model(object)

# Path: django_ratings/models.py
# class TotalRate(models.Model):
#     """
#     save all rating for individual object.
#     """
#     target_ct = models.ForeignKey(ContentType, db_index=True)
#     target_id = models.PositiveIntegerField(_('Object ID'))
#     target = generic.GenericForeignKey('target_ct', 'target_id')
#     amount = models.DecimalField(_('Amount'), max_digits=10, decimal_places=2)
# 
#     objects = TotalRateManager()
# 
#     def __unicode__(self):
#         return u'%s points for %s' % (self.amount, self.target)
# 
#     class Meta:
#         verbose_name = _('Total rate')
#         verbose_name_plural = _('Total rates')
#         unique_together = (('target_ct', 'target_id',),)
# 
# Path: django_ratings/forms.py
# class RateForm(forms.Form):
#     content_type = forms.IntegerField(widget=forms.HiddenInput)
#     target = forms.IntegerField(widget=forms.HiddenInput)
# 
#     def clean_content_type(self):
#         try:
#             value = ContentType.objects.get(pk=self.cleaned_data['content_type'])
#         except models.ObjectDoesNotExist:
#             raise forms.ValidationError, _('The given ContentType object does not exist.')
#         return value
# 
#     def clean_target(self):
#         try:
#             value = self.cleaned_data['content_type'].get_object_for_this_type(pk=self.cleaned_data['content_type'])
#         except models.ObjectDoesNotExist:
#             raise forms.ValidationError, _('The given target object does not exist.')
#         return value
# 
# Path: django_ratings/views.py
# def get_was_rated(request, ct, target):
#     """
#     Returns whether object was rated by current user
# 
#     Rating can fail later on db query, this checks user cookies
#     """
#     if isinstance(ct, ContentType):
#         ct = ct.id
#     if isinstance(target, models.Model):
#         target = target.pk
#     return '%s:%s' % (ct, target) in _get_cookie(request)


# Path: django_ratings/templatetags/ratings.py
from decimal import Decimal
from django import template
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.template.defaultfilters import slugify
from django_ratings.models import TotalRate
from django_ratings.forms import RateForm
from django_ratings.views import get_was_rated
from django.utils.translation import ugettext as _
from recepty import settings

    Examples::

        {% rating for object as object_rating %}
        object {{object}} has rating of {{object_rating}}

        {% rating for object max 1 step 0.5 as object_rating %}
        object {{object}} has rating of {{object_rating}} from (-1, -0.5, 0, 0.5, 1)

    """
    bits = token.split_contents()
    if len(bits) == 5 and bits[1] == 'for' and bits[3] == 'as':
        return RatingNode(bits[2], bits[4])
    if len(bits) == 9 and bits[1] == 'for' and bits[3] == 'max' \
            and bits[5] == 'step' and bits[7] == 'as':
        return RatingNode(bits[2], bits[8], bits[4], bits[6])
    if len(bits) == 11 and bits[1] == 'for' and bits[3] == 'min' \
            and bits[5] == 'max' and bits[7] == 'step' and bits[9] == 'as':
        return RatingNode(bits[2], bits[10], bits[6], bits[8], bits[4])

    raise template.TemplateSyntaxError, \
        "{% rating for OBJ as VAR %} or {% rating for OBJ max X step Y as VAR %}"

class WasRatedNode(template.Node):

    def __init__(self, object, name):
        self.object, self.name = object, name

    def render(self, context):
        object = template.Variable(self.object).resolve(context)
        ct = ContentType.objects.get_for_model(object)

        context[self.name] = get_was_rated(context['request'], ct, object)