예제 #1
0
def create_transfer_membership_activities(original_group, target_group,
                                          transferred_people):
    if len(transferred_people) == 0:
        return

    Activity.objects.bulk_create([
        Activity(
            type=Activity.TYPE_TRANSFERRED_GROUP_MEMBER,
            status=Activity.STATUS_UNDISPLAYED,
            recipient=r,
            supportgroup=target_group,
            meta={"oldGroup": original_group.name},
        ) for r in transferred_people
    ])

    # Create activities for target group managers
    managers_filter = (Q(
        membership_type__gte=Membership.MEMBERSHIP_TYPE_MANAGER)) & Q(
            notifications_enabled=True)
    managing_membership = target_group.memberships.filter(managers_filter)
    managing_membership_recipients = [
        membership.person for membership in managing_membership
    ]
    Activity.objects.bulk_create([
        Activity(
            type=Activity.TYPE_NEW_MEMBERS_THROUGH_TRANSFER,
            recipient=r,
            supportgroup=target_group,
            meta={
                "oldGroup": original_group.name,
                "transferredMemberships": len(transferred_people),
            },
        ) for r in managing_membership_recipients
    ])
예제 #2
0
def someone_joined_notification(membership, membership_count=1):
    recipients = membership.supportgroup.managers

    Activity.objects.bulk_create([
        Activity(
            type=Activity.TYPE_NEW_MEMBER,
            recipient=r,
            supportgroup=membership.supportgroup,
            individual=membership.person,
        ) for r in recipients
    ])

    membership_limit_notication_steps = [
        membership.supportgroup.MEMBERSHIP_LIMIT + step
        for step in GROUP_MEMBERSHIP_LIMIT_NOTIFICATION_STEPS
        if membership.supportgroup.MEMBERSHIP_LIMIT + step > 0
    ]

    if (membership.supportgroup.is_2022
            and membership_count in membership_limit_notication_steps):
        current_membership_limit_notification_step = membership_limit_notication_steps.index(
            membership_count)
        Activity.objects.bulk_create([
            Activity(
                type=Activity.TYPE_GROUP_MEMBERSHIP_LIMIT_REMINDER,
                recipient=r,
                supportgroup=membership.supportgroup,
                status=Activity.STATUS_UNDISPLAYED,
                meta={
                    "membershipLimit":
                    membership.supportgroup.MEMBERSHIP_LIMIT,
                    "membershipCount":
                    membership_count,
                    "membershipLimitNotificationStep":
                    current_membership_limit_notification_step,
                },
            ) for r in recipients
        ])
    if membership.supportgroup.is_2022 and membership_count in [21, 30]:
        transaction.on_commit(
            partial(
                send_alert_capacity_email.delay,
                membership.supportgroup.pk,
                membership_count,
            ))

    transaction.on_commit(
        partial(send_joined_notification_email.delay, membership.pk))
예제 #3
0
def event_report_form_reminder_notification(event_pk):
    try:
        event = Event.objects.get(pk=event_pk)
        form = event.subtype.report_person_form
    except Event.DoesNotExist:
        return

    if (form is None or not form.published or form.submissions.filter(
            data__reported_event_id=event_pk).exists()):
        return

    meta = {
        "title": form.title,
        "description": form.meta_description,
        "slug": form.slug,
    }

    Activity.objects.bulk_create(
        [
            Activity(
                type=Activity.TYPE_REMINDER_REPORT_FORM_FOR_EVENT,
                recipient=organizer,
                event=event,
                meta=meta,
            ) for organizer in event.organizers.all()
        ],
        send_post_save_signal=True,
    )
예제 #4
0
def event_required_document_reminder_notification(event_pk,
                                                  post=False,
                                                  pre=False):
    activity_type = None
    if post:
        activity_type = Activity.TYPE_REMINDER_DOCS_EVENT_NEXTDAY
    elif pre:
        activity_type = Activity.TYPE_REMINDER_DOCS_EVENT_EVE
    if activity_type is None:
        return

    try:
        event = Event.objects.get(pk=event_pk)
    except Event.DoesNotExist:
        return

    Activity.objects.bulk_create(
        [
            Activity(
                type=activity_type,
                recipient=organizer,
                event=event,
            ) for organizer in event.organizers.all()
        ],
        send_post_save_signal=True,
    )
예제 #5
0
def notify_on_event_report(event_pk):
    try:
        event = Event.objects.get(pk=event_pk)
    except (Event.DoesNotExist):
        return

    Activity.objects.bulk_create(
        Activity(type=Activity.TYPE_NEW_REPORT, recipient=r, event=event)
        for r in event.attendees.all()
    )
