
from rest_framework.views import APIView
from rest_framework.response import Response

from authentication.decorators import api_auth_token_required

from core.models.group import Group

from api.serializers import ProviderSerializer


class ProviderList(APIView):
    """
    List of active providers
    """
    @api_auth_token_required
    def get(self, request):
        """
        List all providers accessible by request user
        """
        username = request.user.username
        group = Group.objects.get(name=username)
        providers = group.providers.filter(active=True,
                                           end_date=None).order_by('id')
        #providers = CoreProvider.objects.order_by('id')
        serialized_data = ProviderSerializer(providers, many=True).data
        return Response(serialized_data)


class Provider(APIView):
    """
    Show single provider
    """
    @api_auth_token_required
    def get(self, request, provider_id):
        """
        return provider if accessible by request user
        """
        username = request.user.username
        group = Group.objects.get(name=username)
        provider = group.providers.get(id=provider_id,
                                       active=True, end_date=None)

        serialized_data = ProviderSerializer(provider).data
        return Response(serialized_data)

from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response

from authentication.decorators import api_auth_token_required

from core.models.group import Group
from core.models.provider import Provider as CoreProvider

from api import failureJSON
from api.serializers import ProviderSerializer


class ProviderList(APIView):
    """
    List of active providers
    """
    @api_auth_token_required
    def get(self, request):
        """
        List all providers accessible by request user
        """
        username = request.user.username
        group = Group.objects.get(name=username)
        try:
            providers = group.providers.filter(active=True,
                                               end_date=None).order_by('id')
        except CoreProvider.DoesNotExist:
            errorObj = failureJSON([{
                'code': 404,
                'message':
                'The provider does not exist.'}])
            return Response(errorObj, status=status.HTTP_404_NOT_FOUND)
        serialized_data = ProviderSerializer(providers, many=True).data
        return Response(serialized_data)


class Provider(APIView):
    """
    Show single provider
    """
    @api_auth_token_required
    def get(self, request, provider_id):
        """
        return provider if accessible by request user
        """
        username = request.user.username
        group = Group.objects.get(name=username)
        try:
            provider = group.providers.get(id=provider_id,
                                           active=True, end_date=None)
        except CoreProvider.DoesNotExist:
            errorObj = failureJSON([{
                'code': 404,
                'message':
                'The provider does not exist.'}])
            return Response(errorObj, status=status.HTTP_404_NOT_FOUND)
        serialized_data = ProviderSerializer(provider).data
        return Response(serialized_data)

Fix problem where Provider DoesNotExist.
Fix problem where Provider DoesNotExist.

* Occurs on provider and providerlist endpoints.
* Came to attention as a side effect of fixing ATMO-176.
* Similar changes need to be made all over atmosphere. I'll
  create a ticket.

	modified:   api/provider.py

- 
+ from rest_framework import status
  from rest_framework.views import APIView
  from rest_framework.response import Response
  
  from authentication.decorators import api_auth_token_required
  
  from core.models.group import Group
+ from core.models.provider import Provider as CoreProvider
  
+ from api import failureJSON
  from api.serializers import ProviderSerializer
  
  
  class ProviderList(APIView):
      """
      List of active providers
      """
      @api_auth_token_required
      def get(self, request):
          """
          List all providers accessible by request user
          """
          username = request.user.username
          group = Group.objects.get(name=username)
+         try:
-         providers = group.providers.filter(active=True,
+             providers = group.providers.filter(active=True,
-                                            end_date=None).order_by('id')
+                                                end_date=None).order_by('id')
-         #providers = CoreProvider.objects.order_by('id')
+         except CoreProvider.DoesNotExist:
+             errorObj = failureJSON([{
+                 'code': 404,
+                 'message':
+                 'The provider does not exist.'}])
+             return Response(errorObj, status=status.HTTP_404_NOT_FOUND)
          serialized_data = ProviderSerializer(providers, many=True).data
          return Response(serialized_data)
  
  
  class Provider(APIView):
      """
      Show single provider
      """
      @api_auth_token_required
      def get(self, request, provider_id):
          """
          return provider if accessible by request user
          """
          username = request.user.username
          group = Group.objects.get(name=username)
+         try:
-         provider = group.providers.get(id=provider_id,
+             provider = group.providers.get(id=provider_id,
-                                        active=True, end_date=None)
+                                            active=True, end_date=None)
- 
+         except CoreProvider.DoesNotExist:
+             errorObj = failureJSON([{
+                 'code': 404,
+                 'message':
+                 'The provider does not exist.'}])
+             return Response(errorObj, status=status.HTTP_404_NOT_FOUND)
          serialized_data = ProviderSerializer(provider).data
          return Response(serialized_data)
  
Fix problem where Provider DoesNotExist.
## Code Before:

from rest_framework.views import APIView
from rest_framework.response import Response

from authentication.decorators import api_auth_token_required

from core.models.group import Group

from api.serializers import ProviderSerializer


class ProviderList(APIView):
    """
    List of active providers
    """
    @api_auth_token_required
    def get(self, request):
        """
        List all providers accessible by request user
        """
        username = request.user.username
        group = Group.objects.get(name=username)
        providers = group.providers.filter(active=True,
                                           end_date=None).order_by('id')
        #providers = CoreProvider.objects.order_by('id')
        serialized_data = ProviderSerializer(providers, many=True).data
        return Response(serialized_data)


class Provider(APIView):
    """
    Show single provider
    """
    @api_auth_token_required
    def get(self, request, provider_id):
        """
        return provider if accessible by request user
        """
        username = request.user.username
        group = Group.objects.get(name=username)
        provider = group.providers.get(id=provider_id,
                                       active=True, end_date=None)

        serialized_data = ProviderSerializer(provider).data
        return Response(serialized_data)

## Instruction:
Fix problem where Provider DoesNotExist.
## Code After:
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response

from authentication.decorators import api_auth_token_required

from core.models.group import Group
from core.models.provider import Provider as CoreProvider

from api import failureJSON
from api.serializers import ProviderSerializer


class ProviderList(APIView):
    """
    List of active providers
    """
    @api_auth_token_required
    def get(self, request):
        """
        List all providers accessible by request user
        """
        username = request.user.username
        group = Group.objects.get(name=username)
        try:
            providers = group.providers.filter(active=True,
                                               end_date=None).order_by('id')
        except CoreProvider.DoesNotExist:
            errorObj = failureJSON([{
                'code': 404,
                'message':
                'The provider does not exist.'}])
            return Response(errorObj, status=status.HTTP_404_NOT_FOUND)
        serialized_data = ProviderSerializer(providers, many=True).data
        return Response(serialized_data)


class Provider(APIView):
    """
    Show single provider
    """
    @api_auth_token_required
    def get(self, request, provider_id):
        """
        return provider if accessible by request user
        """
        username = request.user.username
        group = Group.objects.get(name=username)
        try:
            provider = group.providers.get(id=provider_id,
                                           active=True, end_date=None)
        except CoreProvider.DoesNotExist:
            errorObj = failureJSON([{
                'code': 404,
                'message':
                'The provider does not exist.'}])
            return Response(errorObj, status=status.HTTP_404_NOT_FOUND)
        serialized_data = ProviderSerializer(provider).data
        return Response(serialized_data)
