From baf5841502f92f8e5ad7385de3542d66c30bac84 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Thu, 26 Dec 2024 20:05:34 +0100 Subject: [PATCH] Working on wishlist --- app.py | 61 ++++++- db.py | 11 ++ templates/wishlist.html | 348 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 419 insertions(+), 1 deletion(-) create mode 100644 templates/wishlist.html diff --git a/app.py b/app.py index eaddba9..bda926a 100644 --- a/app.py +++ b/app.py @@ -489,6 +489,65 @@ def minifigs(): return render_template('minifigs.html',missing_list=missing_list) +@app.route('/wishlist',methods=['POST','GET']) +def wishlist(): + input_value = 'None' + + if request.method == 'POST': + input_value = request.form.get('inputField') + print(input_value) + + + input_value = input_value.replace(" ","") + if '-' not in input_value: + input_value = input_value + '-1' + + total_set_file = np.genfromtxt("sets.csv",delimiter=",",dtype="str",usecols=(0)) + if input_value not in total_set_file: + print('ERROR: ' + input_value) + #return render_template('wishlist.html',error=input_value) + + else: + set_num = input_value + + input_value = 'None' + conn = sqlite3.connect('app.db') + cursor = conn.cursor() + rb = rebrick.init(os.getenv("REBRICKABLE_API_KEY")) + response = json.loads(rebrick.lego.get_set(set_num).read()) + cursor.execute('''INSERT INTO wishlist ( + set_num, + name, + year, + theme_id, + num_parts, + set_img_url, + set_url, + last_modified_dt + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ''', (response['set_num'], response['name'], response['year'], response['theme_id'], response['num_parts'],response['set_img_url'],response['set_url'],response['last_modified_dt'])) + set_img_url = response["set_img_url"] + res = requests.get(set_img_url, stream = True) + if res.status_code == 200: + with open("./static/sets/"+set_num+".jpg",'wb') as f: + shutil.copyfileobj(res.raw, f) + else: + logging.error('set_img_url: ' + set_num) + + conn.commit() + conn.close() + + conn = sqlite3.connect('app.db') + cursor = conn.cursor() + cursor.execute('SELECT * from wishlist;') + + results = cursor.fetchall() + wishlist = [list(i) for i in results] + if wishlist == None or wishlist == '': + wishlist = '' + conn.commit() + conn.close() + return render_template('wishlist.html',error=input_value,wishlist=wishlist) + @app.route('/create',methods=['POST','GET']) def create(): @@ -552,7 +611,7 @@ def index(): return render_template('index.html',set_list=set_list,themes_list=theme_file,missing_list=missing_list,files=files,minifigs=minifigs) - if request.method == 'POST': + if request.method == 'post': set_num = request.form.get('set_num') u_id = request.form.get('u_id') minif = request.form.get('minif') diff --git a/db.py b/db.py index 213e55f..b31121c 100644 --- a/db.py +++ b/db.py @@ -11,6 +11,17 @@ def initialize_database(): cursor = conn.cursor() # Create the required tables if they do not exist + cursor.execute('''CREATE TABLE IF NOT EXISTS wishlist ( + set_num TEXT, + name TEXT, + year INTEGER, + theme_id INTEGER, + num_parts INTEGER, + set_img_url TEXT, + set_url TEXT, + last_modified_dt TEXT + )''') + cursor.execute('''CREATE TABLE IF NOT EXISTS sets ( set_num TEXT, name TEXT, diff --git a/templates/wishlist.html b/templates/wishlist.html new file mode 100644 index 0000000..41179ee --- /dev/null +++ b/templates/wishlist.html @@ -0,0 +1,348 @@ + + + + + + {{ tmp }} - {{ title }} + + + + + + + + + + + + +
+
+ +
+
+ +
+ +
+
+
+
+ +
+
+
+ +
+
+ +
+ + + + + + + + + + + + {% for sets in wishlist %} + + + + + + + + {% endfor %} + +
IDNameYearParts
{{ sets[0] }}{{ sets[1] }}{{ sets[2] }}{{ sets[4] }}
+
+ + + + + + + + + + + + + + + + +