Skip to content
Snippets Groups Projects
__init__.py 6.95 KiB
Newer Older
#!/usr/bin/python
# -*- coding: UTF-8 -*-

from _collections import defaultdict
from flask import Blueprint
from flask import session, request, jsonify, current_app
import config
from const import pfsp
from utils.decorator import trainer_has_access
from utils.template import my_render_template
from MDmisc.RecursiveDefaultDict import edefaultdict, defDict

trainer_view = Blueprint( "trainer", __name__, template_folder = "templates" )

@trainer_view.route( "/marks/search" )
    """
        Provide the search page.
    """
            files.id,
            files.uuid,
            mark_info.pfsp,
            mark_info.detection_technic,
            mark_info.surface,
            submissions.uuid AS submission_uuid,
        FROM files
        LEFT JOIN mark_info ON mark_info.uuid = files.uuid
        LEFT JOIN submissions ON files.folder = submissions.id
        LEFT JOIN users ON submissions.donor_id = users.id
        WHERE ( files.type = 3 OR files.type = 4 )
        ORDER BY files.id ASC
    """
    marks = config.db.query_fetchall( sql )
    
    sql = """
        SELECT
            submissions.uuid AS submissions_uuid,
            segments_locations.fpc,
            segments_locations.tenprint_id
        
        FROM segments_locations
        INNER JOIN files ON segments_locations.tenprint_id = files.uuid
        LEFT JOIN submissions ON files.folder = submissions.id
        ORDER BY fpc
    """
    ref_list = config.db.query_fetchall( sql )
    refs = defDict()
    for ref in ref_list:
        refs[ ref[ "submissions_uuid" ] ][ ref[ "fpc" ] ] = ref[ "tenprint_id" ]
    
    all_detection_technics = config.db.query_fetchall( "SELECT * FROM detection_technics ORDER BY name ASC" )
    surfaces = config.db.query_fetchall( "SELECT * FROM surfaces ORDER BY name ASC" )
    
    for _, v in enumerate( marks ):
        for col in [ "detection_technic", "surface", "note" ]:
            for old, new in [ ( "{", "" ), ( "}", "" ), ( "\n", "; " ) ]:
                    v[ col ] = v[ col ].replace( old, new )
        
        v[ "username" ] = v[ "username" ].replace( "_", " " )

    # Current working folder
    current_exercise_folder = session.get( "current_exercise_folder", "" )

    # PFSP zones to fpc
    pfsp2fpc = defaultdict( list )
    for pfc in xrange( 1, 11 ):
        for loc in [ "tip", "distal" ]:
            pfsp2fpc[ "F{}-{}".format( pfc, loc ) ].append( pfc )

    pfsp2fpc[ "F1-tip" ].append( 11 )
    pfsp2fpc[ "F1-distal" ].append( 11 )
    pfsp2fpc[ "F6-tip" ].append( 12 )
    pfsp2fpc[ "F6-distal" ].append( 12 )

    for side, fpc in [ ( "Right", 25 ), ( "Left", 27 ) ]:
        for z in [ "grasp", "carpal_delta_area", "wrist_bracelet", "thenar", "hypothenar", "interdigital", "writer_palm" ]:
            pfsp2fpc[ "{}-{}".format( side, z ) ].append( fpc )

    pfsp2fpc[ "Right-writer_palm" ].append( 22 )
    pfsp2fpc[ "Left-writer_palm" ].append( 24 )
    
    for laterality in [ "right", "left" ]:
        for finger in [ "thumb", "index", "middel", "ring", "little" ]:
            for zone in [ "tip", "distal", "medial", "proximal" ]:
                pfspdesc[ "F{}-{}".format( n, zone ) ] = "{} {} {}".format( laterality, finger, zone )
            n += 1

        "trainer/search.html",
        marks = marks,
        current_exercise_folder = current_exercise_folder,
        all_detection_technics = all_detection_technics,
        surfaces = surfaces,
        all_pfsp = pfsp.zones
@trainer_view.route( "/exercises/list" )
    if session[ "account_type_name" ] == "Administrator":
        sql = """
            SELECT
                exercises.*,
                users.username
            FROM exercises
            LEFT JOIN users ON exercises.trainer_id = users.id
            ORDER BY name ASC
        """
        data = ()
    
    else:
        sql = "SELECT * FROM exercises WHERE trainer_id = %s AND active = true ORDER BY name ASC"
        data = ( session[ "user_id" ], )
    
    ex_list = config.db.query_fetchall( sql, data )
@trainer_view.route( "/exercises/new", methods = [ "POST" ] )
    try:
        name = request.form.get( "name" )
        uuid = str( uuid4() )
        trainer_id = session[ "user_id" ]
        sql = sql_insert_generate( "exercises", [ "name", "uuid", "trainer_id" ], "id" )
        config.db.query_fetchone( sql, ( name, uuid, trainer_id, ) )
        config.db.commit()
        return jsonify( {
            "error": False,
            "data": uuid
        } )
    
    except:
        return jsonify( {
            "error": True,
            "msg": "Error while creating the exercises list"
        } )
@trainer_view.route( "/exercises/rename", methods = [ "POST" ] )
def exercises_rename():
    try:
        new_name = request.form.get( "name" )
        uuid = request.form.get( "uuid" )
        
        sql = "UPDATE exercises SET name = %s WHERE uuid = %s AND trainer_id = %s"
        config.db.query( sql, ( new_name, uuid, session[ "user_id" ], ) )
        config.db.commit()
        
        return jsonify( {
            "error": False
        } )

    except:
        return jsonify( {
            "error": True
        } )

@trainer_view.route( "/exercises/add_to_list", methods = [ "POST" ] )
@trainer_has_access
def add_mark_to_list():
    try:
        folder = session[ "current_exercise_folder" ]
    except:
        try:
            sql = "SELECT uuid FROM exercises WHERE trainer_id = %s ORDER BY creationtime"
            folder = config.db.query_fetchone( sql, ( session[ "user_id" ], ) )[ "uuid" ]
        except:
            sql = sql_insert_generate( "exercises", [ "uuid", "trainer_id", "name" ], "uuid" )
            data = ( str( uuid4() ), session[ "user_id" ], "no name", )
            folder = config.db.query_fetchone( sql, data )[ "uuid" ]

    try:
        mark = request.form.get( "mark", False )
        current_app.logger.info( "Add {} to {}".format( mark, folder ) )
        
        sql = "SELECT count(*) FROM exercises_folder WHERE mark = %s AND folder = %s"
        c = config.db.query_fetchone( sql, ( mark, folder, ) )[ "count" ]
        if c == 0:
            sql = sql_insert_generate( "exercises_folder", [ "mark", "folder" ], "id" )
            r = config.db.query_fetchone( sql, ( mark, folder, ) )[ "id" ]
        
        config.db.commit()
        
        return jsonify( {
            "error": False
        } )
    
    except:
        return jsonify( {
            "error": True
        } )