From bec0ae3797b7b1113dfbc7b2f325e14d0b4f45e0 Mon Sep 17 00:00:00 2001 From: NikolajDanger Date: Tue, 24 May 2022 14:12:54 +0200 Subject: [PATCH] :tada: it runs --- aula/Login/routes.py | 53 +++++++++++ aula/__init__.py | 26 ++++++ aula/forms.py | 17 ++++ aula/models.py | 155 +++----------------------------- aula/static/main.css | 84 +++++++++++++++++ aula/templates/about.html | 30 +++++++ aula/templates/account.html | 4 + aula/templates/addcustomer.html | 59 ++++++++++++ aula/templates/home.html | 16 ++++ aula/templates/invest.html | 79 ++++++++++++++++ aula/templates/layout.html | 98 ++++++++++++++++++++ aula/templates/layout_acc.html | 100 +++++++++++++++++++++ aula/templates/login.html | 47 ++++++++++ aula/templates/test.html | 70 +++++++++++++++ aula/templates/transfer.html | 46 ++++++++++ run.py | 2 +- 16 files changed, 743 insertions(+), 143 deletions(-) create mode 100644 aula/Login/routes.py create mode 100644 aula/__init__.py create mode 100644 aula/forms.py create mode 100644 aula/static/main.css create mode 100644 aula/templates/about.html create mode 100644 aula/templates/account.html create mode 100644 aula/templates/addcustomer.html create mode 100644 aula/templates/home.html create mode 100644 aula/templates/invest.html create mode 100644 aula/templates/layout.html create mode 100644 aula/templates/layout_acc.html create mode 100644 aula/templates/login.html create mode 100644 aula/templates/test.html create mode 100644 aula/templates/transfer.html diff --git a/aula/Login/routes.py b/aula/Login/routes.py new file mode 100644 index 0000000..0abbdf3 --- /dev/null +++ b/aula/Login/routes.py @@ -0,0 +1,53 @@ +from flask import render_template, Blueprint, redirect, url_for, flash, request +from flask_login import current_user, login_user + +from aula import bcrypt +from aula.forms import UserLoginForm +from aula.models import select_users + +Login = Blueprint('Login', __name__) + +posts = [{}] + + +@Login.route("/") +@Login.route("/home") +def home(): + return render_template('home.html', posts=posts) + + +@Login.route("/about") +def about(): + return render_template('about.html', title='About') + + +@Login.route("/login", methods=['GET', 'POST']) +def login(): + if current_user.is_authenticated: + return redirect(url_for('Login.home')) + + form = UserLoginForm() + # Først bekræft, at inputtet fra formen er gyldigt... (f.eks. ikke tomt) + if form.validate_on_submit(): + user = select_users(form.id.data) + # Derefter tjek om hashet af adgangskoden passer med det fra databasen... + if user != None and bcrypt.check_password_hash(user[2], form.password.data): + login_user(user, remember=form.remember.data) + flash('Login successful.','success') + next_page = request.args.get('next') + return redirect(next_page) if next_page else redirect(url_for('Login.home')) + else: + flash('Login Unsuccessful. Please check identifier and password', 'danger') + return render_template('login.html', title='Login', form=form) + + +# @Login.route("/logout") +# def logout(): +# logout_user() +# return redirect(url_for('Login.home')) + + +# @Login.route("/account") +# @login_required +# def account(): +# return render_template('account.html', title='Account') diff --git a/aula/__init__.py b/aula/__init__.py new file mode 100644 index 0000000..3966394 --- /dev/null +++ b/aula/__init__.py @@ -0,0 +1,26 @@ +from flask import Flask +import psycopg2 +from flask_bcrypt import Bcrypt +from flask_login import LoginManager + +app = Flask(__name__) +app.config['SECRET_KEY'] = 'fc089b9218301ad987914c53481bff04' +# set your own database +db = "dbname='aula' user='postgres' host='127.0.0.1' password = 'UIS'" +conn = psycopg2.connect(db) + +bcrypt = Bcrypt(app) +login_manager = LoginManager(app) +login_manager.login_view = 'login' +login_manager.login_message_category = 'info' + +from aula.Login.routes import Login +# from bank.Customer.routes import Customer +# from bank.Employee.routes import Employee +# from bank.Pax.routes import Pax +app.register_blueprint(Login) +# app.register_blueprint(Customer) +# app.register_blueprint(Employee) +# app.register_blueprint(Pax) + +#from bank import routes diff --git a/aula/forms.py b/aula/forms.py new file mode 100644 index 0000000..d00f404 --- /dev/null +++ b/aula/forms.py @@ -0,0 +1,17 @@ +from flask_wtf import FlaskForm +from wtforms import StringField, PasswordField, SubmitField, BooleanField, IntegerField, SelectField +from wtforms.validators import DataRequired, Length + +class AddUserForm(FlaskForm): + username = StringField('Username', + validators=[DataRequired(), Length(min=2, max=20)]) + user_id = IntegerField('user_id', + validators=[DataRequired()]) + password = PasswordField('Password', validators=[DataRequired()]) + submit = SubmitField('Add') + +class UserLoginForm(FlaskForm): + user_id = IntegerField('user_id', validators=[DataRequired()]) + password = PasswordField('Password', validators=[DataRequired()]) + remember = BooleanField('Remember Me') + submit = SubmitField('Login') diff --git a/aula/models.py b/aula/models.py index 382ebb7..f272aa7 100644 --- a/aula/models.py +++ b/aula/models.py @@ -1,6 +1,5 @@ # write all your SQL queries in this file. -from datetime import datetime -from bank import conn, login_manager +from aula import conn, login_manager from flask_login import UserMixin from psycopg2 import sql @@ -8,26 +7,17 @@ from psycopg2 import sql def load_user(user_id): cur = conn.cursor() - schema = 'customers' - id = 'cpr_number' - if str(user_id).startswith('60'): - schema = 'employees' - id = 'id' + schema = 'users' + _id = 'user_id' user_sql = sql.SQL(""" SELECT * FROM {} WHERE {} = %s - """).format(sql.Identifier(schema), sql.Identifier(id)) + """).format(sql.Identifier(schema), sql.Identifier(_id)) cur.execute(user_sql, (int(user_id),)) if cur.rowcount > 0: - # return-if svarer til nedenstående: - # if schema == 'employees': - # return Employees(cur.fetchone()) - # else: - # return Customers(cur.fetchone()) - - return Employees(cur.fetchone()) if schema == 'employees' else Customers(cur.fetchone()) + User(cur.fetchone()) else: return None @@ -122,143 +112,24 @@ class Transfers(tuple): self.amount = user_data[1] self.transfer_date = user_data[2] -def insert_Customers(name, CPR_number, password): +def insert_users(user_id, first_name, last_name, password, email, adresse, role): cur = conn.cursor() sql = """ - INSERT INTO Customers(name, CPR_number, password) - VALUES (%s, %s, %s) + INSERT INTO Customers(user_id, first_name, last_name, password, email, adresse, role) + VALUES (%s, %s, %s, %s, %s, %s, %s) """ - cur.execute(sql, (name, CPR_number, password)) + cur.execute(sql, (user_id, first_name, last_name, password, email, adresse, role)) # Husk commit() for INSERT og UPDATE, men ikke til SELECT! conn.commit() cur.close() -def insert_Employees(id, name, password): +def select_users(user_id): cur = conn.cursor() sql = """ - INSERT INTO Employees(id, name, password) - VALUES (%s, %s, %s) + SELECT * FROM users + WHERE user_id = %s """ - cur.execute(sql, (id, name, password)) - # Husk commit() for INSERT og UPDATE, men ikke til SELECT! - conn.commit() - cur.close() - -def select_Customers(CPR_number): - cur = conn.cursor() - sql = """ - SELECT * FROM Customers - WHERE CPR_number = %s - """ - cur.execute(sql, (CPR_number,)) + cur.execute(sql, (user_id,)) user = Customers(cur.fetchone()) if cur.rowcount > 0 else None; cur.close() return user - -def select_Employees(id): - cur = conn.cursor() - sql = """ - SELECT * FROM Employees - WHERE id = %s - """ - cur.execute(sql, (id,)) - user = Employees(cur.fetchone()) if cur.rowcount > 0 else None; - cur.close() - return user - -def select_all_Employees(): - cur = conn.cursor() - sql = """ - SELECT * FROM Employees - """ - cur.execute(sql) - tuple_resultset = cur.fetchall() - cur.close() - return tuple_resultset - -def update_CheckingAccount(amount, CPR_number): - cur = conn.cursor() - sql = """ - UPDATE CheckingAccount - SET amount = %s - WHERE CPR_number = %s - """ - cur.execute(sql, (amount, CPR_number)) - # Husk commit() for INSERT og UPDATE, men ikke til SELECT! - conn.commit() - cur.close() - -def transfer_account(date, amount, from_account, to_account): - cur = conn.cursor() - sql = """ - INSERT INTO Transfers(transfer_date, amount, from_account, to_account) - VALUES (%s, %s, %s, %s) - """ - cur.execute(sql, (date, amount, from_account, to_account)) - # Husk commit() for INSERT og UPDATE, men ikke til SELECT! - conn.commit() - cur.close() - -def select_emp_cus_accounts(emp_cpr_number): - cur = conn.cursor() - sql = """ - SELECT - e.name employee - , c.name customer - , cpr_number - , account_number - FROM manages m - NATURAL JOIN accounts - NATURAL JOIN customers c - JOIN employees e ON m.emp_cpr_number = e.id - WHERE emp_cpr_number = %s - ; - """ - cur.execute(sql, (emp_cpr_number,)) - tuple_resultset = cur.fetchall() - cur.close() - return tuple_resultset - -def select_investments(emp_cpr_number): - cur = conn.cursor() - sql = """ - SELECT i.account_number, a.cpr_number, a.created_date - FROM investmentaccounts i - JOIN accounts a ON i.account_number = a.account_number - JOIN manages m ON m.account_number = a.account_number - JOIN employees e ON e.id = m.emp_cpr_number - WHERE e.id = %s - """ - cur.execute(sql, (emp_cpr_number,)) - tuple_resultset = cur.fetchall() - cur.close() - return tuple_resultset - -def select_investments_with_certificates(emp_cpr_number): - cur = conn.cursor() - sql = """ - SELECT i.account_number, a.cpr_number, a.created_date - , cd.cd_number, start_date, maturity_date, rate, amount - FROM investmentaccounts i - JOIN accounts a ON i.account_number = a.account_number - JOIN certificates_of_deposit cd ON i.account_number = cd.account_number - JOIN manages m ON m.account_number = a.account_number - JOIN employees e ON e.id = m.emp_cpr_number - WHERE e.id = %s - """ - cur.execute(sql, (emp_cpr_number,)) - tuple_resultset = cur.fetchall() - cur.close() - return tuple_resultset - -def select_investments_certificates_sum(emp_cpr_number): - cur = conn.cursor() - sql = """ - SELECT account_number, cpr_number, created_date, sum - FROM vw_cd_sum - WHERE emp_cpr_number = %s - """ - cur.execute(sql, (emp_cpr_number,)) - tuple_resultset = cur.fetchall() - cur.close() - return tuple_resultset diff --git a/aula/static/main.css b/aula/static/main.css new file mode 100644 index 0000000..44e1df8 --- /dev/null +++ b/aula/static/main.css @@ -0,0 +1,84 @@ +body { + background: #fafafa; + color: #333333; + margin-top: 5rem; +} + +h1, h2, h3, h4, h5, h6 { + color: #444444; +} + +.bg-steel { + background-color: #5f788a; +} + +.site-header .navbar-nav .nav-link { + color: #cbd5db; +} + +.site-header li > .nav-item.nav-link { + color: #495057; +} + +.site-header .navbar-nav .nav-link:hover { + color: #ffffff; +} + +.site-header .navbar-nav .nav-link.active { + font-weight: 500; +} + +.content-section { + background: #ffffff; + padding: 10px 20px; + border: 1px solid #dddddd; + border-radius: 3px; + margin-bottom: 20px; +} + +.article-title { + color: #444444; +} + +a.article-title:hover { + color: #428bca; + text-decoration: none; +} + +.article-content { + white-space: pre-line; +} + +.article-img { + height: 65px; + width: 65px; + margin-right: 16px; +} + +.article-metadata { + padding-bottom: 1px; + margin-bottom: 4px; + border-bottom: 1px solid #e3e3e3 +} + +.article-metadata a:hover { + color: #333; + text-decoration: none; +} + +.article-svg { + width: 25px; + height: 25px; + vertical-align: middle; +} + +.account-img { + height: 125px; + width: 125px; + margin-right: 20px; + margin-bottom: 16px; +} + +.account-heading { + font-size: 2.5rem; +} diff --git a/aula/templates/about.html b/aula/templates/about.html new file mode 100644 index 0000000..bc550eb --- /dev/null +++ b/aula/templates/about.html @@ -0,0 +1,30 @@ +{% extends "layout.html" %} +{% block content %} +

