diff --git a/lemarche/siaes/models.py b/lemarche/siaes/models.py index d36d7394c..ff8a03634 100644 --- a/lemarche/siaes/models.py +++ b/lemarche/siaes/models.py @@ -322,7 +322,7 @@ def with_in_user_favorite_list_stats(self, user): def has_contact_email(self): return self.exclude(contact_email__isnull=True).exclude(contact_email__exact="") - def filter_with_tender(self, tender): + def filter_with_tender(self, tender, tender_status=None): """ Filter Siaes with tenders: - first we filter the Siae that are live + can be contacted @@ -354,7 +354,25 @@ def filter_with_tender(self, tender): # filter by siae_kind if len(tender.siae_kind): qs = qs.filter(kind__in=tender.siae_kind) - # return + + # tender status + if tender_status == "INTERESTED": + qs = qs.filter(tendersiae__tender=tender, tendersiae__detail_contact_click_date__isnull=False) + qs = qs.order_by("-tendersiae__detail_contact_click_date") + elif tender_status == "VIEWED": + qs = qs.filter( + Q(tendersiae__tender=tender) + & ( + Q(tendersiae__email_link_click_date__isnull=False) + | Q(tendersiae__detail_display_date__isnull=False) + ) + ) + qs = qs.order_by("-tendersiae__email_link_click_date") + elif tender_status == "ALL": + # why need to filter more ? + qs = qs.filter(tendersiae__tender=tender, tendersiae__email_send_date__isnull=False) + qs = qs.order_by("-tendersiae__email_send_date") + return qs.distinct() def with_tender_stats(self): diff --git a/lemarche/www/siaes/forms.py b/lemarche/www/siaes/forms.py index 9359cded0..c10aa54fe 100644 --- a/lemarche/www/siaes/forms.py +++ b/lemarche/www/siaes/forms.py @@ -306,20 +306,9 @@ def filter_queryset(self, qs=None): # noqa C901 # a Tender author can export its Siae list tender = self.cleaned_data.get("tender", None) - tender_status = self.cleaned_data.get("tender_status", None) if tender: - if tender_status == "INTERESTED": # status == "INTERESTED" - qs = qs.filter(tendersiae__tender=tender, tendersiae__detail_contact_click_date__isnull=False) - elif tender_status == "VIEWED": - qs = qs.filter( - Q(tendersiae__tender=tender) - & ( - Q(tendersiae__email_link_click_date__isnull=False) - | Q(tendersiae__detail_display_date__isnull=False) - ) - ).distinct() - else: - qs = qs.filter(tendersiae__tender=tender, tendersiae__email_send_date__isnull=False) + tender_status = self.cleaned_data.get("tender_status", "ALL") + qs = qs.filter_with_tender(tender=tender, tender_status=tender_status) locations = self.cleaned_data.get("locations", None) if locations: diff --git a/lemarche/www/siaes/views.py b/lemarche/www/siaes/views.py index 7ed5b4f59..b02b39e15 100644 --- a/lemarche/www/siaes/views.py +++ b/lemarche/www/siaes/views.py @@ -137,9 +137,6 @@ def get_queryset(self): Filter results. """ filter_form = SiaeDownloadForm(data=self.request.GET) - import ipdb - - ipdb.set_trace() results = filter_form.filter_queryset() return results diff --git a/lemarche/www/tenders/views.py b/lemarche/www/tenders/views.py index ea11a18f7..164dce4d8 100644 --- a/lemarche/www/tenders/views.py +++ b/lemarche/www/tenders/views.py @@ -2,7 +2,7 @@ from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.core.paginator import Paginator -from django.db.models import Prefetch, Q +from django.db.models import Prefetch from django.http import HttpResponseForbidden, HttpResponseRedirect from django.shortcuts import get_object_or_404, redirect from django.urls import reverse_lazy @@ -482,27 +482,13 @@ def get_queryset(self): qs = super().get_queryset() # first get the tender's siaes self.tender = Tender.objects.get(slug=self.kwargs.get("slug")) - if self.status == "INTERESTED": # status == "INTERESTED" - qs = qs.filter(tendersiae__tender=self.tender, tendersiae__detail_contact_click_date__isnull=False) - qs = qs.order_by("-tendersiae__detail_contact_click_date") - elif self.status == "VIEWED": # status == "INTERESTED" - qs = qs.filter( - Q(tendersiae__tender=self.tender) - & ( - Q(tendersiae__email_link_click_date__isnull=False) - | Q(tendersiae__detail_display_date__isnull=False) - ) - ).distinct() - qs = qs.order_by("-tendersiae__email_link_click_date") - else: # default - qs = qs.filter(tendersiae__tender=self.tender, tendersiae__email_send_date__isnull=False) - qs = qs.order_by("-tendersiae__email_send_date") + qs = qs.filter_with_tender(tender=self.tender, tender_status=self.status) # then filter with the form self.filter_form = SiaeFilterForm(data=self.request.GET) qs = self.filter_form.filter_queryset(qs) return qs - def get(self, request, status=None, *args, **kwargs): + def get(self, request, status="ALL", *args, **kwargs): """ - set status - update 'siae_list_last_seen_date'