コード例 #1
0
ファイル: controller1.py プロジェクト: twintechlabs/flaskdash
def create_or_edit_user_page():
    form = UserProfileForm(request.form, obj=current_user)
    roles = Role.query.all()
    user_id = request.args.get('user_id')
    user = User()

    if user_id:
        user = User.query.filter(User.id == user_id).first()

    if request.method == 'POST':
        if user.id is None:
            user = User.query.filter(
                User.email == request.form['email']).first()
            if not user:
                user = User(email=request.form['email'],
                            full_name=request.form['full_name'],
                            password=current_app.user_manager.hash_password(
                                request.form['password']),
                            active=True,
                            email_confirmed_at=datetime.datetime.utcnow())
                db.session.add(user)
                db.session.commit()
            return redirect(url_for('main.user_admin_page'))
        else:
            user.email = request.form['email']
            user.full_name = request.form['full_name']
            if request.form['password'] is not None and request.form[
                    'password'] is not "":
                user.password = current_app.user_manager.hash_password(
                    request.form['password'])
            db.session.commit()
    return render_template('views/admin/edit_user.html',
                           form=form,
                           roles=roles,
                           user=user)
コード例 #2
0
def parse_authorized_response(resp):

    global uandus_client

    if resp is None:
        flash('Authentication failed.')
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'], request.args['error_description'])
    if isinstance(resp, dict):
        session['access_token'] = (resp['access_token'], '')
        session['refresh_token'] = (resp['refresh_token'], '')
        uandus_client.set_auth(access_token=session['access_token'][0],
                               refresh_token=session['refresh_token'][0])

    elif isinstance(resp, OAuthResponse):
        print(resp.status)
        if resp.status != 200:
            session['access_token'] = None
            session['refresh_token'] = None
            # session['uandus_client'] = None
            return redirect(url_for('index'))
        else:
            session['access_token'] = (resp.data['access_token'], '')
            session['refresh_token'] = (resp.data['refresh_token'], '')
            uandus_client.set_auth(access_token=session['access_token'][0],
                                   refresh_token=session['refresh_token'][0])
    else:
        raise Exception()

    user_info = uandus_client.user_me()

    user = User.query.filter(User.username == user_info['username']).first()
    if not user:
        user = User(username=user_info['username'],
                    first_name=user_info['first_name'],
                    last_name=user_info['last_name'],
                    email=user_info['email'])
        db.session.add(user)
        db.session.commit()
    elif user:
        user.first_name = user_info['first_name']
        user.last_name = user_info['last_name']
        user.email = user_info['email']
        db.session.commit()
    login_user(user, True)
    return user
コード例 #3
0
def create_user_page():
    user_id = request.args.get('user_id')
    # Initialize form
    user = User()
    if user_id is not None:
        user = User.query.filter_by(id=user_id).first()
    form = UserProfileForm(request.form, obj=user)

    if request.method == 'POST':
        form.populate_obj(user)
        role = Role.query.filter(Role.name == "admin").first()
        if user.id is None:
            user = User(email=request.form['email'],
                        first_name=request.form['first_name'],
                        last_name=request.form['last_name'],
                        password=current_app.user_manager.hash_password(
                            request.form['password']),
                        active=True,
                        email_confirmed_at=datetime.datetime.utcnow())
            if str(request.form['role']) == "admin":
                user.roles.append(role)
            else:
                user.roles = []
            db.session.add(user)
            db.session.commit()
        else:
            user.email = request.form['email']
            user.first_name = request.form['first_name']
            user.last_name = request.form['last_name']
            if request.form['password'] is not None and request.form[
                    'password'] is not "":
                user.password = current_app.user_manager.hash_password(
                    request.form['password'])
            if str(request.form['role']) == "admin":
                user.roles.append(role)
            else:
                user.roles = []
            db.session.commit()
        flash('You successfully updated your user!', 'success')
        return redirect(url_for('main.user_admin_page'))
    return render_template('pages/admin/create_user.html',
                           user=user,
                           form=form)
コード例 #4
0
def admin_create_tutor():

    form = TutorCustomForm()
    # this next line needed for a validation in the user model class
    form.id = -1

    # adding the full set of select options to the select list
    addTutorFormChoices(form)
    if form.add_time.data:
        form.dates.append_entry()
        return render_template('admin/admin_create_edit_tutor.html',
                               form=form,
                               time_state='manage_time',
                               state='Create')

    if form.remove_time.data:
        removeTime(form)
        return render_template('admin/admin_create_edit_tutor.html',
                               form=form,
                               time_state='manage_time',
                               state='Create')

    if form.validate_on_submit():
        user = User()
        user.first_name = form.first_name.data
        user.last_name = form.last_name.data
        user.email = form.email.data
        user.roles = []
        for role_id in form.roles.data:
            roleObj = Role.query.filter(Role.id == role_id).first()
            user.roles.append(roleObj)

        user.password = current_app.user_manager.password_manager.hash_password(
            form.password.data)

        tutor = Tutor()
        tutor.tutor_phone = form.phone.data
        tutor.display_in_sched = form.display_in_sched.data

        tutor.languages = []
        for lang in form.languages:
            if lang.checked is True:
                langObj = Language.query.filter(
                    Language.id == lang.data).first()
                tutor.languages.append(langObj)

        tutor.courses = []
        for course in form.courses:
            if course.checked is True:
                courseObj = Course.query.filter(
                    Course.id == course.data).first()
                tutor.courses.append(courseObj)

        for date_group in form.dates:
            time = Time()
            time.time_day = date_group['time_day'].data
            time.time_start = date_group['time_start'].data
            time.time_end = date_group['time_end'].data
            tutor.dates.append(time)

        user.tutor = tutor
        db.session.add(user)
        db.session.commit()

        flash('User Created!!', 'success')
        return redirect(url_for('admin.admin_list_users'))
    return render_template('admin/admin_create_edit_tutor.html',
                           form=form,
                           state='Create')