From e794fc6032537bc19039872ecfad1452830b4db4 Mon Sep 17 00:00:00 2001 From: fram3d Date: Tue, 3 Oct 2023 21:02:33 +0200 Subject: [PATCH] add sql support --- README.md | 8 +++++++- build-deb/freeriders/DEBIAN/control | 2 +- freeriders/__init__.py | 11 +++++++++++ freeriders/models.py | 6 ++++++ freeriders/routes.py | 16 ++++++++++++++-- init_db.py | 10 ++++++++++ requirments.txt | 1 + 7 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 freeriders/models.py create mode 100755 init_db.py diff --git a/README.md b/README.md index d61de00..3ed6db5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ # freeriders -Web app for tracking current ticket numbers \ No newline at end of file +Web app for tracking current ticket numbers + +# Architecture + +- keep all ticket numbers and timestamps in SQL table "tickets" +- display last ticket number +- don't allow tickets that are out of probable ticket number range diff --git a/build-deb/freeriders/DEBIAN/control b/build-deb/freeriders/DEBIAN/control index 4d1f7cf..687888f 100644 --- a/build-deb/freeriders/DEBIAN/control +++ b/build-deb/freeriders/DEBIAN/control @@ -4,7 +4,7 @@ Priority: optional Architecture: all Essential: no Installed-Size: 2000 -Depends: python3-flask, gunicorn +Depends: gunicorn, python3-flask-sqlalchemy Homepage: https://gitea.dmz.rs/Decentrala/freeriders Maintainer: Decentrala Description: Web app that tracks bus transport ticket numbers diff --git a/freeriders/__init__.py b/freeriders/__init__.py index 4e58cf5..f607f93 100644 --- a/freeriders/__init__.py +++ b/freeriders/__init__.py @@ -1,5 +1,16 @@ from flask import Flask +from flask_sqlalchemy import SQLAlchemy +import os # if you wanna have db credenitalas in os.environ app = Flask(__name__) +# SQLAlchemy setup +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///freeriders.db' + +#MySql setup +#app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://user:pass@localhost/dbname' + +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +db = SQLAlchemy(app) + from freeriders import routes diff --git a/freeriders/models.py b/freeriders/models.py new file mode 100644 index 0000000..983d0e2 --- /dev/null +++ b/freeriders/models.py @@ -0,0 +1,6 @@ +from freeriders import db + +class Tickets(db.Model): + id = db.Column(db.Integer, primary_key=True) + ticket = db.Column(db.Integer, nullable=False) + timestamp = db.Column(db.Integer, nullable=False) diff --git a/freeriders/routes.py b/freeriders/routes.py index 86484e1..92ce069 100644 --- a/freeriders/routes.py +++ b/freeriders/routes.py @@ -1,11 +1,23 @@ from flask import render_template, request, redirect -from freeriders import app +from freeriders import app, db +from freeriders.models import Ticket +import time @app.route('/', methods=['POST', 'GET']) def changepassword(): if request.method == 'GET': return render_template('index.html') elif request.method == 'POST': - return 'Post' + timenow = int(time.time()) + ticket_input = request.form['ticket'] + ticket = Ticket(ticket=ticket_input, timestamp=timenow) + + try: + db.session.add(ticket) + db.session.commit() + return 'Ticket je dodat' + except: + return 'Dodavanje ticketa nije uspelo' + else: return 'HTTP request method not recogniezed' diff --git a/init_db.py b/init_db.py new file mode 100755 index 0000000..10bebe8 --- /dev/null +++ b/init_db.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 +from freeriders import db + +print('[i] Trying to create databse...') +try: + db.create_all() + print('[+] Success you can proceed with deployment!') +except: + print('[-] Creating db failed :/') + diff --git a/requirments.txt b/requirments.txt index 7e10602..58b56f8 100644 --- a/requirments.txt +++ b/requirments.txt @@ -1 +1,2 @@ flask +SQLAlchemy