예제 #6
0
def get_announcements(person=None):
    today = timezone.now()
    cond = Q(start_date__lt=today) & (Q(end_date__isnull=True) | Q(end_date__gt=today))

    # Les annonces sont affichés :
    # - avec les plus grandes priorités d'abord
    # - à priorité égale, les plus récentes d'abord
    # - à priorité et date de début égales, celles qui disparaitront les premières d'abord
    announcements = (
        Announcement.objects.filter(cond)
        .select_related("segment")
        .order_by("-priority", "-start_date", "end_date")
    )

    if person:
        announcements = (
            announcements.filter(
                pk__in=[
                    a.pk
                    for a in announcements
                    if a.segment is None
                    or a.segment.get_subscribers_queryset()
                    .filter(pk=person.id)
                    .exists()
                ]
            )
            .annotate(
                activity_id=Subquery(
                    Activity.objects.filter(
                        recipient=person, announcement_id=OuterRef("id")
                    ).values("id")[:1]
                ),
            )
            .distinct()
        )
        # Automatically create an activity for the person if none exists for the announcement
        Activity.objects.bulk_create(
            [
                Activity(
                    type=Activity.TYPE_ANNOUNCEMENT,
                    recipient=person,
                    announcement=announcement,
                )
                for announcement in announcements
                if announcement.activity_id is None
            ],
            ignore_conflicts=True,
        )

        return announcements
    else:
        return announcements.filter(segment__isnull=True)
예제 #7
0
def new_event_around_people_notification(event, recipients):
    activity_config = {
        "type": Activity.TYPE_NEW_EVENT_AROUNDME,
        "event": event,
        "status": Activity.STATUS_UNDISPLAYED,
    }
    if event.organizers_groups.count() > 0:
        activity_config["supportgroup"] = event.organizers_groups.first()
    else:
        activity_config["individual"] = event.organizers.first()

    Activity.objects.bulk_create([
        Activity(
            **activity_config,
            recipient=recipient,
        ) for recipient in recipients
    ])
예제 #8
0
def geocode_event(event_pk):
    try:
        event = Event.objects.get(pk=event_pk)
    except Event.DoesNotExist:
        return

    geocode_element(event)
    event.save()

    if (
        event.coordinates_type is not None
        and event.coordinates_type >= Event.COORDINATES_NO_POSITION
    ):
        Activity.objects.bulk_create(
            Activity(
                type=Activity.TYPE_WAITING_LOCATION_EVENT, recipient=r, event=event
            )
            for r in event.organizers.all()
        )
예제 #9
0
def new_comment_restricted_notifications(comment):

    message_initial = comment.message
    allowed_memberships = message_initial.supportgroup.memberships.filter(
        membership_type__gte=message_initial.required_membership_type)
    recipients_id = [
        membership.person.id for membership in allowed_memberships
    ]
    recipients_id = set(recipients_id + [message_initial.author_id])

    # Get only recipients with notification allowed
    recipients_allowed_notif = message_initial.supportgroup.members.filter(
        notification_subscriptions__membership__supportgroup=message_initial.
        supportgroup,
        notification_subscriptions__person__in=recipients_id,
        notification_subscriptions__type=Subscription.SUBSCRIPTION_PUSH,
        notification_subscriptions__activity_type=Activity.
        TYPE_NEW_COMMENT_RESTRICTED,
    )

    Activity.objects.bulk_create(
        [
            Activity(
                individual=comment.author,
                supportgroup=message_initial.supportgroup,
                type=Activity.TYPE_NEW_COMMENT_RESTRICTED,
                recipient=r,
                status=Activity.STATUS_UNDISPLAYED,
                meta={
                    "message": str(message_initial.pk),
                    "comment": str(comment.pk),
                },
            ) for r in recipients_allowed_notif if r.pk != comment.author.pk
        ],
        send_post_save_signal=True,
    )

    send_comment_notification_email.delay(comment.pk)
예제 #10
0
def send_cancellation_notification(event_pk):
    try:
        event = Event.objects.get(pk=event_pk)
    except Event.DoesNotExist:
        return

    # check it is indeed cancelled
    if event.visibility != Event.VISIBILITY_ADMIN:
        return

    event_name = event.name

    notifications_enabled = Q(notifications_enabled=True) & Q(
        person__event_notifications=True
    )

    recipients = [
        rsvp.person
        for rsvp in event.rsvps.filter(notifications_enabled).prefetch_related(
            "person__emails"
        )
    ]

    bindings = {"EVENT_NAME": event_name}

    send_mosaico_email(
        code="EVENT_CANCELLATION",
        subject=_("Un événement auquel vous participiez a été annulé"),
        from_email=settings.EMAIL_FROM,
        recipients=recipients,
        bindings=bindings,
    )

    Activity.objects.bulk_create(
        Activity(type=Activity.TYPE_CANCELLED_EVENT, recipient=r, event=event,)
        for r in recipients
    )
