Example #1
0
def advanced_search(request):
    page = None
    query = request.GET.get('adv_search')
    search_type = request.GET.get('search_type')
    first_author = request.GET.get('first_author')
    first_author_only = True if first_author == "on" else False

    if request.GET.get('search_type') == 'article':
        inc_articles = True
        inc_photos = False
    else:
        inc_articles = True if search_type == 'article' or search_type == 'both' else False

    if request.GET.get('search_type') == 'image':
        inc_photos = True
        inc_articles = False
    else:
        inc_photos = True if search_type == 'image' or search_type == 'both' else False

    adv_search_form = AdvancedSearchForm(request.GET or None)

    if adv_search_form.is_valid():
        cleaned_adv_form = adv_search_form.cleaned_data
        author_pk = cleaned_adv_form.get("author").pk if cleaned_adv_form.get("author") else None
        category_pk = cleaned_adv_form.get("category").pk if cleaned_adv_form.get("category") else None
        topic_pk = cleaned_adv_form.get("topics").pk if cleaned_adv_form.get("topics") else None
        try:
            article_list = searchArticlesAndPhotos(cleaned_adv_form.get("adv_search"),
                                                   inc_articles,
                                                   inc_photos,
                                                   author_pk,
                                                   first_author_only,
                                                   category_pk,
                                                   topic_pk,
                                                   cleaned_adv_form.get("date_from"),
                                                   cleaned_adv_form.get("date_to"))
        except:
            logger.error("Advanced Search Failed calling searchArticlesAndPhotos")
            article_list = models.Article.objects.none()
    else:
        article_list = models.Article.objects.none()

    try:
        num_results = int(request.GET.get('results_per_page'))
    except:
        if search_type == 'image':
            num_results = settings.SEARCH_RESULTS_PER_PAGE * 2
        else:
            num_results = settings.SEARCH_RESULTS_PER_PAGE

    paginator = Paginator(article_list, num_results)
    page_num = request.GET.get('page')
    if page_num is None:
        page_num = 1
    try:
        page = paginator.page(page_num)
    except PageNotAnInteger:
        page = paginator.page(1)
    except EmptyPage:
        page = paginator.page(paginator.num_pages)
    except:
        logger.error("Advanced Search Failed in pagination")
    finally:
        try:
            num_pages = paginator.num_pages
        except:
            logger.error("Advanced Search failed to get num_pages")
            num_pages = 1


    return render(request, 'search/search.html', {'query': query,
                                                  'page': page,
                                                  'page_num': page_num,
                                                  'num_pages': num_pages,
                                                  'num_items': len(article_list),
                                                  'search_type': search_type,
                                                  'adv_search_form': adv_search_form})
Example #2
0
 def test_search(self):
     articles = searchArticlesAndPhotos("cow dog")
     self.assertEqual(len(articles), 1)