Esempio n. 1
0
def user_u(u_id):
    ''' to update user '''
    form = forms.User_a(session.get('lang'))
    user = data.User.query.filter_by(id=u_id).first()

    if user is None:
        flash('Error: user selected does not exist, something wrong !',
              'danger')
        return redirect(url_for('core.root'))

    if user.id == 1:
        return reject_god(lambda: None)()

    if form.validate_on_submit():
        user.name = form.name.data
        user.password = form.password.data
        user.role_id = form.role.data

        # Remove operator if role has changed
        if form.role.data == 3:
            if not data.Office.get(id=form.offices.data):
                flash('Error: Office selected does not exist!', 'danger')
                return redirect(url_for('core.root'))

            operator = data.Operators.get(user.id)

            if not operator:
                db.session.add(data.Operators(user.id, form.offices.data))
            else:
                operator.office_id = form.offices.data
        else:
            to_delete = data.Operators.get(user.id)

            if to_delete is not None:
                db.session.delete(to_delete)

        db.session.commit()
        flash('Notice: user is updated . ', 'info')
        return redirect(url_for('administrate.users'))

    if not form.errors:
        form.name.data = user.name
        form.role.data = user.role_id

        if user.role_id == 3:
            form.offices.data = data.Operators.get(user.id).office_id

    return render_template('user_add.html',
                           form=form,
                           navbar='#snb3',
                           page_title='Update user : ' + user.name,
                           u=user,
                           update=True,
                           offices_count=data.Office.query.count())
Esempio n. 2
0
def user_d(user):
    ''' to delete user '''
    if user.id == 1:
        return reject_god(lambda: None)()

    if user.role_id == 3:
        db.session.delete(data.Operators.get(user.id))
        db.session.commit()

    db.session.delete(user)
    db.session.commit()
    flash('Notice: user is deleted .', 'info')
    return redirect(url_for('administrate.users'))
Esempio n. 3
0
def user_d(u_id):
    ''' to delete user '''
    user = data.User.query.filter_by(id=u_id).first()

    if user is None:
        flash('Error: user selected does not exist, something wrong !',
              'danger')
        return redirect(url_for('core.root'))

    if user.id == 1:
        return reject_god(lambda: None)()

    if user.role_id == 3:
        db.session.delete(data.Operators.get(user.id))
        db.session.commit()

    db.session.delete(user)
    db.session.commit()
    flash('Notice: user is deleted .', 'info')
    return redirect(url_for('administrate.users'))