def process_request(user, target_request): from towerlib.towerlibexceptions import AuthFailed try: # switch the state to processing before trying to execute the process target_request.process() target_request.save() target_request.perform_processing() target_request.save() except AuthFailed: logger.error( f"[admin_request_process] Fail to authenticate with provided token when trying to process request " f"id '{target_request.id}'") return "Fail to authenticate with provided token" except requests.exceptions.SSLError: logger.error( f"[admin_request_process] Certificate verify failed when trying to process request " f"id '{target_request.id}'") return "Certificate verify failed" except requests.exceptions.ConnectionError: logger.error( f"[admin_request_process] Unable to connect to remote server when trying to process request " f"id '{target_request.id}'") return "Unable to connect to remote server" target_request.save() send_mail_request_update(target_request, user_applied_state=user) return ""
def admin_request_accept(request, request_id): target_request = get_object_or_404(Request, id=request_id) if not can_proceed(target_request.accept): raise PermissionDenied parameters = {'request': target_request} if request.method == 'POST': form = AcceptRequestForm(request.user, request.POST, **parameters) if form.is_valid(): form.save() target_request.refresh_from_db() send_mail_request_update(target_request, user_applied_state=request.user) return redirect('service_catalog:request_list') else: form = AcceptRequestForm(request.user, initial=target_request.fill_in_survey, **parameters) breadcrumbs = [ { 'text': 'Requests', 'url': reverse('service_catalog:request_list') }, { 'text': request_id, 'url': "" }, ] context = { 'form': form, 'target_request': target_request, 'breadcrumbs': breadcrumbs } return render(request, 'service_catalog/admin/request/request-accept.html', context=context)
def save(self): # create the instance instance_name = self.validated_data["instance_name"] billing_group = None if self.validated_data["billing_group"]: billing_group = self.validated_data["billing_group"] new_instance = Instance.objects.create(service=self.service, name=instance_name, billing_group=billing_group, spoc=self.request.user) # create the request new_request = Request.objects.create( instance=new_instance, operation=self.create_operation, fill_in_survey=self.validated_data["fill_in_survey"], user=self.request.user) # save the comment message = None if "request_comment" in self.validated_data and self.validated_data[ "request_comment"] is not None: comment = self.validated_data["request_comment"] message = RequestMessage.objects.create(request=new_request, sender=self.request.user, content=comment) from service_catalog.mail_utils import send_mail_request_update send_mail_request_update(target_request=new_request, user_applied_state=new_request.user, message=message) return new_request
def customer_service_request(request, service_id): target_service = get_object_or_404(Service, id=service_id) parameters = {'service_id': service_id} if request.method == 'POST': form = ServiceRequestForm(request.user, request.POST, **parameters) if form.is_valid(): new_request = form.save() send_mail_request_update(target_request=new_request, from_email=request.user.email) return redirect('customer_request_list') else: form = ServiceRequestForm(request.user, **parameters) return render(request, 'customer/catalog/service/service-request.html', { 'form': form, 'service': target_service })
def admin_request_re_submit(request, request_id): target_request = get_object_or_404(Request, id=request_id) parameters = {'request_id': target_request.id, 'message_required': False} if request.method == "POST": form = MessageOnRequestForm(request.user, request.POST, **parameters) if form.is_valid(): if not can_proceed(target_request.re_submit): raise PermissionDenied form.save() target_request.re_submit() target_request.save() send_mail_request_update(target_request, from_email=request.user.email) return redirect(admin_request_list) else: form = MessageOnRequestForm(request.user, **parameters) context = {"form": form, "target_request": target_request} return render(request, "admin/request/request-re-submit.html", context)
def admin_request_process(request, request_id): target_request = get_object_or_404(Request, id=request_id) error = False error_message = "" if request.method == "POST": if not can_proceed(target_request.process): raise PermissionDenied import towerlib try: # switch the state to processing before trying to execute the process target_request.process() target_request.save() target_request.perform_processing() target_request.save() except towerlib.towerlibexceptions.AuthFailed: error = True logger.error( "[admin_request_process] Fail to authenticate with provided token when trying " "to process request id '{}'".format(target_request.id)) error_message = "Fail to authenticate with provided token" except requests.exceptions.SSLError: error = True error_message = "Certificate verify failed" logger.error( "[admin_request_process] Certificate verify failed when trying " "to process request id '{}'".format(target_request.id)) except requests.exceptions.ConnectionError: error = True error_message = "Unable to connect to remote server" logger.error( "[admin_request_process] Unable to connect to remote server when trying " "to process request id '{}'".format(target_request.id)) if not error: target_request.save() send_mail_request_update(target_request, from_email=request.user.email) return redirect(admin_request_list) context = { "target_request": target_request, "error_message": error_message } return render(request, "admin/request/request-process.html", context)
def accept(self, request, pk=None): """ Accept the request and validate/complete the survey : change the state of the request to 'ACCEPTED'. """ target_request = get_object_or_404(Request, id=pk) if not can_proceed(target_request.accept): raise PermissionDenied serializer = AcceptRequestSerializer(data=request.data, target_request=target_request, user=request.user, read_only_form=False) if serializer.is_valid(): target_request = serializer.save() send_mail_request_update(target_request, user_applied_state=request.user) return Response(AdminRequestSerializer(target_request).data, status=status.HTTP_200_OK) return Response(serializer.error_messages, status=status.HTTP_400_BAD_REQUEST)
def save(self, **kwargs): new_request = Request.objects.create( instance=self.target_instance, operation=self.target_operation, fill_in_survey=self.validated_data["fill_in_survey"], user=self.request.user) # save the comment message = None if "request_comment" in self.validated_data and self.validated_data[ "request_comment"] is not None: comment = self.validated_data["request_comment"] message = RequestMessage.objects.create(request=new_request, sender=self.request.user, content=comment) from service_catalog.mail_utils import send_mail_request_update send_mail_request_update(target_request=new_request, user_applied_state=new_request.user, message=message) return new_request
def admin_request_accept(request, request_id): target_request = get_object_or_404(Request, id=request_id) parameters = {'request_id': request_id} if request.method == 'POST': form = AcceptRequestForm(request.user, request.POST, **parameters) if form.is_valid(): form.save() target_request.refresh_from_db() send_mail_request_update(target_request, from_email=request.user.email) return redirect(admin_request_list) else: form = AcceptRequestForm(request.user, initial=target_request.fill_in_survey, **parameters) context = {"form": form, "target_request": target_request} return render(request, 'admin/request/request-accept.html', context=context)
def re_submit(self, request, pk=None): """ Re-submit the request : change the state of the request to 'SUBMITTED'. """ target_request = get_object_or_404(Request, id=pk) if not can_proceed(target_request.re_submit): raise PermissionDenied data = QueryDict.copy(request.data) data['sender'] = request.user.id data['request'] = target_request.id message = RequestMessageSerializer(data=data) if message.is_valid(): message.save() target_request.re_submit() target_request.save() send_mail_request_update(target_request, user_applied_state=request.user, message=message) return Response(AdminRequestSerializer(target_request).data, status=status.HTTP_200_OK)
def admin_request_need_info(request, request_id): target_request = get_object_or_404(Request, id=request_id) parameters = {'request_id': target_request.id, 'message_required': True} if request.method == "POST": form = MessageOnRequestForm(request.user, request.POST, **parameters) if form.is_valid(): # check that we can ask for info the request if not can_proceed(target_request.need_info): raise PermissionDenied form.save() target_request.need_info() target_request.save() message = form.cleaned_data['message'] send_mail_request_update(target_request, from_email=request.user.email, message=message) return redirect(admin_request_list) else: form = MessageOnRequestForm(request.user, **parameters) context = {"form": form, "target_request": target_request} return render(request, "admin/request/request-need-info.html", context)
def admin_request_re_submit(request, request_id): target_request = get_object_or_404(Request, id=request_id) if not can_proceed(target_request.re_submit): raise PermissionDenied if request.method == "POST": form = RequestMessageForm(request.POST or None, request.FILES or None, sender=request.user, target_request=target_request) if form.is_valid(): message = form.save(send_notification=False) target_request.re_submit() target_request.save() send_mail_request_update(target_request, user_applied_state=request.user, message=message) return redirect('service_catalog:request_list') else: form = RequestMessageForm(sender=request.user, target_request=target_request) breadcrumbs = [ { 'text': 'Requests', 'url': reverse('service_catalog:request_list') }, { 'text': request_id, 'url': "" }, ] context = { 'form': form, 'target_request': target_request, 'breadcrumbs': breadcrumbs } return render(request, "service_catalog/admin/request/request-re-submit.html", context)
def save(self): user_provided_survey_fields = dict() for field_key, value in self.cleaned_data.items(): if field_key not in EXCLUDED_SURVEY_FIELDS: user_provided_survey_fields[field_key] = value new_request = Request.objects.create( instance=self.instance, operation=self.operation, fill_in_survey=user_provided_survey_fields, user=self.user) # save the comment request_comment = self.cleaned_data[ "request_comment"] if self.cleaned_data["request_comment"] else None message = None if request_comment is not None: message = RequestMessage.objects.create(request=new_request, sender=self.user, content=request_comment) from service_catalog.mail_utils import send_mail_request_update send_mail_request_update(target_request=new_request, user_applied_state=new_request.user, message=message) return new_request
def save(self): user_provided_survey_fields = dict() for field_key, value in self.cleaned_data.items(): if field_key not in EXCLUDED_SURVEY_FIELDS: user_provided_survey_fields[field_key] = value # create the instance instance_name = self.cleaned_data["instance_name"] billing_group_id = self.cleaned_data[ "billing_group_id"] if self.cleaned_data[ "billing_group_id"] else self.service.billing_group_id billing_group = BillingGroup.objects.get( id=billing_group_id) if billing_group_id else None new_instance = Instance.objects.create(service=self.service, name=instance_name, billing_group=billing_group, spoc=self.user) # create the request new_request = Request.objects.create( instance=new_instance, operation=self.create_operation, fill_in_survey=user_provided_survey_fields, user=self.user) # save the comment request_comment = self.cleaned_data[ "request_comment"] if self.cleaned_data["request_comment"] else None message = None if request_comment is not None: message = RequestMessage.objects.create(request=new_request, sender=self.user, content=request_comment) from service_catalog.mail_utils import send_mail_request_update send_mail_request_update(target_request=new_request, user_applied_state=new_request.user, message=message) return new_request