Exemple #1
0
def update_photo():
    """Allow the user to update their photo."""
    if flask.request.method != 'POST':
        return flask.redirect(flask.request.referrer or
                              flask.url_for('dashboard.profile'))

    if not login.current_user.can_update_photo():
        flask.flash(
            flask.Markup(
                (
                    'You cannot currently change your photo. Please contact '
                    '<a href="{0}">the ticketing officer</a> for assistance.'
                ).format(
                    APP.config['TICKETS_EMAIL_LINK']
                )
            ),
            'error'
        )

        return flask.redirect(flask.request.referrer or
                              flask.url_for('dashboard.profile'))

    if (
            'photo' in flask.request.files and
            flask.request.files['photo'].filename != ''
    ):
        old_photo = login.current_user.photo

        new_photo = photos.save_photo(flask.request.files['photo'])

        login.current_user.photo = new_photo

        DB.session.delete(old_photo)
        DB.session.add(new_photo)

        DB.session.commit()

        # We don't want to delete the photo from S3 until after the DB has
        # been updated
        if old_photo is not None:
            photos.delete_photo(old_photo)

        APP.log_manager.log_event(
            'Updated photo',
            user=login.current_user
        )

        flask.flash(
            'Your photo has been updated',
            'success'
        )
    else:
        flask.flash('You must select a photo to upload.', 'warning')

    return flask.redirect(flask.request.referrer or
                          flask.url_for('dashboard.profile'))
Exemple #2
0
def update_photo():
    """Allow the user to update their photo."""
    if flask.request.method != "POST":
        return flask.redirect(
            flask.request.referrer or flask.url_for("dashboard.profile")
        )

    if not login.current_user.can_update_photo():
        flask.flash(
            flask.Markup(
                (
                    "You cannot currently change your photo. Please contact "
                    '<a href="{0}">the ticketing officer</a> for assistance.'
                ).format(APP.config["TICKETS_EMAIL_LINK"])
            ),
            "error",
        )

        return flask.redirect(
            flask.request.referrer or flask.url_for("dashboard.profile")
        )

    if "photo" in flask.request.files and flask.request.files["photo"].filename != "":
        old_photo = login.current_user.photo

        new_photo = photos.save_photo(flask.request.files["photo"])

        login.current_user.photo = new_photo

        DB.session.delete(old_photo)
        DB.session.add(new_photo)

        DB.session.commit()

        # We don't want to delete the photo from S3 until after the DB has
        # been updated
        if old_photo is not None:
            photos.delete_photo(old_photo)

        APP.log_manager.log_event("Updated photo", user=login.current_user)

        flask.flash("Your photo has been updated", "success")
    else:
        flask.flash("You must select a photo to upload.", "warning")

    return flask.redirect(flask.request.referrer or flask.url_for("dashboard.profile"))
def update_photo():
    """Allow the user to update their photo."""
    if flask.request.method != 'POST':
        return flask.redirect(flask.request.referrer
                              or flask.url_for('dashboard.profile'))

    if not login.current_user.can_update_photo():
        flask.flash(
            flask.Markup(
                ('You cannot currently change your photo. Please contact '
                 '<a href="{0}">the ticketing officer</a> for assistance.'
                 ).format(APP.config['TICKETS_EMAIL_LINK'])), 'error')

        return flask.redirect(flask.request.referrer
                              or flask.url_for('dashboard.profile'))

    if ('photo' in flask.request.files
            and flask.request.files['photo'].filename != ''):
        old_photo = login.current_user.photo

        new_photo = photos.save_photo(flask.request.files['photo'])

        login.current_user.photo = new_photo

        DB.session.delete(old_photo)
        DB.session.add(new_photo)

        DB.session.commit()

        # We don't want to delete the photo from S3 until after the DB has
        # been updated
        if old_photo is not None:
            photos.delete_photo(old_photo)

        APP.log_manager.log_event('Updated photo', user=login.current_user)

        flask.flash('Your photo has been updated', 'success')
    else:
        flask.flash('You must select a photo to upload.', 'warning')

    return flask.redirect(flask.request.referrer
                          or flask.url_for('dashboard.profile'))
