# Path: pinder/peering_requests/models.py
# class Request(models.Model):
# 
#     STATE_WAITING = "waiting"
#     STATE_IN_PROGRESS = "in-progress"
#     STATE_HOLD = "hold"
#     STATE_REJECTED = "rejected"
#     STATE_FINISHED = "finished"
# 
#     STATES = (
#         (STATE_WAITING, "Waiting"),
#         (STATE_IN_PROGRESS, "In Progress"),
#         (STATE_HOLD, "On Hold"),
#         (STATE_REJECTED, "Rejected"),
#         (STATE_FINISHED, "Finished"),
#     )
# 
#     sender = models.ForeignKey(
#         settings.AUTH_USER_MODEL, related_name="requests_received")
#     receiver = models.ForeignKey("users.Isp", related_name="requests_sent")
#     ixlan_id = models.PositiveIntegerField()
# 
#     state = models.CharField(
#         choices=STATES, default=STATE_WAITING, max_length=8)
# 
#     sender_is_ready = models.BooleanField(default=False)
#     receiver_is_ready = models.BooleanField(default=False)
# 
#     created = models.DateTimeField(auto_now_add=True)
#     modified = models.DateTimeField(auto_now=True)
# 
#     def __str__(self):
#         return "Peering request from {} to {}".format(
#             self.sender, self.receiver)
# 
# Path: pinder/peering_requests/serializers.py
# class RequestSerializer(serializers.ModelSerializer):
# 
#     sender = UserSerializer(read_only=True)
#     receiver = IspSerializer(read_only=True)
# 
#     class Meta(object):
#         model = Request
#         fields = (
#             "id",
#             "sender",
#             "receiver",
#             "ixlan_id",
#             "state",
#             "sender_is_ready",
#             "receiver_is_ready",
#             "created",
#             "modified"
#         )
# 
# class PostRequestSerializer(serializers.ModelSerializer):
# 
#     class Meta(object):
#         model = Request
#         fields = (
#             "id",
#             "sender",
#             "receiver",
#             "ixlan_id",
#         )
# 
# Path: pinder/peering_requests/filters.py
# class RequestFilterSet(django_filters.FilterSet):
# 
#     class Meta:
#         model = Request
#         fields = (
#             "state",
#             "sender_is_ready",
#             "receiver_is_ready",
#             "ixlan_id"
#         )


from django.views.generic import DetailView, ListView
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import AllowAny
from rest_framework.renderers import JSONRenderer, BrowsableAPIRenderer
from rest_framework.viewsets import ModelViewSet
from .models import Request
from .serializers import RequestSerializer, PostRequestSerializer
from .filters import RequestFilterSet




class StandardPagination(PageNumberPagination):
    page_size = 25
    page_size_query_param = "page-size"
    max_page_size = 100000


class RequestViewSet(ModelViewSet):
    model = Request
    queryset = Request.objects.all()
    serializer_class = RequestSerializer
    pagination_class = StandardPagination
    permission_classes = (AllowAny,)
    filter_backends = (DjangoFilterBackend, filters.OrderingFilter)
    filter_class = RequestFilterSet
    ordering_fields = ("sender", "receiver", "created", "modified")
    renderer_classes = (JSONRenderer, BrowsableAPIRenderer)

    def get_serializer_class(self, *args, **kwargs):
        if self.request.method == "POST":

# Path: pinder/peering_requests/models.py
# class Request(models.Model):
# 
#     STATE_WAITING = "waiting"
#     STATE_IN_PROGRESS = "in-progress"
#     STATE_HOLD = "hold"
#     STATE_REJECTED = "rejected"
#     STATE_FINISHED = "finished"
# 
#     STATES = (
#         (STATE_WAITING, "Waiting"),
#         (STATE_IN_PROGRESS, "In Progress"),
#         (STATE_HOLD, "On Hold"),
#         (STATE_REJECTED, "Rejected"),
#         (STATE_FINISHED, "Finished"),
#     )
# 
#     sender = models.ForeignKey(
#         settings.AUTH_USER_MODEL, related_name="requests_received")
#     receiver = models.ForeignKey("users.Isp", related_name="requests_sent")
#     ixlan_id = models.PositiveIntegerField()
# 
#     state = models.CharField(
#         choices=STATES, default=STATE_WAITING, max_length=8)
# 
#     sender_is_ready = models.BooleanField(default=False)
#     receiver_is_ready = models.BooleanField(default=False)
# 
#     created = models.DateTimeField(auto_now_add=True)
#     modified = models.DateTimeField(auto_now=True)
# 
#     def __str__(self):
#         return "Peering request from {} to {}".format(
#             self.sender, self.receiver)
# 
# Path: pinder/peering_requests/serializers.py
# class RequestSerializer(serializers.ModelSerializer):
# 
#     sender = UserSerializer(read_only=True)
#     receiver = IspSerializer(read_only=True)
# 
#     class Meta(object):
#         model = Request
#         fields = (
#             "id",
#             "sender",
#             "receiver",
#             "ixlan_id",
#             "state",
#             "sender_is_ready",
#             "receiver_is_ready",
#             "created",
#             "modified"
#         )
# 
# class PostRequestSerializer(serializers.ModelSerializer):
# 
#     class Meta(object):
#         model = Request
#         fields = (
#             "id",
#             "sender",
#             "receiver",
#             "ixlan_id",
#         )
# 
# Path: pinder/peering_requests/filters.py
# class RequestFilterSet(django_filters.FilterSet):
# 
#     class Meta:
#         model = Request
#         fields = (
#             "state",
#             "sender_is_ready",
#             "receiver_is_ready",
#             "ixlan_id"
#         )


# Path: pinder/peering_requests/views.py
from django.views.generic import DetailView, ListView
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import AllowAny
from rest_framework.renderers import JSONRenderer, BrowsableAPIRenderer
from rest_framework.viewsets import ModelViewSet
from .models import Request
from .serializers import RequestSerializer, PostRequestSerializer
from .filters import RequestFilterSet





class StandardPagination(PageNumberPagination):
    page_size = 25
    page_size_query_param = "page-size"
    max_page_size = 100000


class RequestViewSet(ModelViewSet):
    model = Request
    queryset = Request.objects.all()
    serializer_class = RequestSerializer
    pagination_class = StandardPagination
    permission_classes = (AllowAny,)
    filter_backends = (DjangoFilterBackend, filters.OrderingFilter)
    filter_class = RequestFilterSet
    ordering_fields = ("sender", "receiver", "created", "modified")
    renderer_classes = (JSONRenderer, BrowsableAPIRenderer)

    def get_serializer_class(self, *args, **kwargs):
        if self.request.method == "POST":

            return PostRequestSerializer