def get_context_data(self, **kwargs): person = self.request.user.person mandats = [] for type, model in types_elus.items(): qs = model.objects.filter(person=person) if type == "municipal": qs = qs.annotate(epci=Case( When( ~Q(communautaire=MandatMunicipal. MANDAT_EPCI_PAS_DE_MANDAT), then=F("conseil__epci__nom"), ), default=None, output_field=CharField(), )) else: qs = qs.annotate(epci=Value(None, output_field=CharField())) mandats.extend(qs) if not mandats or person.membre_reseau_elus == Person.MEMBRE_RESEAU_EXCLUS: kwargs["form"] = None return super().get_context_data( **kwargs, mandats=mandats, person=person, )
def mandats(self, obj): if obj is None: return "-" mandats = [] for attr, model in list(types_elus.items())[1:]: for m in model.objects.filter(person=obj): mandats.append(( reverse(f"admin:elus_mandat{attr}_change", args=(m.id, )), m.titre_complet(conseil_avant=True), )) if not mandats: return "-" return format_html_join(mark_safe("<br>"), '<a href="{}">{}</a>', mandats)
def annotate_elus(self, current=True): from agir.elus.models import types_elus annotations = { f"elu_{label}": klass.objects.filter(person_id=models.OuterRef("id")) for label, klass in types_elus.items() } if current: today = timezone.now().date() annotations = { label: subq.filter(dates__contains=today) for label, subq in annotations.items() } return self.annotate( **{label: models.Exists(subq) for label, subq in annotations.items()} )