Ejemplo n.º 1
0
def tag_del(id):
    tag = Tag.query.get_or_404(id)
    with db.auto_commit():
        Oplog('删除标签:' + tag.name + ',id:' + str(tag.id))
        db.session.delete(tag)
        flash('标签删除成功~', 'ok')
        return redirect(url_for('admin.tag_list', page=1))
Ejemplo n.º 2
0
def save_drift(drift_form, current_gift):
    with db.auto_commit():
        drift = Drift()
        # 使用poplutate_obj,如果模型中字段的名称和form中字段名称相同
        # 就可以将form中的模型字段拷贝到drift模型中
        drift_form.populate_obj(drift)

        drift.gift_id = current_gift.id
        drift.requester_id = current_user.id
        drift.requester_nickname = current_user.nickname
        drift.gifter_nickname = current_gift.user.nickname
        drift.gifter_id = current_gift.user.id

        # book原本是字典,需要通过字典的方式来访问
        # 为了保证调用的统一性将current_gift获取到的数据
        # 用BookViewModel构建成一个对象
        book = BookViewModel(current_gift.book)

        drift.book_title = book.title
        drift.book_author = book.author
        drift.book_img = book.image
        drift.isbn = book.isbn

        current_user.beans -= 1

        db.session.add(drift)
Ejemplo n.º 3
0
def role_del(id):
    role = Role.query.get_or_404(id)
    with db.auto_commit():
        Oplog('删除角色:' + role.name + ',id:' + str(role.id))
        db.session.delete(role)
        flash('删除角色成功~', 'ok')
        return redirect(url_for('admin.role_list', page=1))
Ejemplo n.º 4
0
def init_cmdb():
    try:
        # 取host (在server表里)
        # fields = ['id', 'hostname', 'ip', 'vm_status', 'st', 'uuid', 'manufacturers', 'server_type', 'server_cpu', 'os',
        #           'server_disk', 'server_mem', 'mac_address', 'manufacture_date', 'check_update_time', 'server_purpose',
        #           'server_run', 'expire', 'server_up_time']
        # 将角色对应的p_id都转为name,最终返回的结果p_id的值都是name
        hosts = Server.query.all()

        with db.auto_commit():
            for h in hosts:
                h_s = dict(h)
                data = {'cmdb_hostid': h_s['id']}
                # where = {'ip': h_s['ip']}
                # result = app.config['cursor'].execute_update_sql('zbhost', data, where)
                try:
                    zbh = Zbhost.query.filter(
                        Zbhost.ip == h_s['ip']).first_or_404()
                    zbh.set_attrs(data)
                    db.session.add(zbh)
                except:
                    continue

    # 更新到cache表, ip
    except:
        return ""
Ejemplo n.º 5
0
def init():
    zbhost = Zbhost.query.all()
    if zbhost:
        with db.auto_commit():
            [db.session.delete(z) for z in zbhost]
    init_zabbix()
    init_cmdb()
Ejemplo n.º 6
0
def login():
    response = {'code': 200, 'msg': '登录成功', 'data': {}}
    request_value = request.values
    code = request_value['code'] if 'code' in request_value else ''
    if not code or len(code) < 1:
        response['code'] = -1
        response['msg'] = '登录失败, 未得到code凭证'
        return jsonify(response)

    # spider = LoginSpider()   # spider is loaded with openid
    # spider.get_openid(code)
    # result = UserBind.in_table(spider.openID)  # result is a record
    Test_openid = '119'
    result = UserBind.in_table(Test_openid)

    if result:  # user's openid has been bound
        # login procedure
        login_user(result.user, remember=True)
        response['data'] = {
            'gender': result.user.gender,
            'nickName': result.user.nickname,
            'id': result.user.id,
            'avatarUrl': result.user.avatarUrl
        }
    else:  # register procedure
        with db.auto_commit():
            user = User()
            user.nickname = request_value['nickName']
            user.gender = request_value['gender']
            user.avatarUrl = request_value['avatarUrl']
            db.session.add(user)
        with db.auto_commit():
            user_bind = UserBind()
            #user_bind.openid = spider.openID          ################
            user_bind.openid = Test_openid
            user_bind.user = user
            # Mind this statement, not sure if it can work
            db.session.add(user_bind)

        login_user(user, remember=True)
        response['data'] = {  # response after the register procedure
            'gender': user.gender,
            'nickName': user.nickname,
            'id': user.id,
            'avatarUrl': user.avatarUrl
        }
    return jsonify(response)
Ejemplo n.º 7
0
def refuse_float(id):
    """拒绝对方请求"""
    with db.auto_commit():
        item = Float.query.filter_by(giver_id=current_user.id, id=id).first_or_404()
        item.status = FloatStatus.Refused
        # 把对方预付的豆子从系统中退还
        item.requester.beans += current_app.config["BEANS_REQUEST_PER_BOOK"]
    return redirect(url_for("web.transactions"))
