# Path: tracking/models.py
# class Rate(models.Model):
#     objects = RateQuerySet.as_manager()
# 
#     id = models.UUIDField(primary_key=True,
#                           editable=False,
#                           default=uuid4,
#                           unique=True)
# 
#     user = models.ForeignKey(User)
#     description = models.CharField(max_length=120)
#     amount = models.DecimalField(max_digits=8, decimal_places=2)
#     days = models.PositiveIntegerField(validators=[MinValueValidator(1)])
#     amount_per_day = models.DecimalField(max_digits=8,
#                                          decimal_places=3,
#                                          editable=False,
#                                          blank=True)
# 
#     def save(self, *args, **kwargs):
#         self.amount_per_day = self.amount / Decimal(self.days)
#         return super(Rate, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return '{0} ({1})'.format(self.description,
#                                   self.amount_per_day)
# 
#     class Meta:
#         ordering = ('-amount_per_day', )
# 
# class Transaction(models.Model):
#     objects = TransactionQuerySet.as_manager()
# 
#     id = models.UUIDField(primary_key=True,
#                           editable=False,
#                           default=uuid4,
#                           unique=True)
# 
#     user = models.ForeignKey(User)
#     description = models.CharField(max_length=120)
#     amount = models.DecimalField(max_digits=8, decimal_places=2)
#     timestamp = models.DateTimeField(default=timezone.now)
# 
#     def __unicode__(self):
#         return '{0} ({1})'.format(self.description,
#                                   self.amount)
# 
#     class Meta:
#         ordering = ('-timestamp', )


from rest_framework import serializers
from django.utils import timezone
from pytz import utc
from dateutil import parser
from tracking.models import Rate, Transaction

    def to_internal_value(self, value):
        if getattr(value, 'astimezone', False):
            utc_value = value.astimezone(utc)
        else:
            naive = parser.parse(value)
            utc_value = naive.astimezone(utc)
        return super(TimeZoneDateTimeField, self).to_internal_value(utc_value)


def assign_user(func):
    def wrapper(self, valid_data):
        request = self.context.get('request')
        valid_data['user'] = request.user
        return func(self, valid_data)
    return wrapper


class RateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Rate
        exclude = ()


class RateCreateSerializer(serializers.ModelSerializer):
    @assign_user
    def create(self, *args, **kwargs):
        return super(RateCreateSerializer, self).create(*args, **kwargs)

    class Meta:

# Path: tracking/models.py
# class Rate(models.Model):
#     objects = RateQuerySet.as_manager()
# 
#     id = models.UUIDField(primary_key=True,
#                           editable=False,
#                           default=uuid4,
#                           unique=True)
# 
#     user = models.ForeignKey(User)
#     description = models.CharField(max_length=120)
#     amount = models.DecimalField(max_digits=8, decimal_places=2)
#     days = models.PositiveIntegerField(validators=[MinValueValidator(1)])
#     amount_per_day = models.DecimalField(max_digits=8,
#                                          decimal_places=3,
#                                          editable=False,
#                                          blank=True)
# 
#     def save(self, *args, **kwargs):
#         self.amount_per_day = self.amount / Decimal(self.days)
#         return super(Rate, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return '{0} ({1})'.format(self.description,
#                                   self.amount_per_day)
# 
#     class Meta:
#         ordering = ('-amount_per_day', )
# 
# class Transaction(models.Model):
#     objects = TransactionQuerySet.as_manager()
# 
#     id = models.UUIDField(primary_key=True,
#                           editable=False,
#                           default=uuid4,
#                           unique=True)
# 
#     user = models.ForeignKey(User)
#     description = models.CharField(max_length=120)
#     amount = models.DecimalField(max_digits=8, decimal_places=2)
#     timestamp = models.DateTimeField(default=timezone.now)
# 
#     def __unicode__(self):
#         return '{0} ({1})'.format(self.description,
#                                   self.amount)
# 
#     class Meta:
#         ordering = ('-timestamp', )


# Path: tracking/serializers.py
from rest_framework import serializers
from django.utils import timezone
from pytz import utc
from dateutil import parser
from tracking.models import Rate, Transaction


    def to_internal_value(self, value):
        if getattr(value, 'astimezone', False):
            utc_value = value.astimezone(utc)
        else:
            naive = parser.parse(value)
            utc_value = naive.astimezone(utc)
        return super(TimeZoneDateTimeField, self).to_internal_value(utc_value)


def assign_user(func):
    def wrapper(self, valid_data):
        request = self.context.get('request')
        valid_data['user'] = request.user
        return func(self, valid_data)
    return wrapper


class RateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Rate
        exclude = ()


class RateCreateSerializer(serializers.ModelSerializer):
    @assign_user
    def create(self, *args, **kwargs):
        return super(RateCreateSerializer, self).create(*args, **kwargs)

    class Meta:

        model = Rate