def create_account(): # A cool way of passing the arguments to the flask template template_arguments = dict ( languages= Language.all(), native_languages = Language.native_languages(), default_learned= Language.default_learned() ) # GET if flask.request.method == "GET": return flask.render_template("create_account.html", **template_arguments) # POST form = flask.request.form password = form.get("password", None) email = form.get("email", None) name = form.get("name", None) code = form.get("code", None) language = Language.find(form.get("language", None)) native_language = Language.find(form.get("native_language", None)) if not (code == "Kairo" or code == "unibe" or code == "rug" or code =="42"): flash("Invitation code is not recognized. Please contact us.") elif password is None or email is None or name is None: flash("Please enter your name, email address, and password") else: try: zeeguu.db.session.add(User(email, name, password, language, native_language)) zeeguu.db.session.commit() user = User.authorize(email, password) flask.session["user"] = user.id return flask.redirect(flask.url_for("account.my_account")) except ValueError: flash("Username could not be created. Please contact us.") except sqlalchemy.exc.IntegrityError: flash(email + " is already in use. Please select a different email.") except: flash("Something went wrong. Please contact us.") finally: zeeguu.db.session.rollback() return flask.render_template("create_account.html", **template_arguments)
def get_session(email): """ If the email and password match, a new sessionId is created, and returned as a string. This sessionId has to be passed along all the other requests that are annotated with @with_user in this file """ password = request.form.get("password", None) if password is None: flask.abort(400) user = User.authorize(email, password) if user is None: flask.abort(401) session = Session.for_user(user) zeeguu.db.session.add(session) zeeguu.db.session.commit() return str(session.id)
def login(): form = flask.request.form if flask.request.method == "POST" and form.get("login", False): password = form.get("password", None) email = form.get("email", None) if password is None or email is None: flask.flash("Please enter your email address and password") else: user = User.authorize(email, password) if user is None: flask.flash("Invalid email and password combination") else: flask.session["user"] = user.id flask.session.permanent = True return flask.redirect( flask.request.args.get("next") or flask.url_for("account.my_account")) return flask.render_template("login.html")