from rest_framework import viewsets
from core.models import QuotaRequest, IdentityMembership
from api.v2.serializers.details import QuotaRequestSerializer
from rest_framework.permissions import IsAuthenticated, IsAdminUser


class QuotaRequestViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows providers to be viewed or edited.
    """
    queryset = QuotaRequest.objects.all()
    serializer_class = QuotaRequestSerializer
    permission_classes = (IsAuthenticated,)
    filter_fields = ('status__id', 'status__name')
    http_method_names = ['get', 'post', 'put', 'patch', 'head', 'options', 'trace']

    def perform_create(self, serializer):
        identity_id = serializer.initial_data.get('identity')
        membership = IdentityMembership.objects.get(identity=identity_id)
        serializer.save(
            membership=membership,
            created_by=self.request.user
        )

    def get_queryset(self):
        """
        Filter quota requests by current user
        """
        user = self.request.user
        return QuotaRequest.objects.filter(created_by=user)

    def get_permissions(self):
        method = self.request.method
        if method == 'PUT':
            self.permission_classes = (IsAdminUser,)

        return super(viewsets.ModelViewSet, self).get_permissions()

@@ -16,49 +16,75 @@
 work
- import viewsets%0Afrom core.models
+.permissions import IsAuthenticated%0A%0Afrom api.v2.views.base
  import 
 Quot
@@ -83,40 +83,26 @@
 ort 
-Quota
+Base
 Request
-, IdentityMembership
+ViewSet
 %0Afro
@@ -163,76 +163,116 @@
 izer
-%0Afrom rest_framework.permissions import IsAuthenticated, IsAdminUser
+, UserQuotaRequestSerializer%0Afrom core.models import QuotaRequest%0Afrom web.emails import quota_request_email
 %0A%0A%0Ac
@@ -296,30 +296,27 @@
 ViewSet(
-viewsets.Model
+BaseRequest
 ViewSet)
@@ -358,17 +358,21 @@
 ows 
-providers
+quota request
  to 
@@ -440,18 +440,100 @@
 cts.
-all()%0A    
+none()%0A    model = QuotaRequest%0A    serializer_class = UserQuotaRequestSerializer%0A    admin_
 seri
@@ -669,270 +669,202 @@
 e')%0A
+%0A
     
-http_method_names = %5B'get', 'post', 'put', 'patch', 'head', 'options', 'trace'%5D%0A%0A    def perform_create(self, serializer):%0A        identity_id = serializer.initial_data.get('identity')%0A        membership = IdentityMembership.objects.get(identity=identity_id)
+def submit_action(self, instance):%0A        %22%22%22%0A        Submits a quota request email%0A        %22%22%22%0A        requested_quota = instance.request%0A        reason_for_request = instance.description
 %0A   
@@ -872,332 +872,408 @@
     
+u
 ser
-ializer.save(%0A            membership=membership,%0A            created_by=self.request.user%0A        )%0A%0A    def get_queryset(self):%0A        %22%22%22%0A        Filter quota requests by current user%0A        %22%22%22%0A        user = self.request.user%0A        return QuotaRequest.objects.filter(created_by=user)%0A%0A    def get_permiss
+name = self.request.user.username%0A        quota_request_email(self.request, username, requested_quota, reason_for_request)%0A%0A    def approve_action(self, instance):%0A        %22%22%22%0A        Updates the quota for the request%0A        %22%22%22%0A        membership = instance.membership%0A        membership.quota = instance.quota%0A        membership.approve_quota(instance.uuid)%0A%0A    def deny_act
 ion
-s
 (self
+, instance
 ):%0A 
@@ -1283,183 +1283,72 @@
     
-method = self.request.method%0A        if method == 'PUT':%0A            self.permission_classes = (IsAdminUser,)%0A%0A        return super(viewsets.ModelViewSet, self).get_permissions()
+%22%22%22%0A        Notify the user that the request was denied%0A        %22%22%22
 %0A

switch quota request to request viewset