Skip to content

Commit

Permalink
clean some code
Browse files Browse the repository at this point in the history
  • Loading branch information
madjid-asa committed Nov 6, 2023
1 parent 0c986ae commit e3797e9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 35 deletions.
22 changes: 20 additions & 2 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
15 changes: 2 additions & 13 deletions lemarche/www/siaes/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions lemarche/www/siaes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 3 additions & 17 deletions lemarche/www/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit e3797e9

Please sign in to comment.