Skip to content
module.py 99.5 KiB
Newer Older
    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, id, ) )
        config.db.commit()
        
        return jsonify( {
            "error": False
            "error": True
@app.route( baseurl + "/template/tenprint/<id>/set/resolution" )
@login_required
def update_tptemplate_res( 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, id, ) )
        config.db.commit()
        
        return jsonify( {
            "error": False
            "error": True
################################################################################
#    PiAnoS API

@app.route( baseurl + "/pianos_api" )
@admin_required
def pianos_actions():
    """
        Serve the page with all actions related to the dedicated PiAnoS server.
    """
    return render_template( 
        "PiAnoS/actions.html",
        baseurl = baseurl,
        js = config.cdnjs,
        css = config.cdncss,
        session_timeout = config.session_timeout,
        envtype = envtype
@app.route( baseurl + "/pianos_api/add_user/all" )
@admin_required
def pianos_update_all_accounts():
    """
        serve the function to update the users in PiAnoS
    """
        "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
        """
        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()
@app.route( baseurl + "/pianos_api/add_segments/all" )
@admin_required
def pianos_copy_all_segments():
    """
        Route to push all segments to PiAnoS.
    """
    return jsonify( {
        "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" ] )
            print segment[ "resolution" ]
            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()
################################################################################
#    Home page

@app.route( baseurl + "/" )
def home():
    """
        Serve the homepage to all users.
    """
    return render_template( 
        "index.html",
        baseurl = baseurl,
        js = config.cdnjs,
        css = config.cdncss,
Marco De Donno's avatar
Marco De Donno committed
        session_timeout = config.session_timeout,
        account_type = session.get( "account_type", None ),
        session_security_key = session.get( "session_security_key" ),
        envtype = envtype
################################################################################
#    Main application configuration
gpg = gnupg.GPG( **config.gpg_options )
 
for file in os.listdir( config.keys_folder ):
    with open( config.keys_folder + "/" + file, "r" ) as fp:
        gpg.import_keys( fp.read() )