About Page

+

Dette er UIS-prototypen bragt til dig af UIS-holdet 2021/2022:

+ + +

I kan registrere nye kundekonti, men medarbejderkonti skal indtastes ved at foretage jeres ændringer i DML-scriptet schema_ins.sql. For at logge ind har vi oprettet nogle testkonti. Kunde-login kan tilgås ved hjælp af et kunde-cpr i intervallerne 5001 til 5007 med adgangskoden "UIS" med store bogstaver. Medarbejder-login kan tilgås ved hjælp af medarbejder-id 6001..6007 og samme adgangskode.

+

For at tilføje flere eksempeldata skal du foretage dine ændringer i DML-scriptet schema_ins.sql.

+

Prototypen er vertikal. Vi har implementeret nogle funktioner, men projektet er ufuldstændigt. Prototypen giver et udgangspunkt for jeres arbejde.

+ +

EN:

+ +

You can register new customer accounts, but employee accounts must be entered by making your changes to the DML-script schema_ins.sql. To log in we have created some test accounts. The customer login can be accesed using a customer cpr in ranges 5001 to 5007 with the password 'UIS' in uppercase. The employee login can be accessed using employee id 6001..6007 and the same password.

+

To add more sample data make your changes to the DML-script schema_ins.sql.

+

The prototype is vertical. We have implemented some functions and not completed. The prototype provides a starting point for your work.

