Ejemplo n.º 1
0
def register():
    if not current_user.is_active():
        to_page = request.args.get("next")
        if to_page == url_for("logout"):
            to_page = False
        form = RegistrationForm()
        if form.is_submitted():
            if form.validate():
                try:
                    db.session.add(User(form.email.data, form.username.data,
                        form.password.data, form.name.data, form.phone.data))
                    db.session.commit()
                    flash("Congratualtions, " + (form.name.data or
                        form.username.data) + "! You have registered\
                                successfully!", "success")
                    return redirect(to_page or url_for("index"))
                except:
                    if not app.debug:
                        flash("We're sorry, we were unable to register you.\
                                Feel free to try again.", "error")
                    else:
                        raise
            else:
                for key, msg in form.errors.items():
                    flash("Could not validate " + key + ": " + ", ".join(msg),
                            "error")
        return render_template("register.html", form=form)
    else:
        flash("You are already logged in!", "info")
        return redirect(request.args.get("next") or url_for("index"))
Ejemplo n.º 2
0
def login():
    if not current_user.is_active():
        to_page = request.args.get("next")
        if to_page == url_for("logout"):
            to_page = False
        form = LoginForm()
        if form.is_submitted():
            if form.validate():
                user = check_login(form.username.data, form.password.data)
                if user:
                    login_user(user, form.remember_me.data)
                    flash("Logged in successfully.", "success")
                    return redirect(to_page or url_for("index"))
                else:
                    flash("Invalid Username/Password", "error")
            else:
                for key, msg in form.errors.items():
                    flash("Could not validate " + key + ": " + ", ".join(msg),
                            "error")
        return render_template("login.html", form=form)
    else:
        flash("You are already logged in!", "info")
        return redirect(request.args.get("next") or url_for("index"))
Ejemplo n.º 3
0
def inject_user():
    """Tell the view whether someone is logged in or not"""
    return {'logged_in': current_user.is_active(),
            'is_admin': current_user.is_active() and current_user.is_admin()}