Exemple #1
0
def get_user(db, role, defaults):
    while True:
        new_social = 'local$' + random_alphanumeric(32)
        existing_user = UserModel.query.filter_by(social_id=new_social).first()
        if existing_user:
            continue
        break
    the_user = UserModel.query.filter_by(nickname=defaults['nickname']).first()
    if the_user:
        return the_user
    user_auth = UserAuthModel(password=app.user_manager.hash_password(
        defaults.get('password', 'password')))
    the_user = UserModel(active=defaults.get('active', True),
                         nickname=defaults['nickname'],
                         social_id=new_social,
                         email=defaults['email'],
                         user_auth=user_auth,
                         first_name=defaults.get('first_name', ''),
                         last_name=defaults.get('last_name', ''),
                         country=defaults.get('country', ''),
                         subdivisionfirst=defaults.get('subdivisionfirst', ''),
                         subdivisionsecond=defaults.get(
                             'subdivisionsecond', ''),
                         subdivisionthird=defaults.get('subdivisionthird', ''),
                         organization=defaults.get('organization', ''),
                         confirmed_at=datetime.datetime.now())
    the_user.roles.append(role)
    db.session.add(user_auth)
    db.session.add(the_user)
    db.session.commit()
    return the_user
Exemple #2
0
def user_add():
    setup_translation()
    user_role = db.session.execute(
        select(Role).filter_by(name='user')).scalar_one()
    add_form = UserAddForm(request.form, role_id=[str(user_role.id)])
    add_form.role_id.choices = [(r.id, r.name) for r in db.session.execute(
        select(Role.id, Role.name).where(Role.name != 'cron').order_by('name'))
                                ]
    add_form.role_id.default = user_role.id
    if str(add_form.role_id.data) == 'None':
        add_form.role_id.data = user_role.id
    if request.method == 'POST' and add_form.validate():
        user, user_email = app.user_manager.find_user_by_email(
            add_form.email.data)
        if user:
            flash(word("A user with that e-mail has already registered"),
                  "error")
            return redirect(url_for('user_add'))
        user_auth = UserAuthModel(
            password=app.user_manager.hash_password(add_form.password.data))
        while True:
            new_social = 'local$' + random_alphanumeric(32)
            existing_user = db.session.execute(
                select(UserModel).filter_by(social_id=new_social)).scalar()
            if existing_user:
                continue
            break
        the_user = UserModel(active=True,
                             nickname=re.sub(r'@.*', '', add_form.email.data),
                             social_id=new_social,
                             email=add_form.email.data,
                             user_auth=user_auth,
                             first_name=add_form.first_name.data,
                             last_name=add_form.last_name.data,
                             confirmed_at=datetime.datetime.now())
        num_roles = 0
        for role in db.session.execute(select(Role).order_by('id')).scalars():
            if role.id in add_form.role_id.data:
                the_user.roles.append(role)
                num_roles += 1
        if num_roles == 0:
            the_user.roles.append(user_role)
        db.session.add(user_auth)
        db.session.add(the_user)
        db.session.commit()
        #docassemble.webapp.daredis.clear_user_cache()
        flash(word("The new user has been created"), "success")
        return redirect(url_for('user_list'))
    response = make_response(
        render_template('users/add_user_page.html',
                        version_warning=None,
                        bodyclass='daadminbody',
                        page_title=word('Add User'),
                        tab_title=word('Add User'),
                        form=add_form), 200)
    response.headers[
        'Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
    return response
def user_add():
    user_role = Role.query.filter_by(name='user').first()
    add_form = UserAddForm(request.form, role_id=[text_type(user_role.id)])
    add_form.role_id.choices = [(r.id, r.name)
                                for r in db.session.query(Role).filter(
                                    Role.name != 'cron').order_by('name')]
    add_form.role_id.default = user_role.id
    if text_type(add_form.role_id.data) == 'None':
        add_form.role_id.data = user_role.id
    if request.method == 'POST' and add_form.validate():
        user, user_email = app.user_manager.find_user_by_email(
            add_form.email.data)
        if user:
            flash(word("A user with that e-mail has already registered"),
                  "error")
            return redirect(url_for('user_add'))
        user_auth = UserAuthModel(
            password=app.user_manager.hash_password(add_form.password.data))
        while True:
            new_social = 'local$' + random_alphanumeric(32)
            existing_user = UserModel.query.filter_by(
                social_id=new_social).first()
            if existing_user:
                continue
            break
        the_user = UserModel(active=True,
                             nickname=re.sub(r'@.*', '', add_form.email.data),
                             social_id=new_social,
                             email=add_form.email.data,
                             user_auth=user_auth,
                             first_name=add_form.first_name.data,
                             last_name=add_form.last_name.data,
                             confirmed_at=datetime.datetime.now())
        num_roles = 0
        for role in Role.query.order_by('id'):
            if role.id in add_form.role_id.data:
                the_user.roles.append(role)
                num_roles += 1
        if num_roles == 0:
            the_user.roles.append(user_role)
        db.session.add(user_auth)
        db.session.add(the_user)
        db.session.commit()
        #docassemble.webapp.daredis.clear_user_cache()
        flash(word("The new user has been created"), "success")
        return redirect(url_for('user_list'))
    return render_template('users/add_user_page.html',
                           version_warning=None,
                           bodyclass='daadminbody',
                           page_title=word('Add User'),
                           tab_title=word('Add User'),
                           form=add_form)
Exemple #4
0
def get_user(the_db, role, defaults, result=None):
    if result is None:
        result = {}
    the_user = the_db.session.execute(
        select(UserModel).filter_by(nickname=defaults['nickname'])).scalar()
    if the_user:
        return the_user
    while True:
        new_social = 'local$' + random_alphanumeric(32)
        existing_user = the_db.session.execute(
            select(UserModel).filter_by(social_id=new_social)).scalar()
        if existing_user:
            continue
        break
    user_auth = UserAuthModel(password=app.user_manager.hash_password(
        defaults.get('password', 'password')))
    the_user = UserModel(active=defaults.get('active', True),
                         nickname=defaults['nickname'],
                         social_id=new_social,
                         email=defaults['email'],
                         user_auth=user_auth,
                         first_name=defaults.get('first_name', ''),
                         last_name=defaults.get('last_name', ''),
                         country=defaults.get('country', ''),
                         subdivisionfirst=defaults.get('subdivisionfirst', ''),
                         subdivisionsecond=defaults.get(
                             'subdivisionsecond', ''),
                         subdivisionthird=defaults.get('subdivisionthird', ''),
                         organization=defaults.get('organization', ''),
                         confirmed_at=datetime.datetime.now())
    the_user.roles.append(role)
    the_db.session.add(user_auth)
    the_db.session.add(the_user)
    the_db.session.commit()
    result['changed'] = True
    return the_user