+

AL/PR 20220504

+{% endblock content %} + diff --git a/aula/templates/account.html b/aula/templates/account.html new file mode 100644 index 0000000..f441fcc --- /dev/null +++ b/aula/templates/account.html @@ -0,0 +1,4 @@ +{% extends "layout.html" %} +{% block content %} +

{{ current_user.name }}

+{% endblock content %} diff --git a/aula/templates/addcustomer.html b/aula/templates/addcustomer.html new file mode 100644 index 0000000..4ff368a --- /dev/null +++ b/aula/templates/addcustomer.html @@ -0,0 +1,59 @@ +{% extends "layout.html" %} +{% block content %} +
+
+ {{ form.hidden_tag() }} +
+ Add a new customer +
+ {{ form.username.label(class="form-control-label") }} + + {% if form.username.errors %} + {{ form.username(class="form-control form-control-lg is-invalid") }} +
+ {% for error in form.username.errors %} + {{ error }} + {% endfor %} +
+ {% else %} + {{ form.username(class="form-control form-control-lg") }} + {% endif %} +
+
+ {{ form.CPR_number.label(class="form-control-label") }} + {% if form.CPR_number.errors %} + {{ form.CPR_number(class="form-control form-control-lg is-invalid") }} +
+ {% for error in form.CPR_number.errors %} + {{ error }} + {% endfor %} +
+ {% else %} + {{ form.CPR_number(class="form-control form-control-lg") }} + {% endif %} +
+
+ {{ form.password.label(class="form-control-label") }} + {% if form.password.errors %} + {{ form.password(class="form-control form-control-lg is-invalid") }} +
+ {% for error in form.password.errors %} + {{ error }} + {% endfor %} +
+ {% else %} + {{ form.password(class="form-control form-control-lg") }} + {% endif %} +
+
+
+ {{ form.submit(class="btn btn-outline-info") }} +
+
+
+
+ + Already Have An Account? Sign In + +
+{% endblock content %} diff --git a/aula/templates/home.html b/aula/templates/home.html new file mode 100644 index 0000000..9b418e3 --- /dev/null +++ b/aula/templates/home.html @@ -0,0 +1,16 @@ +{% extends "layout.html" %} +{% block content %} +

