def process_delete_file_form(global_config, form):
    global_config['logger'].debug( 'Process Attribute Delete Form' )

    data_filename = form[attr_delete_file_label].value
    if form[attr_remove_file_processed_label].value == 'Yes':
        remove_from_processed_files = True
    else:
        remove_from_processed_files = False
    
    # Initialize the database session connection
    db_name  = global_config['db_name'] + global_config['this_season']
    session  = DbSession.open_db_session(db_name)
    
    attrdef_filename = WebCommonUtils.get_attrdef_filename(short_comp=global_config['this_competition'])
    if attrdef_filename is not None:
        attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
        attr_definitions.parse(attrdef_filename)

        try:
            ProcessFiles.remove_file_data(global_config, session, attr_definitions, \
                                          data_filename, remove_from_processed_files)
            result = 'Scouting Data File %s Attributes Successfully Removed' % (data_filename)
            session.commit()
        except ValueError as reason:   
            result = 'Error Removing Scouting Data File %s: %s' % (data_filename, reason)
    
    session.remove()
    return result
def recalculate_scoring(global_config, competition=None, attr_definitions=None):
    
    if competition is None:
        competition = global_config['this_competition'] + global_config['this_season']
        if competition == None or competition == '':
            raise Exception( 'Competition Not Specified!')

    # Build the attribute definition dictionary from the definitions csv file
    if global_config['attr_definitions'] == None:
        return
    
    if attr_definitions is None:
        attrdef_filename = WebCommonUtils.get_attrdef_filename(competition)
        if attrdef_filename is not None:
            attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
            attr_definitions.parse(attrdef_filename)
        else:
            return

    session = DbSession.open_db_session(global_config['db_name'] + global_config['this_season'])
    team_rankings = getTeamsInRankOrder(session, competition)
    for team_entry in team_rankings:
        score = calculateTeamScore(session, team_entry.team, competition, attr_definitions)
        setTeamScore(session, team_entry.team, competition, score)
    session.commit()
    dump_database_as_csv_file(session, global_config, attr_definitions, competition)
    session.remove()
def process_delete_attr_form(global_config, form):
    global_config['logger'].debug( 'Process Attribute Delete Form' )

    season = form[attr_delete_season_label].value
    comp = form[attr_delete_comp_label].value
    team = form[attr_delete_team_number_label].value
    attr_name = form[attr_delete_attribute_name_label].value
    old_value = form[attr_delete_old_value_label].value
    
    # Initialize the database session connection
    db_name  = global_config['db_name'] + global_config['this_season']
    session  = DbSession.open_db_session(db_name)
    
    attrdef_filename = WebCommonUtils.get_attrdef_filename(short_comp=comp)
    if attrdef_filename is not None:
        attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
        attr_definitions.parse(attrdef_filename)
        attr_def = attr_definitions.get_definition(attr_name)

        try:
            DataModel.deleteAttributeValue(session, team, comp+season, attr_name, old_value, attr_def)
            result = 'Scouting Data Attribute Value %s Successfully Removed From %s' % (old_value,attr_name)
            session.commit()
        except ValueError as reason:   
            result = 'Error Removing Scouting Data Attribute Value %s From %s: %s' % (old_value,attr_name,reason)
                
    session.remove()
    return result
def process_form(global_config, form):
    global_config['logger'].debug( 'Process Attribute Modify Form' )

    season = form[attr_modify_season_label].value
    comp = form[attr_modify_comp_label].value
    team = form[attr_modify_team_number_label].value
    attr_name = form[attr_modify_attribute_name_label].value
    old_value = form[attr_modify_old_value_label].value
    new_value = form[attr_modify_new_value_label].value
    
    # Initialize the database session connection
    db_name  = global_config['db_name'] + global_config['this_season']
    session  = DbSession.open_db_session(db_name)
    
    attrdef_filename = WebCommonUtils.get_attrdef_filename(short_comp=comp)
    if attrdef_filename is not None:
        attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
        attr_definitions.parse(attrdef_filename)
        attr_def = attr_definitions.get_definition(attr_name)

        try:
            DataModel.modifyAttributeValue(session, team, comp+season, attr_name, old_value, new_value, attr_def)
            result = 'Attribute %s Modified From %s to %s For Team %s' % (attr_name,old_value,new_value,team)
            session.commit()
        except ValueError as reason:   
            result = 'Error Modifying Scouting Addribute %s For Team %s: %s' % (attr_name,team,reason)
    
    session.remove()
    return result
