def get(self, request): """ Get all user detail. url: user/list """ try: user_data = ProfileSerializer(UserProfile.objects.all(), many=True) return ApiResponse().success(user_data.data, 200) except Exception as err: print(err) return ApiResponse().error("Error", 400)
def get(self, request, employee_id=None): """ Get single employee detail using employee id. url: employee/detail/employee_id """ try: get_data = EmployeeSerializer(Employee.objects.get(id=employee_id)) return ApiResponse().success(get_data.data, 200) except Exception as err: print(err) return ApiResponse().error("please provide valid project id", 400)
def get(self, request, project_id=None): """ Get single project detail using project id. url: project/detail/project_id """ try: get_data = ProjectSerializer( Project.objects.get(is_deleted=False, id=project_id)) return ApiResponse().success(get_data.data, 200) except Exception as err: print(err) return ApiResponse().error("please provide valid project id", 400)
def get(self, request): """ Get all employees detail. url: employee/list """ try: emplyee_data = Employee.objects.all() get_data = EmployeeSerializer(emplyee_data, many=True) return ApiResponse().success(get_data.data, 200) except Exception as err: print(err) return ApiResponse().error("Error while getting the employee list", 400)
def delete(self, request, user_id): """ This api make user disable url: user/disable/user_id """ try: email_val = User.objects.get(id=user_id).email User.objects.filter(id=user_id).update(is_active=False) UserProfile.objects.filter(user=user_id).update( is_deleted=True, deleted_val=email_val) return ApiResponse().success("User disabled", 200) except Exception as err: print(err) return ApiResponse().error("Please send valid id", 500)
def get(self, request): """ Get all project detail. url: project/list """ try: project_data = Project.objects.filter(is_deleted=False) get_data = ProjectSerializer(project_data, many=True) # pagination_class = ProjectPagination(get_data) return ApiResponse().success(get_data.data, 200) except Exception as err: print(err) return ApiResponse().error( "Error while getting the project details", 500)
def post(self, request): """ This api create new project. url: project/create required field: project(string),system_of_origin(string),project_status(string),database_stamp(string) """ try: project_data = ProjectSerializer(data=request.data) if not (project_data.is_valid()): return ApiResponse().error(project_data.errors, 400) project_data.save() return ApiResponse().success(project_data.data, 200) except Exception as err: return ApiResponse().error("Error while creating project", 500)
def delete(self, request, project_id): """ This api make project disable url: project/disable/project_id """ try: project = Project.objects.filter( id=project_id, is_deleted=False).update(is_deleted=True) if (project) == 0: return ApiResponse().success("Project not exists", 200) return ApiResponse().success("Project deleted successfully", 200) except Exception as err: print(err) return ApiResponse().error("Please send valid project id", 400)
def post(self, request): """ This api create new employee. url: employee/create """ try: employee_data = EmployeeSerializer(data=request.data) if not (employee_data.is_valid()): return ApiResponse().error(employee_data.errors, 400) employee_data.save() return ApiResponse().success(employee_data.data, 200) except Exception as err: return ApiResponse().error("Error while creating project", 500)
def post(self, request): try: print(request.FILES.getlist("files")) for i in request.FILES.getlist("files"): print(i) if request.data.get("destination") is None: return ApiResponse.error("Not getting any destination", 400) destination = request.data.get("destination") if request.data.get("field") is None: return ApiResponse.error("Not getting any field", 400) destination = request.data.get("destination") fieldname = request.data.get("field") AWS_ACCESS_KEY_ID = getattr(settings, "AWS_S3_ACCESS_KEY_ID") AWS_SECRET_ACCESS_KEY = getattr(settings, "AWS_S3_SECRET_ACCESS_KEY") AWS_S3_HOST = getattr(settings, "AWS_S3_HOST") bucket_name = getattr(settings, "AWS_STORAGE_BUCKET_NAME") filesList = request.FILES.getlist(fieldname) fileArray = [] url = '' for files in filesList: conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, host=AWS_S3_HOST) bucket = conn.get_bucket(bucket_name) k = Key(bucket) fileName = str(int(datetime.today().timestamp())) + files.name k.key = getattr( settings, "AWS_MAIN_DIR") + '/' + destination + '/' + fileName k.set_contents_from_file(files) url = 'https://' + getattr(settings, "AWS_BUCKET_URL") + '/' + k.key fileArray.append(url) conn.close() response = fileArray if len(filesList) < 2: response = url return ApiResponse().success(response, 200) except Exception as err: print(err) print("Error in mediaUpload class media method") return ApiResponse().error("Error while uploading media file", 500)
def put(self, request, project_id): """ This api Update project details. url: project/edit/project_id """ try: get_data = Project.objects.get(pk=project_id) project_data = ProjectSerializer(get_data, data=request.data) if not (project_data.is_valid()): return ApiResponse().error( "Error while update project details", 400) project_data.save() return ApiResponse().success(project_data.data, 200) except Exception as err: print(err) return ApiResponse().success("Error", 500)
def put(self, request, employee_id): """ This api Update employee details. url: employee/edit/employee_id """ try: get_data = Employee.objects.get(pk=employee_id) employee_data = EmployeeSerializer(get_data, data=request.data) if not (employee_data.is_valid()): return ApiResponse().error( "Error while update employee details", 400) employee_data.save() return ApiResponse().success(employee_data.data, 200) except Exception as err: print(err) return ApiResponse().success("Error", 500)
def put(self, request, user_id): """ User can change password."old_password","new_password","confirm_new_password" field is required. url: user/changepassword/user_id """ try: user = User.objects.get(is_active=True, id=user_id) if not request.data.get("old_password"): return ApiResponse().error("Please enter current password.", 400) if UserProfile.objects.filter(is_deleted=True, user=user_id): return ApiResponse().success("User does not exist", 400) if request.data.get("old_password") is not None: if authenticate( username=user, password=request.data.get("old_password")) is None: return ApiResponse().error( "Invalid current password entered.", 400) password = request.data.get("new_password") confirm_new_password = request.data.get("confirm_new_password") if password != '' and confirm_new_password != '': if password != confirm_new_password: return ApiResponse().error( "New Password and Confirm Password does not match", 400) user.set_password(request.data.get("new_password")) user.save() return ApiResponse().success("password changed successfully", 200) return ApiResponse().error("Password empty", 400) except Exception as err: print(err) return ApiResponse().error("Error while change password", 500)
def post(self, request): """ Users can send a forgot password email and recover account url:user/forgotpassword """ try: user = User.objects.get(email=request.data.get("email")) except Exception as err: return ApiResponse().error("This email is not registered", 400) password = User.objects.make_random_password() user.set_password(password) user.save() frm = 'XYZ' body = "Hi there. \n You have requested a new password for your account.\nYour temporary password is " + password + "" if Email.sendMail("Forgot password", body, frm, user.email) is True: return ApiResponse().success( "New password have to sent to your email", 200) return ApiResponse().error("Error while sending the email", 500)
def get(self, request, user_id): """ Get single user detail using user id. url: user/detail/user_id """ try: if (user_id): try: user_data = ProfileSerializer( UserProfile.objects.get(is_deleted=False, user=user_id)) except Exception as err: print(err) return ApiResponse().error("please provide valid user id", 400) return ApiResponse().success(user_data.data, 200) except Exception as err: print(err) return ApiResponse().error("Error", 500)
def put(self, request, user_id): try: """ This api update user details url: user/edit/user_id """ get_data = UserProfile.objects.get(user__id=user_id) RequestOverwrite().overWrite(request, {'user': user_id}) User.objects.filter(id=user_id).update( email=request.data.get('email'), username=request.data.get('email')) update_data = ProfileSerializer(get_data, data=request.data) if update_data.is_valid(): update_data.save() return ApiResponse().success(update_data.data, 200) else: return ApiResponse().error(update_data.errors, 400) except Exception as err: print(err) return ApiResponse().error("Error", 500)
def post(self, request): """ This api create user.'Email' and 'Password' field is required. url: user/create """ try: user_info = UserSerializer(data=request.data) if not user_info.is_valid(): return ApiResponse().error(user_info.errors, 400) user = self.create_user(request) if not (user): return ApiResponse().error("This email is already exists", 400) RequestOverwrite().overWrite(request, {'user': user.id}) user_data = ProfileSerializer(data=request.data) if not (user_data.is_valid()): return ApiResponse().error(user_data.errors, 400) user_data.save() return ApiResponse().success(user_data.data, 200) except Exception as err: print(err) return ApiResponse().error( "There is a problem while creating user", 500)
def post(self, request): """ This api make user login. url: user/login """ try: if request.data.get('email') and request.data.get('password'): user = UserSerializer(data=request.data) if not user.is_valid(): return ApiResponse().error(user.errors, 400) try: user_email = User.objects.get( email=request.data.get('email')) auth_user = authenticate( username=user_email, password=request.data.get('password')) except Exception as err: print(err) return ApiResponse().error("Invalid username or password", 400) if not auth_user: return ApiResponse().error("invalid username or password", 400) token, create = Token.objects.get_or_create( user_id=auth_user.id) if (auth_user): try: userprofile = UserProfile.objects.get( user_id=auth_user.id, is_deleted=False) user_data = ProfileSerializer(userprofile) except Exception as err: print(err) return ApiResponse().error("invalid email or password", 400) token_value = { 'token': token.key, } user_response = user_data.data user_response.update(token_value) return ApiResponse().success(user_response, 200) return ApiResponse().error( "Please send correct email and password", 400) except Exception as err: print(err) return ApiResponse().error("Error while login", 500)