コード例 #1
0
ファイル: user.py プロジェクト: viaict/viaduct
def edit(user_id, form_cls):
    """
    Create user for admins and edit for admins and users.

    User and form type are passed based on routes below.
    """
    if user_id:
        user = user_service.get_user_by_id(user_id)
        user.avatar = user_service.user_has_avatar(user_id)
    else:
        user = User()

    form = init_form(form_cls, obj=user)
    form.new_user = user.id == 0

    # Add education.
    educations = Education.query.all()
    form.education_id.choices = [(e.id, e.name) for e in educations]

    def edit_page():
        is_admin = role_service.user_has_role(current_user, Roles.USER_WRITE)
        return render_template('user/edit.htm', form=form, user=user,
                               is_admin=is_admin)

    if form.validate_on_submit():

        # Only new users need a unique email.
        query = User.query.filter(User.email == form.email.data)
        if user_id:
            query = query.filter(User.id != user_id)

        if query.count() > 0:
            flash(_('A user with this e-mail address already exist.'),
                  'danger')
            return edit_page()

        # Because the user model is constructed to have an ID of 0 when it is
        # initialized without an email adress provided, reinitialize the user
        # with a default string for email adress, so that it will get a unique
        # ID when committed to the database.
        if not user_id:
            user = User('_')

        # TODO Move this into the service call.
        try:
            user.update_email(form.email.data.strip())
        except HttpError as e:
            if e.resp.status == 404:
                flash(_('According to Google this email does not exist. '
                        'Please use an email that does.'), 'danger')
                return edit_page()
            raise e

        # Note: student id is updated separately.
        user.first_name = form.first_name.data.strip()
        user.last_name = form.last_name.data.strip()
        user.locale = form.locale.data
        if role_service.user_has_role(current_user, Roles.USER_WRITE):
            user.has_paid = form.has_paid.data
            user.honorary_member = form.honorary_member.data
            user.favourer = form.favourer.data
            user.disabled = form.disabled.data
            user.alumnus = form.alumnus.data
        user.education_id = form.education_id.data
        user.birth_date = form.birth_date.data
        user.study_start = form.study_start.data
        user.receive_information = form.receive_information.data

        user.phone_nr = form.phone_nr.data.strip()
        user.address = form.address.data.strip()
        user.zip = form.zip.data.strip()
        user.city = form.city.data.strip()
        user.country = form.country.data.strip()

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

        avatar = request.files.get('avatar')
        if avatar:
            user_service.set_avatar(user.id, avatar)

        if user_id:
            copernica.update_user(user)
            flash(_('Profile succesfully updated'))
        else:
            copernica.update_user(user, subscribe=True)
            flash(_('Profile succesfully created'))

        if current_user.id == user_id:
            return redirect(url_for('user.view_single_self'))
        else:
            return redirect(url_for('user.view_single_user', user_id=user.id))

    return edit_page()