Exemple #1
0
def create_or_login(resp):
    """This is called when login with OpenID succeeded and it's not
    necessary to figure out if this is the users's first login or not.
    This function has to redirect otherwise the user will be presented
    with a terrible URL which we certainly don't want.
    """
    session["openid"] = resp.identity_url
    user = User.get_with_first("openid", resp.identity_url)
    # not a new user
    if user is not None:
        flash("Logged in successfully.", "success")
        login_user(user)
    else:
        # Create new user
        user = User(
            openid=resp.identity_url,
            datasets=[],
            is_active=True,
            is_authenticated=True,
            is_anonymous=False,
            confirmed_on=datetime.datetime.now(),
        )
        user.save()
        login_user(user)
        flash(
            "Success!, Since this is your first login, a local profile was "
            "created for you which is now you active user"
            "for you",
            "success",
        )
    return redirect(oid.get_next_url())
Exemple #2
0
def approve_auth(token):
    data = confirm_token(token)
    if data is False:
        flash("Confirmation failed, either it is invalid or expired.",
              "danger")
        return redirect(url_for("projects.projects"))

    if "email" not in data:
        flash("Confirmation failed, required email is not present", "danger")
        return redirect(url_for("projects.projects"))

    user = User.get_with_first("email", data["email"])
    if user is not None:
        flash("That email has already been registered")
        return redirect(url_for("projects.projects"))
    else:
        # Setup
        user = User(
            email=data["email"],
            password=hashpw(os.urandom(24), gensalt()),
            projects=[],
            is_active=False,
            is_authenticated=False,
            is_anonymous=False,
            confirmed_on=datetime.datetime.now(),
        )
        user.save()

        token = generate_confirmation_token(data=data["email"])
        reset_url = url_for("projects.reset_password",
                            token=token,
                            _external=True)
        html = render_template(
            "projects/email/reset_password.html",
            email=data["email"],
            reset_password_url=reset_url,
        )
        msg = Message(
            subject="{} Projects Account approval".format(
                config.get("PROJECTS", "title")),
            html=html,
            recipients=[data["email"]],
            sender=app.config["MAIL_USERNAME"],
        )
        mail.send(msg)
        flash(
            "The account {} has been approved and created".format(
                data["email"]),
            "success",
        )
    return redirect(url_for("projects.projects"))
Exemple #3
0
    def setUp(self):
        app.config["TESTING"] = True
        app.config["DEBUG"] = True
        folders = {}
        folders["DATA_FOLDER"] = app.config["DATA_FOLDER"] = os.path.join(
            os.getcwd(), "tests/data"
        )

        folders["UPLOAD_FOLDER"] = app.config["UPLOAD_FOLDER"] = os.path.join(
            os.getcwd(), "tests/images"
        )
        app.config["WTF_CSRF_ENABLED"] = True
        # Create required folders for the application if they don't exist
        for _, folder in folders.items():
            try:
                os.makedirs(folder)
                print("Created: " + folder)
            except FileExistsError:
                pass

        # Override default DB setting ->use a testing db instead of the default
        app.config["DB"] = os.path.join(app.config["DATA_FOLDER"], "fair_test")
        self.username = "******"
        self.password = "******"
        user = User.get_with_first("email", self.username)
        hashed_pw = hashpw(bytes(self.password, "utf-8"), gensalt())

        if user is None:
            user = User(
                email=self.username,
                password=hashed_pw,
                projects=[],
                is_active=True,
                is_authenticated=True,
                is_anonymous=False,
                confirmed_on=datetime.datetime.now(),
            )
            user.save()
        self.user = user

        self.client = app.test_client()
        # Setup valid token
        self.csrf_token = None
        with self.client as client:
            resp = client.get("/index")
            assert resp.status_code == 200
            self.csrf_token = g.csrf_token
Exemple #4
0
    dest="ip",
    type=str,
    default="127.0.0.1",
    help="The interface the webserver should listen on",
)
parser.add_argument(
    "--port",
    dest="port",
    type=int,
    default=8080,
    help="The port the webserver should listen on",
)
args = parser.parse_args()

if __name__ == "__main__":
    # Implement test user
    if args.debug:
        user = User.get_with_first("email", "*****@*****.**")
        if user is None:
            user = User(
                email="*****@*****.**",
                password=hashpw(bytes("test", "utf-8"), gensalt()),
                projects=[],
                is_active=True,
                is_authenticated=True,
                is_anonymous=False,
                confirmed_on=datetime.datetime.now(),
            )
            user.save()
    app.run(host=args.ip, port=args.port, debug=args.debug)
Exemple #5
0
def contact(request):
    message = ''
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        signup_form = SignupForm(request.POST, prefix="signup")
        signdown_form = SigndownForm(request.POST, prefix="signdown")
        if signup_form.is_valid():
            # Get data from form
            first_name = signup_form.cleaned_data['first_name']
            last_name = signup_form.cleaned_data['last_name']
            comp_id = signup_form.cleaned_data['comp_id']
            major = signup_form.cleaned_data['major']
            degree_program = signup_form.cleaned_data['degree_program']
            graduation_year = signup_form.cleaned_data['graduation_year']

            email_domain = "@virginia.edu"
            email = comp_id + email_domain

            user_exists_with_comp_id = User.objects.filter(
                comp_id=comp_id).exists()
            if user_exists_with_comp_id:
                message = "There's already a person registered with that computing ID!"

            # else no one exists with that - that's all we need!
            else:
                person = User(first_name=first_name,
                              last_name=last_name,
                              comp_id=comp_id,
                              email=email,
                              major=major,
                              degree_program=degree_program,
                              graduation_year=graduation_year,
                              need_added_to_email=True)
                person.save()
                message = """
                    You will be added to our mailing list as soon as possible.
                    If you suspect you have not been added after some time, try emailing us.
                    Thank you for your interest!
                    """

        # Signdown form currently unavailable - it's hard.
        #elif signdown_form.is_valid():
        #comp_id = signdown_form.cleaned_data['comp_id']
        #try:
        #person_to_delete = User.objects.get(comp_id=comp_id)
        # Need to send a confirmation email - but... tricky without users
        #person_to_delete.need_removed_from_email = True
        #message = "User with computing id " + comp_id + " will be removed shortly!"

        #except ObjectDoesNotExist:
        #message = """
        #User not in our database.
        #If you are sure that you are on our mailing list,
        #try emailing us and we will remove you.
        #We apologize for the inconvenience.
        #"""

    signup_form = SignupForm(prefix="signup")
    #signdown_form = SigndownForm(prefix="signdown")

    return render(
        request,
        'contact.html',
        {
            'signup_form': signup_form,
            #'signdown_form': signdown_form,
            'message': message,
        })