# 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

        {% rate_url for object as tpl_var %}

    Example::

        {% rate_url for object as r_url %}
        <form action="{{r_url}}" method="POST">
            <input type="text" name="rating" value="0"/>
            <input type="submit" value="Rate it"/>
        </form>
    """
    bits = token.split_contents()
    if len(bits) != 5:
        raise template.TemplateSyntaxError('rate_rul template tag should be used like this: {% rate_url for object as tpl_var %}')
    return RateUrlNode(bits[2], bits[4])

class RatingNode(template.Node):
    def __init__(self, object, name, max=None, step=None, min2=None):
        self.object, self.name = object, name
        self.min, self.max, self.step, self.min2 = min, max, step, min2

    def render(self, context):
        obj = template.Variable(self.object).resolve(context)
        if obj:
            value = 0
            if (self.min != None and self.max!=None and self.min2 != None):
                self.step = Decimal(self.step)
                self.min2 = Decimal(self.min2)
                self.max = Decimal(self.max)
                possible_values = int((self.max - self.min2)/self.step+1)

# 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


        {% rate_url for object as tpl_var %}

    Example::

        {% rate_url for object as r_url %}
        <form action="{{r_url}}" method="POST">
            <input type="text" name="rating" value="0"/>
            <input type="submit" value="Rate it"/>
        </form>
    """
    bits = token.split_contents()
    if len(bits) != 5:
        raise template.TemplateSyntaxError('rate_rul template tag should be used like this: {% rate_url for object as tpl_var %}')
    return RateUrlNode(bits[2], bits[4])

class RatingNode(template.Node):
    def __init__(self, object, name, max=None, step=None, min2=None):
        self.object, self.name = object, name
        self.min, self.max, self.step, self.min2 = min, max, step, min2

    def render(self, context):
        obj = template.Variable(self.object).resolve(context)
        if obj:
            value = 0
            if (self.min != None and self.max!=None and self.min2 != None):
                self.step = Decimal(self.step)
                self.min2 = Decimal(self.min2)
                self.max = Decimal(self.max)
                possible_values = int((self.max - self.min2)/self.step+1)

                value = TotalRate.objects.get_normalized_rating(obj, 1, Decimal("1.0")/(possible_values/2))