def getSubList(): # page_index start from 1 page_size = 10 page_index = request.form.get('page', None) if page_index is None: ret = {'code': status.get('ERROR'), 'MESSAGE': '参数不合法'} return jsonify(ret) page_index = int(page_index) subjectList = session.query(Subject).order_by(Subject.tid.desc()).slice( (page_index - 1) * page_size, page_index * page_size) data = list() for v in subjectList: user = session.query(User).filter(User.uid == v.uid).first() username = '******' if user is None else user.username avater = 'http://pic1.cugapp.com/FikstAllXLweowBEXpy5FQxPd8td.jpg' if user is None else user.avatar_url threads = session.query(Post).filter(Post.tid == v.tid).count() item = { 'tid': v.tid, 'username': username, 'title': v.title, 'post_time': v.post_time, 'threads': threads, 'avatar': avater, } data.append(item) ret = {'code': status.get('SUCCESS'), 'MESSAGE': "获取列表成功", 'data': data} return jsonify(ret)
def like(): tid = request.form.get("tid") fid = request.form.get("fid") likeValue = int(request.form.get("like")) if likeValue < -2 or likeValue > 2: ret = {"code": status.get('ERROR'), "MESSAGE": '点赞值超过限制'} return jsonify(ret) like_uid = g.uid floor = session.query(Post).filter(Post.tid == tid, Post.fid == fid).first() liked_uid = floor.uid if like_uid == liked_uid: ret = {"code": status.get('ERROR'), "MESSAGE": '无法点赞自己'} return jsonify(ret) floor.like += likeValue comment = session.query(Comment).filter( Comment.uid_commenter == like_uid, Comment.uid_commentee == liked_uid).first() if comment is None: comment = Comment(uid_commenter=like_uid, uid_commentee=liked_uid, counter=0) session.add(comment) comment.counter += likeValue session.commit() result = { "code": status.get("SUCCESS"), "MESSAGE": "点赞成功", } return jsonify(result)
def setUserInfo(): username = request.form.get('username', None) pic_file = request.files.get('new_pic', None) phone = request.files.get('new_phone', None) email = request.files.get('new_email', None) qq = request.files.get('new_qq', None) wechat = request.files.get('new_wechat', None) uid = ses.get('uid') user = session.query(User).filter(User.uid == uid).first() if user is None: ret = {'code': status.get('ERROR'), 'MESSAGE': '未找到该用户'} return jsonify(ret) if username is not None: user.username = username if phone is not None: user.phone = phone if email is not None: user.email = email if qq is not None: user.qq = qq if wechat is not None: user.wechat = wechat if pic_file is not None: pic_info = requests.post('http://api.cugxuan.cn:8080/upload', files={'file': pic_file}) pic_info = pic_info.json() if pic_info['code'] == 200: pic_url = 'http://pic1.cugapp.com/' + pic_info['name'] user.avatar_url = pic_url else: ret = {'code': status.get('ERROR'), 'MESSAGE': '无法上传图片'} return jsonify(ret) session.commit() ret = {"code": status.get("SUCCESS"), "messages": '修改用户信息成功'} return jsonify(ret)
def modSubject(): tid = request.form.get('tid', None) title = request.form.get('title', None) uid = ses.get("uid", None) if tid is None or title is None: ret = {'code': status.get('ERROR'), 'MESSAGE': '参数不合法'} return jsonify(ret) subject = session.query(Subject).filter(Subject.tid == tid).first() ret = {'code': status.get('ERROR'), 'MESSAGE': '找不到该贴'} if subject is not None: subject.title = title session.commit() ret['code'] = status.get('SUCCESS') ret['MESSAGE'] = '修改成功' return jsonify(ret)
def comment(): tid = request.form.get("tid") content = request.form.get("content") comment_floor = request.form.get("comment_floor", 1) last_floor = session.query(Post).order_by(Post.fid.desc()).first() message = Post(tid=tid, content=content, comment_floor=int(comment_floor), fid=last_floor.fid + 1, like=0, uid=g.uid) session.add(message) commentee = session.query(Post).filter(Post.fid == comment_floor).first() relation = session.query(Comment).filter( Comment.uid_commenter == g.uid, Comment.uid_commentee == commentee.uid).first() if commentee.uid != g.uid: if relation is None: comment = Comment(uid_commenter=g.uid, uid_commentee=commentee.uid, counter=1) session.add(comment) elif relation.uid_commentee != g.uid: relation.counter += 1 session.commit() result = { "code": status.get("SUCCESS"), "message": "回复成功!", } return jsonify(result)
def logout(): ses.pop("uid") result = { "code": status.get("SUCCESS"), "Message": "登出成功", } return jsonify(result)
def getSubCount(): subject_count = session.query(Subject).count() ret = { 'code': status.get('SUCCESS'), 'MESSAGE': "获取主题贴总数成功", 'data': subject_count } return jsonify(ret)
def getUid(): uid = ses.get('uid', None) print(uid) if uid is None: result = { "code": status.get("ERROR"), "MESSAGE": "获取UID失败", "uid": -1, } print(result) return jsonify(result) result = { "code": status.get("SUCCESS"), "MESSAGE": "获取UID成功", "uid": uid, } print(result) return jsonify(result)
def getUserInfo(): that_uid = request.form.get('uid', None) uid = ses.get('uid', None) if that_uid is None: ret = {'code': status.get('ERROR'), 'MESSAGE': '参数不合法'} return jsonify(ret) user = session.query(User).filter(User.uid == that_uid).first() if user is None: ret = {'code': status.get('ERROR'), 'MESSAGE': '未找到该用户'} return jsonify(ret) uid = int(uid) that_uid = int(that_uid) print(uid, that_uid) if uid != that_uid: intimate1 = session.query(Comment).filter( Comment.uid_commentee == that_uid, Comment.uid_commenter == uid).first() intimate2 = session.query(Comment).filter( Comment.uid_commentee == uid, Comment.uid_commenter == that_uid).first() if intimate1 is None or intimate2 is None or min( intimate1.counter, intimate2.counter) < 70: ret = { 'code': status.get('PERMISSION'), 'MESSAGE': '亲密度未达到查看个人信息要求' } return jsonify(ret) ret = { "code": status.get("SUCCESS"), "messages": '获取用户信息成功', 'data': { 'uid': uid, 'username': user.username, 'avatar': user.avatar_url, 'phone': user.phone, 'email': user.email, 'qq': user.qq, 'wechat': user.wechat } } print(ret) return jsonify(ret)
def register(): username = request.form.get("username") password = request.form.get("password") phone = request.form.get("phone") email = request.form.get("email") qq = request.form.get("qq") wechat = request.form.get("wechat") if username and password and phone and email: hash = hashlib.md5() hash.update(password.encode(encoding='utf-8')) user = session.query(User).filter(User.username == username).first() if not user: token = str(int(random.uniform(100, 10000))) active[token] = username user = User(username=username, password=hash.hexdigest(), avatar_url='http://pic1.cugapp.com/FikstAllXLweowBEXpy5FQxPd8td.jpg', phone=phone, email=email, qq=qq, wechat=wechat, is_active=0) session.add(user) session.commit() send_mail(email, token) result = { "code": status.get("SUCCESS"), "MESSAGE": "邮件发送成功", } return jsonify(result) result = { "code": status.get("FAIL"), "message": "注册失败,用户名已存在", } return jsonify(result) result = { "code": status.get("FAIL"), "MESSAGE": "参数不足", } return jsonify(result)
def login(): username = request.form.get("username") password = request.form.get("password") if username and password: hash = hashlib.md5() hash.update(password.encode(encoding='utf-8')) user = session.query(User).filter(User.username == username, User.password == hash.hexdigest()).first() if user and user.is_active: ses["uid"] = user.uid result = { "code": status.get("SUCCESS"), "MESSAGE": "登陆成功", } return jsonify(result) elif user and not user.is_active: session.delete(user) session.commit() result = { "code": status.get("FAIL"), "MESSAGE": "登陆失败,请检查用户密码", } return jsonify(result)
def Hot(id): relation = session.query(Comment).filter( Comment.uid_commentee == id, Comment.uid_commenter == g.uid).first() if relation: hot = relation.counter else: hot = 0 result = { "code": status.get("SUCCESS"), "MESSAGE": "亲密度获取成功", "result": hot, } return jsonify(result)
def getUidByUsername(): username = request.form.get('username', None) if username is None: result = { "code": status.get("ERROR"), "MESSAGE": "获取UID失败", } return jsonify(result) user = session.query(User).filter(User.username == username).first() if user is None: result = { "code": status.get("ERROR"), "MESSAGE": "不存在该用户", } return jsonify(result) uid = user.uid result = { "code": status.get("SUCCESS"), "MESSAGE": "获取UID成功", "uid": uid, } return jsonify(result)
def post(): title = request.form.get('title', None) content = request.form.get("content") uid = g.uid if title is None: ret = {'code': status.get('ERROR'), 'MESSAGE': '参数不合法'} return jsonify(ret) new_subject = Subject(uid=uid, title=title) session.add(new_subject) session.commit() message = Post(tid=new_subject.tid, content=content, comment_floor=1, fid=1, like=0, uid=g.uid) session.add(message) session.commit() result = { "code": status.get("SUCCESS"), "message": "发帖成功!", } return jsonify(result)
def delete(): tid = request.form.get("tid") subject = session.query(Subject).filter(Subject.tid == tid, Subject.uid == g.uid).first() if subject: session.delete(subject) floors = session.query(Post).filter(Post.tid == tid).all() for floor in floors: session.delete(floor) session.commit() result = { "code": status.get("SUCCESS"), "MESSAGE": "删除成功", } return jsonify(result)
def history(): passages = session.query(Subject).filter(Subject.uid == g.uid).all() infos = [] for passage in passages: info = { "tid": passage.tid, "title": passage.title, "post_time": passage.post_time } infos.append(info.copy()) result = { "code": status.get("SUCCESS"), "MESSAGE": "获取个人历史发帖纪录成功", "infos": infos } return jsonify(result)
def sendSecMessage(): to_uid = request.form.get("to_uid", None) from_uid = g.uid msg_type = int(request.form.get("msg_type", 0)) if msg_type == 0: message = request.form.get('message', None) else: message = request.files.get('message', None) if message is None or to_uid is None: ret = {'code': status.get('ERROR'), 'MESSAGE': '参数不合法'} return jsonify(ret) intimate1 = session.query(Comment).filter( Comment.uid_commentee == from_uid, Comment.uid_commenter == to_uid).first() intimate2 = session.query(Comment).filter( Comment.uid_commentee == to_uid, Comment.uid_commenter == from_uid).first() if intimate1 is None or intimate2 is None or min(intimate1.counter, intimate2.counter) < 30: ret = {'code': status.get('PERMISSION'), 'MESSAGE': '亲密度未达到文本私信要求'} return jsonify(ret) from_name = session.query(User).filter(User.uid == from_uid).first() to_name = session.query(User).filter(User.uid == to_uid).first() if to_name is None: ret = {'code': status.get('ERROR'), 'MESSAGE': '参数不合法'} return jsonify(ret) if msg_type == 1: if intimate1 is None or intimate2 is None or min( intimate1.counter, intimate2.counter) < 50: ret = {'code': status.get('PERMISSION'), 'MESSAGE': '亲密度未达到文本私信要求'} return jsonify(ret) pic_info = requests.post('http://api.cugxuan.cn:8080/upload', files={'file': message}) pic_info = pic_info.json() if pic_info['code'] == 200: pic_url = 'http://pic1.cugapp.com/' + pic_info['name'] message = pic_url else: ret = {'code': status.get('ERROR'), 'MESSAGE': '无法上传图片'} return jsonify(ret) new_message = SecMessage(uid_sender=from_uid, uid_receiver=to_uid, message=message, name_sender=from_name.username, name_receiver=to_name.username, msg_type=msg_type) session.add(new_message) session.commit() result = { "code": status.get("SUCCESS"), "message": "发送成功!", } return jsonify(result)
def getSecMessage(): uid = g.uid msgs = session.query(SecMessage).filter( SecMessage.uid_receiver == uid or SecMessage.uid_sender == uid).all() messages = [] for msg in msgs: message = { "from_id": msg.uid_sender, "from_name": msg.name_sender, "to_id": msg.uid_receiver, "to_name": msg.name_receiver, "text": msg.message, "msg_type": msg.msg_type } messages.append(message.copy()) result = { "code": status.get("SUCCESS"), "MESSAGE": '私信获取成功', 'data': messages } return jsonify(result)
def view(tid): floors = session.query(Post).filter(Post.tid == tid).all() messages = [] for floor in floors: commentee = session.query(Post).filter( Post.fid == floor.comment_floor).first() commentee_name = session.query(User).filter( User.uid == commentee.uid).first().username commenter = session.query(User).filter(User.uid == floor.uid).first() commenter_name = commenter.username avatar = commenter.avatar_url message = { "content": floor.content, "commentee_floor": floor.comment_floor, "commentee_name": commentee_name, "commenter_name": commenter_name, "avatar": avatar, "post_time": floor.post_time, "like": floor.like, "fid": floor.fid, } messages.append(message.copy()) result = {"code": status.get("SUCCESS"), "messages": messages, "tid": tid} return jsonify(result)