from django.utils.translation import ugettext_lazy as _
from pygeocoder import Geocoder
from rest_framework import serializers

from tarambay.events.models import Category, Event


class CategorySerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.UUIDField(source='uuid')

    class Meta:
        model = Category
        fields = ('id', 'self', 'name', 'description')
        extra_kwargs = {
            'self': {'lookup_field': 'uuid'}
        }


class EventSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.UUIDField(source='uuid', read_only=True)

    class Meta:
        model = Event
        fields = ('id', 'self', 'category', 'title', 'description', 'latitude',
                  'longitude', 'start', 'end', 'admin', 'tags')
        extra_kwargs = {
            'self': {'lookup_field': 'uuid'},
            'admin': {'lookup_field': 'uuid'},
            'category': {'lookup_field': 'uuid'},
        }


class CreateEventSerializer(EventSerializer):
    location = serializers.CharField(write_only=True, required=False, allow_blank=True)
    latitude = serializers.CharField(required=False, allow_blank=True)
    longitude = serializers.CharField(required=False, allow_blank=True)

    class Meta:
        model = Event
        fields = ('id', 'self', 'category', 'title', 'description', 'location',
                  'latitude', 'longitude', 'start', 'end', 'tags')
        extra_kwargs = {
            'self': {'lookup_field': 'uuid'},
            'admin': {'lookup_field': 'uuid'},
            'category': {'lookup_field': 'uuid'},
        }

    def validate(self, data):
        """
        Check that location is a valid address
        """
        location = data.pop('location', None)
        if location:
            result = Geocoder.geocode(location)
            if result.valid_address:
                data['latitude'] = result.latitude
                data['longitude'] = result.longitude
            else:
                raise serializers.ValidationError(_("That is not a valid location."))
        elif not self.partial:
            latitude = data.get('latitude', None)
            longitude = data.get('longitude', None)
            if not latitude or not longitude:
                raise serializers.ValidationError(_(
                    "Either a valid address for location should be provided or"
                    " latitude and longitude should be provided."))
        if self.partial:
            return data
        data['admin'] = self.context['request'].user
        return data

    def update(self, instance, validated_data):
        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()
        return instance

@@ -1734,32 +1734,108 @@
 ocation', None)%0A
+        start = data.get('start', None)%0A        end = data.get('end', None)%0A
         if locat
@@ -2128,24 +2128,206 @@
 ocation.%22))%0A
+        if start and end:%0A            if end %3C= start:%0A            raise serializers.ValidationError(%22The end date and time must be %5C%0A                after the start date and time%22)%0A
         elif
@@ -3007,8 +3007,114 @@
 nstance%0A
+%0A%0Aclass InviteEventSerializer(EventSerializer):%0A    class Meta:%0A        fields = ('id', 'self', 'invited')

add event start and end validation