Esempio n. 1
0
    def post(self):
        # 获取用户传过来的数据
        data = request.json

        # 验证参数有效性
        form = UserForm(data=data)
        if form.validate():
            # # 创建用户
            # user = UserProfile(user_profile_email=data.get("email"),
            #                    user_profile_name=data.get("name"),
            #                    password=data.get("password"))
            # db.session.add(user)
            # db.session.commit()
            # result = user_schema.dump(user)
            UserProfile.create_user(user_profile_email=form.email.data,
                                    user_profile_name=form.name.data,
                                    password=form.password.data)
            user = UserProfile.query.filter_by(user_profile_email=data.get("email")).first()
            result = user_schema.dump(user)

            # 返回结果
            return generate_response(data=result)
        else:
            result = form.errors
            raise FormValidateException(message=result)
Esempio n. 2
0
def index():
    form = UserForm(nickname=current_user.nickname, email=current_user.email)
    if request.method == 'POST' and form.validate():
        mongo.db.users.update({'_id': ObjectId(current_user.get_id())}, {'$set': {'email': form.email.data, 'nickname': form.nickname.data}})
        mongo.db.bookmarks.update({'user._id': ObjectId(current_user.get_id())}, {'$set': {'user.email': form.email.data, 'user.nickname': form.nickname.data}}, multi=True)
        if form.new_password.data:
            mongo.db.users.update({'_id': ObjectId(current_user.get_id())}, {'$set': {'password': bcrypt.generate_password_hash(form.new_password.data, rounds=12)}})
        flash(u'Les modifications ont été correctement enregistrées.')

    return render_template('profil/index.html', form=form)
Esempio n. 3
0
def user_edit(user_id: int):
    user = get(User, user_id)
    if request.method == 'POST':
        form = UserForm(request.form, obj=user)
        if form.validate():
            form.populate_obj(user)
            user.save()
            flash('Yes, usuário alterado com sucesso.', 'success')
            return redirect(url_for('users.user_index'))
    else:
        form = UserForm(obj=user)
    return render_template('users/edit.html', form=form)
Esempio n. 4
0
def user_new():
    user = User()
    if request.method == 'POST':
        form = UserForm(request.form, obj=user)
        if form.validate():
            try:
                form.populate_obj(user)
                validate_user(user)
                user.save()
                flash('Yes, usuário cadastrado com sucesso.', 'success')
                return redirect(url_for('users.user_index'))
            except AttributeError as e:
                flash(str(e), 'warning')
    else:
        form = UserForm(obj=user)
    return render_template('users/new.html', form=form)
Esempio n. 5
0
def create():
    form = UserForm(get_data(request))

    if form.validate():
        newUser = User(
            str(uuid4()), form.name.data, False, form.receive_marketing.data,
            form.timezone.data)
        skills = list(filter(None, form.skills.data.split(',')))
        [newUser.addSkill(skill.strip()) for skill in skills]
        newUser.setExternalId(form.external_id.data)
        newUser.changeEmail(form.email.data)
        users.addUser(newUser)
        return jsonify({
            "data":
            newUser.toDict(),
            "links": userHateoasLinks(newUser)
        }), 201

    return jsonify({"errors": list(form.errors.items())}), 400
Esempio n. 6
0
def edit_user():

    form = UserForm()

    if request.method == 'GET':

        user_email = request.args.get('user_email')
        db = OracleDb()
        user = db.sqlalchemy_session.query(ormUser).filter(
            ormUser.user_email == user_email).one()

        # fill form and send to teacher
        form.user_email.data = user.user_email
        form.user_name.data = user.user_name
        form.user_phone.data = user.user_phone

        return render_template('user.html',
                               form=form,
                               form_name="Edit user",
                               action="edit_user")

    else:

        if form.validate() == False:
            return render_template('user.html',
                                   form=form,
                                   form_name="Edit user",
                                   action="edit_user")
        else:
            db = OracleDb()
            # find user
            user = db.sqlalchemy_session.query(ormUser).filter(
                ormUser.user_email == form.user_email.data).one()

            # update fields from form data
            user.user_email = form.user_email.data
            user.user_name = form.user_name.data
            user.user_phone = form.user_phone.data

            db.sqlalchemy_session.commit()

            return redirect(url_for('all_user'))
Esempio n. 7
0
def update(pk):
    try:
        user = users.getUser(pk=pk)
        form = UserForm(get_data(request))
        if form.validate():
            user.changeName(form.name.data)
            user.changeMarketingPreferences(form.receive_marketing.data)
            user.changeTimeZone(form.timezone.data)
            skills = list(filter(None, form.skills.data.split(',')))
            [user.addSkill(skill.strip()) for skill in skills]
            user.setExternalId(form.external_id.data)
            user.changeEmail(form.email.data)
            return jsonify({
                "data":
                user.toDict(),
                "links": userHateoasLinks(user)
            }), 200
        return jsonify({"errors": list(form.errors.items())}), 400
    except IndexError:
        return Response("User not found", 404)
Esempio n. 8
0
def new_user():

    form = UserForm()

    if request.method == 'POST':
        if form.validate() == False:
            return render_template('user.html',
                                   form=form,
                                   form_name="New user",
                                   action="new_user")
        else:
            new_user = ormUser(user_name=form.user_name.data,
                               user_email=form.user_email.data,
                               user_phone=form.user_phone.data)
            db = OracleDb()
            db.sqlalchemy_session.add(new_user)
            db.sqlalchemy_session.commit()

            return redirect(url_for('all_user'))

    return render_template('user.html',
                           form=form,
                           form_name="New user",
                           action="new_user")