{{ current_user.name }}

+ {% for post in posts %} +
+
+ +

{{ post.title }}

+

{{ post.content }}

+
+
+ {% endfor %} +{% endblock content %} diff --git a/aula/templates/invest.html b/aula/templates/invest.html new file mode 100644 index 0000000..c36cef7 --- /dev/null +++ b/aula/templates/invest.html @@ -0,0 +1,79 @@ +{% extends "layout_acc.html" %} +{% block content %} +

{{ current_user.name }}

+ + +
+

Investments:

+ + + + + + + + + + {% for n in inv %} + + + + + + {% endfor %} + +
account_numbercpr_numbercreation date
{{n[0]}}{{n[1]}}{{n[2]}}
+
+
+

Investment accounts:

+ + + + + + + + + + + {% for n in inv_sums %} + + + + + + + {% endfor %} + +
account_numbercpr_numberaccount createddeposit total (CD)
{{n[0]}}{{n[1]}}{{n[2]}}{{n[3]}}
+
+
+

Investment certificats of deposit:

+ + + + + + + + + + + + + {% for n in inv_cd_list %} + + + + + + + + + {% endfor %} + +
acccd_numberstart_datematurity_daterateamount
{{n[0]}}{{n[3]}}{{n[4]}}{{n[5]}}{{n[6]}}{{n[7]}}
+
+ + +{% endblock content %} diff --git a/aula/templates/layout.html b/aula/templates/layout.html new file mode 100644 index 0000000..6f391b7 --- /dev/null +++ b/aula/templates/layout.html @@ -0,0 +1,98 @@ + + + + + + + + + + + + + {% if title %} + UIS Prototype - {{ title }} + {% else %} + UIS Prototype + {% endif %} + + + +
+
+
+ {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} + {% for category, message in messages %} +
+ {{ message }} +
+ {% endfor %} + {% endif %} + {% endwith %} + {% block content %}{% endblock %} +
+
+
+

