def channel_new_message(): user = get_login_user() content = request.form['content'] image_id = request.values.get('image_id', default=0, type=int) if content == '': return jsonify({'status': 2}) if user.ban == 1: return jsonify({ 'status': 1, 'message': '你被禁言了,无法发送!' }) if image_id == 0: image_id = None db = DBSession() try: channel = Channel(content=content, image_id=image_id, user_id=user.id) db.add(channel) db.commit() db.close() return jsonify({ 'status': 0, 'message': '发出去辣' }) except Exception as e: db.close() return jsonify({ 'status': 2, 'message': str(e) })
def is_admin(): dbs = DBSession() user_id = session.get('user_id') user = dbs.query(User).filter(User.id == user_id).first() dbs.close() if user is not None: if user.admin == 1: return True return False
def get_login_user(): if is_login(): dbs = DBSession() user_id = session.get('user_id') user = dbs.query(User).filter(User.id == user_id).first() dbs.close() return user else: return None
def users_page_count(): try: db_session = DBSession() users = db_session.query(User).all() count = len(users) / 10 return jsonify({ 'status': 0, 'message': '获取成功', 'page_count': int(count) + 1 }) except Exception as e: return jsonify({ 'status': 1, 'message': '获取失败', 'error_message': str(e) })
def users_admin_update(): try: form = request.form admin = form['admin'] ban = form['ban'] nickname = form['nickname'] sex = form['sex'] password = form['password'] db_session = DBSession() user_id = form['id'] user = db_session.query(User).filter_by(id=user_id).first() if admin != None and admin != '': user.admin = admin if ban != None and ban != '': user.ban = ban if nickname != None and nickname != '': user.nickname = nickname if sex != None and sex != '': user.sex = sex if password != '' and password != None: password_encoded = password_encode(password) user.password = password_encoded db_session.commit() db_session.close() return jsonify({'status': 0, 'message': '修改成功'}) except Exception as e: return jsonify({ 'status': 1, 'message': '获取失败', 'error_message': str(e) })
def users_login(): form = request.form if None == form['username'] or form['username'] == '': return jsonify({'status': 0, 'message': '请输入用户名!'}) if None == form['password'] or form['password'] == '': return jsonify({'status': 1, 'message': '请输入密码!'}) db_session = DBSession() user = db_session.query(User).filter( User.username == form['username']).first() db_session.close() if None is not user and password_auth( password_to_be_checked=form['password'], password=user.password): set_login(user) return jsonify({'status': 2, 'message': '登录成功'}) else: return jsonify({'status': 3, 'message': '登录失败'})
def users_admin_delete(): try: form = request.form db_session = DBSession() user_id = form['id'] user = db_session.query(User).filter_by(id=user_id).first() db_session.delete(user) db_session.commit() db_session.close() return jsonify({'status': 0, 'message': '删除成功'}) except Exception as e: return jsonify({ 'status': 1, 'message': '获取失败', 'error_message': str(e) })
def users_channel_count(): try: db_session = DBSession() userid = session.get('user_id') # db操作 # user = db_session.query(Channel).filter_by(user_id=userid).all() # # countNum = int(len(user)) countNum = db_session.query(Channel).filter_by(user_id=userid).count() db_session.close() return jsonify({ 'status': 0, 'message': '获得数据成功', 'countNum': countNum }) except Exception as e: print(e) return jsonify({'status': 1, 'message': '没有登录'})
def upload_avatar(): user = get_login_user() file = request.files['file'] try: suffix = file.filename.rsplit('.', 1)[1] cwd = os.getcwd() url = generate_random_name(12) + '.' + suffix file.save(os.path.join(cwd + '/app/api/static/upload/' + url)) db = DBSession() user = db.query(User).filter(User.id == user.id).first() user.avatar = '/api/static/upload/' + url db.commit() url = user.avatar db.close() return jsonify({'status': 0, 'message': '保存成功', 'data': {'url': url}}) except Exception as e: return jsonify({'status': 1, 'message': '保存失败:'})
def upload(): file = request.files['file'] try: suffix = file.filename.rsplit('.', 1)[1] cwd = os.getcwd() url = generate_random_name(12) + '.' + suffix file.save(os.path.join(cwd + '/app/api/static/upload/' + url)) db = DBSession() image = Image(url='/api/static/upload/' + url) db.add(image) db.commit() url = image.url image_id = image.id db.close() return jsonify({ 'status': 0, 'message': '保存成功', 'data': { 'image_id': image_id, 'url': url } }) except Exception as e: return jsonify({'status': 1, 'message': '保存失败:' + str(e)})
#!/usr/bin/python3 # -*- coding:utf-8 -*- from app.base.extensions import DBSession from app.model.User import User session = DBSession() new_user = User(username='******', password='******') session.add(new_user) session.commit() session.close()
def users_create(): form = request.form try: username = form['username'] if username == None or username == '': return jsonify({'status': 1, 'message': '用户名为空'}) password = form['password'] if password == None or password == '': return jsonify({'status': 2, 'message': '密码为空'}) password_again = form['password_again'] if password_again == None or password_again == '': return jsonify({'status': 3, 'message': '确认密码为空'}) if password_again != password: return jsonify({'status': 3, 'message': '两次密码不同'}) email = form['email'] if email == None or email == '': return jsonify({'status': 4, 'message': '邮箱空'}) if correct_email(email) == False: return jsonify({'status': 4, 'message': '邮箱格式错误'}) # sex = form['sex'] # nickname = form['nickname'] # 密码加密 password_encoded = password_encode(password) # db操作 db_session = DBSession() user = db_session.query(User).filter(User.username == username).first() if user is not None: db_session.close() return jsonify({'status': 1, 'message': '用户名已存在'}) email_db = db_session.query(User).filter(User.email == email).first() if email_db is not None: db_session.close() return jsonify({'status': 4, 'message': '邮箱重复'}) user = User(username=username, password=password_encoded, email=email) db_session.add(user) db_session.commit() user = db_session.query(User).filter_by(username=username).first() db_session.close() set_login(user) # 自动登录 return jsonify({'status': 0, 'message': '注册成功, 即将跳转个人中心完善个人信息'}) except Exception as e: print(e) return jsonify({'status': 5, 'message': '未知错误'})
def users_list(): try: db_session = DBSession() page_num = int(request.form['page']) page_cur = (page_num - 1) * 10 user_dict_list = [] users = db_session.query(User).limit(11).offset(page_cur).all() if len(users) <= 10: for i in users: user_dict = {} user_id = i.id user_username = i.username user_admin = i.admin user_ban = i.ban user_nickname = i.nickname user_sex = i.sex user_dict.update({ 'uid': user_id, 'username': user_username, 'admin': user_admin, 'ban': user_ban, 'nickname': user_nickname, 'sex': user_sex }) user_dict_list.append(user_dict) return jsonify({ 'status': 2, 'message': '这是最后了', 'data': user_dict_list, 'page': page_num }) for i in range(10): user_dict = {} user_id = users[i].id user_username = users[i].username user_admin = users[i].admin user_ban = users[i].ban user_nickname = users[i].nickname user_sex = users[i].sex user_dict.update({ 'uid': user_id, 'username': user_username, 'admin': user_admin, 'ban': user_ban, 'nickname': user_nickname, 'sex': user_sex }) user_dict_list.append(user_dict) db_session.close() return jsonify({ 'status': 0, 'message': '获取成功', 'data': user_dict_list, 'page': page_num }) except Exception as e: return jsonify({ 'status': 1, 'message': '获取失败', 'data': {}, 'error_message': str(e) })
def follow(): user = get_login_user() channel_user_id = request.values.get('id', default=0, type=int) if channel_user_id == 0: return jsonify({'status': 1, 'message': '错误的id'}) db = DBSession() try: user = db.query(User).filter(User.id == channel_user_id).first() if user is not None: followed = db.query(Follow).filter(user_id=user.id, channel_user_id=channel_user_id).first() if followed is None: db.add(Follow(user_id=user.id, channel_user_id=channel_user_id, status=1)) db.commit() return jsonify({ 'status': 0, 'message': '关注成功' }) else: db.delete(followed) db.commit() return jsonify({ 'status': 0, 'message': '取关成功' }) else: return jsonify({ 'status': 1, 'message': '该用户不存在' }) except Exception as e: db.close() return jsonify({ 'status': 1, 'message': str(e), 'error_message': str(e) })
def channel_like(): user = get_login_user() channel_id = request.values.get('id', default=0, type=int) if channel_id == 0: return jsonify({'status': 1, 'message': '错误的id'}) db = DBSession() try: channel = db.query(Channel).filter(Channel.id == channel_id).first() if channel is not None: liked = db.query(Like).filter(Like.user_id == user.id, Like.channel_id == channel_id).first() if liked is not None: db.delete(liked) db.commit() like_count = db.query(Like).filter(Like.channel_id == channel_id).count() return jsonify({ 'status': -1, 'message': 'ok', 'data': {'like_count': int(like_count)} }) else: like = Like(user_id=user.id, status=1, channel_id=channel_id) db.add(like) db.commit() like_count = db.query(Like).filter(Like.channel_id == channel_id).count() return jsonify({ 'status': 0, 'message': 'ok', 'data': {'like_count': int(like_count)} }) else: return jsonify({ 'status': 1, 'message': '消息可能已被删除' }) except Exception as e: db.close() return jsonify({ 'status': 1, 'message': str(e), 'error_message': str(e) })
def list_follow(): user = get_login_user() page = request.values.get('page', default=1, type=int) db = DBSession() try: # 取本人订阅的频道主的最近消息 query = db.query(Channel) \ .join(Follow, Channel.user_id == Follow.channel_user_id) \ .filter(Follow.user_id == user.id) count = query.count() channels = query.order_by(Channel.id.desc()).limit(10).offset((page - 1) * 10).all() is_end = (page * 10 >= count) data = [] for channel in channels: user = db.query(User).filter(User.id == channel.user_id).first() avatar = None if user.avatar != '0': # image = db.query(Image).filter(Image.id == user.avatar_id).first() avatar = user.avatar if user.avatar == '0': avatar = '/web/static/asset/chisec/avator.jpg' media = None if channel.image_id is not None and channel.image_id is not '': image = db.query(Image).filter(Image.id == channel.image_id).first() media = image.url like_count = db.query(Like) \ .filter(Like.channel_id == channel.id) \ .count() if is_login(): liked = db.query(Like) \ .filter(Like.channel_id == channel.id, Like.user_id == user.id) \ .count() if liked > 0: liked = True else: liked = False else: liked = 0 data.append({ 'id': channel.id, 'user_id': user.id, 'content': channel.content, 'channel_name': user.channel_name, 'username': user.username, 'avatar': avatar, 'media': media, 'like_count': like_count, 'liked': liked, 'count': count, 'create_time': pd_time(channel.create_time) }) db.close() if is_end: return jsonify({ 'status': 2, 'message': '没有更多消息了', 'data': data }) else: return jsonify({ 'status': 0, 'message': 'ok', 'data': data }) except Exception as e: db.close() return jsonify({ 'status': 1, 'message': '拉取失败', 'error_message': str(e) })