from __future__ import absolute_import, unicode_literals
from rest_framework import viewsets, serializers
from physical import models


class EnvironmentSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = models.Environment
        fields = ('url', 'id', 'name',)


class EnvironmentAPI(viewsets.ReadOnlyModelViewSet):

    """
    Environment API
    """
    serializer_class = EnvironmentSerializer
    queryset = models.Environment.objects.all()

from __future__ import absolute_import, unicode_literals
from rest_framework import viewsets, serializers
from physical.models import Environment


class EnvironmentSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Environment
        fields = ('url', 'id', 'name', 'stage', 'provisioner')


class EnvironmentAPI(viewsets.ReadOnlyModelViewSet):

    """
    Environment API
    """
    model = Environment
    serializer_class = EnvironmentSerializer
    queryset = Environment.objects.all()
    filter_fields = (
        'id',
        'name',
        'stage',
        'provisioner'
    )

    def get_queryset(self):
        params = self.request.GET.dict()
        filter_params = {}
        for k, v in params.iteritems():
            if k == 'get_provisioner_by_label':
                if hasattr(self.model, v.upper()):
                    label_id = getattr(self.model, v.upper())
                    filter_params['provisioner'] = label_id
                else:
                    return self.model.objects.none()
            elif k.split('__')[0] in self.filter_fields:
                filter_params[k] = v
        return self.model.objects.filter(**filter_params)

Add povisioner and stage on env API
Add povisioner and stage on env API

  from __future__ import absolute_import, unicode_literals
  from rest_framework import viewsets, serializers
- from physical import models
+ from physical.models import Environment
  
  
  class EnvironmentSerializer(serializers.HyperlinkedModelSerializer):
  
      class Meta:
-         model = models.Environment
+         model = Environment
-         fields = ('url', 'id', 'name',)
+         fields = ('url', 'id', 'name', 'stage', 'provisioner')
  
  
  class EnvironmentAPI(viewsets.ReadOnlyModelViewSet):
  
      """
      Environment API
      """
+     model = Environment
      serializer_class = EnvironmentSerializer
-     queryset = models.Environment.objects.all()
+     queryset = Environment.objects.all()
+     filter_fields = (
+         'id',
+         'name',
+         'stage',
+         'provisioner'
+     )
  
+     def get_queryset(self):
+         params = self.request.GET.dict()
+         filter_params = {}
+         for k, v in params.iteritems():
+             if k == 'get_provisioner_by_label':
+                 if hasattr(self.model, v.upper()):
+                     label_id = getattr(self.model, v.upper())
+                     filter_params['provisioner'] = label_id
+                 else:
+                     return self.model.objects.none()
+             elif k.split('__')[0] in self.filter_fields:
+                 filter_params[k] = v
+         return self.model.objects.filter(**filter_params)
+ 
Add povisioner and stage on env API
## Code Before:
from __future__ import absolute_import, unicode_literals
from rest_framework import viewsets, serializers
from physical import models


class EnvironmentSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = models.Environment
        fields = ('url', 'id', 'name',)


class EnvironmentAPI(viewsets.ReadOnlyModelViewSet):

    """
    Environment API
    """
    serializer_class = EnvironmentSerializer
    queryset = models.Environment.objects.all()

## Instruction:
Add povisioner and stage on env API
## Code After:
from __future__ import absolute_import, unicode_literals
from rest_framework import viewsets, serializers
from physical.models import Environment


class EnvironmentSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Environment
        fields = ('url', 'id', 'name', 'stage', 'provisioner')


class EnvironmentAPI(viewsets.ReadOnlyModelViewSet):

    """
    Environment API
    """
    model = Environment
    serializer_class = EnvironmentSerializer
    queryset = Environment.objects.all()
    filter_fields = (
        'id',
        'name',
        'stage',
        'provisioner'
    )

    def get_queryset(self):
        params = self.request.GET.dict()
        filter_params = {}
        for k, v in params.iteritems():
            if k == 'get_provisioner_by_label':
                if hasattr(self.model, v.upper()):
                    label_id = getattr(self.model, v.upper())
                    filter_params['provisioner'] = label_id
                else:
                    return self.model.objects.none()
            elif k.split('__')[0] in self.filter_fields:
                filter_params[k] = v
        return self.model.objects.filter(**filter_params)
