示例#1
0
文件: user.py 项目: wovari/scoremodel
def v_user_create():
    db_roles = a_roles.list()
    possible_roles = []
    for db_role in db_roles:
        possible_roles.append((db_role.id, db_role.role))
    form = UserCreateForm()
    form.roles.choices = possible_roles

    if request.method == 'POST' and form.validate_on_submit():
        a_user = UserApi()
        input_data = {
            'email': form.email.data,
            'password': form.password.data,
            'roles': form.roles.data,
            'username': form.email.data,
        }
        try:
            new_user = a_user.create(input_data)
        except DatabaseItemAlreadyExists as e:
            flash(
                _('A user called {0} already exists.').format(
                    input_data['email']))
            return render_template('admin/user/create.html', form=form)
        except RequiredAttributeMissing as e:
            flash(
                _('A required form element was not submitted: {0}').format(e))
            return render_template('admin/user/create.html', form=form)
        except Exception as e:  # Remove this after debugging
            #    flash('An unexpected error occurred: {0}'.format(e))
            flash(_('An unexpected error occurred.'))
            return render_template('admin/user/create.html', form=form)
        else:
            return redirect(url_for('admin.v_user_list'))

    return render_template('admin/user/create.html', form=form)
示例#2
0
文件: auth.py 项目: wovari/scoremodel
def v_register():
    if current_user.is_authenticated:
        # Force logout
        logout_user()
    form = RegistrationForm()
    a_user = UserApi()
    a_role = RoleApi()
    a_lang = LangApi()
    a_o_type = OrganisationTypeApi()
    a_org = OrganisationApi()
    form.language.choices = [(l.id, l.lang) for l in a_lang.list()]
    form.organisation_type.choices = [(t.id, t.type) for t in a_o_type.list()]
    if request.method == 'POST' and form.validate_on_submit():
        user_data = {
            'email': form.email.data,
            'password': form.password.data,
            'username': form.email.data,
            'lang_id': form.language.data
        }
        try:
            public_role = a_role.get_by_role('public')
        except DatabaseItemDoesNotExist:
            flash(_('An unexpected error occurred.'))
            return redirect(url_for('admin.v_register'))

        user_data['roles'] = [public_role.id]
        if form.organisation_name:
            # Add to organisation & create
            organisation_data = {
                'name': form.organisation_name.data,
                'size': form.organisation_size.data,
                'type_id': form.organisation_type.data
            }
            try:
                new_organisation = a_org.create(organisation_data)
            except Exception as e:
                flash(_('An unexpected error occurred.'))
                logger.exception(str(e))
                return redirect(url_for('admin.v_register'))
            user_data['organisation_id'] = new_organisation.id
        try:
            new_user = a_user.create(user_data)
        except DatabaseItemAlreadyExists:
            flash(_('This e-mail address is already in use.'))
        except RequiredAttributeMissing as e:
            flash(
                _('A required form element was not submitted: {0}').format(e))
        except Exception as e:  # Remove this after debugging
            #    flash('An unexpected error occurred: {0}'.format(e))
            flash(_('An unexpected error occurred.'))
            logger.exception(str(e))
            return redirect(url_for('admin.v_register'))
        else:
            flash(
                _('You have been successfully registered. Please log in using your username and password.'
                  ))
            return redirect(url_for('admin.v_login'))
    return render_template('admin/user/register.html', form=form)
示例#3
0
def add_admin():
    user_api = UserApi()
    role_api = RoleApi()
    admin_role = role_api.get_by_role('administrator')
    if app.config['DEBUG'] is True:
        password = '******'
    else:
        password = ''.join(SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(10))
    user_data = {
        'email': '*****@*****.**',
        'password': password,
        'roles': [
            admin_role.id
        ]
    }
    try:
        user_api.get_by_user('*****@*****.**')
    except DatabaseItemDoesNotExist:
        admin = user_api.create(user_data)
        return {
            'user': admin,
            'password': user_data['password']
        }