Newer
Older
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
@app.route( baseurl + '/submission/<id>/tenprint/<tid>/segment/<pc>/set/gp', methods = [ 'POST' ] )
@login_required
def submission_segment_set_gp( id, tid, pc ):
gp = request.form.get( "gp" )
sql = "SELECT id FROM gp WHERE name = %s"
r = config.db.query( sql, ( gp, ) ).fetchone()
if r == None:
return jsonify( {
'error': True,
'message': 'General patter not recognized'
} )
gp_id = r[ 'id' ]
sql = "UPDATE files_segments SET gp = %s WHERE tenprint = %s AND pc = %s"
config.db.query( sql, ( gp_id, tid, pc, ) )
config.db.commit()
return jsonify( {
'error': False
} )
################################################################################
# Tenprint templates
@app.route( baseurl + '/template/tenprint/list' )
@admin_required
def template_tenprint_list():
sql = "SELECT id, country_code, name FROM tenprint_cards"
tp_templates = config.db.query( sql ).fetchall()
return render_template(
"tp_template/list.html",
baseurl = baseurl,
js = config.cdnjs,
css = config.cdncss,
session_timeout = config.session_timeout,
tp_templates = tp_templates,
envtype = envtype

Marco De Donno
committed
@app.route( baseurl + '/template/tenprint/new' )
@admin_required
def template_tenprint_new_meta():
return render_template(
"tp_template/new_meta.html",
baseurl = baseurl,
js = config.cdnjs,
css = config.cdncss,
session_timeout = config.session_timeout,
envtype = envtype

Marco De Donno
committed
)
@app.route( baseurl + '/template/tenprint/new/<id>/images' )
@admin_required
def template_tenprint_new_images( id ):
sql = "SELECT id, name, country_code FROM tenprint_cards WHERE id = %s"
card = config.db.query( sql, ( id, ) ).fetchone()
return render_template(
"tp_template/new_images.html",
baseurl = baseurl,
js = config.cdnjs,
css = config.cdncss,
session_timeout = config.session_timeout,
card = card,
envtype = envtype
)

Marco De Donno
committed
@app.route( baseurl + '/template/tenprint/new/insert', methods = [ 'POST' ] )
@admin_required
def template_tenprint_new_do():
name = request.form.get( "name" )
country_code = request.form.get( "country_code" )
sql = "INSERT INTO tenprint_cards ( name, country_code ) VALUES ( %s, %s ) RETURNING id"
q = config.db.query( sql, ( name, country_code, ) )
id = q.fetchone()[ 0 ]

Marco De Donno
committed
for pc in [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 27 ]:
sql = "INSERT INTO tenprint_zones ( card, pc, angle, tl_x, tl_y, br_x, br_y ) VALUES ( %s, %s, %s, %s, %s, %s, %s )"
config.db.query( sql, ( id, pc, 0, 0, 0, 0, 0 ) )

Marco De Donno
committed
config.db.commit()
return jsonify( {
'error': False,
'id': id
} )
@app.route( baseurl + '/template/tenprint/new/<id>/upload_image', methods = [ 'POST' ] )
@admin_required
def template_tenprint_new_uploadimage( id ):
face = request.form.get( "card_face" )
if face in [ "front", "back" ]:
data = request.files[ 'file' ]
img = Image.open( data )
image_width, image_height = img.size

Marco De Donno
committed
try:
res = img.info[ 'dpi' ][ 0 ]
width = round( image_width * 2.54 / float( res ) )
height = round( image_height * 2.54 / float( res ) )
except:
res = 0
width = 0
height = 0
fp = StringIO()
img.save( fp, format = "JPEG" )
fp.seek( 0 )
data = fp.getvalue()
data = base64.b64encode( data )
sql = """
UPDATE tenprint_cards
SET
image_{0} = %s,
image_{0}_width = %s,
image_{0}_height = %s,
image_resolution = %s,
image_format = %s,
width = %s,
height = %s
WHERE id = %s""".format( face )
config.db.query( sql, ( data, image_width, image_height, res, "JPEG", width, height, id, ) )
config.db.commit()

Marco De Donno
committed
if res != 0:
return jsonify( {
'error': False
} )
else:
return jsonify( {
'need_action': True,
'action': "set_resolution"
} )
else:
return abort( 403 )
@app.route( baseurl + '/template/tenprint/<id>/set/resolution', methods = [ 'POST' ] )

Marco De Donno
committed
@admin_required
def template_tenprint_new_setresolution( id ):
res = request.form.get( "resolution" )
try:
sql = "UPDATE tenprint_cards SET image_resolution = %s WHERE id = %s"
config.db.query( sql, ( res, id, ) )
config.db.commit()
return jsonify( {
'error': False
} )

Marco De Donno
committed
except:
return jsonify( {
'error': True
} )
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
def get_tenprint_template_zones( id, t ):
sql = """
SELECT
tenprint_zones.pc, tl_x, tl_y, br_x, br_y, angle, pc.name
FROM tenprint_zones
JOIN tenprint_zones_location ON tenprint_zones.pc = tenprint_zones_location.pc
JOIN pc ON tenprint_zones.pc = pc.id
WHERE
card = %s AND
tenprint_zones_location.side = %s
ORDER BY pc
"""
r = config.db.query( sql, ( id, t, ) ).fetchall()
zones = []
for pc, tl_x, tl_y, br_x, br_y, angle, pc_name in r:
tl_x = float_or_null( tl_x )
tl_y = float_or_null( tl_y )
br_x = float_or_null( br_x )
br_y = float_or_null( br_y )
zones.append( {
"pc": pc,
"tl_x": tl_x,
"tl_y": tl_y,
"br_x": br_x,
"br_y": br_y,
"angle": angle,
"pc_name": pc_name
} )
return zones
@app.route( baseurl + '/template/tenprint/<id>/<t>' )
@login_required
def tp_template( id, t ):
if t in [ 'front', 'back' ]:
sql = """SELECT
tenprint_zones.pc, tl_x, tl_y, br_x, br_y, angle, pc.name
FROM tenprint_zones
JOIN tenprint_zones_location ON tenprint_zones.pc = tenprint_zones_location.pc
JOIN pc ON tenprint_zones.pc = pc.id
WHERE card = %s AND tenprint_zones_location.side = %s ORDER BY pc
"""
r = config.db.query( sql, ( id, t, ) ).fetchall()
zones = []
for pc, tl_x, tl_y, br_x, br_y, angle, pc_name in r:
tl_x = float_or_null( tl_x )
tl_y = float_or_null( tl_y )
br_x = float_or_null( br_x )
br_y = float_or_null( br_y )
zones.append( {
"pc": pc,
"tl_x": tl_x,
"tl_y": tl_y,
"br_x": br_x,
"br_y": br_y,
"angle": angle,
"pc_name": pc_name
datacolumns = [ 'tl_x', 'tl_y', 'br_x', 'br_y', 'angle' ]

Marco De Donno
committed

Marco De Donno
committed
sql = 'SELECT id, name, country_code, width, height, size_display, image_' + t + '_width, image_' + t + '_height, image_resolution FROM tenprint_cards WHERE id = %s LIMIT 1'
r = config.db.query( sql, ( id, ) )
img_info = r.fetchone()
card_info = {
'width': int( round( float( img_info[ 'width' ] ) / 2.54 * img_info[ 'image_resolution' ] ) ),
'height': int( round( float( img_info[ 'height' ] ) / 2.54 * img_info[ 'image_resolution' ] ) ),
}

Marco De Donno
committed
svg_hw_factor = float( img_info[ 'image_' + t + '_width' ] ) / float( img_info[ 'image_' + t + '_height' ] )
"tp_template/template.html",
baseurl = baseurl,
admin = int( session[ 'account_type' ] ) == 1,
js = config.cdnjs,
css = config.cdncss,
session_timeout = config.session_timeout,
account_type = session.get( "account_type", None ),
zones = zones,
img_info = img_info,
card_info = card_info,

Marco De Donno
committed
svg_hw_factor = svg_hw_factor,

Marco De Donno
committed
t = t,
datacolumns = datacolumns,
**config.misc
)
else:
return abort( 403 )
@app.route( baseurl + '/template/tenprint/<id>/set/zones', methods = [ "POST" ] )
@login_required
def update_zone_coordinates( id ):
id = int( id )
data = request.form.get( "data" )

Marco De Donno
committed
if data != None:
data = json.loads( data )
for pc, value in data.iteritems():
pc = int( pc )
for coordinate, v in value.iteritems():
sql = "UPDATE tenprint_zones SET " + coordinate + " = %s WHERE card = %s AND pc = %s"
config.db.query( sql, data )

Marco De Donno
committed
config.db.commit()
return jsonify( {
'error': False
} )
else:
return abort( 403 )
@app.route( baseurl + '/template/tenprint/<id>/delete/zone', methods = [ "POST" ] )
@login_required
def delete_zone_coordinates( id ):
pc = request.form.get( "pc" )
try:
sql = "DELETE FROM tenprint_zones WHERE card = %s AND pc = %s"
config.db.query( sql, ( id, pc, ) )
config.db.commit()
return jsonify( {
'error': False
} )
except:
return jsonify( {
'error': True
} )
################################################################################
# PiAnoS API
@app.route( baseurl + '/pianos_api' )
@admin_required
def pianos_actions():
return render_template(
"PiAnoS/actions.html",
baseurl = baseurl,
js = config.cdnjs,
css = config.cdncss,
session_timeout = config.session_timeout,
envtype = envtype
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
)
@app.route( baseurl + '/pianos_api/add_user/all' )
@admin_required
def pianos_update_all_accounts():
action = do_pianos_update_all_accounts()
if action:
return jsonify( {
'error': False
} )
else:
return jsonify( {
'error': True
} )
def do_pianos_update_all_accounts():
try:
sql = """
SELECT users.username, users.password, account_type.name as g
FROM users
LEFT JOIN account_type ON users.type = account_type.id
"""
for user in config.db.query_fetchall( sql ):
username, h, group_name = user
groupid = config.pianosdb.create_group( group_name )
config.pianosdb.create_user( username = username, hash = h, groupid = groupid )
config.pianosdb.reset_user( username, hash = h )
config.pianosdb.commit()
return True
except:
return False
################################################################################
# Home page
@app.route( baseurl + '/' )
@login_required
return render_template(
"index.html",
baseurl = baseurl,
admin = int( session[ 'account_type' ] ) == 1,
js = config.cdnjs,
css = config.cdncss,
account_type = session.get( "account_type", None ),
session_security_key = session.get( "session_security_key" ),
envtype = envtype
################################################################################
# Main startup
if __name__ == '__main__':
for file in os.listdir( config.keys_folder ):
with open( config.keys_folder + "/" + file, "r" ) as fp:
gpg.import_keys( fp.read() )
app.run( debug = debug, host = "0.0.0.0", threaded = True )