<commit_before>import framework.util as util
import giveaminute.location as mLocation
import giveaminute.project as mProject
import giveaminute.idea as mIdea
import giveaminute.projectResource as mProjectResource
from framework.controller import *
import json

class Search(Controller):
    def GET(self, action=None):
        if action == 'map':
            return self.showMap()
        else:
            return self.showSearch()   
                        
    def showMap(self):
        locationData = self.getLocationData()
    
        self.template_data['locations_scored'] = json.dumps(locationData)
        self.template_data['max_score'] = locationData[0]['score']
    
        return self.render('map')
        
    def showSearch(self):
        if (self.request('terms')):
            terms = self.request('terms').split(',')
        else:
            terms = []
            
        locationId = self.request('location_id')
        
        self.template_data['search_terms'] = self.request('terms')
        self.template_data['search_location_id'] = locationId
        
        log.info("*** search for '%s', %s" % (terms, locationId))
        
        projects = self.searchProjects(terms, locationId)
        resources = self.searchProjectResources(terms, locationId)
        ideas = self.searchIdeas(terms, locationId)
        
        results = dict(projects = projects, resources = resources, ideas = ideas)
        
        self.template_data['results'] = dict(json = json.dumps(results), data = results)
        
        locations_list = mLocation.getSimpleLocationDictionary(self.db)
        self.template_data['locations'] = dict(json = json.dumps(locations_list), data = locations_list)
        
        locationData = self.getLocationData()
        
        self.template_data['locations_scored'] = json.dumps(locationData)
        self.template_data['max_score'] = locationData[0]['score']
        
        return self.render('search')
            
    def getLocationData(self):
        data = mLocation.getLocationsWithScoring(self.db)
        
        locations = []
        
        for item in data:
            score = self.calcScore(item.num_projects, item.num_ideas, item.num_project_resources)
        
            locations.append(dict(name = item.name, 
                                location_id = item.location_id, 
                                lat = str(item.lat), 
                                lon = str(item.lon),
                                n_projects = item.num_projects,
                                n_ideas = item.num_ideas,
                                n_resources = item.num_project_resources,
                                score = score))
                                
        sortedLocations = sorted(locations, key = lambda k:k['score'], reverse = True) 
        
        return sortedLocations
        
    def calcScore(self, numProjects, numIdeas, numResources):
        score = numProjects + numIdeas + numResources
        
        return score 

    ## DEBUG ONLY
    def getDummyResources(self):
        limit = 10
        sql = "select project_resource_id as link_id, title, url, image_id from project_resource limit $limit"
        data = list(self.db.query(sql, {'limit':limit}))
                
        return data
    ## END DEBUG ONLY

    def searchProjects(self, terms, locationId):
        return mProject.searchProjects(self.db, terms, locationId)
        
    def searchProjectResources(self, terms, locationId):
        return mProjectResource.searchProjectResources(self.db, terms, locationId)

    def searchIdeas(self, terms, locationId):
        return mIdea.searchIdeas(self.db, terms, locationId)

            
            
            
                <commit_msg>search pagination (stubbed out total result count)<commit_after>import framework.util as util
import giveaminute.location as mLocation
import giveaminute.project as mProject
import giveaminute.idea as mIdea
import giveaminute.projectResource as mProjectResource
from framework.controller import *
import json