def get_team_score_breakdown_json(global_config, name, comp=None, store_json_file=False):
        
    global_config['logger'].debug( 'GET Team Score Breakdown: %s', name )
    
    if comp == None:
        comp = global_config['this_competition'] + global_config['this_season']
        season = global_config['this_season']
    else:
        season = WebCommonUtils.map_comp_to_season(comp)
    session = DbSession.open_db_session(global_config['db_name'] + season)
    
    attrdef_filename = WebCommonUtils.get_attrdef_filename(comp=comp)
    attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
    attr_definitions.parse(attrdef_filename)
    
    result = []

    result.append('{ "score_breakdown": [\n')

    team_attributes = DataModel.getTeamAttributesInOrder(session, name, comp)
    for attribute in team_attributes:
        attr_def = attr_definitions.get_definition( attribute.attr_name )
        if attr_def:
            try:
                stat_type = attr_def['Statistic_Type']
            except:
                stat_type = 'Total'

            weight = int(float(attr_def['Weight']))
            if weight != 0:
                if stat_type == 'Average':
                    value = int(attribute.cumulative_value/attribute.num_occurs)
                else:
                    value = int(attribute.cumulative_value)
                data_str = '{"attr_name": "%s", "raw_score": %d, "weighted_score": %d}' % (attribute.attr_name,int(value),int(weight*value)) 
                result.append(data_str)
                result.append(',\n')
    if len(team_attributes) > 0:
        result = result[:-1]
        result.append('\n')
    result.append(']}')
    
    json_str = ''.join(result)
    
    if store_json_file is True:
        try:
            FileSync.put( global_config, '%s/EventData/TeamData/team%s_scouting_scorebreakdown.json' % (comp,name), 'text', json_str)
        except:
            raise
        
    session.remove()
    return json_str
def process_attr_def_form(global_config, form):
    global_config['logger'].debug( 'Process Attribute Definitions Form' )
    
    attrdef_filename = WebCommonUtils.get_attrdef_filename(global_config['this_competition'])
    if attrdef_filename is not None:
        attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
        attr_definitions.parse(attrdef_filename)
        attr_dict = attr_definitions.get_definitions()

        for key, attr_def in sorted(attr_dict.items()):
            attr_def['Weight'] = form[key].value
                            
        attr_definitions.write_attr_overrides();
        competition = global_config['this_competition'] + global_config['this_season']
        if competition == None:
            raise Exception( 'Competition Not Specified!')
            
        DataModel.recalculate_scoring(global_config, competition, attr_definitions)
def get_team_attributes_page(global_config):
        
    global_config['logger'].debug( 'GET Team Attributes' )
    
    session = DbSession.open_db_session(global_config['db_name'] + global_config['this_season'])
    comp = global_config['this_competition'] + global_config['this_season']

    attrdef_filename = WebCommonUtils.get_attrdef_filename(comp=comp)
    attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
    attr_definitions.parse(attrdef_filename)
    
    web.header('Content-Type', 'application/json')
    result = []
    result.append('{ "attributes": [\n')
    team_rankings = DataModel.getTeamsInRankOrder(session, comp)
    for team_entry in team_rankings:
        result.append("{ 'Team': " + str(team_entry.team))
        result.append(", 'Score': " + '%.2f' % team_entry.score )
        team_attributes = DataModel.getTeamAttributesInOrder(session, team_entry.team, comp)
        for attribute in team_attributes:
            attr_def = attr_definitions.get_definition( attribute.attr_name )
            if attr_def:
                weight = int(float(attr_def['Weight']))
                if weight != 0:
                    result.append( ", '" + attribute.attr_name + "': ")
                    if ( attr_def['Statistic_Type'] == 'Total'):
                        #result.append( str(attribute.cumulative_value) )
                        result.append( DataModel.mapValueToString(attribute.cumulative_value, attribute.all_values, attr_def, True) )
                    elif ( attr_def['Statistic_Type'] == 'Average'):
                        #result.append( str(attribute.avg_value) )
                        result.append( DataModel.mapValueToString(attribute.avg_value, attribute.all_values, attr_def, True) )
                    else:
                        #result.append( str(attribute.attr_value) )
                        result.append( DataModel.mapValueToString(attribute.attr_value, attribute.all_values, attr_def, True) )
                    
        result.append(' }')
        result.append(',\n')
    if len(team_rankings) > 0:
        result = result[:-1]
        result.append('\n')
    result.append(']}')
    session.remove()
    return ''.join(result)
