From e783e53a0cad8cc5941389bd957d408e76331e49 Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Thu, 22 Feb 2024 14:02:41 +0100 Subject: [PATCH 1/9] add attribute model to tendersiae for delete --- .../0078_tendersiae_is_deleted_by_siae.py | 17 +++++++++++++++++ lemarche/tenders/models.py | 1 + 2 files changed, 18 insertions(+) create mode 100644 lemarche/tenders/migrations/0078_tendersiae_is_deleted_by_siae.py diff --git a/lemarche/tenders/migrations/0078_tendersiae_is_deleted_by_siae.py b/lemarche/tenders/migrations/0078_tendersiae_is_deleted_by_siae.py new file mode 100644 index 000000000..4f81e2122 --- /dev/null +++ b/lemarche/tenders/migrations/0078_tendersiae_is_deleted_by_siae.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.9 on 2024-02-22 13:01 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("tenders", "0077_alter_tender_siae_kind"), + ] + + operations = [ + migrations.AddField( + model_name="tendersiae", + name="is_deleted_by_siae", + field=models.BooleanField(db_index=True, default=False, verbose_name="Supprimé par l'utilisateur ?"), + ), + ] diff --git a/lemarche/tenders/models.py b/lemarche/tenders/models.py index cdc966730..ffd115256 100644 --- a/lemarche/tenders/models.py +++ b/lemarche/tenders/models.py @@ -948,6 +948,7 @@ class TenderSiae(models.Model): default=tender_constants.TENDER_SIAE_SOURCE_EMAIL, ) found_with_ai = models.BooleanField("Trouvé par l'IA", default=False) + is_deleted_by_siae = models.BooleanField("Supprimé par l'utilisateur ?", default=False, db_index=True) # stats: relation email_send_date = models.DateTimeField("Date d'envoi de l'e-mail", blank=True, null=True) From ed28fa0ec6704c362ee6875124dfd5cf9a6333e1 Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Mon, 4 Mar 2024 22:23:41 +0100 Subject: [PATCH 2/9] add current siae in to the context --- lemarche/templates/tenders/_list_item_siae.html | 2 +- lemarche/www/tenders/views.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lemarche/templates/tenders/_list_item_siae.html b/lemarche/templates/tenders/_list_item_siae.html index fe5bfbeaa..725bcdf63 100644 --- a/lemarche/templates/tenders/_list_item_siae.html +++ b/lemarche/templates/tenders/_list_item_siae.html @@ -16,7 +16,7 @@ {% endif %} Disponible jusqu'au : {{ tender.deadline_date|default:"" }} - {% if not tender.tendersiae_set.first.detail_display_date %} + {% if not current_siae.detail_display_date %} Nouveau {% endif %} {% endif %} diff --git a/lemarche/www/tenders/views.py b/lemarche/www/tenders/views.py index 555c72ed6..914e2c9c7 100644 --- a/lemarche/www/tenders/views.py +++ b/lemarche/www/tenders/views.py @@ -2,7 +2,6 @@ from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.core.paginator import Paginator -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 @@ -277,6 +276,7 @@ class TenderListView(LoginRequiredMixin, ListView): paginate_by = 10 paginator_class = Paginator status = None + siae: Siae = None def get_queryset(self): """ @@ -288,11 +288,9 @@ def get_queryset(self): if user.kind == User.KIND_SIAE and user.siaes: siaes = user.siaes.all() if siaes: - # filtered prefetch to get detail_display_date on tendersiae_set related to user's siaes - tendersiae_qs = TenderSiae.objects.filter(siae__in=siaes) - qs = Tender.objects.filter_with_siaes(siaes).prefetch_related( - Prefetch("tendersiae_set", queryset=tendersiae_qs) - ) + # we get the first siae by default + self.siae = siaes[0] + qs = Tender.objects.filter_with_siaes(siaes) else: qs = Tender.objects.by_user(user).with_siae_stats() if self.status: @@ -326,6 +324,8 @@ def get_context_data(self, **kwargs): context["title_kind_sourcing_siae"] = TITLE_KIND_SOURCING_SIAE context["tender_constants"] = tender_constants context["filter_form"] = self.filter_form + if self.siae: + context["current_siae"] = self.siae return context From ef08228122a79ea771903d4faace8e01b1d3e1ca Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Wed, 6 Mar 2024 15:42:20 +0100 Subject: [PATCH 3/9] add new annotation --- lemarche/tenders/models.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/lemarche/tenders/models.py b/lemarche/tenders/models.py index ffd115256..b40b5d961 100644 --- a/lemarche/tenders/models.py +++ b/lemarche/tenders/models.py @@ -4,7 +4,20 @@ from django.conf import settings from django.contrib.contenttypes.fields import GenericRelation from django.db import IntegrityError, models, transaction -from django.db.models import BooleanField, Case, Count, ExpressionWrapper, F, IntegerField, Q, Sum, Value, When +from django.db.models import ( + BooleanField, + Case, + Count, + Exists, + ExpressionWrapper, + F, + IntegerField, + OuterRef, + Q, + Sum, + Value, + When, +) from django.db.models.functions import Greatest from django.urls import reverse from django.utils import timezone @@ -136,13 +149,28 @@ def in_sectors(self, sectors): else: return self + def with_is_new_for_siaes(self, siaes, limit_date=datetime.today()): + tender_siae_subquery = TenderSiae.objects.filter( + tender=OuterRef("pk"), + siae__in=siaes, + detail_display_date__isnull=False, + ) + return self.annotate( + is_new_for_siaes=Exists( + # if the tender deadline_date is less than limite date the tender is not new + # that why we user this filter + tender_siae_subquery.filter(tender__deadline_date__lt=limit_date) + ) + ) + def filter_with_siaes(self, siaes): """ Return the list of tenders corresponding to the list of - we return only sent tenders - the tender-siae matching has already been done with filter_with_tender() + - with annotation to new if it's new for siaes """ - return self.sent().filter(tendersiae__siae__in=siaes).distinct() + return self.sent().filter(tendersiae__siae__in=siaes).distinct().with_is_new_for_siaes(siaes) def with_deadline_date_is_outdated(self, limit_date=datetime.today()): return self.annotate( From 8f71c464d842e61f20ccb6e85886efca1fe2630c Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Wed, 6 Mar 2024 16:48:36 +0100 Subject: [PATCH 4/9] allow to delete siaetenders --- .../templates/tenders/_list_item_siae.html | 29 ++++++++++++++----- lemarche/www/tenders/urls.py | 6 ++++ lemarche/www/tenders/views.py | 19 ++++++++++-- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/lemarche/templates/tenders/_list_item_siae.html b/lemarche/templates/tenders/_list_item_siae.html index 725bcdf63..679cbe8f7 100644 --- a/lemarche/templates/tenders/_list_item_siae.html +++ b/lemarche/templates/tenders/_list_item_siae.html @@ -1,9 +1,8 @@ {% load static humanize %} - -