def follower(): auth = request.headers.get('Authorization') auth_user_id = Auth.identify(auth) # Authorization header不正确 if auth_user_id is None: result = return_status(-2) return jsonify(result) # 获取用户 user = User() user.set_user_id(auth_user_id) user_dao = UserDAO() try: retrieve_user = user_dao.retrieve(user) except: result = return_status(-2) return jsonify(result) # 用户不存在 if retrieve_user is None: result = return_status(-1) return jsonify(result) follower_dao = FollowerDAO() try: followers = follower_dao.retrieve(retrieve_user.get_user_id()) results = return_follower(followers) return jsonify(results) except: result = return_status(-2) return jsonify(result)
def login(): data = request.get_json() if 'phone' not in data or 'password' not in data: return "信息缺失" phone = data['phone'] password = data['password'] # 判断电话号码是否为空 if phone is None: return "The phone number is empty!" # 判断密码是否为空 if password is None: return "The password is empty!" user = User() user.set_phone(phone) user.set_password(password) try: user = UserDAO().retrieve(user) except: return "Server Failure!" # 用户不存在 if user is None: result = return_status(-1) return jsonify(result) # 授权 result = Auth.authorize(user) return jsonify(result)
def retrieve(self, user): retrieve_user = None if user.get_user_id() is None: sql = 'select * from user where phone = "%s" and password = "******"' % ( user.get_phone(), user.get_password()) else: sql = 'select * from user where user_id = %s' % ( user.get_user_id()) connection = pymysql.connect(host=self.__db_host, user=self.__db_admin, password=self.__db_password, database=self.__db, port=self.__port, charset=self.__charset) cursor = connection.cursor() try: cursor.execute(sql) result = cursor.fetchone() if result is not None: retrieve_user = User() retrieve_user.set_user_id(result[0]) retrieve_user.set_phone(result[1]) retrieve_user.set_password(result[2]) except: traceback.print_exc() finally: connection.close() cursor.close() return retrieve_user
def upload_homepage(): auth = request.headers.get('Authorization') auth_user_id = Auth.identify(auth) # Authorization header不正确 if auth_user_id is None: result = return_status(-2) return jsonify(result) # 获取用户 user = User() user.set_user_id(auth_user_id) user_dao = UserDAO() try: retrieve_user = user_dao.retrieve(user) except: result = return_status(-2) return jsonify(result) # 用户不存在 if retrieve_user is None: result = return_status(-1) return jsonify(result) # 设置路径 folder_path = 'background/' + str(auth_user_id) mkdir(folder_path) information = Information() path = folder_path + '/background.jpg' information.set_user_id(auth_user_id) information.set_background_photo(path) # 读取背景图片 try: homepage = request.get_data() if homepage is None: return "上传的图片为空" with open(path, 'wb') as f: f.write(homepage) except: result = return_status(-2) return jsonify(result) # 数据库修改 information_dao = InformationDAO() try: result = information_dao.update_background_photo(information) if result is not None: result = return_homepage(result, path) return jsonify(result) else: result = return_status(-2) return jsonify(result) except: result = return_status(-2) return jsonify(result)
def get_mylike(): auth = request.headers.get('Authorization') auth_user_id = Auth.identify(auth) # Authorization header不正确 if auth_user_id is None: result = return_status(-2) return jsonify(result) user_id = request.args.get('userid') if user_id is None: return "信息不完整" user_id = int(user_id) # 获取用户 user = User() user.set_user_id(user_id) user_dao = UserDAO() try: retrieve_user = user_dao.retrieve(user) except: result = return_status(-2) return jsonify(result) # 用户不存在 if retrieve_user is None: result = return_status(-1) return jsonify(result) my_like_work_ids = user_dao.get_my_like(retrieve_user) my_like_works = WorkDAO().list(my_like_work_ids) start = request.args.get('start') if start is None: return "信息不完整" start = int(start) count = request.args.get('count') if count is None: return "信息不完整" count = int(count) type = request.args.get('type') if type is None: return "信息不完整" type = str(type) if type == 'home': result = return_home_my_like(my_like_works, start, count) return jsonify(result) if type == 'detail': result = return_detail_my_like(my_like_works, start, count) return jsonify(result) else: return "信息不正确"
def modifyInformation(): auth = request.headers.get('Authorization') auth_user_id = Auth.identify(auth) # Authorization header不正确 if auth_user_id is None: result = return_status(-2) return jsonify(result) # 获取用户 user = User() user.set_user_id(auth_user_id) user_dao = UserDAO() try: retrieve_user = user_dao.retrieve(user) except: result = return_status(-2) return jsonify(result) # 用户不存在 if retrieve_user is None: result = return_status(-1) return jsonify(result) information = Information() information.set_user_id(auth_user_id) data = request.get_json() if 'NickName' not in data: return "上传的信息不完整" nick_name = data['NickName'] nick_name = str(nick_name) information.set_nick_name(nick_name) if 'Avatar' not in data: return "上传的信息不完整" avatar = data['Avatar'] avatar = str(avatar) information.set_avatar(avatar) if 'Signature' not in data: return "上传的信息不完整" signature = data['Signature'] signature = str(signature) information.set_signature(signature) if 'BackgroundPhoto' not in data: return "上传的信息不完整" background_photo = data['BackgroundPhoto'] background_photo = str(background_photo) information.set_background_photo(background_photo) information_dao = InformationDAO() result = information_dao.update(information) result = return_status(result) return jsonify(result)
def get_myworks(): auth = request.headers.get('Authorization') auth_user_id = Auth.identify(auth) # Authorization header不正确 if auth_user_id is None: result = return_status(-2) return jsonify(result) user_id = request.args.get('userid') if user_id is None: return "信息不完整" # 获取用户 user = User() user.set_user_id(user_id) user_dao = UserDAO() try: retrieve_user = user_dao.retrieve(user) except: result = return_status(-2) return jsonify(result) # 用户不存在 if retrieve_user is None: result = return_status(-1) return jsonify(result) type = request.args.get('type') if type is None: return "信息不完整" type = str(type) top = request.args.get('top') if top is None: return "信息不完整" top = str(top) work_dao = WorkDAO() works = work_dao.retrieve(user_id) if type == 'home': if top is 'true' or top is 'True': pass else: result = return_home(works) return jsonify(result) if type == 'detail': if top is 'true' or top is 'True': pass else: result = return_detail(works) return jsonify(result) else: return "信息不正确"
def like(): auth = request.headers.get('Authorization') auth_user_id = Auth.identify(auth) # Authorization header不正确 if auth_user_id is None: result = return_status(-2) return jsonify(result) # 获取用户 user = User() user.set_user_id(auth_user_id) user_dao = UserDAO() try: retrieve_user = user_dao.retrieve(user) except: result = return_status(-2) return jsonify(result) # 用户不存在 if retrieve_user is None: result = return_status(-1) return jsonify(result) data = request.get_json() if 'id' not in data or 'Cancel' not in data: return "信息缺失" id = data['id'] cancel_like = data['Cancel'] work_dao = WorkDAO() if cancel_like == 'True' or cancel_like == 'true' or cancel_like is True: work_dao.delete_my_like(auth_user_id, id) result = return_status(1) return jsonify(result) if cancel_like == 'False' or cancel_like == 'false' or cancel_like is False: work_dao.add_my_like(auth_user_id, id) result = return_status(0) return jsonify(result) else: result = return_status(-1) return jsonify(result)
def follow(): auth = request.headers.get('Authorization') auth_user_id = Auth.identify(auth) # Authorization header不正确 if auth_user_id is None: result = return_status(-2) return jsonify(result) # 获取用户 user = User() user.set_user_id(auth_user_id) user_dao = UserDAO() try: retrieve_user = user_dao.retrieve(user) except: result = return_status(-2) return jsonify(result) # 用户不存在 if retrieve_user is None: result = return_status(-1) return jsonify(result) data = request.get_json() if 'UserID' not in data or 'Cancel' not in data: return "信息缺失" user_id = data['UserID'] cancel_follow = data['Cancel'] follow_dao = FollowDAO() if cancel_follow == 'True' or cancel_follow == 'true' or cancel_follow is True: follow_dao.delete(user_id, auth_user_id) result = return_status(1) return jsonify(result) if cancel_follow == 'False' or cancel_follow == 'false' or cancel_follow is False: follow_dao.add(user_id, auth_user_id) result = return_status(0) return jsonify(result) else: result = return_status(-1) return jsonify(result)
def register(): data = request.get_json() if 'phone' not in data or 'password' not in data: return "信息缺失" phone = data['phone'] password = data['password'] # 判断电话号码是否为空 if phone is None: return "The phone number is empty!" # 判断密码是否为空 if password is None: return "The password is empty!" # 检测手机是否已经使用 phone_is_used = verify_phone(phone) if phone_is_used: result = return_status(-1) # 手机号码被使用 return jsonify(result) # 检测手机格式是否正确 phone_format_false = verify_phone_format(phone) if phone_format_false: result = return_status(-2) # 手机格式不正确 return jsonify(result) user = User() user.set_phone(phone) user.set_password(password) try: user_dao = UserDAO() user_dao.add(user) result = return_status(0) return jsonify(result) # 注册成功 except: return "Server failure!"
def colorization(): auth = request.headers.get('Authorization') auth_user_id = Auth.identify(auth) # Authorization header不正确 if auth_user_id is None: result = return_status(-2) return jsonify(result) # 获取用户 user = User() user.set_user_id(auth_user_id) user_dao = UserDAO() try: retrieve_user = user_dao.retrieve(user) except: result = return_status(-2) return jsonify(result) # 用户不存在 if retrieve_user is None: result = return_status(-1) return jsonify(result) # 获取信息 data = request.get_json() if 'image' not in data or 'points' not in data: return "信息不完整" image = data['image'] points = data['points'] for _ in range(len(points)): points[_][1] = 1 - points[_][1] # data = datas['data'] # # anchor = data['anchor'] # anchor_x = anchor['x'] # anchor_y = anchor['y'] # anchor_color = anchor['color'] # print(anchor_x + ' ' + anchor_y + ' ' + anchor_color) # # hint = data['hint'] # 处理图片 try: image = get_request_image(image) image = from_png_to_jpg(image) except: result = return_status(-1) return jsonify(result) # 生成图片id id = get_work_id() path = 'works/' + str(auth_user_id) + '/' + str(id) path = mkdir(path) cv2.imwrite(path + '/sketch.jpg', image) address = Address() address.set_work_id(id) address.set_user_id(auth_user_id) address.set_path(path) original_image = str(auth_user_id) + str(id) + '0' address.set_original_image(original_image) colorization_image = str(auth_user_id) + str(id) + '1' address.set_colorization_image(colorization_image) receipt = str(id) + 'r' + str(auth_user_id) address.set_receipt(receipt) address_dao = AddressDAO() address_dao.add(address) path = path + '/result.jpg' pool = [image, points, path] threading.Thread(target=handle_colorization, args=(pool, )).start() # cv2.imwrite(path, image) result = return_receipt(0, address) return jsonify(result)
def upload(): auth = request.headers.get('Authorization') auth_user_id = Auth.identify(auth) # Authorization header不正确 if auth_user_id is None: result = return_status(-2) return jsonify(result) # 获取用户 user = User() user.set_user_id(auth_user_id) user_dao = UserDAO() try: retrieve_user = user_dao.retrieve(user) except: result = return_status(-2) return jsonify(result) # 用户不存在 if retrieve_user is None: result = return_status(-1) return jsonify(result) data = request.get_json() if 'name' not in data or 'created' not in data or 'description' not in data or 'tags' not in data or 'allow_download' not in data or 'allow_sketch' not in data or 'allow_fork' not in data or 'original_image' not in data or 'colorization_image' not in data: return '信息不完整' work = Work() work.set_artist(auth_user_id) name = data['name'] name = str(name) work.set_name(name) created_time = data['created'] created_time = str(created_time) work.set_created(created_time) description = data['description'] description = str(description) work.set_description(description) tags = data['tags'] work.set_tags(tags) allow_downloaded = data['allow_download'] allow_downloaded = bool(allow_downloaded) work.set_allow_fork(allow_downloaded) allow_sketch = data['allow_sketch'] allow_sketch = bool(allow_sketch) work.set_allow_sketch(allow_sketch) allow_fork = data['allow_fork'] allow_fork = bool(allow_fork) work.set_allow_fork(allow_fork) original_image = data['original_image'] original_image = str(original_image) colorization_image = data['colorization_image'] colorization_image = str(colorization_image) address = Address() address.set_original_image(original_image) address.set_colorization_image(colorization_image) work_dao = WorkDAO() try: work_dao.add_work(work, address) result = return_status(0) return jsonify(result) except: result = return_status(-2) return jsonify(result)