def get_attr_def_form(global_config):
    global_config['logger'].debug( 'GET Attribute Definitions Form' )

    form = attrdef_form()
    
    attrdef_filename = WebCommonUtils.get_attrdef_filename(global_config['this_competition'])
    if attrdef_filename is not None:
        attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
        attr_definitions.parse(attrdef_filename)
        attr_dict = attr_definitions.get_definitions()
                                            
        for key, attr_def in sorted(attr_dict.items()):
            try:
                weight = float(attr_def['Weight'])
            except:
                weight = 0.0
            form[key].value = str(int(weight))
                
    return form
def get_team_attr_rankings_page(global_config, comp, attr_name):
        
    global_config['logger'].debug( 'GET Team Attribute Rankings' )
    
    session = DbSession.open_db_session(global_config['db_name'] + global_config['this_season'])
        
    attrdef_filename = WebCommonUtils.get_attrdef_filename(comp=comp)
    attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
    attr_definitions.parse(attrdef_filename)
    attr = attr_definitions.get_definition(attr_name)
    try:
        stat_type = attr['Statistic_Type']
    except:
        stat_type = 'Total'

    web.header('Content-Type', 'application/json')
    result = []
    result.append('{ "rankings": [\n')
            
    if stat_type == 'Average':
        team_rankings = DataModel.getTeamAttributesInAverageRankOrder(session, comp, attr_name, False)        
    else:
        team_rankings = DataModel.getTeamAttributesInRankOrder(session, comp, attr_name, False)

    for team in team_rankings:
        if stat_type == 'Average':
            value = int(team.cumulative_value/team.num_occurs)
        else:
            value = int(team.cumulative_value)
        data_str = '{ "team": %d, "value": %d }' % (team.team,value)
        result.append(data_str)
        result.append(',\n')
    if len(team_rankings) > 0:
        result = result[:-1]
        result.append('\n')
    result.append(']}')
    session.remove()
    return ''.join(result)
