Esempio n. 1
0
def search(request):
    # if has been requested
    if request.GET:
        form = SecretSearchForm(request.GET)
    # otherwise default settings
    else:
        form = SecretSearchForm({'page': 1})
    
    if request.is_ajax():
        form.Meta.results_per_page = 500
        form.Meta.default_template = 'location'
    
    # get the results
    if form.is_valid():
        results = form.save()
    else:
        results = []
    
    search_template = 'secret/layout/%s.html' % form.render_template()
    if request.is_ajax():
        render_template = search_template
    else:
        render_template = 'secret/search.html'
    
    # return
    return context_response(request, render_template, {
                'form': form,
                'results': results,
                'search_template':  search_template,
                # this will be hard coded into tabs
                'template_types': SECRET_RENDER_TEMPLATES,
            })
Esempio n. 2
0
def search(request):
    # if has been requested
    if request.GET:
        form = SecretSearchForm(request.GET)
    # otherwise default settings
    else:
        form = SecretSearchForm({'page': 1})
    
    # setup template
    if request.is_ajax():
        template = 'location'
    elif request.GET.get('template') and request.GET['template']:
        template = request.GET['template']
    else:
        template = 'list'
    
    # process form
    if form.is_valid():
        form.chosen_template = template
        results = form.save()
    else:
        results = []
    
    # setup result numbers
    template_map_numbers = {
        'location': 50,
        'list': 10,
        'photo': 20,
    }
    
    # get the results
    search_template = 'secret/layout/%s.html' % template
    form.render_template = template
    if request.is_ajax():
        render_template = search_template
    else:
        render_template = 'secret/search.html'
    
    # return
    return context_response(request, render_template, {
                'form': form,
                'results': results,
                'search_template':  search_template,
                # this will be hard coded into tabs
            }, tabs=['secrets', form.render_template])
Esempio n. 3
0
def search(request, city):
    " Landing page to site. Much more to come... "
    
    request.session[CITY_SESSION_NAME] = city
    request.session.modified = True
    
    RESULTS_PER_PAGE = 10
    show_large_search = True
    START_DATE = datetime.datetime(*settings.START_DATE)
    NOW = datetime.datetime.now()    
    
    #determine if POST or GET and set dict for later use
    if request.GET:
        req_dict = request.GET
        CURRENT_TYPE = request.GET
    elif request.POST:
        req_dict = request.POST
    else:
        req_dict = {'page':1}
    
    CURRENT_QUERY = req_dict.get('text', '')
    CURRENT_SORT = req_dict.get('usort', 'relevance')
    CURRENT_PAGE = int(req_dict.get('page', 1))
    CURRENT_TYPE = req_dict.get('type', 'secrets')
    CURRENT_LOCATION = req_dict.get('location','')
    NEXT_PAGE = CURRENT_PAGE + 1
    
    if CURRENT_TYPE == "discussions":
        discussion_form = DiscussionSearchForm(req_dict)
        available_sorts = discussion_form.get_available_sort_orders()               
        num_results = 0
        rendered_results = ""
        if discussion_form.is_valid():
            discussion_results = discussion_form.save()      
            for r in discussion_results.documents:   
                discussion = Discussion.objects.get(pk=r.pk_field.value)
                discussion.title = r.fields['title'].highlighting()
                discussion.text = r.fields['text'].highlighting()
                discussion.highlighted_body = r.fields['blob'].highlighting()
                rendered_results += render_to_string('discussion/render/singular.html', { 'discussion': discussion, 'show_image': True }, RequestContext(request))
                num_results += 1
        
        RESULTS_PER_PAGE = discussion_form.Meta.results_per_page
        
    else:        
        secret_form = SecretSearchForm(req_dict)
        available_sorts = secret_form.get_available_sort_orders()
        template = 'secret/render/singular.html'
        if CURRENT_TYPE == "photos":
            secret_form.chosen_template = "photo"
            template = 'secret/render/photo.html'
            photo_browse = True
        num_results = 0
        rendered_results = ""
        if secret_form.is_valid():
            secret_results = secret_form.save()      
            for r in secret_results.documents:
                secret = Secret.objects.get(pk=r.pk_field.value)
                secret.title = r.fields['title'].highlighting()
                secret.location = r.fields['location'].highlighting()
                rendered_results += render_to_string(template, { 'secret': secret }, RequestContext(request))
                num_results += 1
        
        RESULTS_PER_PAGE = secret_form.Meta.results_per_page
    
    if CURRENT_PAGE > 1:    
        rendered_results = "<div id='page%s' class='paged'>%s</div>" % (CURRENT_PAGE, rendered_results)
    
    HAS_MORE_RESULTS = (num_results == RESULTS_PER_PAGE) #if loaded full page's worth, then there could be more
    
    if request.method == "POST":
        from django.utils import simplejson
        
        response_dict = {"rendered_results":rendered_results, "num_results":num_results, "has_more_results":HAS_MORE_RESULTS}
        
        return HttpResponse(simplejson.dumps(response_dict))
    else:
        return context_response(request, 'utilz/search.html', locals(), tabs=['home'])