def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) if serializer.is_valid(): validated_data = serializer.validated_data kra = self.perform_create(serializer) headers = self.get_success_headers(serializer.data) message = "KRA setting saved as draft" if not validated_data.get("is_draft", False): message = "KRA setting submitted for assessment" if self.request.user.profile.manager: NotificationManager.kra_setting_notification( self.request, kra) return Response(utils.build_response(True, message, serializer.data), headers=headers) return Response(utils.build_response(False, None, serializer.errors))
def update(self, request, *args, **kwargs): partial = kwargs.pop('partial', False) instance = self.get_object() serializer = self.get_serializer(instance, data=request.data, partial=partial) if serializer.is_valid(): validated_data = serializer.validated_data kra = self.perform_update(serializer) message = "KRA setting saved as draft" if not validated_data.get("is_draft", False): message = "KRA setting submitted for assessment" if self.request.user.profile.manager: NotificationManager.kra_setting_notification( self.request, kra) return Response( utils.build_response(True, message, KraSerializer(kra).data)) return Response(utils.build_response(False, None, serializer.errors))
def form_valid(self, form): try: if self.kra: bucket = self.kra.bucket else: bucket = KraBucket.objects.get(reference=self.request.GET.get("bucket"), company=self.request.user.profile.company, status=constants.KRA_BUCKET_OPEN_STATUS) # , kras__user = self.request.user, # kras__is_accepted = True has_kra_already = False if self.kra is None: try: has_kra_already = bucket.kras.filter(user=self.request.user, status__in=[constants.KRA_OPEN_STATUS, constants.KRA_LOCKED_STATUS, constants.KRA_CLOSED_STATUS], is_draft=False).exists() except: has_kra_already = False if has_kra_already: form._non_form_errors = form.error_class(["Sorry, you already have a KRA setting for '"+bucket.title+"'"]) return self.form_invalid(form) if self.kra is None: self.object = Kra.objects.create(user=self.request.user, bucket=bucket, is_draft=self.request.POST.get("action", "submit").lower() != "submit", status=constants.KRA_OPEN_STATUS if self.request.POST.get("action", "submit").lower() != "submit" else constants.KRA_LOCKED_STATUS) form.instance = self.object else: self.object = self.kra self.object.status = constants.KRA_OPEN_STATUS if self.request.POST.get("action", "submit").lower() != "submit" else constants.KRA_LOCKED_STATUS self.object.is_draft = self.request.POST.get("action", "submit").lower() != "submit" self.object.save() form.save() if self.object.is_draft: messages.success(self.request, "KRA setting saved as draft") else: if self.request.user.profile.manager: NotificationManager.kra_setting_notification(self.request, self.object) messages.success(self.request, "KRA setting submitted for assessment") return HttpResponseRedirect(self.get_success_url()) except KraBucket.DoesNotExist: form._non_form_errors = form.error_class(["Sorry, the KRA bucket could not be found"]) return self.form_invalid(form)