Exemple #1
0
    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)

        # Get all events to display on an event index page.
        # Apply ascending order by start_date to upcoming events,
        # but for past events apply descending ordering by start_date.
        date_now = timezone.now().date()
        events = self._annotated_descendant_events()
        events = events.annotate(upcoming_order=models.Case(
            models.When(latest_date__gte=date_now, then='start_date'),
            default=models.Value(None),
            output_field=models.DateField()))
        events = events.annotate(past_order=models.Case(
            models.When(latest_date__lt=date_now, then='start_date'),
            default=models.Value(None),
            output_field=models.DateField()))
        events = events.order_by('upcoming_order', '-past_order')

        paginator = Paginator(events, settings.DEFAULT_PER_PAGE)
        try:
            events = paginator.page(request.GET.get('page'))
        except PageNotAnInteger:
            events = paginator.page(1)
        except EmptyPage:
            events = paginator.page(paginator.num_pages)

        context.update({
            'events': events,
            'sidebar_pages': self.get_siblings().live().public(),
        })
        context.update(get_adjacent_pages(paginator, events.number))

        return context
Exemple #2
0
def search(request):
    search_query = request.GET.get('query', None)
    page_number = request.GET.get('page', 1)

    # Allow to filter search results using a page types, if specified
    page_types = PageType.objects.all()
    selected_page_type_pks = request.GET.getlist('type', None)
    # Use really existing pks
    try:
        selected_page_type_pks = \
            set(int(pk) for pk in selected_page_type_pks) & \
            set(obj.pk for obj in page_types)
    except ValueError:
        selected_page_type_pks = []

    search_results = get_search_queryset(request, selected_page_type_pks)

    # Do not display ResearchSummaryPage pages in the main search
    search_results = search_results.not_type(ResearchSummaryPage)

    # Search
    if search_query:
        search_results = search_results.search(search_query, operator='and')

        query = Query.get(search_query)

        # Record hit
        query.add_hit()

    # Pagination
    paginator = Paginator(search_results, settings.DEFAULT_PER_PAGE)
    try:
        search_results = paginator.page(page_number)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    extra_url_params = '&'.join(['type={}'.format(type) for type in selected_page_type_pks])

    context = {
        'search_query': search_query,
        'search_results': search_results,
        'page_types': page_types,
        'selected_page_type_pks': selected_page_type_pks,
        'extra_url_params': extra_url_params,
    }
    context.update(get_adjacent_pages(paginator, page_number))
    return render(request, 'search/search.html', context)
Exemple #3
0
    def get_context(self, request, *args, **kwargs):
        paginator = Paginator(self.people, settings.DEFAULT_PER_PAGE)
        try:
            people = paginator.page(request.GET.get('page'))
        except PageNotAnInteger:
            people = paginator.page(1)
        except EmptyPage:
            people = paginator.page(paginator.num_pages)

        context = super().get_context(request, *args, **kwargs)
        context.update(
            people=people,
            sidebar_pages=self.get_siblings().live().public(),
        )
        context.update(get_adjacent_pages(paginator, people.number))

        return context
Exemple #4
0
    def get_context(self, request, *args, **kwargs):
        news = NewsPage.objects.live().public().descendant_of(self).annotate(
            date=Coalesce('publication_date', 'first_published_at')
        ).order_by('-date')

        # Pagination
        paginator = Paginator(news, settings.DEFAULT_PER_PAGE)
        try:
            news = paginator.page(request.GET.get('page'))
        except PageNotAnInteger:
            news = paginator.page(1)
        except EmptyPage:
            news = paginator.page(paginator.num_pages)

        context = super().get_context(request, *args, **kwargs)
        context.update(
            news=news,
            sidebar_pages=self.get_siblings().live().public(),
        )
        context.update(get_adjacent_pages(paginator, news.number))
        return context
Exemple #5
0
    def get_context(self, request, *args, **kwargs):
        date_from = request.GET.get('date_from', None)
        search_date_from = None
        date_to = request.GET.get('date_to', None)
        search_date_to = None
        search_research_type = request.GET.get('research_type', None)
        search_rec_opinion = request.GET.get('rec_opinion', None)
        search_query = request.GET.get('query', None)

        search_results = self._children_research_summary

        # Convert dates to the Python format
        if date_from:
            try:
                # Can return None or raise ValueError in case of bad format
                search_date_from = parse_date(date_from, dayfirst=True).date()
            except ValueError:
                search_date_from = None

        if date_to:
            try:
                # Can return None or raise ValueError in case of bad format
                search_date_to = parse_date(date_to, dayfirst=True).date()
            except ValueError:
                search_date_to = None

        # Swap dates around if "date from" happens after "date to".
        if search_date_from and search_date_to:
            if search_date_from > search_date_to:
                search_date_to, search_date_from = search_date_from, search_date_to

        # Search queryset
        if search_date_to:
            search_results = search_results.filter(
                date_of_rec_opinion__lte=search_date_to)

        if search_date_from:
            search_results = search_results.filter(
                date_of_rec_opinion__gte=search_date_from)

        # Research types to be displayed as is
        non_aliased_research_types = dict(
            ResearchType.objects.filter(
                harp_study_type_id__in=ResearchType.NON_ALIASED_STUDY_TYPE_IDS
            ).values_list('pk', 'name'))
        # Add the alias for rest research types
        alias_fake_id = 0
        display_research_types = non_aliased_research_types.copy()
        display_research_types.update({
            alias_fake_id: ResearchType.ALIAS_NAME,
        })

        try:
            search_research_type = int(search_research_type)

            if search_research_type == alias_fake_id:
                search_results = search_results.exclude(
                    research_type__in=non_aliased_research_types.keys())
            elif search_research_type in non_aliased_research_types.keys():
                search_results = search_results.filter(
                    research_type=search_research_type)
        except (TypeError, ValueError):
            pass

        # rec opinion filter
        if search_rec_opinion:
            search_results = search_results.filter(
                rec_opinion=search_rec_opinion)

        if search_query:
            search_results = search_results.search(search_query,
                                                   operator='and')

        # Pagination
        paginator = Paginator(search_results, settings.DEFAULT_PER_PAGE)
        try:
            search_results = paginator.page(request.GET.get('page'))
        except PageNotAnInteger:
            search_results = paginator.page(1)
        except EmptyPage:
            search_results = paginator.page(paginator.num_pages)

        extra_url_params = {
            'date_from': date_from,
            'date_to': date_to,
            'research_type': search_research_type,
            'rec_opinion': search_rec_opinion,
        }

        context = super().get_context(request, *args, **kwargs)
        context.update({
            'search_research_type': search_research_type,
            'search_rec_opinion': search_rec_opinion,
            'search_query': search_query,
            'search_results': search_results,
            'display_research_types': display_research_types,
            'search_date_from': search_date_from,
            'search_date_to': search_date_to,
            'rec_opinions': REC_OPINION_CHOICES,
            'extra_url_params': urlencode(extra_url_params),
        })
        context.update(get_adjacent_pages(paginator, search_results.number))

        return context