Newer
Older
width = %s,
height = %s
WHERE id = %s""".format( face )
config.db.query( sql, ( data, image_width, image_height, res, "JPEG", width, height, template_id, ) )
config.db.commit()

Marco De Donno
committed
if res != 0:
return jsonify( {

Marco De Donno
committed
} )
else:
return jsonify( {
"need_action": True,
"action": "set_resolution"

Marco De Donno
committed
} )
else:
return abort( 403 )
@app.route( baseurl + "/template/tenprint/<template_id>/set/resolution", methods = [ "POST" ] )

Marco De Donno
committed
@admin_required
def template_tenprint_new_setresolution( template_id ):
"""
Set the resolution for a tenprint template image.
"""

Marco De Donno
committed
res = request.form.get( "resolution" )
try:
sql = "UPDATE tenprint_cards SET image_resolution = %s WHERE id = %s"
config.db.query( sql, ( res, template_id, ) )

Marco De Donno
committed
config.db.commit()
return jsonify( {
} )

Marco De Donno
committed
except:
return jsonify( {

Marco De Donno
committed
} )
def get_tenprint_template_zones( template_id, t ):
"""
Get all the segments zones for a template passed in parameter.
"""
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, ( template_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/<template_id>/<side>" )
def template_tenprint( template_id, side ):
"""
Serve the tenprint template page.
"""
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, ( template_id, side, ) ).fetchall()
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
sql = """
SELECT
id,
name, country_code,
width, height, size_display,
image_{0}_width, image_{0}_height,
image_resolution
FROM tenprint_cards
WHERE id = %s LIMIT 1
r = config.db.query( sql, ( template_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" ] ) ),
svg_h = float( img_info[ "image_{}_height".format( side ) ] )
svg_w = float( img_info[ "image_{}_width".format( side ) ] )
svg_hw_factor = svg_w / svg_h
"tp_template/template.html",
zones = zones,
img_info = img_info,
card_info = card_info,

Marco De Donno
committed
svg_hw_factor = svg_hw_factor,
card_id = template_id,

Marco De Donno
committed
datacolumns = datacolumns,
**config.misc
)
else:
return abort( 403 )
@app.route( baseurl + "/template/tenprint/<template_id>/set/zones", methods = [ "POST" ] )
@login_required
def update_zone_coordinates( template_id ):
"""
Update the segments zones coordinates in the database.
"""
template_id = int( template_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 {} = %s WHERE card = %s AND pc = %s".format( coordinate )
data = ( v, template_id, pc, )
config.db.query( sql, data )

Marco De Donno
committed
config.db.commit()
return jsonify( {

Marco De Donno
committed
} )
else:
return abort( 403 )
@app.route( baseurl + "/template/tenprint/<template_id>/delete/zone", methods = [ "POST" ] )
@login_required
def delete_zone_coordinates( template_id ):
"""
Delete a unused segment zone for a template (for example FPC 25 and 27 on the front-page, ...)
"""
pc = request.form.get( "pc" )
try:
sql = "DELETE FROM tenprint_zones WHERE card = %s AND pc = %s"
config.db.query( sql, ( template_id, pc, ) )
config.db.commit()
return jsonify( {
} )
except:
return jsonify( {
} )
@app.route( baseurl + "/template/tenprint/<template_id>/set/<varname>", methods = [ "POST" ] )

Marco De Donno
committed
@login_required
def update_tptemplate_var( template_id, varname ):
"""
Update the name, country_code or displayed size variable for a tenprint template.
"""

Marco De Donno
committed
if not varname in [ "name", "country_code", "size_display" ]:

Marco De Donno
committed
return jsonify( {
"error": True

Marco De Donno
committed
} )
else:
try:
data = request.form.get( varname )
data = str( data )
sql = "UPDATE tenprint_cards SET {} = %s WHERE id = %s".forat( varname )
config.db.query( sql, ( data, template_id, ) )
config.db.commit()
return jsonify( {
} )
except:
return jsonify( {

Marco De Donno
committed
@app.route( baseurl + "/template/tenprint/<template_id>/set/hw", methods = [ "POST" ] )
@login_required
def update_tptemplate_hw( template_id ):
"""
Set the image size of a template.
"""
try:
h = request.form.get( "height" )
w = request.form.get( "width" )
h = float( h )
w = float( w )
sql = "UPDATE tenprint_cards SET height = %s, width = %s WHERE id = %s"
config.db.query( sql, ( h, w, template_id, ) )
config.db.commit()
return jsonify( {
} )
except:
return jsonify( {
} )
@app.route( baseurl + "/template/tenprint/<template_id>/set/resolution" )
@login_required
def update_tptemplate_res( template_id ):
"""
Update the resolution of the image for a template.
"""
try:
res = request.form.get( "resolution" )
res = float( res )
sql = "UPDATE tenprint_cards SET image_resolution = %s WHERE id = %s"
config.db.query( sql, ( res, template_id, ) )
config.db.commit()
return jsonify( {
} )
except:
return jsonify( {
} )
################################################################################
# PiAnoS API
@admin_required
def pianos_actions():
"""
Serve the page with all actions related to the dedicated PiAnoS server.
"""
return my_render_template( "PiAnoS/actions.html" )
@app.route( baseurl + "/pianos_api/add_user/all" )
@admin_required
def pianos_update_all_accounts():
"""
serve the function to update the users in PiAnoS
"""
return jsonify( {
"error": not do_pianos_update_all_accounts()
def do_pianos_update_all_accounts():
"""
Copy/update the credentials for all users.
This function keep the credentials in sync between ICNML and PiAnoS.
"""
try:
sql = """
SELECT users.username, users.password, account_type.name as g
FROM users
LEFT JOIN account_type ON users.type = account_type.id
WHERE users.password IS NOT NULL
"""
for user in config.db.query_fetchall( sql ):
username, h, group_name = user
groupid = config.pianosdb.create_group( group_name )

Marco De Donno
committed
pianos_user_id = config.pianosdb.create_user( username = username, hash = h, groupid = groupid )
config.pianosdb.reset_user( username, hash = h )

Marco De Donno
committed
config.pianosdb.create_folder( "{}'s folder".format( username ), pianos_user_id, None, pianos_user_id )
@app.route( baseurl + "/pianos_api/add_segments/all" )
@admin_required
def pianos_copy_all_segments():
"""
Route to push all segments to PiAnoS.
"""
"error": not do_pianos_copy_all_segments()
} )
def do_pianos_copy_all_segments():
"""
Copy all segments images to PiAnoS. If the case already exists, the image is not pushed to PiAnoS.
"""
try:
folder_id = config.pianosdb.create_folder( "Annotation" )
img = Image.new( "L", ( 200, 200 ), 255 )
empty_img_res = 500
empty_img_id = config.pianosdb.create_image( "PRINT", img, empty_img_res, "empty" )
sql = """
SELECT files_segments.uuid, files_segments.data, files_v.resolution
FROM files_segments
LEFT JOIN files_v ON files_segments.tenprint = files_v.uuid
"""
for segment in config.db.query_fetchall( sql ):
img = str2img( segment[ "data" ] )
try:
config.pianosdb.create_exercise(
folder_id,
segment[ "uuid" ], "",
img, segment[ "resolution" ],
empty_img_id, empty_img_res
)
except caseExistsInDB:
continue
except:
raise
config.pianosdb.commit()
return True
except:
return False
################################################################################
# Home page
@login_required
"""
Serve the homepage to all users.
"""
return my_render_template( "index.html" )
################################################################################
gpg = gnupg.GPG( **config.gpg_options )
for key_file in os.listdir( config.keys_folder ):
with open( config.keys_folder + "/" + key_file, "r" ) as fp:

Marco De Donno
committed
account_type_id_name = {}
sql = "SELECT id, name FROM account_type"
for at in config.db.query_fetchall( sql ):
account_type_id_name[ at[ "id" ] ] = at[ "name" ]