def get_context_data(self, **kwargs):
        kwargs["referents"] = self.object.memberships.filter(
            membership_type=Membership.MEMBERSHIP_TYPE_REFERENT).order_by(
                "created")
        kwargs["managers"] = self.object.memberships.filter(
            membership_type=Membership.MEMBERSHIP_TYPE_MANAGER).order_by(
                "created")
        kwargs["members"] = self.object.memberships.all().order_by("created")
        kwargs["has_promo_code"] = self.object.tags.filter(
            label=settings.PROMO_CODE_TAG).exists()
        if kwargs["has_promo_code"]:
            kwargs["group_promo_codes"] = get_promo_codes(self.object)

        if is_promo_code_delayed():
            kwargs["promo_code_delay"] = next_promo_code_date()

        kwargs["certifiable"] = (
            self.object.type in settings.CERTIFIABLE_GROUP_TYPES
            or self.object.subtypes.filter(
                label__in=settings.CERTIFIABLE_GROUP_SUBTYPES).exists())
        kwargs["satisfy_requirements"] = len(kwargs["referents"]) > 1
        kwargs["allocation_balance"] = get_balance(self.object)
        kwargs["spending_requests"] = SpendingRequest.objects.filter(
            group=self.object).exclude(status=SpendingRequest.STATUS_PAID)
        kwargs["is_pressero_enabled"] = is_pressero_enabled()

        kwargs["active"] = self.active_panel.get(self.request.POST.get("form"))

        forms = self.get_forms()
        for form_name, form in forms.items():
            kwargs.setdefault(form_name, form)

        return super().get_context_data(**kwargs, )
Esempio n. 2
0
    def get_context_data(self, **kwargs):
        person = self.request.user.person

        rsvped_events = (Event.objects.upcoming().filter(
            attendees=person).order_by("start_time", "end_time"))
        if person.coordinates is not None:
            rsvped_events = rsvped_events.annotate(
                distance=Distance("coordinates", person.coordinates))
        members_groups = list(
            SupportGroup.objects.filter(
                memberships__person=person,
                published=True).order_by("name").annotate(
                    user_is_manager=F("memberships__is_manager")._combine(
                        F("memberships__is_referent"), "OR", False),
                    has_promo_code=Sum(
                        Case(
                            When(tags__label=settings.PROMO_CODE_TAG, then=1),
                            default=0,
                            output_field=BooleanField(),
                        )),
                ))

        if is_promo_code_delayed():
            promo_code_delay = next_promo_code_date()
        else:
            for group in members_groups:
                if group.user_is_manager and group.has_promo_code:
                    group.promo_code = get_next_promo_code(group)
            promo_code_delay = None

        suggested_events = (Event.objects.upcoming(
        ).exclude(rsvps__person=person).filter(
            organizers_groups__in=person.supportgroups.all()
        ).annotate(reason=Value(
            "Cet événément est organisé par un groupe dont vous êtes membre.",
            TextField(),
        )).order_by("start_time"))
        if person.coordinates is not None:
            suggested_events = suggested_events.annotate(
                distance=Distance("coordinates", person.coordinates))

        last_events = (Event.objects.past().filter(Q(attendees=person)
                                                   | Q()).order_by(
                                                       "-start_time",
                                                       "-end_time")[:10])

        if person.coordinates is not None and len(suggested_events) < 10:
            close_events = (Event.objects.upcoming().filter(
                start_time__lt=timezone.now() + timedelta(days=30),
                do_not_list=False,
            ).exclude(pk__in=suggested_events).exclude(
                rsvps__person=person).annotate(reason=Value(
                    "Cet événement se déroule près de chez vous.",
                    TextField())).annotate(
                        distance=Distance("coordinates", person.coordinates)
                    ).order_by("distance")[:(10 - suggested_events.count())])

            suggested_events = close_events.union(suggested_events).order_by(
                "start_time")

        organized_events = (Event.objects.upcoming(
            published_only=False).filter(organizers=person).exclude(
                visibility=Event.VISIBILITY_ADMIN).order_by("start_time"))
        past_organized_events = (Event.objects.past().filter(
            organizers=person).order_by("-start_time", "-end_time")[:10])

        past_reports = (Event.objects.past().exclude(
            rsvps__person=person).exclude(report_content="").filter(
                start_time__gt=timezone.now() - timedelta(days=30),
                do_not_list=False).order_by("-start_time"))
        if person.coordinates is not None:
            past_reports = past_reports.annotate(distance=Distance(
                "coordinates", person.coordinates)).order_by("distance")[:5]

        payments = person.payments.filter(
            status=Payment.STATUS_COMPLETED).order_by("-created")

        kwargs.update({
            "person": person,
            "rsvped_events": group_events_by_day(rsvped_events),
            "members_groups": members_groups,
            "promo_code_delay": promo_code_delay,
            "suggested_events": group_events_by_day(suggested_events),
            "last_events": last_events,
            "past_reports": past_reports,
            "organized_events": organized_events,
            "past_organized_events": past_organized_events,
            "payments": payments,
        })
        return super().get_context_data(**kwargs)