Ejemplo n.º 8
0
def withdraw_float(id):
    """撤销向对方索取书籍"""
    with db.auto_commit():
        item = Float.query.filter_by(request_id=current_user.id, id=id).first_or_404()
        item.status = FloatStatus.Withdrew
        # 拿回自己的豆子
        current_user.beans += current_app.config["BEANS_REQUEST_PER_BOOK"]
    return redirect(url_for("web.transactions"))
Ejemplo n.º 9
0
def change_password():
    form = ForgetPwsRequestForm(request.form)
    if request.method == 'POST' and form.validate():
        user = User.query.get(current_user.id)
        with db.auto_commit():
            user.password = form.password1.data
        return redirect(url_for('web.index'))
    return render_template('auth/forget_password.html', form=form)
Ejemplo n.º 10
0
def redraw_drift(drift_id):
    with db.auto_commit():
        drift = Drift.query.filter_by(
            id=drift_id, requester_id=current_user.id).first_or_404()
        drift.pending = PendingStatus.Redraw
        current_user.beans += 1
        drift.delete()
    return redirect(url_for('drift.pending'))
Ejemplo n.º 11
0
def reject_drift(did):
    with db.auto_commit():
        drift = Drift.query.filter(Gift.uid == current_user.id,
                                   id == did).first_or_404()
        drift.pending = PendingStatus.Reject
        requester = User.query.get_or_404(drift.requester_id)
        requester.beans += 1
    return redirect(url_for("web.pending"))
Ejemplo n.º 12
0
def reject_drift(did):
    with db.auto_commit():
        drift = Drift.query.filter_by(gifter_id=current_user.id,
                                      id=did).first_or_404()
        drift.pending = PendingStatus.Reject
        requester = User.query.filter_by(id=drift.requester_id).first_or_404()
        requester.beans += 1
    return redirect(url_for('web.pending'))
Ejemplo n.º 13
0
def redraw_from_wish(isbn):
    wish = Wish.query.filter_by(isbn=isbn).first()
    if not wish:
        flash('该心愿不存在,删除失败')
    else:
        with db.auto_commit():
            wish.delete()
    return redirect(url_for('web.my_wish'))
Ejemplo n.º 14
0
def reject_drift(did):
    with db.auto_commit():
        drift = Drift.query.filter(Gift.uid == current_user.id,
                                   Drift.id == did).first_or_404()
        drift.pending = PendingStatus.Reject
        requester = User.query.get_or_404(drift.requester_id)
        requester.beans += current_app.config['BEANS_EVERY_DRIFT']
    return redirect(url_for('web.pending'))
Ejemplo n.º 15
0
def register():
    args = request.get_json()
    schema = RegisterValSchema(strict=True).load(args)
    with db.auto_commit():
        user = User()
        user.set_attrs(schema.data)
        db.session.add(user)
    return SuccessResponse(CREATED)()
Ejemplo n.º 16
0
def add_book():
    form = BookForm(request.form)
    if request.method == 'POST' and form.validate():
        with db.auto_commit(throw=False):
            book = Book()
            book.set_attrs(form.data)
            db.session.add(book)
    return render_template('book/add_book.html', form=form)
Ejemplo n.º 17
0
def change_password():
    form = ChangePasswordForm(request.form)
    if request.method == 'POST' and form.validate():
        with db.auto_commit():
            current_user.password = form.new_password1.data
        flash('密码已更新成功')
        return redirect(url_for('web.personal'))
    return render_template('auth/change_password.html', form=form)
Ejemplo n.º 18
0
def redraw_drift(did):
    with db.auto_commit():
        drift = Drift.query.filter_by(request_id=current_user.id,
                                      id=did).first_or_404()
        drift.pending = PendingStatus.Redraw
        db.session.add(drift)
        current_user.beans += 1
    return redirect(url_for("web.pending"))
Ejemplo n.º 19
0
def mailed_drift(did):
    with db.auto_commit():
        drift = Drift.query.filter_by(gifter_id=current_user.id,
                                      id=did).first_or_404()
        drift.pending = PendingStatus.Success
        current_user.beans += 1
        gift = Gift.query.filter_by(id=drift.gift_id).first_or_404()
        gift.launched = True
    return redirect(url_for('web.pending'))
Ejemplo n.º 20
0
def redraw_from_wish(isbn):
    wish = Wish.query.filter_by(
        uid=current_user.id,
        isbn=isbn,
        launched=False
    ).first_or_404()
    with db.auto_commit():
        wish.delete()
    return redirect(url_for('web.my_wish'))
