2024-11-20 21:57:56 +01:00
from flask import Flask , request , redirect , jsonify , render_template , Response , url_for , send_from_directory
2024-06-18 16:46:34 +02:00
import os
2024-02-28 20:08:16 +01:00
import json
2024-04-18 22:11:44 +02:00
from flask_socketio import SocketIO
from threading import Thread
2024-02-29 14:47:12 +01:00
from pprint import pprint as pp
2024-03-03 11:15:38 +01:00
from pathlib import Path
2024-12-27 10:32:01 +01:00
import time , random , string , sqlite3 , csv
2024-04-14 15:56:08 +02:00
import numpy as np
2024-04-16 21:36:34 +02:00
import re #regex
import rebrick #rebrickable api
import requests # request img from web
import shutil # save img locally
2024-06-18 17:40:32 +02:00
import eventlet
2024-12-27 09:46:54 +01:00
from downloadRB import download_and_unzip , get_nil_images , get_retired_sets
2024-06-19 13:42:28 +02:00
from db import initialize_database , get_rows , delete_tables
2024-06-29 17:32:47 +02:00
from werkzeug . middleware . proxy_fix import ProxyFix
2024-12-28 10:54:07 +01:00
from werkzeug . utils import secure_filename
2024-06-29 17:32:47 +02:00
2024-04-16 21:36:34 +02:00
2024-02-28 20:08:16 +01:00
app = Flask ( __name__ )
2024-06-29 17:32:47 +02:00
app . wsgi_app = ProxyFix ( app . wsgi_app , x_for = 1 , x_proto = 1 , x_host = 1 , x_port = 1 , x_prefix = 1 )
2024-06-29 17:38:52 +02:00
socketio = SocketIO ( app , cors_allowed_origins = os . getenv ( " DOMAIN_NAME " ) )
2024-04-18 22:11:44 +02:00
count = 0
2024-02-28 20:08:16 +01:00
2024-12-28 13:58:01 +01:00
if os . getenv ( " RANDOM " ) == ' True ' :
RANDOM = True
else :
RANDOM = False
2024-12-28 09:34:07 +01:00
if os . getenv ( " LINKS " ) :
LINKS = os . getenv ( " LINKS " )
else :
LINKS = False
2024-11-20 21:57:56 +01:00
DIRECTORY = os . path . join ( os . getcwd ( ) , ' static ' , ' instructions ' )
2024-12-28 10:54:07 +01:00
UPLOAD_FOLDER = DIRECTORY
ALLOWED_EXTENSIONS = { ' pdf ' }
app . config [ ' UPLOAD_FOLDER ' ] = UPLOAD_FOLDER
2024-11-20 21:57:56 +01:00
2024-03-04 20:21:11 +01:00
@app.route ( ' /favicon.ico ' )
2024-03-03 11:15:38 +01:00
2024-04-18 22:11:44 +02:00
# SocketIO event handler for client connection
@socketio.on ( ' connect ' , namespace = ' /progress ' )
def test_connect ( ) :
print ( ' Client connected ' )
2024-06-29 16:03:38 +02:00
return ( ' ' , 301 )
2024-04-18 22:11:44 +02:00
# SocketIO event handler for client disconnection
@socketio.on ( ' disconnect ' , namespace = ' /progress ' )
def test_disconnect ( ) :
print ( ' Client disconnected ' )
# SocketIO event handler for starting the task
@socketio.on ( ' start_task ' , namespace = ' /progress ' )
def start_task ( data ) :
input_value = data . get ( ' inputField ' )
print ( input_value )
2024-04-24 20:24:04 +02:00
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 ) )
print ( total_set_file )
if input_value not in total_set_file :
print ( ' ERROR: ' + input_value )
# Reload create.html with error message
2024-04-25 21:41:14 +02:00
socketio . emit ( ' task_failed ' , namespace = ' /progress ' )
#return render_template('create.html',error=input_value)
2024-04-24 20:24:04 +02:00
# Start the task in a separate thread to avoid blocking the serve
2024-04-25 21:41:14 +02:00
else :
print ( ' starting servers ' )
thread = Thread ( target = new_set , args = ( input_value , ) )
thread . start ( )
2024-04-18 22:11:44 +02:00
2024-04-24 20:24:04 +02:00
#return redirect('/')
2024-11-20 21:57:56 +01:00
def hyphen_split ( a ) :
if a . count ( " - " ) == 1 :
return a . split ( " - " ) [ 0 ]
return " - " . join ( a . split ( " - " , 2 ) [ : 2 ] )
2024-12-28 10:54:07 +01:00
def allowed_file ( filename ) :
return ' . ' in filename and \
filename . rsplit ( ' . ' , 1 ) [ 1 ] . lower ( ) in ALLOWED_EXTENSIONS
@app.route ( ' /upload ' , methods = [ ' GET ' , ' POST ' ] )
def uploadInst ( ) :
if request . method == ' POST ' :
# check if the post request has the file part
if ' file ' not in request . files :
flash ( ' No file part ' )
return redirect ( request . url )
file = request . files [ ' file ' ]
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file . filename == ' ' :
flash ( ' No selected file ' )
return redirect ( request . url )
if file and allowed_file ( file . filename ) :
filename = secure_filename ( file . filename )
file . save ( os . path . join ( app . config [ ' UPLOAD_FOLDER ' ] , filename ) )
return redirect ( ' / ' )
return '''
< ! doctype html >
2024-12-28 11:08:55 +01:00
< title > Upload instructions < / title >
< h1 > Upload instructions < / h1 >
< p > Files must be named like : < / p >
< code > & lt ; set number & gt ; - & lt ; version & gt ; - & lt ; part & gt ; . pdf < / code >
< ul >
< li > < code > 7595 - 1. pdf < / code > for set 7595 < / li >
< li > < code > 71039 - 2. pdf < / code > for Moon Knight in < code > Collectible Minifigures : Marvel Series 2 < / code > < / li >
< li > < code > 71039 - 13. pdf < / code > for the whole set < code > Collectible Minifigures : Marvel Series 2 < / code > < / li >
< li > < code > 10294 - 1 - 1. pdf < / code > for the 1 st pdf in the 10294 set
< li > < code > 10294 - 1 - 2. pdf < / code > for the 2 nd pdf in the 10294 set
< li > < code > 10294 - 1 - 3. pdf < / code > for the 3 rd pdf in the 10294 set
< li > < code > 10937 - 1 - 0. pdf < / code > for the comic that comes with set 10937.
< li > < code > 10937 - 1 - 1. pdf < / code > for the 1 st pdf in the 10937 set
< / ul >
2024-12-28 10:54:07 +01:00
< form method = post enctype = multipart / form - data >
< input type = file name = file >
< input type = submit value = Upload >
< / form >
'''
2024-04-18 11:43:23 +02:00
@app.route ( ' /delete/<tmp> ' , methods = [ ' POST ' , ' GET ' ] )
def delete ( tmp ) :
conn = sqlite3 . connect ( ' app.db ' )
cursor = conn . cursor ( )
if request . method == ' POST ' :
print ( " POST " )
if request . method == " GET " :
print ( " GET " )
print ( tmp )
2024-04-18 12:51:09 +02:00
tables = [ ' inventory ' , ' sets ' , ' minifigures ' , ' missing ' ]
for t in tables :
cursor . execute ( ' DELETE FROM ' + t + ' where u_id= " ' + tmp + ' " ; ' )
conn . commit ( )
cursor . close ( )
conn . close ( )
2024-04-18 11:43:23 +02:00
return redirect ( ' / ' )
2024-04-25 21:41:14 +02:00
def progress ( count , total_parts , state ) :
print ( state )
socketio . emit ( ' update_progress ' , { ' progress ' : int ( count / total_parts * 100 ) , ' desc ' : state } , namespace = ' /progress ' )
2024-04-18 22:11:44 +02:00
def new_set ( set_num ) :
global count
###### total count ####
# 1 for set
# 1 for set image
2024-04-25 21:41:14 +02:00
total_parts = 20
2024-04-18 22:11:44 +02:00
2024-04-24 20:24:04 +02:00
2024-04-18 22:11:44 +02:00
# add_duplicate = request.form.get('addDuplicate', False) == 'true'
# Do something with the input value and the checkbox value
# print("Input value:", set_num)
# print("Add duplicate:", add_duplicate)
# You can perform any further processing or redirect to another page
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Opening database ' )
2024-04-16 21:36:34 +02:00
conn = sqlite3 . connect ( ' app.db ' )
cursor = conn . cursor ( )
2024-04-24 20:24:04 +02:00
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Adding set: ' + set_num )
2024-04-16 21:36:34 +02:00
2024-06-18 16:46:34 +02:00
#with open('api','r') as f:
# api_key = f.read().replace('\n','')
# TODO add 401 error on wrong key
rb = rebrick . init ( os . getenv ( " REBRICKABLE_API_KEY " ) )
2024-04-16 21:36:34 +02:00
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Generating Unique ID ' )
2024-04-18 22:11:44 +02:00
unique_set_id = generate_unique_set_unique ( )
# Get Set info and add to SQL
response = ' '
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Get set info ' )
2024-04-24 20:24:04 +02:00
response = json . loads ( rebrick . lego . get_set ( set_num ) . read ( ) )
2024-04-18 22:11:44 +02:00
2024-04-24 20:24:04 +02:00
# except Exception as e:
# #print(e.code)
# if e.code == 404:
# return render_template('create.html',error=set_num)
2024-04-18 22:11:44 +02:00
count + = 1
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Adding set to database ' )
2024-04-18 22:11:44 +02:00
cursor . execute ( ''' INSERT INTO sets (
set_num ,
name ,
year ,
theme_id ,
num_parts ,
set_img_url ,
set_url ,
last_modified_dt ,
mini_col ,
set_check ,
set_col ,
u_id
) 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 ' ],False,False,False,unique_set_id))
conn . commit ( )
# Get set image. Saved under ./static/sets/xxx-x.jpg
set_img_url = response [ " set_img_url " ]
#print('Saving set image:',end='')
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Get set image ' )
2024-04-18 22:11:44 +02:00
res = requests . get ( set_img_url , stream = True )
count + = 1
if res . status_code == 200 :
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Saving set image ' )
2024-04-18 22:11:44 +02:00
with open ( " ./static/sets/ " + set_num + " .jpg " , ' wb ' ) as f :
shutil . copyfileobj ( res . raw , f )
#print(' OK')
else :
#print('Image Couldn\'t be retrieved for set ' + set_num)
logging . error ( ' set_img_url: ' + set_num )
#print(' ERROR')
2024-04-16 21:36:34 +02:00
2024-04-18 22:11:44 +02:00
# Get inventory and add to SQL
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Get set inventory ' )
2024-12-26 21:20:08 +01:00
response = json . loads ( rebrick . lego . get_set_elements ( set_num , page_size = 500 ) . read ( ) )
2024-04-18 22:11:44 +02:00
count + = 1
total_parts + = len ( response [ ' results ' ] )
2024-04-25 21:41:14 +02:00
2024-04-18 22:11:44 +02:00
for i in response [ ' results ' ] :
2024-04-19 10:29:28 +02:00
if i [ ' is_spare ' ] :
continue
2024-04-18 22:11:44 +02:00
# Get part image. Saved under ./static/parts/xxxx.jpg
part_img_url = i [ ' part ' ] [ ' part_img_url ' ]
part_img_url_id = ' nil '
2024-04-18 11:43:23 +02:00
2024-04-18 19:47:05 +02:00
try :
2024-04-18 22:11:44 +02:00
pattern = r ' /([^/]+) \ .(?:png|jpg)$ '
match = re . search ( pattern , part_img_url )
if match :
part_img_url_id = match . group ( 1 )
#print("Part number:", part_img_url_id)
else :
#print("Part number not found in the URL.")
print ( " >>> " + part_img_url )
2024-04-18 19:47:05 +02:00
except Exception as e :
2024-04-18 22:11:44 +02:00
#print("Part number not found in the URL.")
#print(">>> " + str(part_img_url))
print ( str ( e ) )
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Adding ' + i [ ' part ' ] [ ' name ' ] + ' to database ' )
2024-04-18 22:11:44 +02:00
cursor . execute ( ''' INSERT INTO inventory (
2024-04-16 21:36:34 +02:00
set_num ,
2024-04-18 22:11:44 +02:00
id ,
part_num ,
2024-04-16 21:36:34 +02:00
name ,
2024-04-18 22:11:44 +02:00
part_img_url ,
part_img_url_id ,
color_id ,
color_name ,
quantity ,
is_spare ,
element_id ,
2024-04-16 21:36:34 +02:00
u_id
2024-04-18 22:11:44 +02:00
) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) ''' , (set_num, i[ ' id ' ], i[ ' part ' ][ ' part_num ' ],i[ ' part ' ][ ' name ' ],i[ ' part ' ][ ' part_img_url ' ],part_img_url_id,i[ ' color ' ][ ' id ' ],i[ ' color ' ][ ' name ' ],i[ ' quantity ' ],i[ ' is_spare ' ],i[ ' element_id ' ],unique_set_id))
if not Path ( " ./static/parts/ " + part_img_url_id + " .jpg " ) . is_file ( ) :
#print('Saving part image:',end='')
if part_img_url is not None :
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Get part image ' )
2024-04-18 22:11:44 +02:00
res = requests . get ( part_img_url , stream = True )
count + = 1
if res . status_code == 200 :
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Saving part image ' )
2024-04-18 22:11:44 +02:00
with open ( " ./static/parts/ " + part_img_url_id + " .jpg " , ' wb ' ) as f :
shutil . copyfileobj ( res . raw , f )
#print(' OK')
else :
#print('Image Couldn\'t be retrieved for set ' + part_img_url_id)
logging . error ( ' part_img_url: ' + part_img_url_id )
#print(' ERROR')
else :
#print('Part url is None')
print ( i )
2024-04-18 11:43:23 +02:00
2024-04-18 22:11:44 +02:00
conn . commit ( )
2024-04-16 21:36:34 +02:00
2024-04-18 22:11:44 +02:00
# Get minifigs
#print('Savings minifigs')
tmp_set_num = set_num
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Get set minifigs ' )
2024-04-18 22:11:44 +02:00
response = json . loads ( rebrick . lego . get_set_minifigs ( set_num ) . read ( ) )
count + = 1
2024-04-25 21:41:14 +02:00
2024-04-18 22:11:44 +02:00
#print(response)
for i in response [ ' results ' ] :
2024-04-16 21:36:34 +02:00
2024-04-18 22:11:44 +02:00
# Get set image. Saved under ./static/minifigs/xxx-x.jpg
set_img_url = i [ " set_img_url " ]
set_num = i [ ' set_num ' ]
2024-04-16 21:36:34 +02:00
2024-04-18 22:11:44 +02:00
#print('Saving set image:',end='')
if not Path ( " ./static/minifigs/ " + set_num + " .jpg " ) . is_file ( ) :
2024-04-24 20:24:04 +02:00
if set_img_url is not None :
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Get minifig image ' )
2024-04-24 20:24:04 +02:00
res = requests . get ( set_img_url , stream = True )
count + = 1
if res . status_code == 200 :
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Saving minifig image ' )
2024-04-24 20:24:04 +02:00
with open ( " ./static/minifigs/ " + set_num + " .jpg " , ' wb ' ) as f :
shutil . copyfileobj ( res . raw , f )
#print(' OK')
else :
#print('Image Couldn\'t be retrieved for set ' + set_num)
logging . error ( ' set_img_url: ' + set_num )
#print(' ERROR')
2024-04-18 22:11:44 +02:00
else :
2024-04-24 20:24:04 +02:00
print ( i )
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Adding minifig to database ' )
2024-04-18 22:11:44 +02:00
cursor . execute ( ''' INSERT INTO minifigures (
fig_num ,
set_num ,
name ,
quantity ,
set_img_url ,
u_id
) VALUES ( ? , ? , ? , ? , ? , ? ) ''' , (i[ ' set_num ' ],tmp_set_num, i[ ' set_name ' ], i[ ' quantity ' ],i[ ' set_img_url ' ],unique_set_id))
2024-04-16 21:36:34 +02:00
2024-04-18 22:11:44 +02:00
conn . commit ( )
# Get minifigs inventory
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Get minifig inventory ' )
2024-04-18 22:11:44 +02:00
response_minifigs = json . loads ( rebrick . lego . get_minifig_elements ( i [ ' set_num ' ] ) . read ( ) )
2024-04-16 21:36:34 +02:00
count + = 1
2024-04-18 22:11:44 +02:00
for i in response_minifigs [ ' results ' ] :
2024-04-16 21:36:34 +02:00
# Get part image. Saved under ./static/parts/xxxx.jpg
part_img_url = i [ ' part ' ] [ ' part_img_url ' ]
2024-04-18 20:07:50 +02:00
part_img_url_id = ' nil '
try :
pattern = r ' /([^/]+) \ .(?:png|jpg)$ '
match = re . search ( pattern , part_img_url )
2024-04-16 21:36:34 +02:00
2024-04-18 20:07:50 +02:00
if match :
part_img_url_id = match . group ( 1 )
2024-04-18 22:11:44 +02:00
#print("Part number:", part_img_url_id)
if not Path ( " ./static/parts/ " + part_img_url_id + " .jpg " ) . is_file ( ) :
#print('Saving part image:',end='')
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Get minifig image ' )
2024-04-18 22:11:44 +02:00
res = requests . get ( part_img_url , stream = True )
count + = 1
if res . status_code == 200 :
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Saving minifig image ' )
2024-04-18 22:11:44 +02:00
with open ( " ./static/parts/ " + part_img_url_id + " .jpg " , ' wb ' ) as f :
shutil . copyfileobj ( res . raw , f )
#print(' OK')
else :
#print('Image Couldn\'t be retrieved for set ' + part_img_url_id)
logging . error ( ' part_img_url: ' + part_img_url_id )
#print(' ERROR')
else :
print ( part_img_url_id + ' .jpg exists! ' )
2024-04-18 20:07:50 +02:00
except Exception as e :
2024-04-18 22:11:44 +02:00
#print("Part number not found in the URL.")
#print(">>> " + str(part_img_url))
2024-04-18 20:07:50 +02:00
print ( str ( e ) )
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Adding minifig inventory to database ' )
2024-04-16 21:36:34 +02:00
cursor . execute ( ''' INSERT INTO inventory (
set_num ,
id ,
part_num ,
name ,
part_img_url ,
part_img_url_id ,
color_id ,
color_name ,
quantity ,
is_spare ,
element_id ,
u_id
2024-04-18 22:11:44 +02:00
) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) ''' , (i[ ' set_num ' ], i[ ' id ' ], i[ ' part ' ][ ' part_num ' ],i[ ' part ' ][ ' name ' ],i[ ' part ' ][ ' part_img_url ' ],part_img_url_id,i[ ' color ' ][ ' id ' ],i[ ' color ' ][ ' name ' ],i[ ' quantity ' ],i[ ' is_spare ' ],i[ ' element_id ' ],unique_set_id))
2024-04-16 21:36:34 +02:00
2024-04-18 22:11:44 +02:00
2024-04-16 21:36:34 +02:00
conn . commit ( )
2024-04-18 22:11:44 +02:00
conn . close ( )
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Closing database ' )
#print('End Count: ' + str(count))
#print('End Total: ' + str(total_parts))
2024-04-18 22:11:44 +02:00
count = total_parts
2024-04-25 21:41:14 +02:00
# >>>>>>>>
progress ( count , total_parts , ' Cleaning up ' )
2024-04-18 22:11:44 +02:00
count = 0
socketio . emit ( ' task_completed ' , namespace = ' /progress ' )
2024-04-16 21:36:34 +02:00
2024-06-19 13:42:28 +02:00
def get_file_creation_dates ( file_list ) :
creation_dates = { }
for file_name in file_list :
file_path = f " { file_name } "
if os . path . exists ( file_path ) :
creation_time = os . path . getctime ( file_path )
creation_dates [ file_name ] = time . ctime ( creation_time )
else :
creation_dates [ file_name ] = " File not found "
return creation_dates
2024-06-18 19:27:51 +02:00
@app.route ( ' /config ' , methods = [ ' POST ' , ' GET ' ] )
def config ( ) :
2024-06-19 13:42:28 +02:00
2024-12-27 09:46:54 +01:00
file_list = [ ' themes.csv ' , ' colors.csv ' , ' sets.csv ' , ' static/nil.png ' , ' static/nil_mf.jpg ' , ' retired_sets.csv ' ]
2024-06-19 13:42:28 +02:00
creation_dates = get_file_creation_dates ( file_list )
row_counts = [ 0 ]
db_exists = Path ( " app.db " )
if db_exists . is_file ( ) :
db_is_there = True
row_counts = get_rows ( )
else :
db_is_there = False
2024-06-18 19:27:51 +02:00
if request . method == ' POST ' :
2024-06-19 13:42:28 +02:00
if request . form . get ( ' CreateDB ' ) == ' Create Database ' :
initialize_database ( )
row_counts = get_rows ( )
return redirect ( url_for ( ' config ' ) )
elif request . form . get ( ' Update local data ' ) == ' Update local data ' :
2024-06-18 19:27:51 +02:00
urls = [ " themes " , " sets " , " colors " ]
for i in urls :
download_and_unzip ( " https://cdn.rebrickable.com/media/downloads/ " + i + " .csv.gz " )
2024-06-19 13:42:28 +02:00
get_nil_images ( )
2024-12-27 09:46:54 +01:00
get_retired_sets ( )
2024-06-19 13:42:28 +02:00
return redirect ( url_for ( ' config ' ) )
elif request . form . get ( ' deletedb ' ) == ' Delete Database ' :
delete_tables ( )
initialize_database ( )
2024-06-18 19:27:51 +02:00
else :
# pass # unknown
return render_template ( " config.html " )
elif request . method == ' GET ' :
# return render_template("index.html")
print ( " No Post Back Call " )
2024-06-19 13:42:28 +02:00
return render_template ( " config.html " , db_is_there = db_is_there , creation_dates = creation_dates , row_counts = row_counts )
2024-06-18 19:27:51 +02:00
2024-04-19 10:21:00 +02:00
@app.route ( ' /missing ' , methods = [ ' POST ' , ' GET ' ] )
def missing ( ) :
conn = sqlite3 . connect ( ' app.db ' )
cursor = conn . cursor ( )
2024-04-26 17:04:10 +02:00
cursor . execute ( ' SELECT part_num, color_id, element_id, part_img_url_id, SUM(quantity) AS total_quantity FROM missing GROUP BY part_num, color_id, element_id; ' )
2024-04-19 10:21:00 +02:00
results = cursor . fetchall ( )
missing_list = [ list ( i ) for i in results ]
cursor . close ( )
conn . close ( )
2024-06-29 16:33:00 +02:00
color_file = np . loadtxt ( " colors.csv " , delimiter = " , " , dtype = " str " )
color_dict = { str ( code ) : name for code , name , _ , _ in color_file }
for item in missing_list :
color_code = str ( item [ 1 ] )
if color_code in color_dict :
item [ 1 ] = color_dict [ color_code ]
2024-04-19 10:21:00 +02:00
return render_template ( ' missing.html ' , missing_list = missing_list )
2024-04-26 16:43:12 +02:00
@app.route ( ' /parts ' , methods = [ ' POST ' , ' GET ' ] )
def parts ( ) :
conn = sqlite3 . connect ( ' app.db ' )
cursor = conn . cursor ( )
2024-12-28 19:37:47 +01:00
cursor . execute ( ' SELECT id, part_num, color_id, color_name, element_id, part_img_url_id, SUM(quantity) AS total_quantity, name FROM inventory GROUP BY part_num, part_img_url_id, color_id, color_name, element_id, name; ' )
2024-04-26 16:43:12 +02:00
results = cursor . fetchall ( )
missing_list = [ list ( i ) for i in results ]
cursor . close ( )
conn . close ( )
2024-11-21 13:23:32 +01:00
#color_file = np.loadtxt("colors.csv",delimiter=",",dtype="str")
2024-06-29 16:34:47 +02:00
2024-11-21 13:23:32 +01:00
#color_dict = {str(code): name for code, name, _, _ in color_file}
2024-06-29 16:34:47 +02:00
2024-11-21 13:23:32 +01:00
#for item in missing_list:
# color_code = str(item[2])
# if color_code in color_dict:
# item[2] = color_dict[color_code]
2024-06-29 16:34:47 +02:00
2024-04-26 16:43:12 +02:00
return render_template ( ' parts.html ' , missing_list = missing_list )
2024-04-19 10:21:00 +02:00
@app.route ( ' /minifigs ' , methods = [ ' POST ' , ' GET ' ] )
def minifigs ( ) :
2024-04-19 10:43:05 +02:00
conn = sqlite3 . connect ( ' app.db ' )
cursor = conn . cursor ( )
cursor . execute ( ' SELECT fig_num, name, SUM(quantity) AS total_quantity FROM minifigures GROUP BY fig_num, name; ' )
results = cursor . fetchall ( )
missing_list = [ list ( i ) for i in results ]
cursor . close ( )
conn . close ( )
2024-04-19 10:21:00 +02:00
2024-04-19 10:43:05 +02:00
return render_template ( ' minifigs.html ' , missing_list = missing_list )
2024-04-19 10:21:00 +02:00
2024-12-26 20:05:34 +01:00
@app.route ( ' /wishlist ' , methods = [ ' POST ' , ' GET ' ] )
def wishlist ( ) :
input_value = ' None '
if request . method == ' POST ' :
2024-12-26 20:23:28 +01:00
if ' create_submit ' in request . form :
input_value = request . form . get ( ' inputField ' )
print ( input_value )
2024-12-26 20:05:34 +01:00
2024-12-26 20:23:28 +01:00
input_value = input_value . replace ( " " , " " )
if ' - ' not in input_value :
input_value = input_value + ' -1 '
2024-12-26 20:05:34 +01:00
2024-12-26 20:23:28 +01:00
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)
2024-12-26 20:05:34 +01:00
2024-12-26 20:23:28 +01:00
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 ( )
elif ' add_to_list ' in request . form :
set_num = request . form . get ( ' set_num ' )
2024-12-26 20:05:34 +01:00
conn = sqlite3 . connect ( ' app.db ' )
cursor = conn . cursor ( )
2024-12-26 20:23:28 +01:00
cursor . execute ( ' DELETE FROM wishlist where set_num= " ' + set_num + ' " ; ' )
2024-12-26 20:05:34 +01:00
conn . commit ( )
2024-12-26 20:23:28 +01:00
cursor . close ( )
2024-12-26 20:05:34 +01:00
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 ]
2024-12-27 11:06:43 +01:00
retired_sets_dict = { }
try :
with open ( ' retired_sets.csv ' , mode = ' r ' , encoding = ' utf-8 ' ) as csvfile :
reader = csv . reader ( csvfile )
header = next ( reader )
for row in reader :
key = row [ 2 ]
retired_sets_dict [ key ] = row
for w in wishlist :
set_num = w [ 0 ] . split ( ' - ' ) [ 0 ]
w . append ( retired_sets_dict . get ( set_num , [ " " ] * 7 ) [ 6 ] )
except :
print ( ' No retired list ' )
2024-12-26 20:05:34 +01:00
if wishlist == None or wishlist == ' ' :
wishlist = ' '
conn . commit ( )
conn . close ( )
return render_template ( ' wishlist.html ' , error = input_value , wishlist = wishlist )
2024-04-18 22:11:44 +02:00
@app.route ( ' /create ' , methods = [ ' POST ' , ' GET ' ] )
def create ( ) :
global count
2024-04-16 21:36:34 +02:00
2024-04-18 22:11:44 +02:00
2024-04-16 21:36:34 +02:00
print ( ' Count: ' + str ( count ) )
return render_template ( ' create.html ' )
def generate_unique_set_unique ( ) :
timestamp = int ( time . time ( ) * 1000 ) # Current timestamp in milliseconds
random_chars = ' ' . join ( random . choices ( string . ascii_uppercase + string . digits , k = 8 ) ) # 8-digit alphanumeric
return f ' { timestamp } { random_chars } '
@app.route ( ' / ' , methods = [ ' GET ' , ' POST ' ] )
2024-03-03 11:15:38 +01:00
def index ( ) :
2024-04-16 21:36:34 +02:00
set_list = [ ]
2024-12-26 18:17:54 +01:00
try :
theme_file = np . loadtxt ( " themes.csv " , delimiter = " , " , dtype = " str " )
except : #First time running, no csvs.
initialize_database ( )
urls = [ " themes " , " sets " , " colors " ]
for i in urls :
download_and_unzip ( " https://cdn.rebrickable.com/media/downloads/ " + i + " .csv.gz " )
get_nil_images ( )
return redirect ( ' /create ' )
2024-04-16 21:36:34 +02:00
if request . method == ' GET ' :
conn = sqlite3 . connect ( ' app.db ' )
cursor = conn . cursor ( )
cursor . execute ( ' SELECT * from sets; ' )
2024-04-16 21:43:30 +02:00
results = cursor . fetchall ( )
set_list = [ list ( i ) for i in results ]
2024-12-28 13:58:01 +01:00
if RANDOM :
2024-12-28 13:51:02 +01:00
random . shuffle ( set_list )
2024-04-19 08:37:17 +02:00
cursor . execute ( ' SELECT DISTINCT u_id from missing; ' )
results = cursor . fetchall ( )
missing_list = [ list ( i ) [ 0 ] for i in results ]
2024-04-18 22:11:44 +02:00
#print(set_list)
2024-04-16 21:43:30 +02:00
for i in set_list :
try :
i [ 3 ] = theme_file [ theme_file [ : , 0 ] == str ( i [ 3 ] ) ] [ 0 ] [ 1 ]
except Exception as e :
print ( e )
2024-11-20 22:19:15 +01:00
cursor . execute ( ' select distinct set_num from minifigures; ' )
results = cursor . fetchall ( )
minifigs = [ list ( i ) [ 0 ] for i in results ]
2024-04-16 21:36:34 +02:00
cursor . close ( )
conn . close ( )
2024-11-20 21:57:56 +01:00
files = [ f for f in os . listdir ( DIRECTORY ) if f . endswith ( ' .pdf ' ) ]
#files = [re.match(r'^([\w]+-[\w]+)', f).group() for f in os.listdir(DIRECTORY) if f.endswith('.pdf')]
print ( files . sort ( ) )
2024-12-28 09:34:07 +01:00
return render_template ( ' index.html ' , set_list = set_list , themes_list = theme_file , missing_list = missing_list , files = files , minifigs = minifigs , links = LINKS )
2024-04-18 22:11:44 +02:00
2024-12-26 20:05:34 +01:00
if request . method == ' post ' :
2024-04-18 22:11:44 +02:00
set_num = request . form . get ( ' set_num ' )
u_id = request . form . get ( ' u_id ' )
minif = request . form . get ( ' minif ' )
scheck = request . form . get ( ' scheck ' )
scol = request . form . get ( ' scol ' )
conn = sqlite3 . connect ( ' app.db ' )
cursor = conn . cursor ( )
2024-04-16 21:36:34 +02:00
2024-04-18 22:11:44 +02:00
if minif != None :
if minif == ' true ' :
val = 1
else :
val = 0
cursor . execute ( ''' UPDATE sets
SET mini_col = ?
WHERE set_num = ? AND
u_id = ? ''' ,
( val , set_num , u_id ) )
conn . commit ( )
if scheck != None :
if scheck == ' true ' :
val = 1
else :
val = 0
cursor . execute ( ''' UPDATE sets
SET set_check = ?
WHERE set_num = ? AND
u_id = ? ''' ,
( val , set_num , u_id ) )
conn . commit ( )
if scol != None :
if scol == ' true ' :
val = 1
else :
val = 0
cursor . execute ( ''' UPDATE sets
SET set_col = ?
WHERE set_num = ? AND
u_id = ? ''' ,
( val , set_num , u_id ) )
conn . commit ( )
cursor . close ( )
conn . close ( )
return ( ' ' , 204 )
2024-04-16 21:36:34 +02:00
2024-11-20 21:57:56 +01:00
# Route to serve individual files
@app.route ( ' /files/<path:filename> ' , methods = [ ' GET ' ] )
def serve_file ( filename ) :
try :
return send_from_directory ( DIRECTORY , filename )
except Exception as e :
return jsonify ( { ' error ' : str ( e ) } ) , 404
2024-04-16 21:36:34 +02:00
@app.route ( ' /<tmp>/<u_id> ' , methods = [ ' GET ' , ' POST ' ] )
def inventory ( tmp , u_id ) :
if request . method == ' GET ' :
conn = sqlite3 . connect ( ' app.db ' )
cursor = conn . cursor ( )
# Get set info
2024-04-17 22:42:48 +02:00
cursor . execute ( " SELECT * from sets where set_num = ' " + tmp + " ' and u_id = ' " + u_id + " ' ; " )
2024-04-16 21:36:34 +02:00
results = cursor . fetchall ( )
set_list = [ list ( i ) for i in results ]
# Get inventory
2024-04-17 22:42:48 +02:00
cursor . execute ( " SELECT * from inventory where set_num = ' " + tmp + " ' and u_id = ' " + u_id + " ' ; " )
2024-04-16 21:36:34 +02:00
results = cursor . fetchall ( )
inventory_list = [ list ( i ) for i in results ]
# Get missing parts
2024-04-17 22:42:48 +02:00
cursor . execute ( " SELECT * from missing where u_id = ' " + u_id + " ' ; " )
2024-04-16 21:36:34 +02:00
results = cursor . fetchall ( )
missing_list = [ list ( i ) for i in results ]
2024-04-26 16:43:12 +02:00
print ( missing_list )
2024-04-16 21:36:34 +02:00
2024-04-17 22:42:48 +02:00
# Get minifigures
cursor . execute ( " SELECT * from minifigures where set_num = ' " + tmp + " ' and u_id = ' " + u_id + " ' ; " )
results = cursor . fetchall ( )
minifig_list = [ list ( i ) for i in results ]
minifig_inventory_list = [ ]
for i in minifig_list :
cursor . execute ( " SELECT * from inventory where set_num = ' " + i [ 0 ] + " ' and u_id = ' " + u_id + " ' ; " )
results = cursor . fetchall ( )
tmp_inv = [ list ( i ) for i in results ]
minifig_inventory_list . append ( tmp_inv )
2024-04-16 21:36:34 +02:00
cursor . close ( )
conn . close ( )
2024-04-18 11:43:23 +02:00
return render_template ( ' table.html ' , u_id = u_id , tmp = tmp , title = set_list [ 0 ] [ 1 ] , set_list = set_list , inventory_list = inventory_list , missing_list = missing_list , minifig_list = minifig_list , minifig_inventory_list = minifig_inventory_list )
2024-04-16 21:36:34 +02:00
if request . method == ' POST ' :
set_num = request . form . get ( ' set_num ' )
id = request . form . get ( ' id ' )
part_num = request . form . get ( ' part_num ' )
2024-04-26 16:43:12 +02:00
part_img_url_id = request . form . get ( ' part_img_url_id ' )
2024-04-16 21:36:34 +02:00
color_id = request . form . get ( ' color_id ' )
element_id = request . form . get ( ' element_id ' )
u_id = request . form . get ( ' u_id ' )
missing = request . form . get ( ' missing ' )
conn = sqlite3 . connect ( ' app.db ' )
cursor = conn . cursor ( )
# If quantity is not empty
2024-04-16 21:43:30 +02:00
if missing != ' ' and missing != ' 0 ' :
2024-04-16 21:36:34 +02:00
#Check if there's an existing entry
2024-06-19 12:16:30 +02:00
#print('in first')
#print(missing)
#cursor.execute('''SELECT quantity FROM missing
# WHERE set_num = ? AND
# id = ? AND
# part_num = ? AND
# part_img_url_id = ? AND
# color_id = ? AND
# element_id = ? AND
# u_id = ?''',
# (set_num, id, part_num, part_img_url_id, color_id, element_id, u_id))
#
#existing_quantity = cursor.fetchone()
#print("existing" + str(existing_quantity))
#conn.commit()
2024-04-16 21:36:34 +02:00
#If there's an existing entry or if entry isn't the same as the new value
2024-06-19 12:16:30 +02:00
# First, check if a row with the same values for the other columns exists
cursor . execute ( '''
SELECT quantity FROM missing WHERE
set_num = ? AND
id = ? AND
part_num = ? AND
part_img_url_id = ? AND
color_id = ? AND
element_id = ? AND
u_id = ?
''' , (set_num, id, part_num, part_img_url_id, color_id, element_id, u_id))
# Fetch the result
row = cursor . fetchone ( )
if row :
# If a row exists and the missing value is different, update the row
if row [ 0 ] != missing :
cursor . execute ( '''
UPDATE missing SET
quantity = ?
WHERE set_num = ? AND
id = ? AND
part_num = ? AND
part_img_url_id = ? AND
color_id = ? AND
element_id = ? AND
u_id = ?
''' , (missing, set_num, id, part_num, part_img_url_id, color_id, element_id, u_id))
else :
# If no row exists, insert a new row
cursor . execute ( '''
INSERT INTO missing (
2024-04-16 21:36:34 +02:00
set_num ,
id ,
part_num ,
2024-04-26 16:43:12 +02:00
part_img_url_id ,
2024-04-16 21:36:34 +02:00
color_id ,
quantity ,
element_id ,
u_id
2024-06-19 12:16:30 +02:00
) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )
''' , (set_num, id, part_num, part_img_url_id, color_id, missing, element_id, u_id))
2024-04-16 21:36:34 +02:00
conn . commit ( )
2024-06-19 12:16:30 +02:00
# if existing_quantity is None:
# print('in second')
# print(existing_quantity)
# cursor.execute('''INSERT INTO missing (
# set_num,
# id,
# part_num,
# part_img_url_id,
# color_id,
# quantity,
# element_id,
# u_id
# ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''',
# (set_num, id, part_num, part_img_url_id, color_id, missing, element_id, u_id))
#
# conn.commit()
#
# else:
# try:
# if int(existing_quantity[0]) != int(missing):
# print('in third')
# print(existing_quantity)
# cursor.execute('''update missing set (
# set_num,
# id,
# part_num,
# part_img_url_id,
# color_id,
# quantity,
# element_id,
# u_id
# ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''',
# (set_num, id, part_num, part_img_url_id, color_id, missing, element_id, u_id))
#
# conn.commit()
# except:
# pass
2024-04-16 21:36:34 +02:00
# If quantity is empty, delete the entry.
else :
cursor . execute ( ''' DELETE FROM missing
WHERE set_num = ? AND
id = ? AND
part_num = ? AND
2024-04-26 16:43:12 +02:00
part_img_url_id = ? AND
2024-04-16 21:36:34 +02:00
color_id = ? AND
element_id = ? AND
u_id = ? ''' ,
2024-04-26 16:43:12 +02:00
( set_num , id , part_num , part_img_url_id , color_id , element_id , u_id ) )
2024-04-16 21:36:34 +02:00
conn . commit ( )
cursor . close ( )
conn . close ( )
return ( ' ' , 204 )
@app.route ( ' /old ' , methods = [ ' GET ' , ' POST ' ] )
def frontpage ( ) :
2024-03-03 11:15:38 +01:00
pathlist = Path ( ' ./info/ ' ) . rglob ( ' *.json ' )
set_list = [ ]
2024-04-14 15:56:08 +02:00
json_file = { }
theme_file = np . loadtxt ( " themes.csv " , delimiter = " , " , dtype = " str " )
if request . method == ' GET ' :
for path in pathlist :
set_num = re . findall ( r " \ b \ d+(?:- \ d+)? \ b " , str ( path ) ) [ 0 ]
with open ( ' ./static/sets/ ' + set_num + ' /info.json ' ) as info :
info_file = json . loads ( info . read ( ) )
try :
info_file [ ' theme_id ' ] = theme_file [ theme_file [ : , 0 ] == str ( info_file [ ' theme_id ' ] ) ] [ 0 ] [ 1 ]
except Exception as e :
print ( e )
with open ( ' ./info/ ' + set_num + ' .json ' ) as info :
json_file [ set_num ] = json . loads ( info . read ( ) )
set_list . append ( info_file )
return render_template ( ' frontpage.html ' , set_list = set_list , themes_list = theme_file , json_file = json_file )
if request . method == ' POST ' :
set_num = request . form . get ( ' set_num ' )
2024-04-14 22:10:07 +02:00
index = request . form . get ( ' index ' )
2024-04-14 15:56:08 +02:00
minif = request . form . get ( ' minif ' )
scheck = request . form . get ( ' scheck ' )
scol = request . form . get ( ' scol ' )
with open ( ' ./info/ ' + set_num + ' .json ' ) as info :
json_file = json . loads ( info . read ( ) )
if minif != None :
2024-04-14 22:10:07 +02:00
json_file [ ' unit ' ] [ int ( index ) ] [ ' Minifigs Collected ' ] = minif
2024-04-14 15:56:08 +02:00
if scheck != None :
2024-04-14 22:10:07 +02:00
json_file [ ' unit ' ] [ int ( index ) ] [ ' Set Checked ' ] = scheck
2024-04-14 15:56:08 +02:00
if scol != None :
2024-04-14 22:10:07 +02:00
json_file [ ' unit ' ] [ int ( index ) ] [ ' Set Collected ' ] = scol
2024-04-14 15:56:08 +02:00
with open ( ' ./info/ ' + set_num + ' .json ' , ' w ' ) as dump_file :
json . dump ( json_file , dump_file )
return ( ' ' , 204 )
2024-03-03 11:15:38 +01:00
2024-04-16 21:36:34 +02:00
@app.route ( ' /old/<tmp> ' , methods = [ ' GET ' , ' POST ' ] )
2024-03-03 11:15:38 +01:00
def sets ( tmp ) :
2024-03-14 08:18:05 +01:00
2024-03-03 12:43:24 +01:00
with open ( ' ./static/sets/ ' + tmp + ' /info.json ' ) as info :
2024-02-28 21:31:43 +01:00
info_file = json . loads ( info . read ( ) )
2024-04-12 14:46:02 +02:00
with open ( ' ./static/sets/ ' + tmp + ' /minifigs.json ' ) as info :
minifigs_file = json . loads ( info . read ( ) )
2024-03-03 12:43:24 +01:00
with open ( ' ./static/sets/ ' + tmp + ' /inventory.json ' ) as inventory :
2024-02-28 21:31:43 +01:00
inventory_file = json . loads ( inventory . read ( ) )
2024-02-29 13:24:47 +01:00
with open ( ' ./info/ ' + tmp + ' .json ' ) as info :
json_file = json . loads ( info . read ( ) )
2024-03-14 08:18:05 +01:00
if request . method == ' POST ' :
part_num = request . form . get ( ' brickpartpart_num ' )
color = request . form . get ( ' brickcolorname ' )
index = request . form . get ( ' index ' )
number = request . form . get ( ' numberInput ' )
is_spare = request . form . get ( ' is_spare ' )
# print(part_num)
# print(color)
# print(index)
# print(number)
# print(is_spare)
if number is not None :
print ( part_num )
print ( color )
print ( number )
print ( is_spare )
with open ( ' ./info/ ' + tmp + ' .json ' ) as info :
json_file = json . loads ( info . read ( ) )
print ( json_file [ ' count ' ] )
data = ' { " brick " : { " ID " : " ' + part_num + ' " , " is_spare " : " ' + is_spare + ' " , " color_name " : " ' + color + ' " , " amount " : " ' + number + ' " }} '
if len ( json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] ) == 0 :
json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] . append ( json . loads ( data ) )
print ( json_file )
elif number == ' ' :
for idx , i in enumerate ( json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] ) :
if i [ ' brick ' ] [ ' ID ' ] == part_num and i [ ' brick ' ] [ ' is_spare ' ] == is_spare and i [ ' brick ' ] [ ' color_name ' ] == color :
json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] . pop ( idx )
else :
found = False
for idx , i in enumerate ( json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] ) :
if not found and i [ ' brick ' ] [ ' ID ' ] == part_num and i [ ' brick ' ] [ ' is_spare ' ] == is_spare and i [ ' brick ' ] [ ' color_name ' ] == color :
json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] [ idx ] [ ' brick ' ] [ ' amount ' ] = number
found = True
if not found :
json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] . append ( json . loads ( data ) )
with open ( ' ./info/ ' + tmp + ' .json ' , ' w ' ) as dump_file :
json . dump ( json_file , dump_file )
#return Response(status=200)
return ( ' ' , 204 )
else :
return render_template ( ' bootstrap_table.html ' , tmp = tmp , title = info_file [ ' name ' ] ,
2024-04-12 14:46:02 +02:00
info_file = info_file , inventory_file = inventory_file , json_file = json_file , minifigs_file = minifigs_file )
2024-03-14 08:18:05 +01:00
2024-02-28 20:08:16 +01:00
2024-02-29 19:44:28 +01:00
@app.route ( ' /<tmp>/saveNumber ' , methods = [ ' POST ' ] )
def save_number ( tmp ) :
2024-03-14 08:18:05 +01:00
part_num = request . form . get ( ' brickpartpart_num ' )
color = request . form . get ( ' brickcolorname ' )
2024-03-04 15:41:48 +01:00
index = request . form . get ( ' index ' )
2024-02-28 20:08:16 +01:00
number = request . form . get ( ' numberInput ' )
2024-03-02 09:02:37 +01:00
is_spare = request . form . get ( ' is_spare ' )
2024-02-28 20:08:16 +01:00
if number is not None :
2024-02-28 21:31:43 +01:00
2024-03-04 15:41:48 +01:00
print ( part_num )
print ( color )
2024-02-28 21:31:43 +01:00
print ( number )
2024-03-02 09:02:37 +01:00
print ( is_spare )
2024-02-28 21:31:43 +01:00
with open ( ' ./info/ ' + tmp + ' .json ' ) as info :
json_file = json . loads ( info . read ( ) )
2024-03-04 15:41:48 +01:00
data = ' { " brick " : { " ID " : " ' + part_num + ' " , " is_spare " : " ' + is_spare + ' " , " color_name " : " ' + color + ' " , " amount " : " ' + number + ' " }} '
2024-02-29 15:22:55 +01:00
2024-03-04 18:17:19 +01:00
if len ( json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] ) == 0 :
json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] . append ( json . loads ( data ) )
print ( json_file )
2024-03-14 08:18:05 +01:00
elif number == ' ' :
2024-03-04 18:17:19 +01:00
for idx , i in enumerate ( json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] ) :
if i [ ' brick ' ] [ ' ID ' ] == part_num and i [ ' brick ' ] [ ' is_spare ' ] == is_spare and i [ ' brick ' ] [ ' color_name ' ] == color :
2024-03-04 20:10:56 +01:00
json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] . pop ( idx )
2024-03-04 18:17:19 +01:00
else :
found = False
for idx , i in enumerate ( json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] ) :
if not found and i [ ' brick ' ] [ ' ID ' ] == part_num and i [ ' brick ' ] [ ' is_spare ' ] == is_spare and i [ ' brick ' ] [ ' color_name ' ] == color :
json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] [ idx ] [ ' brick ' ] [ ' amount ' ] = number
found = True
if not found :
json_file [ ' unit ' ] [ int ( index ) ] [ ' bricks ' ] [ ' missing ' ] . append ( json . loads ( data ) )
2024-03-04 15:41:48 +01:00
2024-03-04 20:21:11 +01:00
with open ( ' ./info/ ' + tmp + ' .json ' , ' w ' ) as dump_file :
json . dump ( json_file , dump_file )
2024-03-04 15:41:48 +01:00
2024-03-14 08:18:05 +01:00
return Response ( status = 204 )
2024-02-28 20:08:16 +01:00
if __name__ == ' __main__ ' :
2024-06-18 19:27:51 +02:00
socketio . run ( app . run ( host = ' 0.0.0.0 ' , debug = True , port = 3333 ) )