def get_team_attr_rankings_json(global_config, comp=None, attr_name=None):
        
    global_config['logger'].debug( 'GET Team Attribute Rankings Json' )
    store_data_to_file = False
    
    if comp == None:
        comp = global_config['this_competition'] + global_config['this_season']
        season = global_config['this_season']
    else:
        season = WebCommonUtils.map_comp_to_season(comp)

    session = DbSession.open_db_session(global_config['db_name'] + season)

    attrdef_filename = WebCommonUtils.get_attrdef_filename(comp=comp)
    attr_definitions = AttributeDefinitions.AttrDefinitions()
    attr_definitions.parse(attrdef_filename)
    attr_def = attr_definitions.get_definition(attr_name)
    try:
        stat_type = attr_def['Statistic_Type']
    except:
        stat_type = 'Total'
        
    web.header('Content-Type', 'application/json')
    result = []
    result.append('{  "attr_name" : "%s",\n' % attr_name)
    
    # add the columns bases on the attribute definition type
    result.append('   "columns" : [\n')
    result.append('      { "sTitle": "Team" }')
    result.append(',\n')
    columns = []
    if attr_def['Type'] == 'Map_Integer':
        map_values = attr_def['Map_Values'].split(':')
        for map_value in map_values:
            item_name = map_value.split('=')[0]
            columns.append(item_name)
            result.append('      { "sTitle": "%s" }' % item_name)
            result.append(',\n')
        result = result[:-1]
        result.append('\n')
        result.append('   ],\n')

    if stat_type == 'Average':
        team_rankings = DataModel.getTeamAttributesInAverageRankOrder(session, comp, attr_name)        
    else:
        team_rankings = DataModel.getTeamAttributesInRankOrder(session, comp, attr_name)

    result.append('   "rankings" : [\n')
    for team_attr in team_rankings:
        data_str = '      [ %d,' % team_attr.team
        value_dict = DataModel.mapAllValuesToDict(attr_def, team_attr.all_values)
        for column in columns:
            try:
                value = value_dict[column]
            except:
                value = 0
            data_str += ' %d,' % value
        data_str = data_str.rstrip(',')
        data_str += ' ]'
        result.append(data_str)
        result.append(',\n')

    if len(team_rankings) > 0:
        result = result[:-1]
    result.append('\n')
    result.append('   ]\n}')    
    
    json_str = ''.join(result)
    
    if store_data_to_file is True:
        try:
            file_name = 'attrrankings_%s' % attr_name
            FileSync.put( global_config, '%s/EventData/%s.json' % (comp,file_name), 'text', json_str)
        except:
            raise
        
    session.remove()
    return json_str
def get_team_rankings_json(global_config, season, event, attr_filters=[], filter_name=None, 
                           thumbnails = False, store_json_file=False):
        
    global_config['logger'].debug( 'GET Team Rankings Json' )
    store_data_to_file = False
    
    comp = WebCommonUtils.map_event_code_to_comp(event, season)

    session = DbSession.open_db_session(global_config['db_name'] + season)

    result = []
    result.append('{ "rankings": [\n')
    rank_added = False
    
    if len(attr_filters) == 0:
        team_rankings = DataModel.getTeamsInRankOrder(session, comp, False)
        for team in team_rankings:
            # round the score to an integer value
            team.score = float(int(team.score))
            if team.score > 0:
                thumbnails_snippet = ''
                if thumbnails:
                    thumbnails_snippet = ',\n' + get_team_scouting_thumbnails_json_snippet(global_config, comp, str(team.team))
                
                result.append( '  { "score": %0.1f, "competition": "%s", "team": %d%s }' % (team.score, comp, team.team,thumbnails_snippet))
                result.append(',\n')
                rank_added = True
    else:
        # we'll need the attribute definitions in order to retrieve the correct attribute value
        # and attribute weighting
        attrdef_filename = WebCommonUtils.get_attrdef_filename(comp=comp)
        attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
        attr_definitions.parse(attrdef_filename)
        
        team_rank_dict = dict()
        for attr_filter in attr_filters:
            try:
                attr_name, attr_value = attr_filter.split('=')
            except:
                attr_name = attr_filter
                attr_value = None
            
            attr_def = attr_definitions.get_definition(attr_name)
            
            if attr_value is None:
                team_rankings = DataModel.getTeamAttributesInRankOrder(session, comp, attr_name, False)
                
                for team in team_rankings:
                    try:
                        stat_type = attr_def['Statistic_Type']
                    except:
                        stat_type = 'Total'
        
                    weight = int(float(attr_def['Weight']))
                    if stat_type == 'Average':
                        score = int(team.cumulative_value/team.num_occurs*weight)
                    else:
                        score = int(team.cumulative_value*weight)
                                                    
                    try:       
                        team_rank_dict[team.team] += score
                    except:
                        team_rank_dict[team.team] = score
            else:
                team_rankings = DataModel.getTeamAttributesWithValue(session, comp, attr_name, attr_value, False)
                
                for team in team_rankings:
                    score = team.all_values.count(attr_value)
                    try:       
                        team_rank_dict[team.team] += score
                    except:
                        team_rank_dict[team.team] = score
                
                    
        sorted_team_rank = sorted(team_rank_dict.items(), key=operator.itemgetter(1))
        for team, score in sorted_team_rank:
            # round the score to an integer value
            score = float(int(score))
            if score > 0:
                thumbnails_snippet = ''
                if thumbnails:
                    thumbnails_snippet = ',\n' + get_team_scouting_thumbnails_json_snippet(global_config, comp, str(team))
                
                result.append( '  { "score": %0.1f, "competition": "%s", "team": %d%s }' % (score, comp, team, thumbnails_snippet))
                result.append(',\n')
                rank_added = True
        
    if rank_added == True:
        result = result[:-1]

    result.append(']}')
    
    json_str = ''.join(result)
    
    if store_json_file is True:
        try:
            if filter_name is None:
                file_name = 'scoutingrankings'
            else:
                file_name = 'scoutingrankings_%s' % filter_name
            FileSync.put( global_config, '%s/EventData/%s.json' % (comp,file_name), 'text', json_str)
        except:
            raise
        
    session.remove()
    return json_str