Account

+
    + {% if current_user.is_authenticated %} +
  • Transfer
  • +
  • Checking Accounts
  • +
  • Investment Accounts
  • +
  • View investment accounts
  • +
  • etc
  • + {% else %} +
  • etc
  • + {% endif %} +
+

+
+
+
+
+ + + + + + + + + diff --git a/aula/templates/layout_acc.html b/aula/templates/layout_acc.html new file mode 100644 index 0000000..0be041e --- /dev/null +++ b/aula/templates/layout_acc.html @@ -0,0 +1,100 @@ + + + + + + + + + + + + + {% if title %} + UIS Prototype - {{ title }} + {% else %} + UIS Prototype + {% endif %} + + + +
+
+
+ {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} + {% for category, message in messages %} +
+ {{ message }} +
+ {% endfor %} + {% endif %} + {% endwith %} + {% block content %}{% endblock %} +
+
+
+

Account

+ +

+
+
+
+
+ + + + + + + + + diff --git a/aula/templates/login.html b/aula/templates/login.html new file mode 100644 index 0000000..876435b --- /dev/null +++ b/aula/templates/login.html @@ -0,0 +1,47 @@ +{% extends "layout.html" %} +{% block content %} +
+
+ {{ form.hidden_tag() }} +
+ Log In +
+ {{ form.user_id.label(class="form-control-label") }} + {% if form.user_id.errors %} + {{ form.user_id(class="form-control form-control-lg is-invalid") }} +
+ {% for error in form.user_id.errors %} + {{ error }} + {% endfor %} +
+ {% else %} + {{ form.user_id(class="form-control form-control-lg") }} + {% endif %} +
+
+ {{ form.password.label(class="form-control-label") }} + {% if form.password.errors %} + {{ form.password(class="form-control form-control-lg is-invalid") }} +
+ {% for error in form.password.errors %} + {{ error }} + {% endfor %} +
+ {% else %} + {{ form.password(class="form-control form-control-lg") }} + {% endif %} +
+
+ {{ form.remember(class="form-check-input") }} + {{ form.remember.label(class="form-check-label") }} +
+
+
+ {{ form.submit(class="btn btn-outline-info") }} +
+ + Forgot Password? + +
+
+{% endblock content %} diff --git a/aula/templates/test.html b/aula/templates/test.html new file mode 100644 index 0000000..d03d989 --- /dev/null +++ b/aula/templates/test.html @@ -0,0 +1,70 @@ +{% extends "layout.html" %} +{% block content %} +

