def register(): name = request.args.get("name") password = request.args.get("password") email = request.args.get("email") mark = request.args.get( "mark") # to see if the user want to use captcha or not if not args_verification(name, password, email, mark): return statusVo("Arguments mismatch.", "ERROR") user = User(name=name, email=email) user.set_password(password) db.session.add(user) try: db.session.commit() except IntegrityError: return statusVo("This email has been used.", "ERROR") if mark == "captcha": source = list(string.ascii_letters) source.extend(map(lambda x: str(x), range(0, 10))) # source.extend(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]) captcha = "".join(random.sample(source, 6)) # randomly select 6 digits send_captcha(user=user, captcha=captcha) cache.set(email, captcha) # store captcha into cache return statusVo("Send captcha success.", "OK") else: token = generate_token(user=user, operation=Operations.CONFIRM) send_confirm_account_email(user=user, token=token) return statusVo("Send confirm email success.", "OK")
def sendMessage(): url = current_app.config['WEBSOCKET_URL'] chatroom_id = request.form.get("chatroom_id") user_id = request.form.get("user_id") name = request.form.get("name") message = request.form.get("message") if not args_verification(chatroom_id, user_id, name, message): return statusVo("Arguments mismatch.", "ERROR") chatroom = Chatroom.query.filter(Chatroom.id == chatroom_id).one_or_none() message = Message(chatroom_id=chatroom_id, user_id=user_id, name=name, message=message) db.session.add(message) db.session.commit() websocket_message = message.__repr__() websocket_message['chatroom_name'] = chatroom.name print(websocket_message) res = requests.post(url=url, json=websocket_message) result = res.json() print(result) if result['status'] == 'OK': return statusVo("Insert successfully.", "OK") else: return statusVo("Insert failed.", "ERROR")
def upload_file(): upload_folder = current_app.config['UPLOAD_FOLDER'] url = current_app.config['SENDFILE_URL'] if request.method == 'POST': file = request.files['file'] # input key-value parameter uploader = request.args.get("name") # the name of the user uploader_id = request.args.get("user_id") chatroom_id = request.args.get("chatroom_id") type = request.args.get("type") if file and allowed_file(file.filename) and args_verification( uploader, chatroom_id): filename = secure_filename(file.filename) (filename, extension) = os.path.splitext(filename) filename = filename + '_' + uploader + '_' + str(int( time.time())) + extension filepath = os.path.join(os.path.dirname(current_app.root_path), upload_folder) filepath = os.path.join(filepath, filename) file.save(filepath) m = Message(chatroom_id=chatroom_id, user_id=uploader_id, name=uploader, type=type) f = File(file_name=filename, file_path=filepath, uploader_id=uploader_id) db.session.add(f) db.session.add(m) f.messages.append(m) db.session.commit() websocket_message = { "message_id": m.id, "chatroom_id": chatroom_id, "user_id": uploader_id, "name": uploader, "message": m.message, "message_time": m.message_time.strftime('%Y-%m-%d %H:%M'), "type": type, "file_id": m.id, "filename": filename, "file_path": filepath, "api_path": url_for("messages.download", filename=filename) } # websocket_message = f.__repr__() # websocket_message["name"] = uploader # websocket_message["type"] = type # websocket_message["chatroom_id"] = chatroom_id # websocket_message["api_path"] = url_for("messages.download", filename=filename) print(websocket_message) res = requests.post(url=url, json=websocket_message) result = res.json() print(result) if result['status'] == 'OK': return statusVo("Send file successfully.", "OK") else: return statusVo("Send file failed.", "ERROR") else: return statusVo("Upload Failed.", "ERROR") else: return statusVo("Upload Failed.", "ERROR")
def download(filename): print(filename) upload_folder = current_app.config["UPLOAD_FOLDER"] # filename = request.args.get("filename") if request.method == 'GET' and args_verification(filename): uploads = os.path.join(os.path.dirname(current_app.root_path), upload_folder) if os.path.isfile(os.path.join(uploads, filename)): return send_from_directory(uploads, filename, as_attachment=True) else: return statusVo("File does not exist.", "ERROR") else: return statusVo("Method mismatch.", "ERROR")
def validate_captcha(): captcha = request.args.get("captcha").lower() email = request.args.get("email") if not args_verification(captcha, email): return statusVo("Arguments mismatch.", "ERROR") cache_captcha = cache.get(email) print(cache_captcha) if cache_captcha and cache_captcha.lower() == captcha: user = User.query.filter(User.email == email).one_or_none() if user is None: return statusVo("This email has been used.", "ERROR") else: user.confirm = True db.session.commit() return statusVo("This user has been confirmed.", "OK") else: return statusVo("The captcha is not correct", "ERROR")
def login(): # id = request.args.get("id") email = request.args.get("email") password = request.args.get("password") if not args_verification(email, password): return statusVo("Arguments mismatch.", "ERROR") user = User.query.filter(User.email == email).first() if current_user.is_authenticated: print("User: "******"has been authenticated before.") return jsonify(resultVo(user.__repr__(), "OK")) if user: if user.validate_password(password): login_user(user) if current_user.is_authenticated: print("User: "******"has login.") return jsonify(resultVo(user.__repr__(), "OK")) else: return statusVo("Verification failed.", "ERROR") else: return statusVo("User does't exist.", "ERROR")
def getMessages(): result = [] chatroom_id = request.args.get("chatroom_id") page = request.args.get("page") if not args_verification(chatroom_id, page): return statusVo("Arguments mismatch.", "ERROR") page = int(page) # messages = Message \ # .query \ # .filter(Message.chatroom_id == chatroom_id) \ # .order_by(Message.message_time.desc()) \ # .paginate(page=page, per_page=5, error_out=False) messages = db.session.query( Message.id, Message.chatroom_id, Message.user_id, Message.name, Message.message, Message.message_time, Message.type, File.id, File.file_name, File.file_path).outerjoin(File, Message.file_id == File.id).order_by( Message.message_time.desc()).paginate(page=page, per_page=5, error_out=False) for each in messages.items: (message_id, chatroom_id, user_id, name, message, message_time, type, file_id, filename, file_path) = each api_path = None if type != 1: api_path = url_for("messages.download", filename=filename) each = { "message_id": message_id, "chatroom_id": chatroom_id, "user_id": user_id, "name": name, "message": message, "message_time": message_time, "type": type, "file_id": file_id, "filename": filename, "file_path": file_path, "api_path": api_path } each["message_time"] = each["message_time"].strftime('%Y-%m-%d %H:%M') result.append(each) dict = {} dict["current_page"] = page dict["messages"] = result dict["total_pages"] = messages.pages return jsonify(resultVo(dict, "OK"))
def cookie_test(): return statusVo("Test success.", "OK")
def mail_test(): send_mail_test() return statusVo("Send mail, please check.", "OK")
def confirm(token): if validate_token(token=token, operation=Operations.CONFIRM): return statusVo("Confirm success.", "OK") else: return statusVo("Confirm failed.", "OK")
def logout(): if current_user.is_authenticated: print("User: "******"has logout.") logout_user() return statusVo("Logout success.", "OK")