def get(self, request, *args, **kwargs): try: UUID(str(kwargs.get("uuid")), version=3) except Exception as error: print(error) return Response(ReturnResponse.Response(0, __name__, "Bad UUID", "Failed").return_json(), status=status.HTTP_400_BAD_REQUEST) try: custom_user = CustomUser.objects.get(uuid=str(kwargs.get("uuid")), user_profile__disabled=False) custom_user.status = UserStatus.objects.get( pk=USER_STATUS['ACTIVE']) custom_user.save() email_handler = EmailHandler( api="http://api.incamedical.com", website="http://audits.incamedical.com") email_handler.send_template(EMAIL_TEMPLATE['WELCOME'], custom_user) return redirect("http://audits.incamedical.com/static/login.html") except Exception as error: print(error) return Response(ReturnResponse.Response(0, __name__, "Bad User", "Failed").return_json(), status=status.HTTP_400_BAD_REQUEST)
def create(self, request, *args, **kwargs): data = request.data serializer = CustomUserListRegisterSerializer( data=data, context={"confirm_password": data.get("confirm_password")}) if serializer.is_valid(): try: with transaction.atomic(): new_user = get_user_model().objects.create_user( email=serializer.validated_data.get('email'), password=serializer.validated_data.get('password'), status=USER_STATUS['REGISTERED'], firstname=data.get("firstname"), lastname=data.get("lastname")) email_handler = EmailHandler( api="http://api.incamedical.com", website="http://audits.incamedical.com") email_handler.send_template(EMAIL_TEMPLATE['CONFIRM'], new_user) return Response(ReturnResponse.Response( 0, __name__, new_user.pk, "success").return_json(), status=status.HTTP_201_CREATED) except Exception as e: return Response(ReturnResponse.Response( 1, __name__, e, "error2").return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response(1, __name__, serializer.errors, "error3").return_json(), status=status.HTTP_400_BAD_REQUEST)
def put(self, request): audit_area = AuditAreaListSerializer(data=request.data, context={ "manager": request.data.pop("manager"), "director": request.data.pop("director"), "phone": request.data.pop("phone") }) try: audit_area.is_valid(raise_exception=True) audit_area = audit_area.save() except (RuntimeError, TypeError, NameError) as e: result = '{0}:'.format(e) print(result) logger.error("SerializeR Error: {0}: error:{1}".format( audit_area.errors, result)) return Response(ReturnResponse.Response(1, __name__, 'Company Already Exists', result).return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response(0, __name__, audit_area.pk, "success").return_json(), status=status.HTTP_201_CREATED)
def get(self, request, *args, **kwargs): if self.get_queryset(): user = self.get_queryset() email_handler = EmailHandler( api="http://api.incamedical.com", website="http://audits.incamedical.com") email_handler.send_template(EMAIL_TEMPLATE['FORGOT'], user) return Response(ReturnResponse.Response(0, __name__, user.pk, "success").return_json(), status=status.HTTP_200_OK) return Response(ReturnResponse.Response(0, __name__, 0, "Not Found").return_json(), status=status.HTTP_400_BAD_REQUEST)
def update(self, request, *args, **kwargs): serializer = CompanySerializer(self.get_object(), data=request.data) try: serializer.is_valid(raise_exception=True) company = serializer.save() except ValidationError as error: result = '{0}:'.format(error) logger.error("SerializeR Error: {0}: error:{1}".format( serializer.errors, result)) return Response(ReturnResponse.Response(1, __name__, 'Company Already Exists', result).return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response(0, __name__, company.id, "success").return_json(), status=status.HTTP_201_CREATED)
def put(self, request): category = CategorySerializer(data=request.data) try: category.is_valid(raise_exception=True) category = category.save() except ValidationError as error: result = '{0}:'.format(error) logger.error("SerializeR Error: {0}: error:{1}".format( category.errors, result)) return Response(ReturnResponse.Response(1, __name__, 'Category Already Exists', result).return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response(0, __name__, category.pk, "success").return_json(), status=status.HTTP_201_CREATED)
def post(self, request, pk, audit): file_upload_obj = request.data['fileToUpload'] file_upload_type = "" upload = "" for upload_type in UploadType.objects.all(): file_upload_type = upload_type if upload_type.type == file_upload_obj.name.split(".")[-1].lower(): file_upload_type = upload_type break fs = FileSystemStorage() try: filename = fs.save(join(str(audit), str(pk), file_upload_obj.name), file_upload_obj) upload = Upload.objects.create(name=filename, uploaded_name=file_upload_obj.name, size=file_upload_obj.size, type=file_upload_type) upload_serializer = self.get_serializer(upload) except (RuntimeError, TypeError, NameError) as e: result = '{0}:'.format(e) print(result) return Response(ReturnResponse.Response(1, __name__, 'Indicator Option Exists', result).return_json(), status=status.HTTP_400_BAD_REQUEST) try: AuditIndicatorUpload.objects.create( audit=Audit.objects.get(pk=audit), upload=upload, indicator=self.get_object()) except (RuntimeError, TypeError, NameError) as e: result = '{0}:'.format(e) print(result) return Response(ReturnResponse.Response(1, __name__, 'Indicator Option Exists', result).return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response( 0, __name__, json.dumps(upload_serializer.data), "success").return_json(), status=status.HTTP_201_CREATED)
def put(self, request): indicator = IndicatorSerializer(data=request.data, partial=True) try: indicator.is_valid(raise_exception=True) indicator = indicator.save() except ValidationError as error: result = '{0}:'.format(error) logger.error("SerializeR Error: {0}: error:{1}".format( indicator.errors, result)) return Response(ReturnResponse.Response( 1, __name__, 'Indicator Already Exists', result).return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response(0, __name__, indicator.pk, "success").return_json(), status=status.HTTP_201_CREATED)
def update(self, request, *args, **kwargs): audit_area = self.get_serializer(self.get_object(), data=request.data, partial=True) try: audit_area.is_valid(raise_exception=True) audit_area = audit_area.save() except ValidationError as error: result = '{0}:'.format(error) logger.error("SerializeR Error: {0}: error:{1}".format( audit_area.errors, result)) return Response(ReturnResponse.Response( 1, __name__, 'Audit Area Already Exists', result).return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response(0, __name__, audit_area.id, "success").return_json(), status=status.HTTP_201_CREATED)
def put(self, request): data = request.data company = data.pop("company") note = NoteSerializer(data=request.data, context=company) try: note.is_valid(raise_exception=True) note = note.save() except ValidationError as error: result = '{0}:'.format(error) logger.error("SerializeR Error: {0}: error:{1}".format( note.errors, result)) return Response(ReturnResponse.Response(1, __name__, 'Note Failed', result).return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response(0, __name__, note.pk, "success").return_json(), status=status.HTTP_201_CREATED)
def update(self, request, *args, **kwargs): serializer = CustomUserDetailSerializer(self.get_object(), data=request.data, partial=True) try: serializer.is_valid(raise_exception=True) serializer = serializer.save() except ValidationError as error: result = '{0}:'.format(error) logger.error("SerializeR Error: {0}: error:{1}".format( serializer.errors, result)) return Response(ReturnResponse.Response(1, __name__, 'Custom USer Error', result).return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response(0, __name__, serializer.id, "success").return_json(), status=status.HTTP_200_OK)
def patch(self, request, *args, **kwargs): template = TemplateDetailSerializer(self.get_object(), data=request.data, partial=True) try: template.is_valid(raise_exception=True) template = template.save() except ValidationError as error: result = '{0}:'.format(error) logger.error("SerializeR Error: {0}: error:{1}".format( template.errors, result)) return Response(ReturnResponse.Response(1, __name__, 'Template Already Exists', result).return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response(0, __name__, template.pk, "success").return_json(), status=status.HTTP_201_CREATED)
def put(self, request): data = request.data company = data.pop("company") template = TemplateListSerializer(data=request.data, context={"company": company}) try: template.is_valid(raise_exception=True) template = template.save() except ValidationError as error: result = '{0}:'.format(error) logger.error("SerializeR Error: {0}: error:{1}".format( template.errors, result)) return Response(ReturnResponse.Response(1, __name__, 'Template Already Exists', result).return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response(0, __name__, template.pk, "success").return_json(), status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): audit = AuditCreateSerializer(data=request.data) try: audit.is_valid(raise_exception=True) audit = audit.save() except (RuntimeError, TypeError, NameError) as e: result = '{0}:'.format(e) print(result) result = '{0}:'.format(e) print(result) result = '{0}:'.format(e) print("SerializeR Error: {0}: error:{1}".format( audit.errors, result)) return Response(ReturnResponse.Response(1, __name__, 'Audit Already Exists', result).return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response(0, __name__, audit.pk, "success").return_json(), status=status.HTTP_201_CREATED)
def post(self, request, pk): image = ImageSerializer(data=request.data) try: image.is_valid(raise_exception=ValueError) image.save() except (RuntimeError, TypeError, NameError) as e: result = '{0}:'.format(e) print(result) return Response(ReturnResponse.Response(1, __name__, 'Image failed to create', "failed").return_json(), status=status.HTTP_400_BAD_REQUEST) indicator = self.get_object() indicator.images.add(image.instance) indicator.save() res = image.data res['indicator'] = indicator.id return Response(ReturnResponse.Response(0, __name__, json.dumps(res), "success").return_json(), status=status.HTTP_201_CREATED)
def update(self, request, *args, **kwargs): data = request.data.dict() print(data) if not data.get("confirm_password"): raise ValidationError("Empty Confirm Password") if not data.get('password'): raise ValidationError("Empty Password") if data.get('password') != data.get("confirm_password"): raise ValidationError("Mismatch") user = self.get_object() if data.get('password'): user.set_password(data.get('password')) user.save() return Response(ReturnResponse.Response(0, __name__, user.pk, "success").return_json(), status=status.HTTP_200_OK) return Response(ReturnResponse.Response(0, __name__, "failed", "success").return_json(), status=status.HTTP_400_BAD_REQUEST)
def update(self, request, *args, **kwargs): serializer = IndicatorSerializer(instance=self.get_object(), data=request.data, partial=True, read_only=False) try: serializer.is_valid(raise_exception=True) serializer.save() except (RuntimeError, TypeError, NameError) as e: result = '{0}:'.format(e) print(result) result = '{0}:'.format(e) print(result) result = '{0}:'.format(e) logger.error("SerializeR Error: {0}: error: {1}".format( serializer.errors, result)) return Response(ReturnResponse.Response( 1, __name__, 'Indicator Already Exists', result).return_json(), status=status.HTTP_400_BAD_REQUEST) return Response(ReturnResponse.Response(0, __name__, serializer.validated_data, "success").return_json(), status=status.HTTP_201_CREATED)
def update(self, request, *args, **kwargs): notes = request.data.pop("notes") note = Note.objects.create(note=notes['note'], type=NoteType.objects.get(pk=notes['type']), creator=DEFAULT_USER) audit_indicator_note = AuditIndicatorNote.objects.create( audit=Audit.objects.get(pk=request.data['audit']), note=note, indicator=self.get_object()) return Response(ReturnResponse.Response(0, __name__, audit_indicator_note.pk, "success").return_json(), status=status.HTTP_201_CREATED)
def post(self, request): if request.auth is None: custom_user = None try: custom_user = CustomUser.objects.get( email=request.data.get('username')) except CustomUser.DoesNotExist: return Response(ReturnResponse.Response( 1, __name__, "no user", "error").return_json(), status=status.HTTP_400_BAD_REQUEST) try: with transaction.atomic(): custom_user = authenticate( request, username=custom_user.username, password=request.data.get('password')) if custom_user is None: return Response(ReturnResponse.Response( 1, __name__, "Failed AuthenticaTion", "error").return_json(), status=status.HTTP_401_UNAUTHORIZED) login(request, custom_user) uri, http_method, body, headers = self._extract_params( request) data = body params = dict(parse.parse_qsl(data)) uri = OAuthLibCore().create_authorization_response( request=request, scopes={ "read": "Read Scope", "write": "Write Scope" }, credentials={ "redirect_uri": params['redirect_uri'], "response_type": params['response_type'], "client_id": params['client_id'] }, allow=True) params = parse.urlparse(uri[0]) params = dict(parse.parse_qsl(params.fragment)) return Response(params, status=200) if status_code != 200: raise Exception( json.loads(body).get("error_description", "")) return Response(json.loads(body), status=status_code) except Exception as error: print("error") print(error) return Response(ReturnResponse.Response( 1, __name__, error, "error").return_json(), status=status.HTTP_400_BAD_REQUEST) print("error2") return Response(ReturnResponse.Response(1, __name__, "error", "error").return_json(), status=status.HTTP_400_BAD_REQUEST) print("error3") return Response(ReturnResponse.Response(1, __name__, "error", "error").return_json(), status=status.HTTP_403_FORBIDDEN)
def post(self, request, *args, **kwargs): print(request) return Response(ReturnResponse.Response(0, __name__, request.data, "success").return_json(), status=status.HTTP_201_CREATED)