Exemple #4
0
def register():
    """Process a registration.

    After registration, the user must click a link in an email sent to the
    address they registered with to confirm that it is valid.
    """
    if flask.request.method != 'POST':
        return flask.redirect(flask.url_for('router'))

    flashes = []

    if models.User.get_by_email(flask.request.form['email']) is not None:
        flask.flash(('That email address already has an associated account. '
                     'Use the links below to verify your email or reset your '
                     'password.'), 'error')
        return flask.redirect(flask.url_for('front.home'))

    if ('password' not in flask.request.form
            or 'confirm' not in flask.request.form or
            flask.request.form['password'] != flask.request.form['confirm']):
        flashes.append('Passwords do not match')

    if ('forenames' not in flask.request.form
            or flask.request.form['forenames'] == ''):
        flashes.append('Forenames cannot be blank')

    if ('surname' not in flask.request.form
            or flask.request.form['surname'] == ''):
        flashes.append('Surname cannot be blank')

    if ('email' not in flask.request.form
            or flask.request.form['email'] == ''):
        flashes.append('Email cannot be blank')

    if ('password' not in flask.request.form
            or flask.request.form['password'] == ''):
        flashes.append('Password cannot be blank')
    elif len(flask.request.form['password']) < 8:
        flashes.append('Password must be at least 8 characters long')

    if ('phone' not in flask.request.form
            or flask.request.form['phone'] == ''):
        flashes.append('Phone cannot be blank')

    # if (
    #         'college' not in flask.request.form or
    #         flask.request.form['college'] == '---'
    # ):
    #     flashes.append('Please select a college')
    #
    # if (
    #         'affiliation' not in flask.request.form or
    #         flask.request.form['affiliation'] == '---'
    # ):
    #     flashes.append('Please select an affiliation')

    if APP.config['REQUIRE_USER_PHOTO'] and (
            'photo' not in flask.request.files
            or flask.request.files['photo'].filename == ''):
        flashes.append('Please upload a photo')

    if 'accept_terms' not in flask.request.form:
        flashes.append('You must accept the Terms and Conditions')

    if flashes:
        flask.flash(('There were errors in your provided details. Please fix '
                     'these and try again'), 'error')
        for msg in flashes:
            flask.flash(msg, 'warning')

        return flask.render_template(
            'front/home.html',
            form=flask.request.form,
            colleges=models.College.query.all(),
            affiliations=models.Affiliation.query.all())

    if APP.config['REQUIRE_USER_PHOTO']:
        photo = photos.save_photo(flask.request.files['photo'])

        DB.session.add(photo)
        DB.session.commit()
    else:
        photo = None

    user = models.User(
        flask.request.form['email'],
        flask.request.form['password'],
        flask.request.form['forenames'],
        flask.request.form['surname'],
        flask.request.form['phone'],
        models.College.query.get_or_404(1),  #flask.request.form['college']),
        models.Affiliation.query.get_or_404(flask.request.form['affiliation']),
        photo)

    DB.session.add(user)
    DB.session.commit()

    APP.log_manager.log_event('Registered', user=user)

    #todo: reinstate
    APP.email_manager.send_template(
        flask.request.form['email'],
        'Confirm your Email Address',
        'email_confirm.email',
        name=user.forenames,
        confirmurl=flask.url_for('front.confirm_email',
                                 user_id=user.object_id,
                                 secret_key=user.secret_key,
                                 _external=True),
        destroyurl=flask.url_for('front.destroy_account',
                                 user_id=user.object_id,
                                 secret_key=user.secret_key,
                                 _external=True))

    flask.flash('Your user account has been registered', 'success')
    flask.flash(
        ('Before you can log in, you must confirm your email address. '
         'Please check your email for further instructions. If the message '
         'does not arrive, please check your spam/junk mail folder.'), 'info')

    affiliation_logic.maybe_verify_affiliation(user)

    return flask.redirect(flask.url_for('front.home'))
