Browse Source

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

master
kompot r00t 2 weeks ago
parent
commit
6fe899be17
  1. 1
      .env.dist
  2. 33
      app.py
  3. 3
      auth.py
  4. 3
      menu.py
  5. 3
      populate_db_once.py
  6. 1
      search_cloud.py
  7. 8
      upload.py

1
.env.dist

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

33
app.py

@ -1,5 +1,5 @@
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.orm import sessionmaker
@ -23,29 +23,20 @@ def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
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
SECRET_KEY=CONFIG['SECRET_KEY']
)
@app.route('/')
@login_required
def index():
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"])
def deck():
if not 'user_id' in session:
@ -109,16 +100,18 @@ def create_app(test_config=None):
if next_card >= deck_object.number_of_cards:
deck_object.completed = True
dbsession.commit()
dbsession.close()
flash("Deck rating finished!")
return redirect(url_for("menu.index"))
deck_object.current_card = next_card
dbsession.commit()
#dbsession.query(Card).filter(Card.id == card.id).update({'last_review': date()})
# Loudamo naslednjo karto v decku
show_card_index = deck_object.current_card
show_card_id = deck_object.cards_by_id.split(",")[show_card_index]
show_card = dbsession.query(Card).get(show_card_id)
dbsession.close()
if not show_card:
print("show_card missing")
@ -157,4 +150,4 @@ def create_app(test_config=None):
app = create_app()
app.run(debug=True, host="0.0.0.0")
app.run(debug=True, host="0.0.0.0")

3
auth.py

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

3
menu.py

@ -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)
dbsession.add(deck)
dbsession.commit()
dbsession.close()
return redirect(url_for("deck"))
elif action == "matches":
@ -66,4 +67,4 @@ def index():
pass
return render_template("menu/menu.html", username=username)
return render_template("menu/menu.html", username=username)

3
populate_db_once.py

@ -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")
dbsession.add(card)
dbsession.commit()
dbsession.close()
@ -38,4 +39,4 @@ for item in l:
interest_rate = Column(Float)
owner_id = Column(Integer, ForeignKey("user.id"), nullable=False)
"""
"""

1
search_cloud.py

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

8
upload.py

@ -42,6 +42,12 @@ def upload_file():
upload = request.files["file"]
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__))
temp_path = os.path.join(script_path, "temp")
@ -59,7 +65,9 @@ def upload_file():
#add card
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.commit()
dbsession.close()
return render_template("menu/upload.html", message=message, user_id=user_id, username=username)
Loading…
Cancel
Save