class Search(Controller):
    def GET(self, action = None):
        if action == 'map':
            return self.showMap()
        elif (action == 'ideas'):
            return self.searchIdeasJSON() 
        elif (action == 'resources'):
            return self.searchProjectResourcesJSON() 
        elif (action == 'projects'):
            return self.searchProjectsJSON() 
        else:
            return self.showSearch()   
                        
    def showMap(self):
        locationData = self.getLocationData()
    
        self.template_data['locations_scored'] = json.dumps(locationData)
        self.template_data['max_score'] = locationData[0]['score']
    
        return self.render('map')
        
    def showSearch(self):
        terms = self.request('terms').split(',') if self.request('terms') else []
        limit = int(self.request('n')) if self.request('n') else 6
        offset = int(self.request('offset')) if self.request('offset') else 0
        locationId = self.request('location_id')
        
        self.template_data['search_terms'] = self.request('terms')
        self.template_data['search_location_id'] = locationId
        
        projects = self.searchProjects(terms, locationId, limit, offset)
        resources = self.searchProjectResources(terms, locationId, limit, offset)
        ideas = self.searchIdeas(terms, locationId, limit, offset)
        
        results = dict(projects = projects, resources = resources, ideas = ideas)
        
        self.template_data['results'] = dict(json = json.dumps(results), data = results)

        total_count = dict(projects = 100, resources = 100, ideas = 100)

        self.template_data['total_count'] = dict(json = json.dumps(total_count), data = total_count)
        
        locations_list = mLocation.getSimpleLocationDictionary(self.db)
        self.template_data['locations'] = dict(json = json.dumps(locations_list), data = locations_list)
        
        locationData = self.getLocationData()
        
        self.template_data['locations_scored'] = json.dumps(locationData)
        self.template_data['max_score'] = locationData[0]['score']
        
        return self.render('search')
            
    def getLocationData(self):
        data = mLocation.getLocationsWithScoring(self.db)
        
        locations = []
        
        for item in data:
            score = self.calcScore(item.num_projects, item.num_ideas, item.num_project_resources)
        
            locations.append(dict(name = item.name, 
                                location_id = item.location_id, 
                                lat = str(item.lat), 
                                lon = str(item.lon),
                                n_projects = item.num_projects,
                                n_ideas = item.num_ideas,
                                n_resources = item.num_project_resources,
                                score = score))
                                
        sortedLocations = sorted(locations, key = lambda k:k['score'], reverse = True) 
        
        return sortedLocations
        
    def calcScore(self, numProjects, numIdeas, numResources):
        score = numProjects + numIdeas + numResources
        
        return score 
        
    def searchProjectsJSON(self):
        terms = self.request('terms').split(',') if self.request('terms') else []
        limit = int(self.request('n')) if self.request('n') else 6
        offset = int(self.request('offset')) if self.request('offset') else 0
        locationId = self.request('location_id')
    
        return self.json({'results':mProject.searchProjects(self.db, terms, locationId, limit, offset),
                          'total_count':100})
        
    def searchProjectResourcesJSON(self):
        terms = self.request('terms').split(',') if self.request('terms') else []
        limit = int(self.request('n')) if self.request('n') else 6
        offset = int(self.request('offset')) if self.request('offset') else 0
        locationId = self.request('location_id')
        
        return self.json({'results':mProjectResource.searchProjectResources(self.db, terms, locationId, limit, offset),
                          'total_count':100})

    def searchIdeasJSON(self):
        terms = self.request('terms').split(',') if self.request('terms') else []
        limit = int(self.request('n')) if self.request('n') else 6
        offset = int(self.request('offset')) if self.request('offset') else 0
        locationId = self.request('location_id')
        
        return self.json({'results':mIdea.searchIdeas(self.db, terms, locationId, limit, offset),
                          'total_count':100})

    def searchProjects(self, terms, locationId, limit, offset):
        return mProject.searchProjects(self.db, terms, locationId, limit, offset)
        
    def searchProjectResources(self, terms, locationId, limit, offset):
        return mProjectResource.searchProjectResources(self.db, terms, locationId, limit, offset)

    def searchIdeas(self, terms, locationId, limit, offset):
        return mIdea.searchIdeas(self.db, terms, locationId, limit, offset)

            
            
            
                
{'fraction_non_alphanumeric': 0.10450333979745745, 'fraction_numerical': 0.003447532859297565, 'mean_word_length': 2.134031060094531, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4597623', 'n_tokens_mistral': 2528, 'n_tokens_neox': 2415, 'n_words': 522}