diff --git a/app/__init__.py b/app.py similarity index 88% rename from app/__init__.py rename to app.py index cd9f1aa..30a0c75 100644 --- a/app/__init__.py +++ b/app.py @@ -1,5 +1,6 @@ import os +from auth import bp from flask import Flask @@ -29,13 +30,10 @@ def create_app(test_config=None): def hello(): return 'Hello, Woooorld!' - from . import db - db.init_app(app) - - from . import auth - app.register_blueprint(auth.bp) + app.register_blueprint(bp) return app -app = create_app() \ No newline at end of file +app = create_app() +app.run(debug=True, host="0.0.0.0") \ No newline at end of file diff --git a/app/app.py b/app/app.py deleted file mode 100644 index 1256e0a..0000000 --- a/app/app.py +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/python3 - -from flask import Flask - -app = Flask(__name__) - -@app.route("/") -def hello_world(): - return "

Hello, World!

" - - -""" -from flask import Flask, render_template, Blueprint -from sqlalchemy import create_engine -from sqlalchemy.orm import sessionmaker -from sqlalchemy.sql.sqltypes import TIMESTAMP -from flask_sqlalchemy import SQLAlchemy - -from config import CONFIG - -import datetime - -from create_db import #baze? - - -# Flask app - -app.config['SQLALCHEMY_DATABASE_URI'] = CONFIG['DB_CONNECTION'] -app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True - -db=SQLAlchemy(app) - -# DB session init -engine = create_engine(CONFIG['DB_CONNECTION']) -Base.metadata.bind = engine -DBSession = sessionmaker(bind=engine) -session = DBSession() - -#Query DB for scan_song table -#query_scan_song = session.query(Scan_Song).all() -query = session.query(Scan_Song.id_scan, Scan_Song.id_song, Scan_Song.match_val, Song.naslov, Song.album, Song.izvajalec, Song.trajanje, Scan.timestamp).join(Song, Song.id == Scan_Song.id_song, isouter=True).join(Scan, Scan_Song.id_scan == Scan.id).limit(1000) - -site= Blueprint('site', __name__,template_folder='templates') -@app.route("/") -def index(): - try: - #predvajano_text = '' - skeni.append(scan_dict) - return render_template( - "index.html", - skeni=skeni, - date = datetime.date.today().strftime("%d.%m.%y") - ) - except Exception as e: - error_text = "

ni zadetkov:
" + str(e) + "

" - hed = '

Komada ni v bazi.

' - return hed + error_text - -if __name__ == "__main__": - app.run(debug=True, host = "0.0.0.0") - -""" \ No newline at end of file diff --git a/app/db.py b/app/db.py deleted file mode 100644 index d07f501..0000000 --- a/app/db.py +++ /dev/null @@ -1,57 +0,0 @@ -import sqlite3 ## tu rabim nekaj drugega - -import click -from flask import current_app, g -from flask.cli import with_appcontext - - -def get_db(): - if 'db' not in g: - g.db = sqlite3.connect( - current_app.config['DATABASE'], - detect_types=sqlite3.PARSE_DECLTYPES - ) - g.db.row_factory = sqlite3.Row - - return g.db - - -def close_db(e=None): - db = g.pop('db', None) - - if db is not None: - db.close() - - -""" -open_resource() opens a file relative to the flaskr package, which is useful since you won’t necessarily know where that location is when deploying the application later. get_db returns a database connection, which is used to execute the commands read from the file. - -click.command() defines a command line command called init-db that calls the init_db function and shows a success message to the user. You can read Command Line Interface to learn more about writing commands. -""" - - -def init_db(): - db = get_db() - - with current_app.open_resource('schema.sql') as f: - db.executescript(f.read().decode('utf8')) - - -@click.command('init-db') -@with_appcontext -def init_db_command(): - """Clear the existing data and create new tables.""" - init_db() - click.echo('Initialized the database.') - -def init_app(app): - app.teardown_appcontext(close_db) - app.cli.add_command(init_db_command) - - -""" -app.teardown_appcontext() tells Flask to call that function when cleaning up after returning the response. - -app.cli.add_command() adds a new command that can be called with the flask command. -""" - diff --git a/app/auth.py b/auth.py similarity index 71% rename from app/auth.py rename to auth.py index 86dff1b..07d5f4d 100644 --- a/app/auth.py +++ b/auth.py @@ -1,21 +1,31 @@ import functools +from pickle import NONE +from shutil import ExecError from flask import ( Blueprint, flash, g, redirect, render_template, request, session, url_for ) -from werkzeug.security import check_password_hash, generate_password_hash +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker -# from db import get_db error +from werkzeug.security import check_password_hash, generate_password_hash +from hashlib import md5 + +from config import CONFIG + +from create_db import User bp = Blueprint('auth', __name__, url_prefix='/auth') +engine = create_engine(CONFIG['DB_CONNECTION']) +dbsessionmaker = sessionmaker(bind=engine) +dbsession = dbsessionmaker() @bp.route('/register', methods=('GET', 'POST')) def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] - db = get_db() error = None if not username: @@ -25,13 +35,13 @@ def register(): if error is None: try: - db.execute( - "INSERT INTO user (username, password) VALUES (?, ?)", - (username, generate_password_hash(password)), - ) - db.commit() - except db.IntegrityError: - error = f"User {username} is already registered." + user = User(username=username, password=md5(password.encode("utf-8").hexdigest())) + dbsession.add(user) + dbsession.commit() + + + except Exception as e: + error = f"napaka pri registraciji {username} je {e}" else: return redirect(url_for("auth.login")) @@ -45,11 +55,8 @@ def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] - db = get_db() error = None - user = db.execute( - 'SELECT * FROM user WHERE username = ?', (username,) - ).fetchone() + user = dbsession.query(User).filter(User.username == username).all() if user is None: error = 'Incorrect username.' @@ -73,9 +80,7 @@ def load_logged_in_user(): if user_id is None: g.user = None else: - g.user = get_db().execute( - 'SELECT * FROM user WHERE id = ?', (user_id,) - ).fetchone() + g.user = None @bp.route('/logout') diff --git a/app/static/style.css b/static/style.css similarity index 100% rename from app/static/style.css rename to static/style.css diff --git a/app/templates/auth/login.html b/templates/auth/login.html similarity index 100% rename from app/templates/auth/login.html rename to templates/auth/login.html diff --git a/app/templates/auth/register.html b/templates/auth/register.html similarity index 100% rename from app/templates/auth/register.html rename to templates/auth/register.html diff --git a/app/templates/base.html b/templates/base.html similarity index 100% rename from app/templates/base.html rename to templates/base.html