Esempio n. 1
0
def user_a():
    ''' to add a user '''
    form = forms.User_a(session.get('lang'))

    if form.validate_on_submit():
        if data.User.query.filter_by(name=form.name.data).first() is not None:
            flash('Error: user name already exists, choose another name',
                  'danger')
            return redirect(url_for('administrate.user_a'))

        db.session.add(
            data.User(form.name.data, form.password.data, form.role.data))
        db.session.commit()

        # Fix: multiple operators for office
        # adding user to Operators list
        if form.role.data == 3:
            db.session.add(
                data.Operators(
                    data.User.query.filter_by(name=form.name.data).first().id,
                    form.offices.data))
            db.session.commit()

        flash('Notice: user has been added .', 'info')
        return redirect(url_for('administrate.users'))

    return render_template('user_add.html',
                           form=form,
                           navbar='#snb3',
                           page_title='Add user',
                           offices_count=data.Office.query.count())
Esempio n. 2
0
def user_a():
    """ to add a user """
    if current_user.role_id != 1:
        flash('Error: wrong entry, something went wrong', "danger")
        return redirect(url_for('core.root'))
    form = forms.User_a(session.get('lang'))
    if form.validate_on_submit():
        if data.User.query.filter_by(name=form.name.data).first() is not None:
            flash("Error: user name already exists, choose another name",
                  "danger")
            return redirect(url_for('administrate.user_a'))
        db.session.add(
            data.User(form.name.data, form.password.data, form.role.data))
        db.session.commit()
        # Fix: multiple operators for office
        # adding user to Operators list
        if form.role.data == 3:
            db.session.add(
                data.Operators(
                    data.User.query.filter_by(name=form.name.data).first().id,
                    form.offices.data))
            db.session.commit()
        flash("Notice: user has been added .", "info")
        return redirect(url_for('administrate.users'))
    return render_template('user_add.html',
                           form=form,
                           navbar='#snb3',
                           ptitle='Add user')
Esempio n. 3
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. 4
0
def user_u(u_id):
    """ to update user """
    if current_user.role_id != 1:
        flash('Error: only administrator can access the page', "danger")
        return redirect(url_for('core.root'))
    form = forms.User_a(session.get('lang'))
    u = data.User.query.filter_by(id=u_id).first()
    if u is None:
        flash("Error: user selected does not exist, something wrong !",
              "danger")
        return redirect(url_for("core.root"))
    if u.id == 1:
        flash("Error: main admin account cannot be updated .", "danger")
        return redirect(url_for("administrate.users"))
    if form.validate_on_submit():
        u.name = form.name.data
        u.password = form.password.data
        u.role_id = form.role.data
        # Remove operator if role has changed
        if form.role.data == 3:
            if data.Operators.query.filter_by(id=u.id).first() is None:
                db.session.add(data.Operators(u.id, form.offices.data))
        else:
            toRemove = data.Operators.query.filter_by(id=u.id).first()
            if toRemove is not None:
                db.session.delete(toRemove)
        db.session.commit()
        flash("Notice: user is updated . ", "info")
        return redirect(url_for('administrate.users'))
    if not form.errors:
        form.name.data = u.name
        form.role.data = u.role_id
        # Fix: multiple operators for office
        # fetch office id if operator
        if u.role_id == 3:
            form.offices.data = data.Operators.query.filter_by(
                id=u.id).first().office_id
    return render_template('user_add.html',
                           form=form,
                           navbar='#snb3',
                           ptitle='Update user : ' + u.name,
                           u=u,
                           update=True)