How to use generate_response method in localstack

Best Python code snippet using localstack_python

__init__.py

Source:__init__.py Github

copy

Full Screen

...38 # client단에서의 체크가 확실하면 지울부분39 # 길이는 2자 이상, 20자 이하. or 입력 없는지 체크40 id_length_limit = id is None or len(id) < STATIC_NUMBERS.ID_LENGTH_MIN or len(id) > STATIC_NUMBERS.ID_LENGTH_MAX41 if id_length_limit:42 return common_func.generate_response(401, code=Response_code.ID_LENGTH_LIMIT_FAIL)43 ########44 result = dbfunc.id_exist_check(id)45 return common_func.generate_response(200, code=result)46@main_app.route("/login", methods=["GET", "POST"])47def login():48 import arujisama_flask.app.dbcode.dbfunc as dbfunc49 ip = request.remote_addr50 # {URL}/login?id=XXX&pw=YYY의 형태로 보낸다.51 # PW는 평문상태로 온다.52 id, pw = "", ""53 if request.method == "GET":54 id = request.args.get("id", type=str)55 pw = request.args.get("pw", type=str)56 elif request.method == "POST":57 id = request.get_json()["id"]58 pw = request.get_json()["pw"]59 idlengthlimit = id is None or len(id) <= 1 or len(id) > 2060 if idlengthlimit:61 return common_func.generate_response(401, Response_code.ID_LENGTH_LIMIT_FAIL)62 result = dbfunc.login(id, pw, ip)63 if result == Response_code.LOGIN_SUCCESS:64 duration = datetime.now() + timedelta(seconds=60 * 60)65 payload = {66 "id": id,67 "exp": duration.strftime("%Y%m%d%H%M%S")68 }69 token = jwt.encode(payload, main_app.config["SECRET_KEY"], "HS256")70 # print(token)71 return common_func.generate_response(200, code=result, access_token=token.decode("UTF-8"))72 else:73 return common_func.generate_response(401, result)74@main_app.route("/signup", methods=["GET", "POST"])75def signup():76 import arujisama_flask.app.dbcode.dbfunc as dbfunc77 ip = request.remote_addr78 id, pw, name, email = "", "", "", ""79 if request.method == "GET":80 id = request.args.get("id", type=str)81 pw = request.args.get("pw", type=str)82 name = request.args.get("name", type=str)83 email = request.args.get("email", type=str)84 elif request.method == "POST":85 id = request.get_json()["id"]86 pw = request.get_json()["pw"]87 name = request.get_json()["name"]88 email = request.get_json()["email"]89 else:90 return common_func.generate_response(401, code=Response_code.SIGNUP_FAILED)91 result = dbfunc.signup(id, pw, name, email, ip)92 if result == Response_code.SIGNUP_SUCCESS:93 return common_func.generate_response(200, code=result)94 else:95 return common_func.generate_response(409, code=result)96@main_app.route("/tokencheck", methods=["GET", "POST"])97def tokencheck():98 import arujisama_flask.app.dbcode.dbfunc as dbfunc99 access_token = request.headers.get("Authorization")100 if access_token is not None:101 result, id = dbfunc.token_validation_check(access_token)102 print(result, id)103 if result == Response_code.TOKEN_INVALID:104 return common_func.generate_response(401, code=result)105 return common_func.generate_response(200, code=result)106 else:107 return common_func.generate_response(401, code=Response_code.TOKEN_INVALID)108@main_app.route("/removetestid", methods=["POST"])109def removetestid():110 import arujisama_flask.app.dbcode.dbfunc as dbfunc111 keycode = request.get_json()['keycode']112 if keycode == "superduperfantastickeycode":113 result = dbfunc.remove_test_id()114 else:115 result = Response_code.TEST_ID_REMOVE_ERROR116 if result == Response_code.TEST_ID_REMOVED:117 return common_func.generate_response(200, code=result)118 return common_func.generate_response(401, code=result)119@main_app.route("/loadstamp", methods=["GET", "POST"])120def loadstamp():121 import arujisama_flask.app.dbcode.dbfunc as dbfunc122 access_token = request.headers.get("Authorization")123 ip = request.remote_addr124 if access_token is not None:125 try:126 if request.method == "GET":127 page = request.args.get('page', type=int)128 elif request.method == "POST":129 page = request.get_json()['page']130 else:131 page = 0132 except TypeError as e:133 print(e)134 page = 0135 result, data = dbfunc.load_stamp(access_token, page, ip)136 if result == Response_code.LOAD_STAMP_FAILED:137 return common_func.generate_response(401, code=result, data=data)138 return common_func.generate_response(200, code=result, data=data)139 else:140 return common_func.generate_response(401, code=Response_code.TOKEN_INVALID)141@main_app.route("/addstamp", methods=["GET", "POST"])142def addstamp():143 import arujisama_flask.app.dbcode.dbfunc as dbfunc144 access_token = request.headers.get("Authorization")145 ip = request.remote_addr146 if access_token is not None:147 if request.method == "GET":148 memo = request.args.get('memo', type=str)149 elif request.method == "POST":150 memo = request.get_json()['memo']151 else:152 return common_func.generate_response(401, code=Response_code.ADD_STAMP_FAILED)153 result = dbfunc.add_stamp(access_token, memo, ip)154 if result == Response_code.ADD_STAMP_FAILED:155 return common_func.generate_response(401, code=result)156 return common_func.generate_response(200, code=result)157 else:158 return common_func.generate_response(401, code=Response_code.TOKEN_INVALID)159@main_app.route("/editstamp", methods=["GET", "POST"])160def editstamp():161 import arujisama_flask.app.dbcode.dbfunc as dbfunc162 access_token = request.headers.get("Authorization")163 ip = request.remote_addr164 if access_token is not None:165 if request.method == "GET":166 idx = request.args.get('stamp_idx', type=int)167 memo = request.args.get('edit_memo', type=str)168 elif request.method == "POST":169 idx = request.get_json()['stamp_idx']170 memo = request.get_json()['edit_memo']171 else:172 return common_func.generate_response(401, code=Response_code.ADD_STAMP_FAILED)173 result = dbfunc.edit_stamp(access_token, memo, idx, ip)174 return common_func.generate_response(200, code=result)175 else:176 return common_func.generate_response(401, code=Response_code.TOKEN_INVALID)177@main_app.route("/deletestamp", methods=["GET", "POST"])178def deletestamp():179 import arujisama_flask.app.dbcode.dbfunc as dbfunc180 access_token = request.headers.get("Authorization")181 ip = request.remote_addr182 if access_token is not None:183 if request.method == "GET":184 idx = request.args.get('stamp_idx', type=int)185 elif request.method == "POST":186 idx = request.get_json()['stamp_idx']187 else:188 return common_func.generate_response(401, code=Response_code.DELETE_STAMP_FAILED)189 result = dbfunc.delete_stamp(access_token, idx, ip)190 if result == Response_code.DELETE_STAMP_FAILED:191 return common_func.generate_response(401, code=result)192 return common_func.generate_response(200, code=result)193 else:194 return common_func.generate_response(401, code=Response_code.TOKEN_INVALID)195# @RE-A196# Validation : validation process를 위한 빈 페이지197# id-validation : RESTful API를 이용한 실제 인증 과정198@main_app.route("/validation/<token>", methods=["GET", "POST"])199def id_validation_test(token):200 return render_template('validation.html')201@main_app.route("/id-validation/<token>", methods=["GET", "POST"])202def id_validation(token):203 from arujisama_flask.app.email_func.id_validation import try_validation204 result = try_validation(token)205 return common_func.generate_response(200, code=result)206@main_app.route("/find-id", methods=["GET", "POST"])207def findid():208 import arujisama_flask.app.dbcode.dbfunc as dbfunc209 ip = request.remote_addr210 if request.method == "GET":211 name = request.args.get('name', type=str)212 email = request.args.get('email', type=str)213 elif request.method == "POST":214 name = request.get_json()['name']215 email = request.get_json()['email']216 else:217 return common_func.generate_response(401, code=Response_code.FIND_ID_FAILED)218 result, result_id = dbfunc.find_id(name, email, ip)219 if result == Response_code.FIND_ID_FAILED:220 return common_func.generate_response(401, code=result)221 return common_func.generate_response(200, code=result, data={"id": result_id})222@main_app.route("/find-pw", methods=["GET", "POST"])223def findpw():224 import arujisama_flask.app.dbcode.dbfunc as dbfunc225 ip = request.remote_addr226 if request.method == "GET":227 id = request.args.get('id', type=str)228 name = request.args.get('name', type=str)229 email = request.args.get('email', type=str)230 elif request.method == "POST":231 id = request.get_json()['id']232 name = request.get_json()['name']233 email = request.get_json()['email']234 else:235 return common_func.generate_response(401, code=Response_code.FIND_PW_FAILED)236 result = dbfunc.find_pw(id, name, email, ip)237 if result == Response_code.FIND_PW_FAILED:238 return common_func.generate_response(401, code=result)239 return common_func.generate_response(200, code=result)240@main_app.route("/reset-pw-page/<token>", methods=["GET", "POST"])241def resetpwpage(token):242 return render_template("reset-pw-page.html")243@main_app.route("/reset-pw/<token>", methods=["GET", "POST"])244def resetpw(token):245 import arujisama_flask.app.dbcode.dbfunc as dbfunc246 ip = request.remote_addr247 if request.method == "GET":248 pw = request.args.get('pw', type=str)249 elif request.method == "POST":250 pw = request.get_json()['pw']251 else:252 return common_func.generate_response(401, code=Response_code.RESET_PW_FAILED)253 result = dbfunc.reset_pw(token, pw, ip)...

