db work
parent
ba22b3a03d
commit
77ec762b6f
|
@ -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()
|
||||
app = create_app()
|
||||
app.run(debug=True, host="0.0.0.0")
|
75
app/app.py
75
app/app.py
|
@ -1,75 +0,0 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
from flask import Flask
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route("/")
|
||||
def hello_world():
|
||||
return "<p>Hello, World!</p>"
|
||||
|
||||
|
||||
"""
|
||||
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 = '<ul>'
|
||||
skeni = list()
|
||||
for scan in query:
|
||||
scan_dict = dict()
|
||||
scan_dict["id_scan"] = str(scan.id_scan)
|
||||
scan_dict["id_song"] = str(scan.id_song)
|
||||
scan_dict["match_val"] = str(scan.match_val)
|
||||
scan_dict["naslov"] = scan.naslov
|
||||
scan_dict["album"] = scan.album
|
||||
scan_dict["izvajalec"] = scan.izvajalec
|
||||
scan_dict["trajanje"] = str(scan.trajanje)
|
||||
scan_dict["timestamp"] = str(scan.timestamp)
|
||||
#predvajano_text += '<ol>' + id_scan + ' , ' + id_song + ' , ' + match_val +' , ' + naslov + ' , ' + album + ' , ' + izvajalec + ' , ' + trajanje + ' , ' + timestamp + '</ol>'
|
||||
#predvajano_text += '</ul>'
|
||||
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 = "<p>ni zadetkov:<br>" + str(e) + "</p>"
|
||||
hed = '<h1>Komada ni v bazi.</h1>'
|
||||
return hed + error_text
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True, host = "0.0.0.0")
|
||||
|
||||
"""
|
57
app/db.py
57
app/db.py
|
@ -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.
|
||||
"""
|
||||
|
|
@ -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')
|
Loading…
Reference in New Issue