Пример #1
0
def index(request):
    if request.method == 'GET' and request.GET.get('query'):
        query = request.GET.get('query')
        #Sort by sort url parameter:        
        sort = request.GET.get('sort', SITE_SEARCH_DEFAULT_SORT)
        query_kwargs = {'sort': sort}
        sorts_list = get_sorts_list(request)
        
        ctype_filters = get_ctype_filters(request)
        #Filter by content type if URL param is set:        
        ctype_filter = request.GET.get('content_type')
        if ctype_filter and ctype_filter in [c['id'] for c in ctype_filters]:
            app_label, model = ctype_filter.split(".")
            query_kwargs['app_label'] = app_label
            query_kwargs['model'] = model
        results = Search.query(query, **query_kwargs)
        #FIXME: paginate results
        extra_context = {
            'results': results,
            'sorts': sorts_list,
            'filters': ctype_filters,
            'query_string': query            
        }
    else:
        extra_context = {'results': None}
    return simple.direct_to_template(request, 
        template="site_search/index.html",
        extra_context=extra_context
    )
Пример #2
0
def searches():
    if not request.args or not request.args['key'] or request.args['key'] != IOS_API_KEY:
        abort(401)
    if request.method == "POST":
        if not request.json or not 'google_places_id' in request.json or not 'ios_device_id' in request.json:
            abort(400)
        search = Search(
                    google_places_id = request.json['google_places_id'],
                    ios_device_id=request.json['ios_device_id'])
        if 'name' in request.json:
            search.populate(name=request.json['name'])
        if 'vicinity' in request.json:
            search.populate(vicinity=request.json['vicinity'])
        try:
            search.put()
            return jsonify(search.to_dict())
        except CapabilityDisabledError:
            abort(400)
    elif request.method == "GET":
        if not request.args['ios_device_id']:
            logging.error("request.json: %s", request.json)
            logging.error("request.args[ios_device_id]: %@", request.args['ios_device_id'])
            abort(400)
        ios_device_id = request.args['ios_device_id']
        searches = Search.query(Search.ios_device_id==ios_device_id).order(-Search.created_at).fetch(20)
        searchesDeduped = []
        searchesGooglePlacesIds = set()
        for search in searches:
            if search.google_places_id not in searchesGooglePlacesIds:
                if search.name != None and search.vicinity != None:
                    searchesDeduped.append(search)
                    searchesGooglePlacesIds.add(search.google_places_id)
        logging.info("searchesGooglePlacesIds: %s", searchesGooglePlacesIds)
        return jsonify({"searches": [search.to_dict() for search in searchesDeduped]})
    else:
        abort(401)