Test Page

+

Dette er en testside. Hej med dig. fed tekst, kursiv tekst

+

fed tekst, fed og kursiv tekst

+ +

Liste af ansatte:

+ + +
+ {{ form.hidden_tag() }} +
+ Add a new employee + +
+ {{ form.id.label(class="form-control-label") }} + + {% if form.id.errors %} + {{ form.id(class="form-control form-control-lg is-invalid") }} +
+ {% for error in form.id.errors %} + {{ error }} + {% endfor %} +
+ {% else %} + {{ form.id(class="form-control form-control-lg") }} + {% endif %} +
+ +
+ {{ form.username.label(class="form-control-label") }} + + {% if form.username.errors %} + {{ form.username(class="form-control form-control-lg is-invalid") }} +
+ {% for error in form.username.errors %} + {{ error }} + {% endfor %} +
+ {% else %} + {{ form.username(class="form-control form-control-lg") }} + {% endif %} +
+ +
+ {{ form.password.label(class="form-control-label") }} + {% if form.password.errors %} + {{ form.password(class="form-control form-control-lg is-invalid") }} +
+ {% for error in form.password.errors %} + {{ error }} + {% endfor %} +
+ {% else %} + {{ form.password(class="form-control form-control-lg") }} + {% endif %} +
+
+ +
+ {{ form.submit(class="btn btn-outline-info") }} +
+
+ +{% endblock content %} + diff --git a/aula/templates/transfer.html b/aula/templates/transfer.html new file mode 100644 index 0000000..229cbb6 --- /dev/null +++ b/aula/templates/transfer.html @@ -0,0 +1,46 @@ +{% extends "layout.html" %} +{% block content %} +
+
+ {{ form.hidden_tag() }} +
+ {{ form.sourceAccount.label(class="form-control-label") }} + {{ form.sourceAccount(class="form-control")}} +
+
+
+ {{ form.amount.label(class="form-control-label") }} + {% if form.amount.errors %} + {{ form.amount(class="form-control form-control-lg is-invalid") }} +
+ {% for error in form.password.errors %} + {{ error }} + {% endfor %} +
+ {% else %} + {{ form.amount(class="form-control form-control-lg") }} + {% endif %} +
+
+
+ {{ form.targetAccount.label(class="form-control-label") }} + {{ form.targetAccount(class="form-control") }} +
+
+ {{ form.submit(class="btn btn-outline-info") }} +
+
+
+ +
+

Dropdown customer account tuples:

+ +
+

Same list with a filter: {{ drop_cus_acc|join(', ') }}

+
+ +{% endblock content %} diff --git a/run.py b/run.py index cbaecdf..244c0d0 100644 --- a/run.py +++ b/run.py @@ -1,3 +1,3 @@ -from bank import app +from aula import app if __name__ == '__main__': app.run(debug=True)