示例#1
0
def sign_up():
    # Redirect the user to the index page if he or she has been authenticated
    # already.
    if current_user.is_authenticated:
        return redirect(url_for('home.home'))

    form = SignUpForm(request.form)

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

    if form.validate_on_submit():
        query = User.query.filter(User.email == form.email.data)

        if query.count() > 0:
            flash(_('A user with this e-mail address already exists'),
                  'danger')
            return render_template('user/sign_up.htm', form=form)

        user = User(form.email.data, bcrypt.hashpw(form.password.data,
                    bcrypt.gensalt()), form.first_name.data,
                    form.last_name.data, form.student_id.data,
                    form.education_id.data, form.birth_date.data,
                    form.study_start.data, form.receive_information.data)
        user.phone_nr = form.phone_nr.data
        user.address = form.address.data
        user.zip = form.zip.data
        user.city = form.city.data
        user.country = form.country.data

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

        group = Group.query.filter(Group.name == 'all').first()
        group.add_user(user)

        db.session.add(group)
        db.session.commit()

        copernica.update_user(user, subscribe=True)

        login_user(user)

        flash(_('Welcome %(name)s! Your profile has been succesfully '
                'created and you have been logged in!',
                name=current_user.first_name), 'success')

        return redirect(url_for('home.home'))
    else:
        flash_form_errors(form)

    return render_template('user/sign_up.htm', form=form)
示例#2
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()
示例#3
0
def edit(user_id=None):
    """Create user for admins and edit for admins and users."""
    if not ModuleAPI.can_write('user') and\
            (current_user.is_anonymous or current_user.id != user_id):
        return abort(403)

    # Select user
    if user_id:
        user = User.query.get_or_404(user_id)
    else:
        user = User()

    user.avatar = UserAPI.has_avatar(user_id)

    if ModuleAPI.can_write('user'):
        form = EditUserForm(request.form, user)
        is_admin = True
    else:
        form = EditUserInfoForm(request.form, user)
        is_admin = False

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

    def edit_page():
        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('_')

        group = Group.query.filter(Group.name == 'all').first()
        group.add_user(user)

        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)

        user.first_name = form.first_name.data.strip()
        user.last_name = form.last_name.data.strip()
        user.locale = form.locale.data
        if ModuleAPI.can_write('user'):
            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.student_id = form.student_id.data.strip()
        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()

        if form.password.data != '':
            user.password = bcrypt.hashpw(form.password.data, bcrypt.gensalt())

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

        avatar = request.files['avatar']
        if avatar:
            UserAPI.upload(avatar, user.id)

        if user_id:
            copernica.update_user(user)
            flash(_('Profile succesfully updated'))
        else:
            copernica.update_user(user, subscribe=True)
            flash(_('Profile succesfully created'))
        return redirect(url_for('user.view_single', user_id=user.id))
    else:
        flash_form_errors(form)

    return edit_page()