contentmatcher/import_link.py

63 lines
1.9 KiB
Python
Raw Normal View History

from flask import (
Blueprint, render_template, request, session, flash
)
from create_db import Card, get_session
from link_handler import check_response
bp = Blueprint('import_link', __name__, url_prefix='/import_link')
@bp.route("/", methods=["GET", "POST"])
def index():
username = session["username"]
return render_template("import_link.html", username=username)
@bp.route('/import', methods = ('GET', 'POST'))
def import_link():
username = session['username']
user_id = session['user_id']
if request.method == 'POST':
import_type = request.form.get("import_type", False)
print(import_type)
if import_type == "import_txt":
print("!2313232321")
else:
link = request.form.get("link", False)
if check_response(link) == 1:
print(link, 'is ok')
commit_link(link=link, user_id=user_id)
else:
print(link, 'is not ok')
return render_template("import_link.html", user_id=user_id, username=username)
def commit_link(link, user_id):
dbsession = get_session()
existing_link = dbsession.query(Card).filter_by(owner_id=user_id, item_location=link).first()
if existing_link:
flash(f"{link} has already been imported.")
else:
#add card
#TODO ni razlike med title in vsebino urlja ??
card = Card(title=link,
interest_rate=-1.0,
owner_id=user_id,
item_location=link,
last_review=None,
card_type="URL",
share_id=0)
dbsession.add(card)
dbsession.commit()
flash(f"{link} imported successfully")
dbsession.close()
def import_list_of_links(list_of_links, user_id):
#TODO nekak bi bilo dobro, da pokaže kateri so failali....
for link in list_of_links:
commit_link(link, user_id)