Move secret key to .env, bugfixes, handle rollback on DB error

master
kompot r00t 2022-06-14 14:03:29 +02:00
parent e0ae2097e0
commit 6fe899be17
7 changed files with 29 additions and 23 deletions

View File

@ -1,3 +1,4 @@
DB_CONNECTION=mysql+pymysql://DBUSER:DBPASS@DBHOST:3306/DBNAME?charset=utf8mb4 DB_CONNECTION=mysql+pymysql://DBUSER:DBPASS@DBHOST:3306/DBNAME?charset=utf8mb4
NC_USER=NEXTCLOUD_USER NC_USER=NEXTCLOUD_USER
NC_PASSWORD=NEXTCLOUD_PASSWORD NC_PASSWORD=NEXTCLOUD_PASSWORD
SECRET_KEY=dev

33
app.py
View File

@ -1,5 +1,5 @@
import os import os
from flask import Flask, redirect, render_template, session, request, url_for from flask import Flask, redirect, render_template, session, request, url_for, flash
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
@ -23,29 +23,20 @@ def create_app(test_config=None):
# create and configure the app # create and configure the app
app = Flask(__name__, instance_relative_config=True) app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping( app.config.from_mapping(
SECRET_KEY='dev', SECRET_KEY=CONFIG['SECRET_KEY']
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), )
) # = kaj naj tu piše?
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
@app.route('/') @app.route('/')
@login_required @login_required
def index(): def index():
return 'redirecting' # meče skoz nazaj na login TODO return 'redirecting' # meče skoz nazaj na login TODO
# Ce je napaka baze, naredi rollback
@app.errorhandler(500)
def internal_error(error):
session.rollback()
return render_template('500.html'), 500
@app.route('/deck/index', methods=["GET", "POST"]) @app.route('/deck/index', methods=["GET", "POST"])
def deck(): def deck():
if not 'user_id' in session: if not 'user_id' in session:
@ -109,16 +100,18 @@ def create_app(test_config=None):
if next_card >= deck_object.number_of_cards: if next_card >= deck_object.number_of_cards:
deck_object.completed = True deck_object.completed = True
dbsession.commit() dbsession.commit()
dbsession.close()
flash("Deck rating finished!")
return redirect(url_for("menu.index")) return redirect(url_for("menu.index"))
deck_object.current_card = next_card deck_object.current_card = next_card
dbsession.commit() dbsession.commit()
#dbsession.query(Card).filter(Card.id == card.id).update({'last_review': date()})
# Loudamo naslednjo karto v decku # Loudamo naslednjo karto v decku
show_card_index = deck_object.current_card show_card_index = deck_object.current_card
show_card_id = deck_object.cards_by_id.split(",")[show_card_index] show_card_id = deck_object.cards_by_id.split(",")[show_card_index]
show_card = dbsession.query(Card).get(show_card_id) show_card = dbsession.query(Card).get(show_card_id)
dbsession.close()
if not show_card: if not show_card:
print("show_card missing") print("show_card missing")
@ -157,4 +150,4 @@ def create_app(test_config=None):
app = create_app() app = create_app()
app.run(debug=True, host="0.0.0.0") app.run(debug=True, host="0.0.0.0")

View File

@ -34,6 +34,7 @@ def register():
user = User(username=username, password=md5(password.encode("utf-8")).hexdigest()) user = User(username=username, password=md5(password.encode("utf-8")).hexdigest())
dbsession.add(user) dbsession.add(user)
dbsession.commit() dbsession.commit()
dbsession.close()
except Exception as e: except Exception as e:
@ -94,4 +95,4 @@ def login_required(view):
return view(**kwargs) return view(**kwargs)
return wrapped_view return wrapped_view

View File

@ -52,6 +52,7 @@ def index():
deck = Deck(cards_by_id=cards_by_id, owner_id=user_id, number_of_cards=number_of_cards, current_card=0, completed=False) deck = Deck(cards_by_id=cards_by_id, owner_id=user_id, number_of_cards=number_of_cards, current_card=0, completed=False)
dbsession.add(deck) dbsession.add(deck)
dbsession.commit() dbsession.commit()
dbsession.close()
return redirect(url_for("deck")) return redirect(url_for("deck"))
elif action == "matches": elif action == "matches":
@ -66,4 +67,4 @@ def index():
pass pass
return render_template("menu/menu.html", username=username) return render_template("menu/menu.html", username=username)

View File

@ -29,6 +29,7 @@ for item in l:
card = Card(title=name, interest_rate=-1.0, owner_id=1, item_location=public_link, last_review=None, share_id="0") card = Card(title=name, interest_rate=-1.0, owner_id=1, item_location=public_link, last_review=None, share_id="0")
dbsession.add(card) dbsession.add(card)
dbsession.commit() dbsession.commit()
dbsession.close()
@ -38,4 +39,4 @@ for item in l:
interest_rate = Column(Float) interest_rate = Column(Float)
owner_id = Column(Integer, ForeignKey("user.id"), nullable=False) owner_id = Column(Integer, ForeignKey("user.id"), nullable=False)
""" """

View File

@ -25,5 +25,6 @@ for title in l:
) )
session.add(card) session.add(card)
session.commit() session.commit()
session.close()
id = id+1 id = id+1

View File

@ -42,6 +42,12 @@ def upload_file():
upload = request.files["file"] upload = request.files["file"]
filename = secure_filename(upload.filename) filename = secure_filename(upload.filename)
# Is there really a file?
if not filename:
flash('There is no file. Try again?')
return render_template("menu/upload.html", username=username)
script_path = os.path.dirname(os.path.abspath(__file__)) script_path = os.path.dirname(os.path.abspath(__file__))
temp_path = os.path.join(script_path, "temp") temp_path = os.path.join(script_path, "temp")
@ -59,7 +65,9 @@ def upload_file():
#add card #add card
card = Card(title=filename, interest_rate=-1.0, owner_id=user_id, item_location=public_link, last_review=None, share_id=0) card = Card(title=filename, interest_rate=-1.0, owner_id=user_id, item_location=public_link, last_review=None, share_id=0)
flash(f"{filename} uploaded successfully")
dbsession.add(card) dbsession.add(card)
dbsession.commit() dbsession.commit()
dbsession.close()
return render_template("menu/upload.html", message=message, user_id=user_id, username=username) return render_template("menu/upload.html", message=message, user_id=user_id, username=username)