# Path: images/models.py
# class Image(models.Model):
#     name = models.CharField(max_length=30)
#     photo_url = models.URLField()
#     photo_thumbnail_url = models.URLField()
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
#     product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='images')
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name
# 
# Path: products/permissions.py
# class ProductPermission(BasePermission):
# 
#     def has_permission(self, request, view):
# 
#         if view.action in ['list', 'metadata', 'retrieve']:
#             return True
#         elif view.action in ['create', 'update']:
#             return request.user.is_authenticated()
#         else:
#             return request.user.is_authenticated() and request.user.is_staff
# 
# class UserProductPermission(BasePermission):
# 
#     def has_permission(self, request, view):
#         return request.user.is_authenticated()
# 
# Path: products/serializers.py
# class ProductsSerializer(serializers.ModelSerializer):
# 
#     class Meta:
#         model = Product
#         fields = ('id', 'name', 'race', 'gender', 'sterile', 'description', 'state', 'price', 'category',
#                   'active', 'created_at')
# 
# class ProductsListSerializer(serializers.ModelSerializer):
# 
#     race = serializers.StringRelatedField()
#     state = serializers.StringRelatedField()
#     category = serializers.StringRelatedField()
#     images = ImageSerializer(many=True)
#     seller = UserPublicListSerializer()
# 
#     class Meta:
#         model = Product
#         fields = ('id', 'name', 'race', 'gender', 'seller', 'sterile', 'description', 'state', 'price', 'category',
#                   'active', 'latitude', 'longitude', 'created_at', 'updated_at', 'images', 'raceid', 'stateid', 'categoryid')
# 
# Path: products/models.py
# class Product(models.Model):
#     name = models.CharField(max_length=30)
#     race = models.ForeignKey(Race, null=True, related_name='race')
#     seller = models.ForeignKey(UserDetail, null=False)
#     gender = models.CharField(max_length=3, choices=GENDERS, default='NON')
#     sterile = models.BooleanField(default=False)
#     description = models.CharField(max_length=250, default='')
#     state = models.ForeignKey(State, null=True, default=1)
#     price = models.FloatField(null=False, default=0)
#     category = models.ForeignKey(Category)
#     active = models.BooleanField(null=False, default=True)
#     location = models.PointField(null=False)
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     def _get_latitude(self):
#         return str(self.location.y)
#     latitude = property(_get_latitude)
# 
#     def _get_longitude(self):
#         return str(self.location.x)
#     longitude = property(_get_longitude)
# 
#     def _get_race_id(self):
#         return str(self.race.id)
#     raceid = property(_get_race_id)
# 
#     def _get_state_id(self):
#         return str(self.state.id)
#     stateid = property(_get_state_id)
# 
#     def _get_category_id(self):
#         return str(self.category.id)
#     categoryid = property(_get_category_id)
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name
# 
# Path: races/models.py
# class Race(models.Model):
# 
#     name = models.CharField(max_length=40)
#     active = models.BooleanField(default=True)
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name
# 
# Path: states/models.py
# class State(models.Model):
#     name = models.CharField(max_length=30)
#     active = models.BooleanField(default=True)
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name


import uuid
import boto3
from boto3.session import Session
from django.contrib.gis.geos import LineString, Point
from django.shortcuts import get_object_or_404
from math import cos, radians
from rest_framework import status
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from images.models import Image
from products.permissions import ProductPermission, UserProductPermission
from products.serializers import ProductsSerializer, ProductsListSerializer
from products.models import Product
from races.models import Race
from states.models import State
            queryset = queryset.filter(category=category_id_filter)

        if name_filter is not None:
            queryset = queryset.filter(name__contains=name_filter)

        page = self.paginate_queryset(queryset)

        if page is not None:
            serializer = ProductsListSerializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = ProductsListSerializer(queryset, many=True)
        return Response(serializer.data)

    def retrieve(self, request, pk):

        queryset = self.filter_queryset(self.get_queryset())
        product = get_object_or_404(queryset, pk=pk)
        serializer = ProductsListSerializer(product)
        return Response(serializer.data)

    def create(self, request, **kwargs):

        serializer = self.get_serializer(data=request.data)

        latitude = self.request.POST.get('latitude', None)
        longitude = self.request.POST.get('longitude', None)

        if serializer.is_valid():


