示例#1
0
def server_edit(id):
    if current_user.is_root:
        server = Servers.query.get_or_404(id)
        form = ServersForm(obj=server)
    else:
        server = Servers.query.join(User.servers).filter(User.organisation_id==current_user.organisation_id) \
                                                 .filter(Servers.id == id) \
                                                 .order_by(Servers.name).first()
        form = ServersForm(obj=server)
        form.users.query_factory = lambda: User.query.filter(Servers.organisation_id == Organisations.id) \
                                                     .filter(Servers.organisation_id == User.organisation_id) \
                                                     .filter(User.organisation_id == current_user.organisation_id) \
                                                     .filter(Servers.id == id) \
                                                     .all()

    form.organisations.allow_blank = False
    if form.validate_on_submit():
        form.populate_obj(server)

        users = _add_users(form)

        server.users = users
        db.session.add(server)
        db.session.commit()
        flash(_('Server edit'))
        return redirect(url_for("servers.server"))
    return render_template('server_edit.html', form=form)
示例#2
0
def server_add():
    form = ServersForm()
    if not current_user.is_root:
        form.organisations.query_factory = lambda: Organisations.query.filter(Organisations.id == current_user.organisation_id).all()
        form.organisations.allow_blank = False

    if form.validate_on_submit():
        server = Servers(form.name.data, form.address.data,
                         form.login.data, form.password.data)

        users = _add_users(form)
        server.users = users
        server.organisation_id = form.organisations.data.id
        server.protocol = form.protocol.data

        db.session.add(server)
        db.session.commit()
        flash(_('Server added'))
        return redirect(url_for("servers.server"))
    return render_template('server_add.html', form=form)