Skip to content

Commit

Permalink
Add status property/qs/filter
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Mar 7, 2024
1 parent edaedac commit f6c4e51
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 18 deletions.
30 changes: 28 additions & 2 deletions lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,11 +780,25 @@ class TenderSiaeSourceFilter(MultiChoice):
BUTTON_LABEL = "Appliquer"


class TenderSiaeStatusFilter(admin.SimpleListFilter):
title = "Status"
parameter_name = "status"

def lookups(self, request, model_admin):
return tender_constants.TENDER_SIAE_STATUS_CHOICES

def queryset(self, request, queryset):
value = self.value()
return queryset.filter(status_annotated=value)


@admin.register(TenderSiae, site=admin_site)
class TenderSiaeAdmin(admin.ModelAdmin):
list_display = ["created_at", "siae_with_app_link", "tender_with_link", "source"]
list_display = ["created_at", "siae_with_app_link", "tender_with_link", "source", "status"]
list_filter = [
("source", TenderSiaeSourceFilter),
TenderSiaeStatusFilter,
"survey_transactioned_answer",
]

readonly_fields = [field for field in TenderSiae.READONLY_FIELDS] + [
Expand All @@ -800,12 +814,18 @@ class TenderSiaeAdmin(admin.ModelAdmin):
None,
{"fields": ("siae", "siae_with_app_link", "tender_with_link", "source", "found_with_ai")},
),
("Mise en relation", {"fields": TenderSiae.FIELDS_RELATION}),
("Mise en relation", {"fields": (*TenderSiae.FIELDS_RELATION, "status")}),
("Transaction ?", {"fields": TenderSiae.FIELDS_SURVEY_TRANSACTIONED}),
("Stats", {"fields": ("logs_display",)}),
("Dates", {"fields": ("created_at", "updated_at")}),
)

def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.with_prefetch_related()
qs = qs.with_status()
return qs

def has_add_permission(self, request):
return False

Expand All @@ -826,6 +846,12 @@ def tender_with_link(self, tendersiae):
tender_with_link.short_description = "Besoin d'achat (lien vers l'admin)"
tender_with_link.admin_order_field = "tender"

def status(self, tendersiae):
return tendersiae.status

status.short_description = "Status"
status.admin_order_field = "status"

def logs_display(self, tender=None):
if tender:
return pretty_print_readonly_jsonfield(tender.logs)
Expand Down
13 changes: 13 additions & 0 deletions lemarche/tenders/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,16 @@
TENDER_SIAE_SOURCES_EXCEPT_IA = [
source[0] for source in TENDER_SIAE_SOURCE_CHOICES if source[0] != TENDER_SIAE_SOURCE_AI
]

TENDER_SIAE_STATUS_EMAIL_SEND_DATE = "EMAIL_SEND_DATE"
TENDER_SIAE_STATUS_EMAIL_LINK_CLICK_DATE = "EMAIL_LINK_CLICK_DATE"
TENDER_SIAE_STATUS_DETAIL_DISPLAY_DATE = "DETAIL_DISPLAY_DATE"
TENDER_SIAE_STATUS_DETAIL_CONTACT_CLICK_DATE = "DETAIL_CONTACT_CLICK_DATE"
TENDER_SIAE_STATUS_DETAIL_NOT_INTERESTED_CLICK_DATE = "DETAIL_NOT_INTERESTED_CLICK_DATE"
TENDER_SIAE_STATUS_CHOICES = (
(TENDER_SIAE_STATUS_EMAIL_SEND_DATE, "Contactée"),
(TENDER_SIAE_STATUS_EMAIL_LINK_CLICK_DATE, "Cliquée"),
(TENDER_SIAE_STATUS_DETAIL_DISPLAY_DATE, "Vue"),
(TENDER_SIAE_STATUS_DETAIL_CONTACT_CLICK_DATE, "Intéressée"),
(TENDER_SIAE_STATUS_DETAIL_NOT_INTERESTED_CLICK_DATE, "Pas intéressée"),
)
72 changes: 56 additions & 16 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,22 +893,6 @@ def set_sent(self):
self.save()


class TenderSiaeQuerySet(models.QuerySet):
def email_click_reminder(self, gte_days_ago, lt_days_ago):
return (
self.filter(email_send_date__gte=gte_days_ago)
.filter(email_send_date__lt=lt_days_ago)
.filter(email_link_click_date__isnull=True)
.filter(detail_display_date__isnull=True)
.filter(detail_contact_click_date__isnull=True)
)

def detail_contact_click_post_reminder(self, gte_days_ago, lt_days_ago):
return self.filter(detail_contact_click_date__gte=gte_days_ago).filter(
detail_contact_click_date__lt=lt_days_ago
)


class TenderQuestion(models.Model):
text = models.TextField(verbose_name="Intitulé de la question", blank=False)

Expand All @@ -927,6 +911,49 @@ def __str__(self):
return self.text


class TenderSiaeQuerySet(models.QuerySet):
def email_click_reminder(self, gte_days_ago, lt_days_ago):
return (
self.filter(email_send_date__gte=gte_days_ago)
.filter(email_send_date__lt=lt_days_ago)
.filter(email_link_click_date__isnull=True)
.filter(detail_display_date__isnull=True)
.filter(detail_contact_click_date__isnull=True)
)

def detail_contact_click_post_reminder(self, gte_days_ago, lt_days_ago):
return self.filter(detail_contact_click_date__gte=gte_days_ago).filter(
detail_contact_click_date__lt=lt_days_ago
)

def with_prefetch_related(self):
return self.prefetch_related("tender", "siae")

def with_status(self):
return self.annotate(
status_annotated=Case(
When(
detail_not_interested_click_date__isnull=False,
then=Value(tender_constants.TENDER_SIAE_STATUS_DETAIL_NOT_INTERESTED_CLICK_DATE),
),
When(
detail_contact_click_date__isnull=False,
then=Value(tender_constants.TENDER_SIAE_STATUS_DETAIL_CONTACT_CLICK_DATE),
),
When(
detail_display_date__isnull=False,
then=Value(tender_constants.TENDER_SIAE_STATUS_DETAIL_DISPLAY_DATE),
),
When(
email_link_click_date__isnull=False,
then=Value(tender_constants.TENDER_SIAE_STATUS_EMAIL_LINK_CLICK_DATE),
),
When(email_send_date__isnull=False, then=Value(tender_constants.TENDER_SIAE_STATUS_EMAIL_SEND_DATE)),
default=None,
)
)


class TenderSiae(models.Model):
FIELDS_RELATION = [
"email_send_date",
Expand Down Expand Up @@ -1002,6 +1029,19 @@ class Meta:
verbose_name_plural = "Structures correspondantes au besoin"
ordering = ["-created_at"]

@property
def status(self):
if self.detail_not_interested_click_date:
return "Pas intéressée"
if self.contact_click_date:
return "Intéressée"
if self.detail_display_date:
return "Vue"
if self.email_link_click_date:
return "Cliquée"
if self.email_send_date:
return "Contactée"


class PartnerShareTenderQuerySet(models.QuerySet):
def is_active(self):
Expand Down

0 comments on commit f6c4e51

Please sign in to comment.