def fetchSearchResults(query, search_type, page):
        from app.models import Search

        user_info = {'user_id': -1, 'uuid': 'web:-1', 'gcm_id': None}
        user_data = session.get('_user', None)
        if user_data:
            user_info['user_id'] = user_data['user_id'] 
            user_info['uuid'] = 'web:'+str(user_data['user_id']) 

        search = Search(query, user_info)
        page = page - 1
        if search_type == 'category':
            results = search.categorySearch(page)
        elif search_type == 'collection':
            results = search.collectionsSearch(page)
        else: 
            results = search.basicSearch(page, source='web')
        results['items'] = WebUtils.extendItemWebProperties(results['items'])
        return results 
Example #2
0
def searchString():
    response = {'status': 'False'}
    results = {}

    query = Utils.getParam(request.args, 'q')
    page = Utils.getParam(request.args, 'page', var_type='int', default=1)
    search_type = Utils.getParam(request.args, 'type', default='free')
    user_id = Utils.getParam(request.args, 'userId', 'int')
    flow = Utils.getParam(request.args, 'flow', default='borrow')
    gcm_id = Utils.getParam(request.args, 'gcm_id', default=None)
    uuid = Utils.getParam(request.args, 'distinct_id', default=None)
    ref = Utils.getParam(request.args, 'ref', default='mobile')

    if not query:
        return Utils.errorResponse(response, 'HTTP_STATUS_CODE_DATA_MISSING')

    if ref == 'web':
        return json.dumps(WebUtils.fetchSearchResults(query, search_type,
                                                      page))

    user_info = {'user_id': user_id, 'gcm_id': gcm_id, 'uuid': uuid}
    search = Search(query, user_info, flow)
    if search_type == 'free':
        results = search.basicSearch(page=page - 1)
    elif search_type == 'category':
        results = search.categorySearch(page=page - 1)
    elif search_type == 'collections':
        results = search.collectionsSearch(page=page - 1)
    elif search_type == 'isbn':
        results = search.isbnSearch(page=page - 1)
    elif search_type == 'auto':
        results = search.autoComplete()
    elif search_type == 'custom':
        results = search.customQuery()
        return results
    #log
    if user_id not in Utils.getAdmins():
        Search.logSearch({_: request.args.get(_)
                          for _ in request.args}, search_type)

    return jsonify(results) if flow != 'admin' else jsonp(results)