Full Screen

Full Screen

views.py

Source:views.py Github

copy

Full Screen

...12 return self.login(request)13 elif action == "register":14 return self.register(request)15 else:16 return generate_response(17 {"msg": "action error"}, status.HTTP_400_BAD_REQUEST18 )19 # def delete(self, request):20 # return self.log_out(request)21 # def log_out(self, request):22 # email_address = request.data["email_address"]23 # try:24 # user = User.objects.get(email_address=email_address)25 # user.is_login = 026 # user.save()27 # return generate_response({"msg": "logged out"}, status.HTTP_204_NO_CONTENT)28 # except Exception as e:29 # return generate_response(30 # {"msg": "something is error"}, status.HTTP_400_BAD_REQUEST31 # )32 def login(self, request):33 msg = {"msg": "", "user": ""}34 email_address = request.data["email_address"]35 password = request.data["password"]36 # 校验用户是否存在37 try:38 user = User.objects.get(email_address=email_address)39 except Exception as e:40 msg["msg"] = "输入的邮箱未注册!"41 return generate_response(msg, status.HTTP_401_UNAUTHORIZED)42 # 用户存在的情况下校验密码是否正确以及是否已登录,正确则返回200.错误返回40143 if not user.verify_password(password):44 msg["msg"] = "密码输入有误!"45 return generate_response(msg, status.HTTP_401_UNAUTHORIZED)46 # elif user.has_login():47 # msg["msg"] = "you have already logged in"48 # return generate_response(msg, status.HTTP_400_BAD_REQUEST)49 # # 登录后修改标志位50 # user.is_login = 151 # user.save()52 # 将用户的个人信息序列化,并返回前端53 serializer = UserSerializer(user)54 current_user = serializer.data55 msg["msg"] = "登录成功!"56 msg["user"] = current_user57 return generate_response(msg, status.HTTP_200_OK)58 # 发送完验证码后,第二次提交带有验证码的整个表单59 def register(self, request):60 username = request.data["username"]61 email_address = request.data["email_address"]62 password = request.data["password"]63 vtf_code = request.data["vtf_code"]64 # 校验验证码的正确性,失败则返回 40665 try:66 vtf = VertificationCode.objects.get(67 email_address=email_address, vertificationCode=vtf_code68 )69 except Exception as e:70 return generate_response(71 {"msg": "验证码输入有误!"}, status.HTTP_406_NOT_ACCEPTABLE72 )73 # 向数据库插入用户信息,成功返回 200,失败返回 40674 try:75 User.objects.create(76 username=username, email_address=email_address, password=password77 )78 except Exception as e:79 return generate_response(80 {"msg": "注册失败!"}, status.HTTP_406_NOT_ACCEPTABLE81 )82 return generate_response({"msg": "注册成功!"}, status.HTTP_200_OK)83class SendVertificationCode(APIView):84 def post(self, request):85 return self.sendCode(request)86 def sendCode(self, request):87 code = randint(100000, 999999)88 email_address = request.data["email_address"]89 # 试图通过用户提交的邮箱检验是否已经注册过90 # 已经注册过则提示用户,未注册过才向用户发送验证码91 try:92 user = User.objects.get(email_address=email_address)93 except Exception as e:94 # 判断用户是否是第一次发送,如果是第一次,则在数据库中插入记录95 # 若不是第一次发送,则更新数据库中的原有记录96 try:97 vtf = VertificationCode.objects.get(email_address=email_address)98 except Exception as e:99 VertificationCode.objects.create(100 email_address=email_address, vertificationCode=code101 )102 mail.send_vtf_mail(email_address, code)103 return generate_response(104 {"msg": "验证码发送成功!"}, status.HTTP_200_OK105 )106 # 如果不是第一次发送,则更新表中之前保存的验证码107 vtf.vertificationCode = code108 vtf.save()109 mail.send_vtf_mail(email_address, code)110 return generate_response({"msg": "验证码发送成功!"}, status.HTTP_200_OK)111 # 用户已经注册过则不予发送112 return generate_response(113 {"msg": "此邮箱已注册过!"}, status.HTTP_403_FORBIDDEN114 )115class PasswordView(APIView):116 def post(self, request):117 action = request.data["action"]118 if action == "send_vtf":119 return self.send_vtf(request)120 elif action == "reset":121 return self.reset_password(request)122 else:123 return generate_response(124 {"msg": "action error"}, status.HTTP_400_BAD_REQUEST125 )126 def send_vtf(self, request):127 email_address = request.data["email_address"]128 try:129 # 校验用户输入的邮箱是否是已注册130 user = User.objects.get(email_address=email_address)131 # 如果用户已经注册,那么vtf表应该有邮箱记录,更新验证码记录132 vtf = VertificationCode.objects.get(email_address=email_address)133 code = randint(100000, 999999)134 vtf.vertificationCode = code135 vtf.save()136 mail.send_vtf_mail(email_address, code)137 return generate_response({"msg": "验证码发送成功!"}, status.HTTP_200_OK)138 except Exception as e:139 # 用户输入了未注册的邮箱140 return generate_response(141 {"msg": "此邮箱未注册!"},142 status.HTTP_401_UNAUTHORIZED,143 )144 # 点击修改密码时,提交整个页面表单145 def reset_password(self, request):146 email_address = request.data["email_address"]147 vtf_code = request.data["vtf_code"]148 # 校验验证码的正确性149 if self.check_vtf(email_address, vtf_code):150 new_pwd = request.data["new_pwd"]151 user = User.objects.get(email_address=email_address)152 # 新密码与旧密码相同时,不予重置153 if user.password == new_pwd:154 return generate_response(155 {"msg": "新密码与旧密码相同,请重新输入!"},156 status.HTTP_406_NOT_ACCEPTABLE,157 )158 user.password = new_pwd159 user.save()160 return generate_response(161 {"msg": "密码重置成功!"}, status.HTTP_200_OK162 )163 return generate_response(164 {"msg": "验证码输入有误!"}, status.HTTP_401_UNAUTHORIZED165 )166 # 校验验证码的正确性167 def check_vtf(self, email_address, vtf_code):168 try:169 vtf = VertificationCode.objects.get(170 email_address=email_address, vertificationCode=vtf_code171 )172 return True173 except Exception as e:...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful