Add bills func

This commit is contained in:
Emily 2019-04-26 06:50:49 +01:00
parent 72d196ee7f
commit 850cbfe46e
3 changed files with 64 additions and 4 deletions

View File

@ -1,8 +1,15 @@
from wtforms import Form, BooleanField, StringField, PasswordField, validators from wtforms import Form, BooleanField, IntegerField, DecimalField, StringField, DateField, PasswordField, validators
from flask_login import UserMixin from flask_login import UserMixin
from objects import glob from objects import glob
class BillForm(Form):
payment_to = StringField("Payment to", [validators.DataRequired()])
description = StringField("Description")
sum = DecimalField("Sum")
kid = IntegerField("KID")
date_due = DateField("Date Due (YYYY-MM-DD )")
class LoginForm(Form): class LoginForm(Form):
email = StringField("Email", [ email = StringField("Email", [
validators.DataRequired(), validators.DataRequired(),

View File

@ -2,7 +2,7 @@ from flask import render_template, url_for, request, redirect, flash, abort
from wtforms import Form, BooleanField, StringField, PasswordField, validators from wtforms import Form, BooleanField, StringField, PasswordField, validators
import flask_login import flask_login
from forms.login import LoginForm, RegisterForm, User, register_account from forms.login import LoginForm, RegisterForm, BillForm, User, register_account
from objects import glob # Global sharing of python objects in a manageable way from objects import glob # Global sharing of python objects in a manageable way
@ -19,12 +19,25 @@ logged_in_users = []
def dashboard(): def dashboard():
return render_template("pages/dashboard.html") return render_template("pages/dashboard.html")
@glob.app.route("/bills") @glob.app.route("/bills", methods = ["GET", "POST"])
@flask_login.login_required @flask_login.login_required
def bills(): def bills():
form = BillForm(request.form)
conn = glob.make_sql_connection() conn = glob.make_sql_connection()
cur = conn.cursor() cur = conn.cursor()
if request.method == "POST" and form.validate():
cur.execute("""
INSERT
INTO Regninger
VALUES (NULL, %s, %s, %s, %s, %s, 0, 1, %s)
""", (form.payment_to.data, form.description.data, form.kid.data, form.sum.data, form.date_due.data, flask_login.current_user.id))
conn.commit()
return redirect(url_for("bills"))
cur.execute(""" cur.execute("""
SELECT Betaletil, Regningfor, Regningsum, KID, Betalingsfrist, Betalt SELECT Betaletil, Regningfor, Regningsum, KID, Betalingsfrist, Betalt
FROM Regninger FROM Regninger
@ -37,7 +50,7 @@ def bills():
cur.close() cur.close()
conn.close() conn.close()
return render_template("pages/bills.html", data=data) return render_template("pages/bills.html", data=data, form=form)
@glob.app.route("/warranties") @glob.app.route("/warranties")
@flask_login.login_required @flask_login.login_required

View File

@ -6,6 +6,46 @@
<div class="container module"> <div class="container module">
<button type="button" class="btn btn-primary" style="margin:10px;" data-toggle="modal" data-target="#myModal">Add</button>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Add bill</h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<div class="modal-body">
{% macro render_field(field) %}
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
{% endmacro %}
<form method=post>
<dl>
{{ render_field(form.payment_to) }}
{{ render_field(form.description) }}
{{ render_field(form.sum) }}
{{ render_field(form.kid) }}
{{ render_field(form.date_due) }} YYYY-MM-DD
</dl>
<input type=submit value="Add">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<table class="table"> <table class="table">
<thead class="thead-light"> <thead class="thead-light">
<tr> <tr>