Example #1
0
def test_get_all_related_perimeter_ids(perimeters):
    """Finding related perimeters works as expected."""

    related_perimeters_france = get_all_related_perimeter_ids(
        perimeters['herault'].id)  # noqa
    print(related_perimeters_france)
    assert {'id': perimeters['france'].id} in related_perimeters_france
    assert {'id': perimeters['montpellier'].id} in related_perimeters_france
    assert {'id': perimeters['aveyron'].id} not in related_perimeters_france
Example #2
0
    def perimeter_filter(self, qs, search_perimeter):
        """Filter queryset depending on the given perimeter.

        When we search for a given perimeter, we must return all aids:
         - where the perimeter is wider and contains the searched perimeter ;
         - where the perimeter is smaller and contained by the search
         perimeter ;

        E.g if we search for aids in "Hérault (department), we must display all
        aids that are applicable to:

         - Hérault ;
         - Occitanie ;
         - France ;
         - Europe ;
         - M3M (and all other epcis in Hérault) ;
         - Montpellier (and all other communes in Hérault) ;
        """
        perimeter_qs = get_all_related_perimeter_ids(search_perimeter.id)
        qs = qs.filter(perimeter__in=perimeter_qs)
        return qs
Example #3
0
    def get_promotions(self):

        promotions = PromotionPost.objects.filter(status='published')

        searched_backers = self.form.cleaned_data.get('backers', None)
        if searched_backers:
            promotions = promotions \
                .filter(Q(backers__in=searched_backers) | Q(backers__isnull=True))
        else:
            promotions = promotions.filter(backers__isnull=True)

        searched_programs = self.form.cleaned_data.get('programs', None)
        if searched_programs:
            promotions = promotions \
                .filter(Q(programs__in=searched_programs) | Q(programs__isnull=True))
        else:
            promotions = promotions.filter(programs__isnull=True)

        searched_categories = self.form.cleaned_data.get('categories', None)
        if searched_categories:
            promotions = promotions \
                .filter(Q(categories__in=searched_categories) | Q(categories__isnull=True))
        else:
            promotions = promotions.filter(categories__isnull=True)

        searched_perimeter = self.form.cleaned_data.get('perimeter', None)
        if searched_perimeter:
            searched_perimeter = get_all_related_perimeter_ids(searched_perimeter.id)
            promotions = promotions \
                .filter(Q(perimeter__in=searched_perimeter) | Q(perimeter__isnull=True))
        else:
            promotions = promotions.filter(perimeter__isnull=True)

        promotions = promotions.distinct()

        return promotions
Example #4
0
 def queryset(self, request, queryset):
     value = self.value()
     if value is not None:
         perimeter_qs = get_all_related_perimeter_ids(value)
         return queryset.filter(perimeter__in=perimeter_qs)