DB session fixes, nicer upload

master
Jan Kostanjevec 2022-06-14 14:47:12 +02:00
parent 6fe899be17
commit c9e47ec111
10 changed files with 42 additions and 67 deletions

16
app.py
View File

@ -1,9 +1,6 @@
import os
from flask import Flask, redirect, render_template, session, request, url_for, flash
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from auth import login_required, bp as auth_bp
from deck import bp as deck_bp
from menu import bp as menu_bp
@ -11,14 +8,9 @@ from upload import bp as upload_bp
from share import share
from matches import bp as matches_bp
from create_db import Card, Deck
from create_db import Card, Deck, get_session
from config import CONFIG
engine = create_engine(CONFIG['DB_CONNECTION'])
dbsessionmaker = sessionmaker(bind=engine)
dbsession = dbsessionmaker()
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
@ -39,6 +31,7 @@ def create_app(test_config=None):
@app.route('/deck/index', methods=["GET", "POST"])
def deck():
dbsession = get_session()
if not 'user_id' in session:
return index()
@ -110,10 +103,15 @@ def create_app(test_config=None):
# Loudamo naslednjo karto v decku
show_card_index = deck_object.current_card
show_card_id = deck_object.cards_by_id.split(",")[show_card_index]
print("GET CARD PLS", show_card_id)
show_card = dbsession.query(Card).get(show_card_id)
dbsession.close()
if not show_card:
# @TODO how to handle missing card?
#deck_object.completed = 1
#dbsession.commit()
#dbsession.close()
print("show_card missing")
return render_template("error/no_cards_in_collection.html")
#raise Exception("Ne najdem naslednje karte")

12
auth.py
View File

@ -5,20 +5,16 @@ from flask import (
Blueprint, flash, g, redirect, render_template, request, session, url_for
)
from hashlib import md5
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from create_db import User
from create_db import User, get_session
from config import CONFIG
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():
dbsession = get_session()
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
@ -50,6 +46,8 @@ def register():
@bp.route('/login', methods=('GET', 'POST'))
def login():
if request.method == 'POST':
dbsession = get_session()
username = request.form['username']
password = request.form['password']
error = None

View File

@ -13,6 +13,7 @@ from sqlalchemy import Column, Integer, Float, String, Text, DateTime, ForeignKe
from sqlalchemy.ext.declarative import declarative_base
#from sqlalchemy.orm import relationship
from sqlalchemy import func, create_engine,join
from sqlalchemy.orm import sessionmaker
from config import CONFIG
Base = declarative_base()
@ -60,3 +61,8 @@ class Review(Base):
engine = create_engine(CONFIG['DB_CONNECTION'])
Base.metadata.create_all(engine)
def get_session():
engine = create_engine(CONFIG['DB_CONNECTION'])
dbsessionmaker = sessionmaker(bind=engine)
return dbsessionmaker()

10
deck.py
View File

@ -3,21 +3,15 @@ from flask import (
Blueprint, flash, g, redirect, render_template, request, url_for, session
)
from auth import login_required
from create_db import Card, User
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from create_db import Card, User, get_session
from numpy.random import choice
bp = Blueprint('deck', __name__)
engine = create_engine(CONFIG['DB_CONNECTION'])
dbsessionmaker = sessionmaker(bind=engine)
dbsession = dbsessionmaker()
@bp.route('/deck', methods=['GET'])
def get_collection(user_id):
dbsession = get_session()
c = dbsession.query(Card).filter(Card.owner_id == user_id).all()
return c

View File

@ -2,22 +2,14 @@ from flask import (
Blueprint, redirect, render_template, request, session, url_for
)
from requests import get
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from config import CONFIG
from create_db import Card, User
from create_db import Card, User, get_session
bp = Blueprint('matches', __name__, url_prefix='/matches')
engine = create_engine(CONFIG['DB_CONNECTION'])
dbsessionmaker = sessionmaker(bind=engine)
dbsession = dbsessionmaker()
def get_matches(user_id):
dbsession = get_session()
list_of_matches = []
#in all shared cards for the ones you votes yes
@ -59,4 +51,4 @@ def index():
names_by_ids[id] = users.get(id).username
return render_template("matches.html", username=username, list_of_matches=list_of_matches, names_by_ids=names_by_ids)
return render_template("matches.html", username=username, list_of_matches=list_of_matches, names_by_ids=names_by_ids)

14
menu.py
View File

