Beispiel #1
0
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)
        })
Beispiel #2
0
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)
        })
Beispiel #3
0
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': '未知错误'})
Beispiel #4
0
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)
        })
Beispiel #5
0
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': '保存失败:'})
Beispiel #6
0
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)
        })
Beispiel #7
0
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)
        })
Beispiel #8
0
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)})
Beispiel #9
0
#!/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()