def get_team_datafiles_page(global_config, name, display_notes=True):
    
    global_config['logger'].debug( 'GET Team Data Files: %s', name )

    if global_config['attr_definitions'] == None:
        return None
    
    session = DbSession.open_db_session(global_config['db_name'] + global_config['this_season'])

    page=''
    
    team_info = DataModel.getTeamInfo(session, int(name))
    if team_info:
        page += '<h3>Team Info</h3>'
        page += '<li>Team Nickname: ' + team_info.nickname + '</li>'
        page += '<li>Affiliation: ' + team_info.fullname + '</li>'
        page += '<li>Location: ' + team_info.location + '</li>'
        page += '<li>Rookie Season: ' + str(team_info.rookie_season) + '</li>'
        page += '<li>Website: <a href="' + team_info.website + '">' + team_info.website + '</a></li>'
        page += '<br>'
     
    competitions = []
    this_comp = global_config['this_competition']
    season = global_config['this_season']
    competitions.append(this_comp+season)
    competitions_str = global_config['other_competitions']
    competitions_str = competitions_str.replace(this_comp,'')
    if competitions_str.count(',') > 0:
        other_comps = competitions_str.split(',')
        for other_comp in other_comps:
            if other_comp != '':
                competitions.append(other_comp+season)
    elif competitions_str != '':
        competitions.append(competitions_str+season)
        
    for comp in competitions:
        if comp != '':

            attrdef_filename = WebCommonUtils.get_attrdef_filename(comp=comp)
            attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
            attr_definitions.parse(attrdef_filename)

            input_dir = './static/data/' + comp + '/ScoutingData/'
            pattern = 'Team' + name + '_' + '[a-zA-Z0-9_]*.txt'
            datafiles = get_datafiles(input_dir, re.compile(pattern), False, global_config['logger'])
            
            input_dir = './static/data/' + comp + '/ScoutingPictures/'
            pattern = 'Team' + name + '_' + '[a-zA-Z0-9_]*.jpg|mp4'
            mediafiles = get_datafiles(input_dir, re.compile(pattern), False, global_config['logger'])
                        
            if len(datafiles) == 0 and len(mediafiles) == 0:
                continue
            
            page += '<hr>'
            page += '<h3> ' + comp + '</h3>'

            team_attributes = DataModel.getTeamAttributesInOrder(session, name, comp)
            if len(team_attributes) > 0:
                page += '<ul>'
                page += '<h3>Scouting Data Summary:</h3>'
                
                page += '<ul>'
                page += '<table border="1" cellspacing="5">'
                page += '<tr>'
                page += '<th>Attribute Name</th>'
                page += '<th>Matches</th>'
                page += '<th>Cumulative Value</th>'
                page += '<th>Average Value</th>'
                #page += '<th>Last Value</th>'
                page += '<th>All Values</th>'
                page += '</tr>'
    
                for attribute in team_attributes:
                    attr_def = attr_definitions.get_definition( attribute.attr_name )
                    include_attr = False
                    if attr_def:
                        if attr_def.has_key('Include_In_Team_Display') \
                                and attr_def['Include_In_Team_Display'] == 'Yes':
                                include_attr = True
                        elif attr_def.has_key('Include_In_Report') \
                                and attr_def['Include_In_Report'] == 'Yes':
                            include_attr = True
                        elif attr_def.has_key('Weight') \
                                and attr_def['Weight'] != '0':
                            include_attr = True

                    if include_attr == True:   
                        page += '<tr>'
                        if attr_def.has_key('Display_Name'):
                            page += '<td>%s</td>' % attr_def['Display_Name']
                        else:
                            page += '<td>%s</td>' % attr_def['Name']
                            
                        page += '<td>%s</td>' % str(attribute.num_occurs)
                        page += '<td>%s</td>' % str(attribute.cumulative_value)
                        page += '<td>%0.2f</td>' % (attribute.avg_value)
                        #page += '<td>%s</td>' % str(attribute.attr_value)
                        page += '<td>%s</td>' % attribute.all_values
                        page += '</tr>'
                        
                page += '</table>'    
                page += '</ul>'
                page += '</ul>'

            if len(datafiles) > 0:         
                page += '<ul>'
                page += '<h3>Pit and Match Data:</h3>'
                page += '<ul>'
                for filename in datafiles:
                    segments = filename.split('/')
                    basefile = segments[-1]
                    # the following line inserts a hyperlink to the file itself, the second line
                    # inserts a hyperlink to a url that allows the webserver to create a nicer display of
                    # the file contents
                    #page += '<li><a href="' + filename.lstrip('.') + '">' + basefile + '</a></li>'
                    page += '<li><a href="' + '/ScoutingData/' + comp + '/' + basefile + '">' + basefile + '</a></li>'
                page += '</ul>'

            if len(mediafiles) > 0:         
                page += '<h3>Pictures and Videos:</h3>'
                page += '<ul>'
                for filename in mediafiles:
                    segments = filename.split('/')
                    basefile = segments[-1]
                    page += '<li><a href="' + filename.lstrip('.') + '">' + basefile + '</a></li>'
                page += '</ul>'
            page += '</ul>'

    if display_notes == True:        
        page += '<hr>'
        page += '<h3> Notes for Team ' + name + '</h3>'
        page += '<ul>'
            
        comp = global_config['this_competition'] + global_config['this_season']
        
        team_notes = DataModel.getTeamNotes(session, name, comp)
        for note in team_notes:
            page += '<li>' + note.data + '</li>'
        
        page += '</ul>'
    
    session.remove()
    return page