Exemple #5
0
def register():
    """Process a registration.

    After registration, the user must click a link in an email sent to the
    address they registered with to confirm that it is valid.
    """
    if flask.request.method != "POST":
        return flask.redirect(flask.url_for("router"))

    affiliations = models.Affiliation.query.all()
    alumni_affiliation_ids = [
        str(affiliation.object_id) for affiliation in affiliations
        if "Alumnus" in affiliation.name
    ]
    alumni_affiliation_ids_json = json.dumps(alumni_affiliation_ids)

    flashes = []

    if models.User.get_by_email(flask.request.form["email"]) is not None:
        flask.flash(
            ("That email address already has an associated account. "
             "Use the links below to verify your email or reset your "
             "password."),
            "error",
        )
        return flask.redirect(flask.url_for("front.home"))

    if ("password" not in flask.request.form
            or "confirm" not in flask.request.form or
            flask.request.form["password"] != flask.request.form["confirm"]):
        flashes.append("Passwords do not match")

    if "forenames" not in flask.request.form or flask.request.form[
            "forenames"] == "":
        flashes.append("Forenames cannot be blank")

    if "surname" not in flask.request.form or flask.request.form[
            "surname"] == "":
        flashes.append("Surname cannot be blank")

    if "email" not in flask.request.form or flask.request.form["email"] == "":
        flashes.append("Email cannot be blank")

    if "password" not in flask.request.form or flask.request.form[
            "password"] == "":
        flashes.append("Password cannot be blank")
    elif len(flask.request.form["password"]) < 8:
        flashes.append("Password must be at least 8 characters long")

    if "phone" not in flask.request.form or flask.request.form["phone"] == "":
        flashes.append("Phone cannot be blank")

    if "college" not in flask.request.form or flask.request.form[
            "college"] == "---":
        flashes.append("Please select a college")

    if ("affiliation" not in flask.request.form
            or flask.request.form["affiliation"] == "---"):
        flashes.append("Please select an affiliation")

    if flask.request.form["affiliation"] in alumni_affiliation_ids and (
            "alumni-number" not in flask.request.form or
            not re.match(r"^8-\d{8}$", flask.request.form["alumni-number"])):
        flashes.append(
            "Please enter a valid alumni number, starting with '8-' and with 8 digits (e.g. 8-12345678)"
        )

    if APP.config["REQUIRE_USER_PHOTO"] and (
            "photo" not in flask.request.files
            or flask.request.files["photo"].filename == ""):
        flashes.append("Please upload a photo")

    if "accept_terms" not in flask.request.form:
        flashes.append("You must accept the Terms and Conditions")

    if flashes:
        flask.flash(
            ("There were errors in your provided details. Please fix "
             "these and try again"),
            "error",
        )
        for msg in flashes:
            flask.flash(msg, "warning")

        return flask.render_template(
            "front/home.html",
            form=flask.request.form,
            colleges=models.College.query.all(),
            affiliations=affiliations,
            alumni_affiliation_ids=alumni_affiliation_ids_json,
        )

    if APP.config["REQUIRE_USER_PHOTO"]:
        photo = photos.save_photo(flask.request.files["photo"])

        DB.session.add(photo)
        DB.session.commit()
    else:
        photo = None

    user = models.User(
        flask.request.form["email"],
        flask.request.form["password"],
        flask.request.form["forenames"],
        flask.request.form["surname"],
        flask.request.form["phone"],
        models.College.get_by_id(flask.request.form["college"]),
        models.Affiliation.get_by_id(flask.request.form["affiliation"]),
        photo,
    )

    if flask.request.form["affiliation"] in alumni_affiliation_ids:
        user.alumni_number = flask.request.form["alumni-number"]

    DB.session.add(user)
    DB.session.commit()

    APP.log_manager.log_event("Registered", user=user)

    APP.email_manager.send_template(
        flask.request.form["email"],
        "Confirm your Email Address",
        "email_confirm.email",
        name=user.forenames,
        confirmurl=flask.url_for(
            "front.confirm_email",
            user_id=user.object_id,
            secret_key=user.secret_key,
            _external=True,
        ),
        destroyurl=flask.url_for(
            "front.destroy_account",
            user_id=user.object_id,
            secret_key=user.secret_key,
            _external=True,
        ),
    )

    flask.flash("Your user account has been registered", "success")
    flask.flash(
        ("Before you can log in, you must confirm your email address. "
         "Please check your email for further instructions. If the message "
         "does not arrive, please check your spam/junk mail folder."),
        "info",
    )

    affiliation_logic.maybe_verify_affiliation(user)

    return flask.redirect(flask.url_for("front.home"))
