Best Python code snippet using localstack_python
__init__.py
Source:__init__.py
...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)...
views.py
Source:views.py
...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:...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!