def get_team_scouting_data_summary_json(global_config, comp, name, attr_filter=[], filter_name=None, store_json_file=False):
    
    global_config['logger'].debug( 'GET Team %s Scouting Data For Competition %s', name, comp )
    
    season = WebCommonUtils.map_comp_to_season(comp)
    session = DbSession.open_db_session(global_config['db_name'] + season)

    if global_config['attr_definitions'] == None:
        return None
    
    attrdef_filename = WebCommonUtils.get_attrdef_filename(comp=comp)
    attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
    attr_definitions.parse(attrdef_filename)

    result = []

    result.append('{ "competition" : "%s", "team" : "%s",\n' % (comp,name))
    result.append('  "scouting_data_summary" : [\n')

    team_attributes = DataModel.getTeamAttributesInOrder(session, name, comp)
    if len(team_attributes) > 0:
        some_attr_added = False
        for attribute in team_attributes:
            attr_def = attr_definitions.get_definition( attribute.attr_name )
            include_attr = False
            if attr_def:
                if attr_def.has_key('Include_In_Team_Display') \
                        and attr_def['Include_In_Team_Display'] == 'Yes':
                        include_attr = True
                elif attr_def.has_key('Include_In_Report') \
                        and attr_def['Include_In_Report'] == 'Yes':
                    include_attr = True
                elif attr_def.has_key('Weight') \
                        and attr_def['Weight'] != '0':
                    include_attr = True

                # if an attribute filter has been provided, only include the attribute data if the
                # attribute is in the filter
                if len(attr_filter) > 0:
                    if attr_def['Name'] not in attr_filter:
                        include_attr = False
                
            if include_attr == True:  
                some_attr_added = True
                if attr_def.has_key('Display_Name'):
                    attr_name = attr_def['Display_Name']
                else:
                    attr_name = attr_def['Name']
                    
                category = attr_def.get('Sub_Category', '')
                result.append('   { "name": "%s", "matches": "%s", "cumulative_value": "%s", "average_value": "%s", "all_values": "%s", "category": "%s" }' % \
                              (attr_name,str(attribute.num_occurs),str(attribute.cumulative_value),str(round(attribute.avg_value,1)),\
                               DataModel.mapAllValuesToShortenedString(attr_def, attribute.all_values), category) )
                result.append(',\n')
        if some_attr_added:
            result = result[:-1]
        
    result.append(' ] }\n')
    json_str = ''.join(result)
    
    if store_json_file is True:   
        try:
            FileSync.put( global_config, '%s/EventData/TeamData/team%s_scouting_data_summary.json' % (comp,name), 'text', json_str)
        except:
            raise
        
    session.remove()
    return json_str