@ -1,23 +1,18 @@
from flask import (
Blueprint, redirect, render_template, request, session, url_for
)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from config import CONFIG
from create_db import Deck, Card
from create_db import Deck, Card, get_session
from deck import get_deck, probabilistic_deck_generator
from auth import login
bp = Blueprint('menu', __name__, url_prefix='/menu')
engine = create_engine(CONFIG['DB_CONNECTION'])
dbsessionmaker = sessionmaker(bind=engine)
dbsession = dbsessionmaker()
@bp.route("/", methods=("GET", "POST"))
def index():
dbsession = get_session()
if 'user_id' not in session:
return login()
@ -34,10 +29,13 @@ def index():
# @TODO tu bi moral preveriti če je kak unfinished deck, potem bi pisalo continiue, namesto new
#preverimo če so sploh karte v collectionu
print("userID", user_id)
c = dbsession.query(Card).filter(Card.owner_id == user_id).all()
#print(dbsession.query(Card).all())
#print(c)
if c == []:
print("ne najdem collectiona")
return render_template("error/no_cards_in_collection.html")
return render_template("error/no_cards_in_collection.html", username=username)
#ustvari novi vnos v Deck
deck = probabilistic_deck_generator(user_id, 5, 5)

View File

@ -1,17 +1,13 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from create_db import Card
from create_db import Card, get_session
from get_files import get_file_list
from config import CONFIG
import nextcloud_client
engine = create_engine(CONFIG['DB_CONNECTION'])
dbsessionmaker = sessionmaker(bind=engine)
dbsession = dbsessionmaker()
dbsession = get_session()
nc = nextcloud_client.Client('https://obzorje.kompot.si')
nc.login(CONFIG['NC_USER'],CONFIG['NC_PASSWORD'])

View File

@ -1,17 +1,14 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from config import CONFIG
from create_db import User, Card
from create_db import User, Card, get_session
from hashlib import md5
engine = create_engine(CONFIG['DB_CONNECTION'])
dbsessionmaker = sessionmaker(bind=engine)
dbsession = dbsessionmaker()
def share(card, user_id):
dbsession = get_session()
#tu bi lahko naredili nek autoincrement ampak i guess da hash unique idja tudi daje unique share_id, tega potem uporabljamo, da preverimo matche
h = md5(str(card.id).encode("utf-8")).hexdigest()
card.share_id = h
@ -25,4 +22,4 @@ def share(card, user_id):
print("sharing to, ", user.id)
new_card = card = Card(title=card.title, interest_rate=-1.0, owner_id=user.id, item_location=card.item_location, last_review=None, share_id=h)
dbsession.add(new_card)
dbsession.commit()
dbsession.commit()

View File

@ -12,9 +12,8 @@
</body>
<form id="form" method="post" enctype="multipart/form-data" action="/upload/uploader">
<div class="drop-zone">
<span class="drop-zone__prompt">Drop file here or click to upload</span>
<span id="zone-txt" class="drop-zone__prompt">Drop file here or click to upload</span>
<input type="file" name="file" class="drop-zone__input">
<input type="submit">
</div>
</form>
@ -43,6 +42,7 @@
if (e.dataTransfer.files.length) {
inputElement.files = e.dataTransfer.files;
console.log("oddajam form", e);
document.getElementById('zone-txt').innerHTML = "Uploading file...";
document.getElementById("form").submit();
}
@ -52,4 +52,4 @@
</script>
<body>
{% endblock %}
{% endblock %}

View File

@ -4,22 +4,16 @@ from flask import (
Blueprint, flash, g, redirect, render_template, request, session, url_for
)
from werkzeug.utils import secure_filename
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from auth import login_required
from config import CONFIG
from create_db import User, Card
from create_db import User, Card, get_session
import nextcloud_client
bp = Blueprint('upload', __name__, url_prefix='/upload')
engine = create_engine(CONFIG['DB_CONNECTION'])
dbsessionmaker = sessionmaker(bind=engine)
dbsession = dbsessionmaker()
nc = nextcloud_client.Client('https://obzorje.kompot.si')
#print("Loggin in", CONFIG['NC_USER'], CONFIG['NC_PASSWORD'])
@ -34,6 +28,8 @@ def index():
@bp.route('/uploader', methods = ('GET', 'POST'))
def upload_file():
dbsession = get_session()
user_id = session["user_id"]
username = session["username"]
message = ""