def _update_application(self, request, app, **kwargs): data = request.DATA user = request.user data = request.DATA app_owner = app.created_by app_members = app.get_members() if user != app_owner and not Group.check_membership(user, app_members): return failure_response( status.HTTP_403_FORBIDDEN, "You are not the Application owner. " "This incident will be reported") #Or it wont.. Up to operations.. partial_update = True if request.method == 'PATCH' else False serializer = ApplicationSerializer(app, data=data, context={'request': request}, partial=partial_update) if serializer.is_valid(): logger.info('metadata = %s' % data) #TODO: Update application metadata on each machine? #update_machine_metadata(esh_driver, esh_machine, data) serializer.save() if 'created_by_identity' in data: identity = serializer.object.created_by_identity update_application_owner(serializer.object, identity) if 'boot_scripts' in data: _save_scripts_to_application(serializer.object, data.get('boot_scripts', [])) return Response(serializer.data) return failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
def _update_application(self, request, app, **kwargs): data = request.DATA user = request.user data = request.DATA app_owner = app.created_by app_members = app.get_members() if user != app_owner and not Group.check_membership(user, app_members): return failure_response(status.HTTP_403_FORBIDDEN, "You are not the Application owner. " "This incident will be reported") #Or it wont.. Up to operations.. partial_update = True if request.method == 'PATCH' else False serializer = ApplicationSerializer(app, data=data, context={'request': request}, partial=partial_update) if serializer.is_valid(): logger.info('metadata = %s' % data) #TODO: Update application metadata on each machine? #update_machine_metadata(esh_driver, esh_machine, data) serializer.save() if 'created_by_identity' in data: identity = serializer.object.created_by_identity update_application_owner(serializer.object, identity) if 'boot_scripts' in data: _save_scripts_to_application(serializer.object, data.get('boot_scripts',[])) return Response(serializer.data) return failure_response( status.HTTP_400_BAD_REQUEST, serializer.errors)
def applications(self, request, pk=None): obj = self.get_queryset(request.user).get(pk=pk) apps = obj.application_set.all() queryset = apps page = self.paginate_queryset(queryset, request=request) if page is not None: serializer = ApplicationSerializer(page, many=True, context={'request': request}) return self.get_paginated_response(serializer.data) serializer = ApplicationSerializer(queryset, many=True, context={'request': request}) return Response(serializer.data, status=status.HTTP_200_OK)
def get(self, request): """ Using provider and identity, getlist of machines TODO: Cache this request """ applications = Application.objects.filter(private=False) serialized_data = ApplicationSerializer(applications, many=True).data response = Response(serialized_data) return response
def get(self, request): """ """ user = request.user applications = user.application_set.filter(only_active(), projects=None) serialized_data = ApplicationSerializer(applications, many=True, context={ "request": request }).data response = Response(serialized_data) return response
def patch(self, request, app_uuid, **kwargs): """ Update specific application Params:app_uuid -- Unique ID of application """ user = request.user data = request.DATA app = CoreApplication.objects.filter(uuid=app_uuid) if not app: return failure_response(status.HTTP_404_NOT_FOUND, "Application with uuid %s does not exist" % app_uuid) app = app[0] app_owner = app.created_by app_members = app.get_members() if user != app_owner and not any(group for group in user.group_set.all() if group in app_members): return failure_response(status.HTTP_403_FORBIDDEN, "You are not the Application owner. " "This incident will be reported") partial_update = kwargs.get('_partial',True) serializer = ApplicationSerializer(app, data=data, context={'user':request.user}, partial=partial_update) if serializer.is_valid(): logger.info('metadata = %s' % data) #TODO: Update application metadata on each machine? #update_machine_metadata(esh_driver, esh_machine, data) serializer.save() logger.info(serializer.data) return Response(serializer.data) return failure_response( status.HTTP_400_BAD_REQUEST, serializer.errors)
def get(self, request, project_id): """ """ user = request.user group = get_user_group(user.username) #TODO: Check that you have permission! projects = group.projects.get(id=project_id) applications = projects.applications.filter(only_current) serialized_data = ApplicationSerializer(applications, many=True, context={ "user": request.user }).data response = Response(serialized_data) return response
def get(self, request, **kwargs): """Authentication optional, list of applications.""" request_user = kwargs.get('request_user') applications = public_applications() #Concatenate 'visible' if request.user and type(request.user) != AnonymousUser: my_apps = visible_applications(request.user) applications.extend(my_apps) serialized_data = ApplicationSerializer(applications, context={ 'user': request.user }, many=True).data response = Response(serialized_data) return response
def get(self, request, app_uuid, **kwargs): """ Details of specific application's Threshold. Params:app_uuid -- Unique ID of Application """ app = CoreApplication.objects.filter(uuid=app_uuid) if not app: return failure_response( status.HTTP_404_NOT_FOUND, "Application with uuid %s does not exist" % app_uuid) app = app[0] serialized_data = ApplicationSerializer(app, context={ 'request': request }).data response = Response(serialized_data) return response
def get(self, request, project_uuid): """ """ user = request.user group = get_user_group(user.username) project = get_group_project(group, project_uuid) if not project: return Response("Project with ID=%s does not exist" % project_uuid, status=status.HTTP_400_BAD_REQUEST) #user = request.user #group = get_user_group(user.username) ##TODO: Check that you have permission! #projects = get_group_project(group, project_uuid) applications = project.applications.filter(only_current()) serialized_data = ApplicationSerializer(applications, many=True, context={ "request": request }).data response = Response(serialized_data) return response
def get(self, request): """""" data = request.DATA query = request.QUERY_PARAMS.get('query') if not query: return failure_response(status.HTTP_400_BAD_REQUEST, "Query not provided.") identity_id = request.QUERY_PARAMS.get('identity') identity = Identity.objects.filter(id=identity_id) #Empty List or identity found.. if identity: identity = identity[0] #Okay to search w/ identity=None search_result = search([CoreApplicationSearch], query, identity) page = request.QUERY_PARAMS.get('page') if page: paginator = Paginator(search_result, 20) try: search_page = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. search_page = paginator.page(1) except EmptyPage: # Page is out of range. # deliver last page of results. search_page = paginator.page(paginator.num_pages) serialized_data = \ PaginatedApplicationSerializer( search_page, context={'user':request.user}).data else: serialized_data = ApplicationSerializer(search_result, context={ 'user': request.user }).data response = Response(serialized_data) response['Cache-Control'] = 'no-cache' return response