def debug_add_provisioner_role(request): """ Temporarily add provisioner role for Debug purposes, :param request: :return: """ user = request.user if not hasattr(user, 'provider'): provider_name = 'Debug Provider - ' + user.username p = Provider(name=provider_name, crams_user=user) p.save() new_role = CRAMS_PROVISIONER_ROLE return _add_debug_role(user, new_role)
def validate_user_provisions_product(self, product): """validate user provisions product.""" current_user = self.get_current_user() if Provider.is_provider(current_user) and \ current_user.provider == product.provider: return True raise ValidationError( {'message': 'User is not a provider for product {}'.format( repr(product))})
def validate_user_provisions_product(self, product): """validate user provisions product.""" current_user = self.get_current_user() if Provider.is_provider(current_user) and \ current_user.provider == product.provider: return True raise ValidationError({ 'message': 'User is not a provider for product {}'.format(repr(product)) })
def filter_provision_project(self, project_obj): """ :param project_obj: :return: """ self._setActionState() context_request = self.cramsActionState.rest_request current_user = context_request.user pd_context = ProvisionDetailsSerializer.show_error_msg_context() ret_list = [] if not Provider.is_provider(current_user): return ret_list status_filter_set = ProvisionDetails.READY_TO_SEND_SET new_only = context_request.query_params.get('new_request', None) if not new_only: status_filter_set = \ status_filter_set.union(ProvisionDetails.SET_OF_SENT) provider_provision_details = fetch_project_provision_list_for_provider( project_obj, current_user.provider) if not provider_provision_details.exists(): # new Project project_provision = ProjectProvisionDetails(project=project_obj) new_project_pd = new_provision_detail(current_user, status=ProvisionDetails.SENT) project_provision.provision_details = new_project_pd project_provision.save() status_filter_set.union(ProvisionDetails.SENT) query_set = provider_provision_details.filter( provision_details__status__in=status_filter_set) for pp in query_set.all(): pd = pp.provision_details if pd.status == ProvisionDetails.POST_PROVISION_UPDATE: pd.status = ProvisionDetails.POST_PROVISION_UPDATE_SENT pd.save() pd_serializer = ProvisionDetailsSerializer(pd, context=pd_context) ret_list.append(pd_serializer.data) return ret_list
def _filter_common(self, baseQuerySet, getRepresentationFn): self._setActionState() context_request = self.cramsActionState.rest_request current_user = context_request.user ret_list = [] if not Provider.is_provider(current_user): return ret_list query_filter = Q(provision_details__isnull=True) | Q( provision_details__status__in=ProvisionDetails.READY_TO_SEND_SET) new_only = context_request.query_params.get('new_request', None) if not new_only: query_filter = query_filter | Q( provision_details__status__in=ProvisionDetails.SET_OF_SENT) query_set = baseQuerySet.filter(query_filter) valid_provider = fetch_active_provider_object_for_user(current_user) if baseQuerySet.model == ComputeRequest: query_set = query_set.filter( compute_product__provider=valid_provider) elif baseQuerySet.model == StorageRequest: query_set = query_set.filter( storage_product__provider=valid_provider) for productRequest in query_set: ret_list.append(getRepresentationFn(productRequest)) provision_details = productRequest.provision_details update_status = ProvisionDetails.READY_TO_SEND_SET update_sent_status = ProvisionDetails.POST_PROVISION_UPDATE_SENT if provision_details: if provision_details.status in update_status: provision_details.status = update_sent_status provision_details.save() else: # new Product Request # - Create new Sent Provision details for Product Request pd = new_provision_detail(current_user) productRequest.provision_details = pd productRequest.save() return ret_list
def filter_provision_project(self, project_obj): """ :param project_obj: :return: """ self._setActionState() context_request = self.cramsActionState.rest_request current_user = context_request.user pd_context = ProvisionDetailsSerializer.show_error_msg_context() ret_list = [] if not Provider.is_provider(current_user): return ret_list status_filter_set = ProvisionDetails.READY_TO_SEND_SET new_only = context_request.query_params.get('new_request', None) if not new_only: status_filter_set = \ status_filter_set.union(ProvisionDetails.SET_OF_SENT) provider_provision_details = fetch_project_provision_list_for_provider( project_obj, current_user.provider) if not provider_provision_details.exists(): # new Project project_provision = ProjectProvisionDetails(project=project_obj) new_project_pd = new_provision_detail( current_user, status=ProvisionDetails.SENT) project_provision.provision_details = new_project_pd project_provision.save() status_filter_set.union(ProvisionDetails.SENT) query_set = provider_provision_details.filter( provision_details__status__in=status_filter_set) for pp in query_set.all(): pd = pp.provision_details if pd.status == ProvisionDetails.POST_PROVISION_UPDATE: pd.status = ProvisionDetails.POST_PROVISION_UPDATE_SENT pd.save() pd_serializer = ProvisionDetailsSerializer(pd, context=pd_context) ret_list.append(pd_serializer.data) return ret_list
def filter_provision_view_queryset(self, queryset): crams_user = self.request.user if not Provider.is_provider(crams_user): msg = 'User does not have a provider Role' raise rest_exceptions.NotFound(msg) valid_provider = dbUtils.fetch_active_provider_object_for_user( crams_user) if not valid_provider: msg = 'User {} does not have an active provider Role' raise rest_exceptions.NotFound(msg.format(repr(crams_user))) vp = valid_provider if queryset.model is Project: provider_filter = \ Q(requests__compute_requests__compute_product__provider=vp) | \ Q(requests__storage_requests__storage_product__provider=vp) elif queryset.model is Request: provider_filter = \ Q(compute_requests__compute_product__provider=valid_provider) \ | Q(storage_requests__storage_product__provider=valid_provider) return queryset.filter(provider_filter).distinct()
def filter_requests(self, project_obj): """filter requests.""" self._setActionState() context_request = self.cramsActionState.rest_request current_user = context_request.user ret_list = [] if not Provider.is_provider(current_user): return ret_list query_params = context_request.query_params request_id = query_params.get('request_id', None) if request_id: # , parent_request__isnull=True) requests = project_obj.requests.filter(id=request_id) else: requests = project_obj.requests.filter(parent_request__isnull=True) requests = requests.filter( request_status__code=REQUEST_STATUS_APPROVED) valid_provider = fetch_active_provider_object_for_user(current_user) provider_filter = Q( compute_requests__compute_product__provider=valid_provider) | Q( storage_requests__storage_product__provider=valid_provider) for req in requests.filter(provider_filter): req_json = ProvisionRequestSerializer(req, context=self.context).data if req_json: if ('compute_requests' in req_json and len(req_json['compute_requests']) > 0) or \ ('storage_requests' in req_json and len(req_json['storage_requests']) > 0): ret_list.append(req_json) return ret_list
def filter_requests(self, project_obj): """filter requests.""" self._setActionState() context_request = self.cramsActionState.rest_request current_user = context_request.user ret_list = [] if not Provider.is_provider(current_user): return ret_list query_params = context_request.query_params request_id = query_params.get('request_id', None) if request_id: # , parent_request__isnull=True) requests = project_obj.requests.filter(id=request_id) else: requests = project_obj.requests.filter(parent_request__isnull=True) requests = requests.filter( request_status__code=REQUEST_STATUS_APPROVED) valid_provider = fetch_active_provider_object_for_user(current_user) provider_filter = Q( compute_requests__compute_product__provider=valid_provider) | Q( storage_requests__storage_product__provider=valid_provider) for req in requests.filter(provider_filter): req_json = ProvisionRequestSerializer( req, context=self.context).data if req_json: if ('compute_requests' in req_json and len(req_json['compute_requests']) > 0) or \ ('storage_requests' in req_json and len(req_json['storage_requests']) > 0): ret_list.append(req_json) return ret_list
def validate_user_is_provider(self): """validate user is provider.""" current_user = self.get_current_user() if Provider.is_provider(current_user): return True raise ValidationError({'message': 'User is not a provider'})