def update(self, request, pk=None): if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) key = self._get_key(pk) logger.debug('Got GET IWC Data request for key {0!s}'.format(key)) logger.debug('request.data: {0!s}'.format(request.data)) instance = model_access.get_data_resource(request.user.username, key) if instance: serializer = serializers.DataResourceSerializer(instance, data=request.data, context={ 'request': request, 'key': key }, partial=True) if not serializer.is_valid(): logger.error('{0!s}'.format(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializer.save() resp = serializer.data resp = hal.add_hal_structure( resp, request, hal.generate_content_type(request.accepted_media_type)) return Response(resp, status=status.HTTP_200_OK) else: serializer = serializers.DataResourceSerializer(data=request.data, context={ 'request': request, 'key': key }, partial=True) if not serializer.is_valid(): logger.error('ERROR: {0!s}'.format(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializer.save() resp = serializer.data resp = hal.add_hal_structure( resp, request, hal.generate_content_type(request.accepted_media_type)) return Response(resp, status=status.HTTP_201_CREATED)
def IntentView(request, id='0'): """ Single intent """ if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) root_url = hal.get_abs_url_for_iwc( request ) # flake8: noqa TODO: Is Necessary? - Variable not being used in method profile = model_access.get_profile( request.user.username ) # flake8: noqa TODO: Is Necessary? - Variable not being used in method queryset = intent_model_access.get_intent_by_id(id) if not queryset: return Response(status=status.HTTP_404_NOT_FOUND) serializer = intent_serializers.IntentSerializer( queryset, context={'request': request}) data = serializer.data data = hal.add_hal_structure( data, request, hal.generate_content_type(request.accepted_media_type)) return Response(data)
def retrieve(self, request, pk): if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) # listing_root_url = hal.get_abs_url_for_iwc(request) # flake8: noqa TODO: Is Necessary? - Variable not being used in method # data = hal.create_base_structure(request, # flake8: noqa TODO: Is Necessary? - Variable not being used in method # hal.generate_content_type( # request.accepted_media_type)) key = self._get_key(pk) logger.debug('Got GET IWC Data request for key {0!s}'.format(key)) instance = model_access.get_data_resource(request.user.username, key) if not instance: return Response(status=status.HTTP_404_NOT_FOUND) serializer = serializers.DataResourceSerializer(instance, data=request.data, context={'request': request, 'key': key}, partial=True) if not serializer.is_valid(): logger.error('{0!s}'.format(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) resp = serializer.data resp = hal.add_hal_structure(resp, request, hal.generate_content_type( request.accepted_media_type)) return Response(resp, status=status.HTTP_200_OK)
def ApplicationView(request, id='0'): """ Single application """ if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) listing_root_url = hal.get_abs_url_for_iwc(request) profile = model_access.get_profile(request.user.username) # TODO: only include the fields that are necessary for IWC. This will also # allow us to sever ties with ozpcenter.api.listing.serializers # This minimal definition of what a Listing object must have should be # advertised so that others can use IWC with their own systems queryset = listing_model_access.get_listing_by_id(profile.user.username, id) if not queryset: return Response(status=status.HTTP_404_NOT_FOUND) serializer = listing_serializers.ListingSerializer(queryset, context={'request': request}) data = serializer.data data = hal.add_hal_structure(data, request, hal.generate_content_type( request.accepted_media_type)) return Response(data)
def retrieve(self, request, pk): if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) # listing_root_url = hal.get_abs_url_for_iwc(request) # flake8: noqa TODO: Is Necessary? - Variable not being used in method # data = hal.create_base_structure(request, # flake8: noqa TODO: Is Necessary? - Variable not being used in method # hal.generate_content_type( # request.accepted_media_type)) key = self._get_key(pk) logger.debug('Got GET IWC Data request for key {0!s}'.format(key)) instance = model_access.get_data_resource(request.user.username, key) if not instance: return Response(status=status.HTTP_404_NOT_FOUND) serializer = serializers.DataResourceSerializer(instance, data=request.data, context={ 'request': request, 'key': key }, partial=True) if not serializer.is_valid(): logger.error('{0!s}'.format(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) resp = serializer.data resp = hal.add_hal_structure( resp, request, hal.generate_content_type(request.accepted_media_type)) return Response(resp, status=status.HTTP_200_OK)
def ApplicationView(request, id='0'): """ Single application """ if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) listing_root_url = hal.get_abs_url_for_iwc( request ) # flake8: noqa TODO: Is Necessary? - Variable not being used in method profile = model_access.get_profile(request.user.username) # TODO: only include the fields that are necessary for IWC. This will also # allow us to sever ties with ozpcenter.api.listing.serializers # This minimal definition of what a Listing object must have should be # advertised so that others can use IWC with their own systems queryset = listing_model_access.get_listing_by_id(profile.user.username, id) if not queryset: return Response(status=status.HTTP_404_NOT_FOUND) serializer = listing_serializers.ListingSerializer( queryset, context={'request': request}) data = serializer.data data = hal.add_hal_structure( data, request, hal.generate_content_type(request.accepted_media_type)) return Response(data)
def ListDataApiView(request): """ List all data entries for the user """ if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) listing_root_url = hal.get_abs_url_for_iwc( request ) # flake8: noqa TODO: Is Necessary? - Variable not being used in method data = hal.create_base_structure( request, hal.generate_content_type(request.accepted_media_type)) keys = model_access.get_all_keys(request.user.username) embedded_items = [] for k in keys: # remove the leading / k = k[1:] url = hal.get_abs_url_for_iwc(request) + 'self/data/' + k data = hal.add_link_item( url, data, hal.generate_content_type( renderers.DataObjectResourceRenderer.media_type)) # add data items to _embedded key = '/' + k try: instance = model_access.get_data_resource(request.user.username, key) if not instance: return Response(status=status.HTTP_404_NOT_FOUND) serializer = serializers.DataResourceSerializer(instance, data=request.data, context={ 'request': request, 'key': key }, partial=True) if not serializer.is_valid(): logger.error('{0!s}'.format(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) item = hal.add_hal_structure( serializer.data, request, hal.generate_content_type( renderers.DataObjectResourceRenderer.media_type)) item['_links']['self']['href'] += k embedded_items.append(item) except Exception as e: # TODO debug raise e data['_embedded']['item'] = embedded_items return Response(data)
def list(self, request): if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) profile = model_access.get_profile(request.user.username) data = {'username': profile.user.username, 'id': profile.id, 'display_name': profile.display_name} data = hal.add_hal_structure(data, request, hal.generate_content_type( request.accepted_media_type)) return Response(data)
def update(self, request, pk=None): if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) key = self._get_key(pk) logger.debug('Got GET IWC Data request for key {0!s}'.format(key)) logger.debug('request.data: {0!s}'.format(request.data)) instance = model_access.get_data_resource(request.user.username, key) if instance: serializer = serializers.DataResourceSerializer(instance, data=request.data, context={'request': request, 'key': key}, partial=True) if not serializer.is_valid(): logger.error('{0!s}'.format(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializer.save() resp = serializer.data resp = hal.add_hal_structure(resp, request, hal.generate_content_type( request.accepted_media_type)) return Response(resp, status=status.HTTP_200_OK) else: serializer = serializers.DataResourceSerializer( data=request.data, context={'request': request, 'key': key}, partial=True) if not serializer.is_valid(): logger.error('ERROR: {0!s}'.format(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializer.save() resp = serializer.data resp = hal.add_hal_structure(resp, request, hal.generate_content_type( request.accepted_media_type)) return Response(resp, status=status.HTTP_201_CREATED)
def ListDataApiView(request): """ List all data entries for the user """ if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) listing_root_url = hal.get_abs_url_for_iwc(request) data = hal.create_base_structure(request, hal.generate_content_type(request.accepted_media_type)) keys = model_access.get_all_keys(request.user.username) embedded_items = [] for k in keys: # remove the leading / k = k[1:] url = hal.get_abs_url_for_iwc(request) + 'self/data/' + k data = hal.add_link_item(url, data, hal.generate_content_type( renderers.DataObjectResourceRenderer.media_type)) # add data items to _embedded key = '/' + k try: instance = model_access.get_data_resource(request.user.username, key) if not instance: return Response(status=status.HTTP_404_NOT_FOUND) serializer = serializers.DataResourceSerializer(instance, data=request.data, context={'request': request, 'key': key}, partial=True) if not serializer.is_valid(): logger.error('%s' % serializer.errors) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) item = hal.add_hal_structure(serializer.data, request, hal.generate_content_type( renderers.DataObjectResourceRenderer.media_type)) item['_links']['self']['href'] += k embedded_items.append(item) except Exception as e: # TODO debug raise e data['_embedded']['item'] = embedded_items return Response(data)
def UserView(request): """ User info """ if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) profile = model_access.get_profile(request.user.username) data = { 'username': profile.user.username, 'id': profile.id, 'display_name': profile.display_name } data = hal.add_hal_structure( data, request, hal.generate_content_type(request.accepted_media_type)) return Response(data)
def retrieve(self, request, pk): if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) root_url = hal.get_abs_url_for_iwc(request) # flake8: noqa TODO: Is Necessary? - Variable not being used in method profile = model_access.get_profile(request.user.username) # flake8: noqa TODO: Is Necessary? - Variable not being used in method queryset = intent_model_access.get_intent_by_id(pk) if not queryset: return Response(status=status.HTTP_404_NOT_FOUND) serializer = intent_serializers.IntentSerializer(queryset, context={'request': request}) data = serializer.data data = hal.add_hal_structure(data, request, hal.generate_content_type(request.accepted_media_type)) return Response(data)
def IntentView(request, id='0'): """ Single intent """ if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) root_url = hal.get_abs_url_for_iwc(request) profile = model_access.get_profile(request.user.username) queryset = intent_model_access.get_intent_by_id(id) if not queryset: return Response(status=status.HTTP_404_NOT_FOUND) serializer = intent_serializers.IntentSerializer(queryset, context={'request': request}) data = serializer.data data = hal.add_hal_structure(data, request, hal.generate_content_type(request.accepted_media_type)) return Response(data)
def DataApiView(request, key=None): """ Data API --- request_serializer: serializers.DataResourceSerializer """ # ensure key starts with a / and does not end with one if not key.startswith('/'): key = '/' + key if key.endswith('/'): key = key[:-1] logger.debug('Got IWC Data request for key {0!s}'.format(key)) if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) listing_root_url = hal.get_abs_url_for_iwc( request ) # flake8: noqa TODO: Is Necessary? - Variable not being used in method data = hal.create_base_structure( request, # flake8: noqa TODO: Is Necessary? - Variable not being used in method hal.generate_content_type(request.accepted_media_type)) if request.method == 'PUT': try: logger.debug('request.data: {0!s}'.format(request.data)) instance = model_access.get_data_resource(request.user.username, key) if instance: serializer = serializers.DataResourceSerializer( instance, data=request.data, context={ 'request': request, 'key': key }, partial=True) if not serializer.is_valid(): logger.error('{0!s}'.format(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializer.save() resp = serializer.data resp = hal.add_hal_structure( resp, request, hal.generate_content_type(request.accepted_media_type)) return Response(resp, status=status.HTTP_200_OK) else: serializer = serializers.DataResourceSerializer( data=request.data, context={ 'request': request, 'key': key }, partial=True) if not serializer.is_valid(): logger.error('ERROR: {0!s}'.format(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializer.save() resp = serializer.data resp = hal.add_hal_structure( resp, request, hal.generate_content_type(request.accepted_media_type)) return Response(resp, status=status.HTTP_201_CREATED) except Exception as e: # TODO debug # raise e return Response(str(e), status=status.HTTP_400_BAD_REQUEST) if request.method == 'GET': try: instance = model_access.get_data_resource(request.user.username, key) if not instance: return Response(status=status.HTTP_404_NOT_FOUND) serializer = serializers.DataResourceSerializer(instance, data=request.data, context={ 'request': request, 'key': key }, partial=True) if not serializer.is_valid(): logger.error('{0!s}'.format(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) resp = serializer.data resp = hal.add_hal_structure( resp, request, hal.generate_content_type(request.accepted_media_type)) return Response(resp, status=status.HTTP_200_OK) except Exception as e: # TODO debug raise e return Response(str(e), status=status.HTTP_400_BAD_REQUEST) if request.method == 'DELETE': try: instance = model_access.get_data_resource(request.user.username, key) if instance: instance.delete() else: return Response(status=status.HTTP_404_NOT_FOUND) return Response(status=status.HTTP_204_NO_CONTENT) except Exception as e: # TODO debug raise e return Response(str(e), status=status.HTTP_400_BAD_REQUEST)
def DataApiView(request, key=None): """ Data API --- request_serializer: serializers.DataResourceSerializer """ # ensure key starts with a / and does not end with one if not key.startswith('/'): key = '/' + key if key.endswith('/'): key = key[:-1] logger.debug('Got IWC Data request for key %s' % key) if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) listing_root_url = hal.get_abs_url_for_iwc(request) data = hal.create_base_structure(request, hal.generate_content_type( request.accepted_media_type)) if request.method == 'PUT': try: logger.debug('request.data: %s' % request.data) instance = model_access.get_data_resource(request.user.username, key) if instance: serializer = serializers.DataResourceSerializer(instance, data=request.data, context={'request': request, 'key': key}, partial=True) if not serializer.is_valid(): logger.error('%s' % serializer.errors) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializer.save() resp = serializer.data resp = hal.add_hal_structure(resp, request, hal.generate_content_type( request.accepted_media_type)) return Response(resp, status=status.HTTP_200_OK) else: serializer = serializers.DataResourceSerializer( data=request.data, context={'request': request, 'key': key}, partial=True) if not serializer.is_valid(): logger.error('ERROR: %s' % serializer.errors) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializer.save() resp = serializer.data resp = hal.add_hal_structure(resp, request, hal.generate_content_type( request.accepted_media_type)) return Response(resp, status=status.HTTP_201_CREATED) except Exception as e: # TODO debug # raise e return Response(str(e), status=status.HTTP_400_BAD_REQUEST) if request.method == 'GET': try: instance = model_access.get_data_resource(request.user.username, key) if not instance: return Response(status=status.HTTP_404_NOT_FOUND) serializer = serializers.DataResourceSerializer(instance, data=request.data, context={'request': request, 'key': key}, partial=True) if not serializer.is_valid(): logger.error('%s' % serializer.errors) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) resp = serializer.data resp = hal.add_hal_structure(resp, request, hal.generate_content_type( request.accepted_media_type)) return Response(resp, status=status.HTTP_200_OK) except Exception as e: # TODO debug raise e return Response(str(e), status=status.HTTP_400_BAD_REQUEST) if request.method == 'DELETE': try: instance = model_access.get_data_resource(request.user.username, key) if instance: instance.delete() else: return Response(status=status.HTTP_404_NOT_FOUND) return Response(status=status.HTTP_204_NO_CONTENT) except Exception as e: # TODO debug raise e return Response(str(e), status=status.HTTP_400_BAD_REQUEST)