def post(self): data = user_schema.load(request.get_json()) user = UserModule(**data) if user.find_by_username(): return {"msg": "TÊN ĐĂNG NHẬP ĐÃ TỒN TẠI"}, 400 if user.find_by_email(): return {"msg": "EMAIL ĐÃ TỒN TẠI"}, 400 token = generate_confirmation_token(user.email) confirm_url = url_for('userregister', token=token, _external=True) email_confirmation = q.enqueue(Mail.send_mail_confirmation_to_user, args=( user.email, confirm_url, ), result_ttl=0) user.id = user.find_maxium_user() user.password = user.hash_password user.save_to_database(user_schema.dump(user)) return {"msg": "XÁC THỰC EMAIL ĐỂ KÍCH HOẠT TÀI KHOẢN"}, 201
def verify_pw(username, password): if not UserModule.find_by_username(username): return False else: hased_pw = collection_admins.find({"username": username})[0]["password"] return True if bcrypt.checkpw(password.encode("utf-8"), hased_pw) else False
def post(cls): postedData = request.get_json() username = postedData["username"] password = postedData["password"] user = UserModule.find_by_username(username) if not UserModule.find_by_username(username): return {"msg": "Can not found this username"}, 400 check_password = verify_pw(username, password) if check_password: access_token = create_access_token(identity=user.id, fresh=True) refesh_token = create_refresh_token(identity=username) return { 'access_token': access_token, 'refesh_token': refesh_token }, 200 return {"msg": "Invalid Credentials"}, 401
def post(self): postedData = request.get_json() _id = postedData["id"] username = postedData["username"] password = postedData["password"] alias = postedData["alias"] if UserModule.find_by_username(username): return {"msg": "Existed user "}, 400 hashed_pw = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()) collection_admins.insert_one({ "username": username, "password": hashed_pw, "alias": alias, "id": _id, }) return {"msg": "Creating new admin successfully"}, 201
def post(self): cmt_data = request.get_json() _id = CommentModule.create_comment_id() pic = PictureModule(id=cmt_data["pic_id"]) pic_data = pic.find_by_id() pic_title = pic_data["title"] pic_category = pic_data["category"] creator_name = cmt_data["creator_name"] creator = UserModule(username=creator_name) creator_id = creator.find_by_username()["id"] comment = { "id": _id, "username": cmt_data["username"], "pic_id": cmt_data["pic_id"], "content": cmt_data["content"], "user_id": cmt_data["user_id"], "star": cmt_data["star"], "avatarURL": cmt_data["avatarURL"] } notification = { "pic_id": cmt_data["pic_id"], "pic_title": pic_title, "created_at": cmt_data["created_at"], "commenter_username": cmt_data["username"], "creator_id": creator_id, "pic_category": pic_category } comment_channel = "comment-channel_%s" % (creator_id) status = CommentModule.create_comment(comment) NotificationModule.save_notification_to_db(notification) pusher.trigger(comment_channel, 'comment', json_util.dumps(notification)) return make_response( json_util.dumps(comment, ensure_ascii=False).encode('utf8'), 200) if status else None