Ejemplo n.º 21
0
def save_to_gift(bid):
    # 事务
    with db.auto_commit():
        gift = Gift()
        gift.bid = bid
        gift.uid = current_user.id
        current_user.beans = 1
        db.session.add(gift)
    return 'ok'
Ejemplo n.º 22
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['BLOG_COMMENT_PER_PAGE']
    pagination = Comment.query.with_parent(post).filter_by(
        reviewed=True).order_by(Comment.timestamp.asc()).paginate(
            page, per_page)
    comments = pagination.items

    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.name
        form.email.data = current_app.config['BLOG_EMAIL']
        form.site.data = url_for('.index')
        from_admin = True
        reviewed = True
    else:
        form = CommentForm()
        from_admin = False
        reviewed = False

    if form.validate_on_submit():
        with db.auto_commit():
            author = form.author.data
            email = form.email.data
            site = form.email.data
            body = form.body.data
            comment = Comment(author=author,
                              email=email,
                              site=site,
                              body=body,
                              from_admin=from_admin,
                              post=post,
                              reviewed=reviewed)
            replied_id = request.args.get('reply')
            if replied_id:
                replied_comment = Comment.query.get_or_404(replied_id)
                comment.replied = replied_comment
                send_mail('评论有新的回复',
                          replied_comment.email,
                          'email/send_new_reply.html',
                          comment=replied_comment)
            db.session.add(comment)
        if current_user.is_authenticated:
            flash('评论发表成功!', 'success')
        else:
            flash('感谢您的评论,评论将在审核后发表', 'info')
            send_mail('新的评论',
                      current_app.config['BLOG_EMAIL'],
                      'email/send_new_comment.html',
                      post=post)
        return redirect(url_for('web.show_post', post_id=post.id))
    return render_template('blog/post.html',
                           post=post,
                           pagination=pagination,
                           comments=comments,
                           form=form)
Ejemplo n.º 23
0
def redraw_drift(did):
    with db.auto_commit():
        drift = Drift.query.filter_by(requester_id=current_user.id,
                                      id=did).first_or_404()
        drift.pending = PendingStatus.REDRAW
        current_user.beans += 1
    return redirect(url_for('web.pending'))

    pass
Ejemplo n.º 24
0
def redraw_from_wish(isbn):
    wish = Wish.query.filter(Wish.isbn == isbn, Wish.uid == current_user.id,
                             Wish.launched == False).first()
    if not wish:
        flash('该心愿不存在,删除失败')
    else:
        with db.auto_commit():
            wish.delete()
    return redirect(url_for('web.my_wish'))
Ejemplo n.º 25
0
def register():
    form = RegisterForm(request.form)
    if request.method == 'POST' and form.validate():
        with db.auto_commit():
            user = User()
            user.set_attrs(form.data)
            db.session.add(user)
        return redirect(url_for('auth.login'))
    return render_template('auth/register.html', form=form)
Ejemplo n.º 26
0
def redraw_from_gifts(gid):
    gift = Gift.query.get(gid)
    if gift:
        with db.auto_commit():
            gift.delete()
            flash_msg = '撤销书籍《' + gift.book.title + '》成功'
            flash(flash_msg)
    else:
        flash('书籍不存在')
    return redirect(url_for('web.my_gifts'))
Ejemplo n.º 27
0
def save_to_wish(isbn):
    if current_user.can_save_to_list(isbn):
        with db.auto_commit():
            wish = Wish()
            wish.isbn = isbn
            wish.uid = current_user.id
            db.session.add(wish)
    else:
        flash('这本书已添加至你的赠送清单或已存在于你得心愿清单,请不要重复添加')
    return redirect(url_for('web.book_detail', isbn=isbn))
Ejemplo n.º 28
0
def init_user():
    # user
    users = [
        User(nickname='admin',
             phone_number='18666666666',
             email='*****@*****.**')
    ]
    users[0].password = '******'
    with db.auto_commit():
        db.session.add(users)
Ejemplo n.º 29
0
def reject_drift(did):
    with db.auto_commit():
        drift = Drift.query.filter_by(gifter_id=current_user.id,
                                      id=did).first_or_404()
        drift.pending = PendingStatus.Reject
        db.session.add(drift)
        user = User.query.get(drift.requester_id)
        user.beans += 1
        db.session.add(user)
    return redirect(url_for("web.pending"))
Ejemplo n.º 30
0
Archivo: user.py Proyecto: azbhg1/temp
def pwd():
    form = UserPwdForm(request.form)
    if request.method == 'POST' and form.validate():
        user = current_user._get_current_object()
        with db.auto_commit():
            user.pwd = form.newpwd.data
            db.session.add(user)
        flash('密码修改成功,请重新登陆~', 'ok')
        return redirect(url_for('home.logout'))
    return render_template('home/pwd.html', form=form)