diff --git a/budget/forms.py b/budget/forms.py index 0373da81..1e265ee0 100644 --- a/budget/forms.py +++ b/budget/forms.py @@ -21,6 +21,7 @@ class ProjectForm(Form): class AuthenticationForm(Form): + id = TextField("Project identifier", validators=[Required()]) password = TextField("Password", validators=[Required()]) submit = SubmitField("Get in") diff --git a/budget/templates/authenticate.html b/budget/templates/authenticate.html index c745e9f9..8b57c671 100644 --- a/budget/templates/authenticate.html +++ b/budget/templates/authenticate.html @@ -1,6 +1,6 @@ {% extends "layout.html" %} {% block content %} -

Login to "{{ project.name }}"

+

Authentication

{% for errors in form.errors.values() %}

{{ ", ".join(errors) }}

@@ -8,7 +8,9 @@
{{ form.hidden_tag() }} - Password: + +

{{ form.id.label }}
{{ form.id }}

+

{{ form.password.label }}
{{ form.password }}

{{ form.submit }}

{% endblock %} diff --git a/budget/templates/home.html b/budget/templates/home.html new file mode 100644 index 00000000..5fcdf903 --- /dev/null +++ b/budget/templates/home.html @@ -0,0 +1,28 @@ +{% extends "layout.html" %} + +{% block content %} +

Welcome on the budget manager

+ +
+

Log to an existing project...

+ + {{ auth_form.hidden_tag() }} + +

{{ auth_form.id.label }}
{{ auth_form.id }}

+

{{ auth_form.password.label }}
{{ auth_form.password }}

+

{{ auth_form.submit }}

+
+ +
+

...or create a new project

+ + {{ project_form.hidden_tag() }} + +

{{ project_form.name.label }}
{{ project_form.name }}

+

{{ project_form.id.label }}
{{ project_form.id }}

+

{{ project_form.password.label }}
{{ project_form.password }}

+

{{ project_form.contact_email.label }}
{{ project_form.contact_email }}

+

{{ project_form.submit }}

+
+ +{% endblock %} diff --git a/budget/web.py b/budget/web.py index 46226df8..2147359f 100644 --- a/budget/web.py +++ b/budget/web.py @@ -8,34 +8,40 @@ from utils import get_billform_for, requires_auth # create the application, initialize stuff app = Flask(__name__) -@app.route("//authenticate", methods=["GET", "POST"]) -def authenticate(project_id, redirect_url=None): - redirect_url = redirect_url or url_for("list_bills", project_id=project_id) - project = Project.query.get(project_id) - if not project: - return redirect(url_for("create_project", project_id=project_id)) - - # if credentials are already in session, redirect - if project_id in session and project.password == session[project_id]: - return redirect(redirect_url) - - # else create the form and process it - form = AuthenticationForm() - if request.method == "POST": - if form.validate(): - if not form.password.data == project.password: - form.errors['password'] = ["The password is not the right one"] - else: - session[project_id] = form.password.data - session.update() - return redirect(redirect_url) - - return render_template("authenticate.html", form=form, project=project) - @app.route("/") def home(): - # FIXME create a real homepage - return "this is the homepage" + project_form = ProjectForm() + auth_form = AuthenticationForm() + return render_template("home.html", project_form=project_form, auth_form=auth_form) + +@app.route("/authenticate", methods=["GET", "POST"]) +def authenticate(redirect_url=None): + form = AuthenticationForm() + + if form.id.validate(): + + project_id = form.id.data + + redirect_url = redirect_url or url_for("list_bills", project_id=project_id) + project = Project.query.get(project_id) + if not project: + return redirect(url_for("create_project", project_id=project_id)) + + # if credentials are already in session, redirect + if project_id in session and project.password == session[project_id]: + return redirect(redirect_url) + + # else process the form + if request.method == "POST": + if form.validate(): + if not form.password.data == project.password: + form.errors['password'] = ["The password is not the right one"] + else: + session[project_id] = form.password.data + session.update() + return redirect(redirect_url) + + return render_template("authenticate.html", form=form) @app.route("/create", methods=["GET", "POST"]) def create_project(): @@ -59,6 +65,12 @@ def create_project(): return render_template("create_project.html", form=form) +@app.route("/quit") +def quit(): + # delete the session + session = None + return redirect( url_for("home") ) + @app.route("//invite") @requires_auth def invite(project):