# Path: images/models.py
# class Image(models.Model):
#     name = models.CharField(max_length=30)
#     photo_url = models.URLField()
#     photo_thumbnail_url = models.URLField()
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
#     product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='images')
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name
# 
# Path: products/permissions.py
# class ProductPermission(BasePermission):
# 
#     def has_permission(self, request, view):
# 
#         if view.action in ['list', 'metadata', 'retrieve']:
#             return True
#         elif view.action in ['create', 'update']:
#             return request.user.is_authenticated()
#         else:
#             return request.user.is_authenticated() and request.user.is_staff
# 
# class UserProductPermission(BasePermission):
# 
#     def has_permission(self, request, view):
#         return request.user.is_authenticated()
# 
# Path: products/serializers.py
# class ProductsSerializer(serializers.ModelSerializer):
# 
#     class Meta:
#         model = Product
#         fields = ('id', 'name', 'race', 'gender', 'sterile', 'description', 'state', 'price', 'category',
#                   'active', 'created_at')
# 
# class ProductsListSerializer(serializers.ModelSerializer):
# 
#     race = serializers.StringRelatedField()
#     state = serializers.StringRelatedField()
#     category = serializers.StringRelatedField()
#     images = ImageSerializer(many=True)
#     seller = UserPublicListSerializer()
# 
#     class Meta:
#         model = Product
#         fields = ('id', 'name', 'race', 'gender', 'seller', 'sterile', 'description', 'state', 'price', 'category',
#                   'active', 'latitude', 'longitude', 'created_at', 'updated_at', 'images', 'raceid', 'stateid', 'categoryid')
# 
# Path: products/models.py
# class Product(models.Model):
#     name = models.CharField(max_length=30)
#     race = models.ForeignKey(Race, null=True, related_name='race')
#     seller = models.ForeignKey(UserDetail, null=False)
#     gender = models.CharField(max_length=3, choices=GENDERS, default='NON')
#     sterile = models.BooleanField(default=False)
#     description = models.CharField(max_length=250, default='')
#     state = models.ForeignKey(State, null=True, default=1)
#     price = models.FloatField(null=False, default=0)
#     category = models.ForeignKey(Category)
#     active = models.BooleanField(null=False, default=True)
#     location = models.PointField(null=False)
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     def _get_latitude(self):
#         return str(self.location.y)
#     latitude = property(_get_latitude)
# 
#     def _get_longitude(self):
#         return str(self.location.x)
#     longitude = property(_get_longitude)
# 
#     def _get_race_id(self):
#         return str(self.race.id)
#     raceid = property(_get_race_id)
# 
#     def _get_state_id(self):
#         return str(self.state.id)
#     stateid = property(_get_state_id)
# 
#     def _get_category_id(self):
#         return str(self.category.id)
#     categoryid = property(_get_category_id)
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name
# 
# Path: races/models.py
# class Race(models.Model):
# 
#     name = models.CharField(max_length=40)
#     active = models.BooleanField(default=True)
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name
# 
# Path: states/models.py
# class State(models.Model):
#     name = models.CharField(max_length=30)
#     active = models.BooleanField(default=True)
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name


# Path: products/api.py
import uuid
import boto3
from boto3.session import Session
from django.contrib.gis.geos import LineString, Point
from django.shortcuts import get_object_or_404
from math import cos, radians
from rest_framework import status
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from images.models import Image
from products.permissions import ProductPermission, UserProductPermission
from products.serializers import ProductsSerializer, ProductsListSerializer
from products.models import Product
from races.models import Race
from states.models import State

            queryset = queryset.filter(category=category_id_filter)

        if name_filter is not None:
            queryset = queryset.filter(name__contains=name_filter)

        page = self.paginate_queryset(queryset)

        if page is not None:
            serializer = ProductsListSerializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = ProductsListSerializer(queryset, many=True)
        return Response(serializer.data)

    def retrieve(self, request, pk):

        queryset = self.filter_queryset(self.get_queryset())
        product = get_object_or_404(queryset, pk=pk)
        serializer = ProductsListSerializer(product)
        return Response(serializer.data)

    def create(self, request, **kwargs):

        serializer = self.get_serializer(data=request.data)

        latitude = self.request.POST.get('latitude', None)
        longitude = self.request.POST.get('longitude', None)

        if serializer.is_valid():


            state = get_object_or_404(State, pk=1)