# 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

#class RateUrlsNode(template.Node):
#    def __init__(self, object, up_name, down_name, form_name=None):
#        self.object, self.up_name, self.down_name = object, up_name, down_name
#        self.form_name = form_name
#
#    def render(self, context):
#        obj = template.Variable(self.object).resolve(context)
#        if obj and hasattr(obj, 'get_absolute_url'):
#            context[self.up_name] = '%s%s/%s/' % (obj.get_absolute_url(), _('rate'), _('up'))
#            context[self.down_name] = '%s%s/%s/' % (obj.get_absolute_url(), _('rate'), _('down'))
#        elif obj:
#            ct = ContentType.objects.get_for_model(obj)
#            context[self.form_name] = RateForm(initial={'content_type' : ct.id, 'target' : obj._get_pk_val()})
#            context[self.up_name] = reverse('rate_up')
#            context[self.down_name] = reverse('rate_down')
#        return ''

class RateUrlNode(template.Node):
    def __init__(self, object, url_var_name, form_name=None):
        self.object = object
        self.url_var_name =url_var_name
        self.form_name = form_name

    def render(self, context):
        obj = template.Variable(self.object).resolve(context)
        if obj and hasattr(obj, 'get_absolute_url'):
            context[self.url_var_name] = '%s%s/' % (obj.get_absolute_url(), slugify(_('rate')))
        elif obj:
            ct = ContentType.objects.get_for_model(obj)

# 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


#class RateUrlsNode(template.Node):
#    def __init__(self, object, up_name, down_name, form_name=None):
#        self.object, self.up_name, self.down_name = object, up_name, down_name
#        self.form_name = form_name
#
#    def render(self, context):
#        obj = template.Variable(self.object).resolve(context)
#        if obj and hasattr(obj, 'get_absolute_url'):
#            context[self.up_name] = '%s%s/%s/' % (obj.get_absolute_url(), _('rate'), _('up'))
#            context[self.down_name] = '%s%s/%s/' % (obj.get_absolute_url(), _('rate'), _('down'))
#        elif obj:
#            ct = ContentType.objects.get_for_model(obj)
#            context[self.form_name] = RateForm(initial={'content_type' : ct.id, 'target' : obj._get_pk_val()})
#            context[self.up_name] = reverse('rate_up')
#            context[self.down_name] = reverse('rate_down')
#        return ''

class RateUrlNode(template.Node):
    def __init__(self, object, url_var_name, form_name=None):
        self.object = object
        self.url_var_name =url_var_name
        self.form_name = form_name

    def render(self, context):
        obj = template.Variable(self.object).resolve(context)
        if obj and hasattr(obj, 'get_absolute_url'):
            context[self.url_var_name] = '%s%s/' % (obj.get_absolute_url(), slugify(_('rate')))
        elif obj:
            ct = ContentType.objects.get_for_model(obj)

            context[self.form_name] = RateForm(initial={'content_type' : ct.id, 'target' : obj._get_pk_val()})