예제 #11
0
def new_message_notifications(message):

    memberships = message.supportgroup.memberships.filter(
        membership_type__gte=message.required_membership_type)
    recipients = Person.objects.filter(
        id__in=memberships.values_list("person_id", flat=True))

    Activity.objects.bulk_create(
        [
            Activity(
                individual=message.author,
                supportgroup=message.supportgroup,
                type=Activity.TYPE_NEW_MESSAGE,
                recipient=r,
                status=Activity.STATUS_UNDISPLAYED,
                meta={
                    "message": str(message.pk),
                },
            ) for r in recipients if r.pk != message.author.pk
        ],
        send_post_save_signal=True,
    )

    send_message_notification_email.delay(message.pk)
예제 #12
0
def someone_joined_notification(membership, membership_count=1):
    recipients = membership.supportgroup.managers
    activity_type = (Activity.TYPE_NEW_MEMBER if membership.is_active_member
                     else Activity.TYPE_NEW_FOLLOWER)
    Activity.objects.bulk_create(
        [
            Activity(
                type=activity_type,
                recipient=r,
                supportgroup=membership.supportgroup,
                individual=membership.person,
                meta={"email": membership.person.email},
            ) for r in recipients
        ],
        send_post_save_signal=True,
    )

    if not membership.is_active_member:
        return

    membership_limit_notication_steps = [
        membership.supportgroup.MEMBERSHIP_LIMIT + step
        for step in GROUP_MEMBERSHIP_LIMIT_NOTIFICATION_STEPS
        if membership.supportgroup.MEMBERSHIP_LIMIT + step > 0
    ]

    if (membership.supportgroup.type == SupportGroup.TYPE_LOCAL_GROUP
            and membership_count in membership_limit_notication_steps):
        current_membership_limit_notification_step = (
            membership_limit_notication_steps.index(membership_count))
        Activity.objects.bulk_create(
            [
                Activity(
                    type=Activity.TYPE_GROUP_MEMBERSHIP_LIMIT_REMINDER,
                    recipient=r,
                    supportgroup=membership.supportgroup,
                    status=Activity.STATUS_UNDISPLAYED,
                    meta={
                        "membershipLimit":
                        membership.supportgroup.MEMBERSHIP_LIMIT,
                        "membershipCount":
                        membership_count,
                        "membershipLimitNotificationStep":
                        current_membership_limit_notification_step,
                    },
                ) for r in recipients
            ],
            send_post_save_signal=True,
        )
    if (membership.supportgroup.type == SupportGroup.TYPE_LOCAL_GROUP
            and membership_count in [
                21,
                # 30, (Temporarily disabled)
            ]):
        transaction.on_commit(
            partial(
                send_alert_capacity_email.delay,
                membership.supportgroup.pk,
                membership_count,
            ))

    transaction.on_commit(
        partial(send_joined_notification_email.delay, membership.pk))
예제 #13
0
def new_comment_notifications(comment):

    if comment.message.required_membership_type > Membership.MEMBERSHIP_TYPE_FOLLOWER:
        new_comment_restricted_notifications(comment)
        return

    message_initial = comment.message
    comment_authors = list(
        message_initial.comments.values_list("author_id", flat=True))
    comment_authors = set(comment_authors + [message_initial.author_id])

    participant_recipients = message_initial.supportgroup.members.filter(
        notification_subscriptions__membership__supportgroup=message_initial.
        supportgroup,
        notification_subscriptions__person__in=comment_authors,
        notification_subscriptions__type=Subscription.SUBSCRIPTION_PUSH,
        notification_subscriptions__activity_type=Activity.
        TYPE_NEW_COMMENT_RESTRICTED,
    )

    Activity.objects.bulk_create(
        [
            Activity(
                individual=comment.author,
                supportgroup=message_initial.supportgroup,
                type=Activity.TYPE_NEW_COMMENT_RESTRICTED,
                recipient=r,
                status=Activity.STATUS_UNDISPLAYED,
                meta={
                    "message": str(message_initial.pk),
                    "comment": str(comment.pk),
                },
            ) for r in participant_recipients if r.pk != comment.author.pk
        ],
        send_post_save_signal=True,
    )

    other_recipients = message_initial.supportgroup.members.exclude(
        id__in=participant_recipients.values_list("id", flat=True)
    ).filter(
        notification_subscriptions__membership__supportgroup=message_initial.
        supportgroup,
        notification_subscriptions__type=Subscription.SUBSCRIPTION_PUSH,
        notification_subscriptions__activity_type=Activity.TYPE_NEW_COMMENT,
    )

    Activity.objects.bulk_create(
        [
            Activity(
                individual=comment.author,
                supportgroup=message_initial.supportgroup,
                type=Activity.TYPE_NEW_COMMENT,
                recipient=r,
                status=Activity.STATUS_UNDISPLAYED,
                meta={
                    "message": str(message_initial.pk),
                    "comment": str(comment.pk),
                },
            ) for r in other_recipients if r.pk != comment.author.pk
        ],
        send_post_save_signal=True,
    )

    send_comment_notification_email.delay(comment.pk)