def get_attr_tree_json(global_config, filter_name = None, store_data_to_file=False):
    
    global_config['logger'].debug( 'GET Attribute Definitions Tree JSON' )

    attrdef_filename = WebCommonUtils.get_attrdef_filename(global_config['this_competition'])
    if attrdef_filename is None:
        return None

    attr_definitions = AttributeDefinitions.AttrDefinitions(global_config)
    attr_definitions.parse(attrdef_filename)
    
    competition = global_config['this_competition'] + global_config['this_season']
    #categories = attr_definitions.get_sub_categories()
    
    attr_filter = get_saved_filter(filter_name)
    
    result = []
    result.append('{ "item": [\n')
    
    if filter_name != None:
        checked = 0
    else:
        checked = 1
        
    opened = 1
    result.append('    { "text": "%s", "open": %d, "checked": %d, "id": "Skip_%s", "item": [ \n' % ('All Attributes',opened,checked,'All'))
    
    category_dict = attr_definitions.get_attr_dict_by_category()
    for category, attrlist in category_dict.iteritems():
        if category != 'Uncategorized':
            result.append('    { "text": "%s", "checked": %d, "id": "Skip_%s", "item": [ \n' % (category,checked,category))
            for attrname in sorted(attrlist):
                checked_ind = 0
                if filter_name is None:
                    # if there is no specified filter, then set the checked indicator based on the overall setting
                    checked_ind = checked
                else:
                    # otherwise, if a filter is specified, then set the checked indicator based on if the attribute
                    # name is specified in the filter list
                    if attrname in attr_filter:
                        checked_ind = 1
                tree_item_str = get_attr_def_item_json( global_config, attr_definitions.get_definition(attrname), attr_filter, checked_ind, attrname )
                result.append(tree_item_str)
                result.append(',\n')
                          
            if len(attrlist) > 0:
                result = result[:-1]
                result.append('\n')

            result.append( '    ] }')
            result.append(',\n')
            
    attrlist = category_dict['Uncategorized']
    if len( attrlist ) > 0:
        for attrname in sorted(attrlist):
            checked_ind = 0
            if filter_name is None:
                # if there is no specified filter, then set the checked indicator based on the overall setting
                checked_ind = checked
            else:
                # otherwise, if a filter is specified, then set the checked indicator based on if the attribute
                # name is specified in the filter list
                if attrname in attr_filter:
                    checked_ind = 1
            tree_item_str = get_attr_def_item_json( global_config, attr_definitions.get_definition(attrname), attr_filter, checked_ind, attrname )
            result.append(tree_item_str)
            result.append(',\n')
                      
    result = result[:-1]
    result.append('],\n    "id": 1 \n}\n')
    result.append('],\n    "id": 0 \n}\n')
                                 
    json_str = ''.join(result)
    
    if store_data_to_file:
        try:
            if filter_name == None:
                file_name = 'attrtree.json'
            else:
                file_name = 'attrtree_%s.json' % filter_name
            FileSync.put( global_config, '%s/EventData/%s' % (competition,file_name), 'text', json_str)                
        except:
            raise


    return json_str