Exemple #1
0
    def run():
        """Prefill the database."""
        with app.APP.app_context():
            photo = None
            users=[]
            college = models.College.query.first()
            member_aff = models.Affiliation.query.filter_by(object_id=2).first()
            commit_aff = models.Affiliation.query.filter_by(object_id=3).first()
            for member in ML.real_member_list:

                m = member[0]#member email
                c = member[1]#code
                aff=member_aff#affiliation
                if c<0:
                    aff = member_aff#normal member: 1
                elif c==1:
                    aff=commit_aff#comittee - free ticket: 2
                user = models.User(
                    m,
                    'oussmember',
                    m,
                    'Not set',
                    '01234567890',
                    college,
                    aff,
                    photo
                )


                user.note = 'Automatically created user from mailing list.\n'
                user.role = 'User'
                user.verified = True
                user.affiliation_verified = True

                users.append(user)

            for u in users:
                db.DB.session.add(u)

            db.DB.session.commit()

            count = users.__len__()
            c=0
            for u in users:
                if send_set_password(u):
                    c=c+1

            print("successfully sent password email to {} of {} OUSS members".format(c,count))
Exemple #2
0
    def run():
        """Prefill the database."""
        with app.APP.app_context():
            db.DB.session.add_all(static.COLLEGES)
            db.DB.session.add_all(static.AFFILIATIONS)

            if app.APP.config["REQUIRE_USER_PHOTO"]:
                filename = str(uuid.uuid4()) + ".png"

                image_location = os.path.abspath(
                    os.path.join(
                        os.path.dirname(__file__),
                        "..",
                        "static",
                        "images",
                        "eisitirio-logo.png",
                    )
                )

                full_url, thumb_url = photos.upload_photo(
                    filename, image_location, image_location
                )

                photo = models.Photo(filename, full_url, thumb_url)
            else:
                photo = None

            admin_user = models.User(
                "*****@*****.**",
                "password",
                "Admin",
                "Anderson",
                "01234567890",
                static.COLLEGES[-1],
                static.AFFILIATIONS[-1],
                photo,
            )

            admin_user.note = "Automatically created admin user.\n"
            admin_user.role = "Admin"
            admin_user.verified = True

            db.DB.session.add(admin_user)

            db.DB.session.commit()
Exemple #3
0
    def run():
        """Prefill the database."""
        with app.APP.app_context():
            db.DB.session.add_all(static.COLLEGES)
            db.DB.session.add_all(static.AFFILIATIONS)

            if app.APP.config['REQUIRE_USER_PHOTO']:
                filename = str(uuid.uuid4()) + '.png'

                image_location = os.path.abspath(os.path.join(
                    os.path.dirname(__file__),
                    '..',
                    'static',
                    'images',
                    'eisitirio-logo.png'
                ))

                full_url, thumb_url = photos.upload_photo(filename,
                                                          image_location,
                                                          image_location)

                photo = models.Photo(filename, full_url, thumb_url)
            else:
                photo = None

            admin_user = models.User(
                '*****@*****.**',
                'password',
                'Admin',
                'Anderson',
                '01234567890',
                static.COLLEGES[-1],
                static.AFFILIATIONS[-1],
                photo
            )

            admin_user.note = 'Automatically created admin user.\n'
            admin_user.role = 'Admin'
            admin_user.verified = True

            db.DB.session.add(admin_user)

            db.DB.session.commit()
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"))