Exemple #6
0
def register():
    """Process a registration.

    After registration, the user must click a link in an email sent to the
    address they registered with to confirm that it is valid.
    """
    if flask.request.method != 'POST':
        return flask.redirect(flask.url_for('router'))

    flashes = []

    if models.User.get_by_email(flask.request.form['email']) is not None:
        flask.flash(
            (
                'That email address already has an associated account. '
                'Use the links below to verify your email or reset your '
                'password.'
            ),
            'error'
        )
        return flask.redirect(flask.url_for('front.home'))

    if (
            'password' not in flask.request.form or
            'confirm' not in flask.request.form or
            flask.request.form['password'] != flask.request.form['confirm']
    ):
        flashes.append('Passwords do not match')

    if (
            'forenames' not in flask.request.form or
            flask.request.form['forenames'] == ''
    ):
        flashes.append('Forenames cannot be blank')

    if (
            'surname' not in flask.request.form or
            flask.request.form['surname'] == ''
    ):
        flashes.append('Surname cannot be blank')

    if (
            'email' not in flask.request.form or
            flask.request.form['email'] == ''
    ):
        flashes.append('Email cannot be blank')

    if (
            'password' not in flask.request.form or
            flask.request.form['password'] == ''
    ):
        flashes.append('Password cannot be blank')
    elif len(flask.request.form['password']) < 8:
        flashes.append('Password must be at least 8 characters long')

    if (
            'phone' not in flask.request.form or
            flask.request.form['phone'] == ''
    ):
        flashes.append('Phone cannot be blank')

    if (
            'college' not in flask.request.form or
            flask.request.form['college'] == '---'
    ):
        flashes.append('Please select a college')

    if (
            'affiliation' not in flask.request.form or
            flask.request.form['affiliation'] == '---'
    ):
        flashes.append('Please select an affiliation')

    if APP.config['REQUIRE_USER_PHOTO'] and (
            'photo' not in flask.request.files or
            flask.request.files['photo'].filename == ''
    ):
        flashes.append('Please upload a photo')

    if 'accept_terms' not in flask.request.form:
        flashes.append('You must accept the Terms and Conditions')

    if flashes:
        flask.flash(
            (
                'There were errors in your provided details. Please fix '
                'these and try again'
            ),
            'error'
        )
        for msg in flashes:
            flask.flash(msg, 'warning')

        return flask.render_template(
            'front/home.html',
            form=flask.request.form,
            colleges=models.College.query.all(),
            affiliations=models.Affiliation.query.all()
        )

    if APP.config['REQUIRE_USER_PHOTO']:
        photo = photos.save_photo(flask.request.files['photo'])

        DB.session.add(photo)
        DB.session.commit()
    else:
        photo = None

    user = models.User(
        flask.request.form['email'],
        flask.request.form['password'],
        flask.request.form['forenames'],
        flask.request.form['surname'],
        flask.request.form['phone'],
        models.College.get_by_id(flask.request.form['college']),
        models.Affiliation.get_by_id(flask.request.form['affiliation']),
        photo
    )

    DB.session.add(user)
    DB.session.commit()

    APP.log_manager.log_event(
        'Registered',
        user=user
    )

    APP.email_manager.send_template(
        flask.request.form['email'],
        'Confirm your Email Address',
        'email_confirm.email',
        name=user.forenames,
        confirmurl=flask.url_for(
            'front.confirm_email',
            user_id=user.object_id,
            secret_key=user.secret_key,
            _external=True
        ),
        destroyurl=flask.url_for(
            'front.destroy_account',
            user_id=user.object_id,
            secret_key=user.secret_key,
            _external=True
        )
    )

    flask.flash('Your user account has been registered', 'success')
    flask.flash(
        (
            'Before you can log in, you must confirm your email address. '
            'Please check your email for further instructions. If the message '
            'does not arrive, please check your spam/junk mail folder.'
        ),
        'info'
    )

    affiliation_logic.maybe_verify_affiliation(user)

    return flask.redirect(flask.url_for('front.home'))