Skip to content

Commit

Permalink
Use foreign keys for host retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
kholdaway committed Oct 20, 2017
1 parent f2e1886 commit f7311ff
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 29 deletions.
2 changes: 1 addition & 1 deletion quipucords/api/networkprofile_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class NetworkProfile(models.Model):
# ForeignKey in HostRange below.

def __str__(self):
return 'id:%s, name:%s' % (self.id, self.name)
return '{ id:%s, name:%s }' % (self.id, self.name)


class HostRange(models.Model):
Expand Down
95 changes: 67 additions & 28 deletions quipucords/api/networkprofile_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
"""Describes the views associated with the API models"""

from django.shortcuts import get_object_or_404
from rest_framework import status
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from django_filters.rest_framework import (DjangoFilterBackend, FilterSet)
from api.filters import ListFilter
from api.networkprofile_serializer import NetworkProfileSerializer
from api.networkprofile_model import NetworkProfile
from api.hostcredential_model import HostCredential


CREDENTIALS_KEY = 'credentials'
Expand All @@ -29,15 +27,16 @@ def expand_host_credential(profile):
create slim dictionary version of the host credential with name an value
to return to user.
"""
if profile[CREDENTIALS_KEY]:
credentials = profile[CREDENTIALS_KEY]
if profile.credentials:
credentials = profile.credentials
new_creds = []
for cred_id in credentials:
cred = HostCredential.objects.get(pk=cred_id)
slim_cred = {'id': cred_id, 'name': cred.name}

# For each cred, add subset fields to response
for cred in credentials.all():
slim_cred = {'id': cred.id, 'name': cred.name}
new_creds.append(slim_cred)
profile[CREDENTIALS_KEY] = new_creds
return profile
return new_creds
return None


class NetworkProfileFilter(FilterSet):
Expand All @@ -59,34 +58,74 @@ class NetworkProfileViewSet(ModelViewSet):
filter_class = NetworkProfileFilter

def list(self, request): # pylint: disable=unused-argument

# List objects
queryset = self.filter_queryset(self.get_queryset())
serializer = NetworkProfileSerializer(queryset, many=True)
for profile in serializer.data:
profile = expand_host_credential(profile)
return Response(serializer.data)

# For each profile, expand creds
result = []
for profile in queryset:
serializer = NetworkProfileSerializer(profile)
json_profile = serializer.data

# Create expanded host cred JSON
json_creds = profile = expand_host_credential(profile)

# Update profile JSON with cred JSON
if json_creds:
json_profile[CREDENTIALS_KEY] = json_creds

result.append(json_profile)

return Response(result)

# pylint: disable=unused-argument
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
network_profile_out = expand_host_credential(serializer.data)
return Response(network_profile_out, status=status.HTTP_201_CREATED,
headers=headers)
# Create object
response = super().create(request, args, kwargs)

# Modify json for response
json_profile = response.data
profile = get_object_or_404(self.queryset, pk=json_profile['id'])

# Create expanded host cred JSON
json_creds = profile = expand_host_credential(profile)

# Update profile JSON with cred JSON
if json_creds:
json_profile[CREDENTIALS_KEY] = json_creds

response.data = json_profile
return response

def retrieve(self, request, pk=None): # pylint: disable=unused-argument
network_profile = get_object_or_404(self.queryset, pk=pk)
serializer = NetworkProfileSerializer(network_profile)
network_profile_out = expand_host_credential(serializer.data)
return Response(network_profile_out)
# Get object
profile = get_object_or_404(self.queryset, pk=pk)
serializer = NetworkProfileSerializer(profile)
json_profile = serializer.data

# Create expanded host cred JSON
json_creds = profile = expand_host_credential(profile)

# Update profile JSON with cred JSON
if json_creds:
json_profile[CREDENTIALS_KEY] = json_creds
return Response(json_profile)

# pylint: disable=unused-argument
def update(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data,
# Update profile
profile = self.get_object()
serializer = self.get_serializer(profile, data=request.data,
partial=kwargs.get('partial', False))
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
network_profile_out = expand_host_credential(serializer.data)
return Response(network_profile_out)
json_profile = serializer.data

# Create expanded host cred JSON
json_creds = expand_host_credential(profile)

# Update profile JSON with cred JSON
if json_creds:
json_profile[CREDENTIALS_KEY] = json_creds
return Response(json_profile)

0 comments on commit f7311ff

Please sign in to comment.