예제 #1
0
파일: tasks.py 프로젝트: thenewguy/connect
def notify_owners_of_group_request(request_id):
    """Emails group owners when there is a request to join a group."""
    from open_connect.groups.models import GroupRequest

    request = GroupRequest.objects.get(pk=request_id)
    context = {'user': request.user, 'group': request.group}
    notification_html = render_to_string(
        'groups/notifications/group_request_owner_notification.html', context)

    for owner in request.group.owners.all():
        send_system_message.delay(
            owner.pk,
            'Request to join group {group}'.format(group=str(request.group)),
            notification_html)
예제 #2
0
def notify_owners_of_group_request(request_id):
    """Emails group owners when there is a request to join a group."""
    from open_connect.groups.models import GroupRequest

    request = GroupRequest.objects.get(pk=request_id)
    context = {
        'user': request.user,
        'group': request.group
    }
    notification_html = render_to_string(
        'groups/notifications/group_request_owner_notification.html', context)

    for owner in request.group.owners.all():
        send_system_message.delay(
            owner.pk,
            'Request to join group {group}'.format(group=str(request.group)),
            notification_html
        )
예제 #3
0
def add_user_to_group(user_id, group_id, notification=None, period=None):
    """A task that will add a user_id to a group.

    Arguments:
    * user_id - User ID for a user
    * group - Group ID for a group

    Optional Kwargs:
    * notification - A tuple of a subject and a message that will be sent from
                     a system user to the user added to the group. The format
                     is ('Subject', 'Message')
    * period - A string indicating the subscription period desired. Defaults to
               the user's default notification period.

    Upon success this task fires the `open_connect.groups.group_member_added`
    signal providing the 'user' and 'group'
    """
    from open_connect.notifications.models import Subscription
    from open_connect.connectmessages.models import Thread, UserThread
    from open_connect.accounts.models import User
    from open_connect.groups.models import Group

    user = User.objects.get(pk=user_id)
    group = Group.objects.select_related('group').get(pk=group_id)

    if not period:
        period = user.group_notification_period

    # Add a Subscription
    subscription, created = Subscription.objects.get_or_create(
        user_id=user.pk, group=group, defaults={'period': period})

    # If the user already has a subscription, bail out
    if user.groups.filter(group=group).exists() or not created:
        return

    # Add to the django group
    user.groups.add(group.group)

    # Find out what threads this user already has. This way we don't
    # have to do a get_or_create for each thread on the off-chance a user
    # is a previous member of the group
    existing_userthreads = UserThread.objects.with_deleted().filter(
        user_id=user.pk, thread__group=group)
    # Update existing userthreads
    existing_userthreads.update(status='active')
    group_threads = Thread.objects.filter(
        group=group
    ).exclude(
        pk__in=existing_userthreads.values_list('thread', flat=True)
    )

    subscribed_to_email = subscription.period != 'none'
    new_userthreads = []
    for thread in group_threads:
        new_userthreads += [
            UserThread(
                user=user,
                thread=thread,
                subscribed_email=subscribed_to_email,
                read=True
            )]

    # Create all the new userthreads
    UserThread.objects.bulk_create(new_userthreads)

    # Clear the user's 'groups joined' cache
    cache.delete(user.cache_key + 'groups_joined')

    # If necessary, notify the user they've been added to the group
    if notification:
        send_system_message.delay(
            user.pk,
            notification[0],
            notification[1]
        )

    # Send a message to the group owners letting them know about the new member.
    subject = u'Your group {group} has a new member.'.format(
        group=group.group.name)
    message = (
        u'The user <a href="{user_link}">{user}</a>'
        u' has joined <a href="{group_link}">{group}</a>.'.format(
            user_link=user.full_url,
            user=user.get_full_name(),
            group_link=group.full_url,
            group=group.group.name
        )
    )
    for owner in group.owners.filter(
            receive_group_join_notifications=True).iterator():
        send_system_message.delay(
            recipient=owner.pk,
            subject=subject,
            message_content=message
        )

    # Send a signal notifying that a group member was added
    group_member_added.send(Group, user=user, group=group)
예제 #4
0
파일: tasks.py 프로젝트: thenewguy/connect
def add_user_to_group(user_id, group_id, notification=None, period=None):
    """A task that will add a user_id to a group.

    Arguments:
    * user_id - User ID for a user
    * group - Group ID for a group

    Optional Kwargs:
    * notification - A tuple of a subject and a message that will be sent from
                     a system user to the user added to the group. The format
                     is ('Subject', 'Message')
    * period - A string indicating the subscription period desired. Defaults to
               the user's default notification period.

    Upon success this task fires the `open_connect.groups.group_member_added`
    signal providing the 'user' and 'group'
    """
    from open_connect.notifications.models import Subscription
    from open_connect.connectmessages.models import Thread, UserThread
    from open_connect.accounts.models import User
    from open_connect.groups.models import Group

    user = User.objects.get(pk=user_id)
    group = Group.objects.select_related('group').get(pk=group_id)

    if not period:
        period = user.group_notification_period

    # Add a Subscription
    subscription, created = Subscription.objects.get_or_create(
        user_id=user.pk, group=group, defaults={'period': period})

    # If the user already has a subscription, bail out
    if user.groups.filter(group=group).exists() or not created:
        return

    # Add to the django group
    user.groups.add(group.group)

    # Find out what threads this user already has. This way we don't
    # have to do a get_or_create for each thread on the off-chance a user
    # is a previous member of the group
    existing_userthreads = UserThread.objects.with_deleted().filter(
        user_id=user.pk, thread__group=group)
    # Update existing userthreads
    existing_userthreads.update(status='active')
    group_threads = Thread.objects.filter(group=group).exclude(
        pk__in=existing_userthreads.values_list('thread', flat=True))

    subscribed_to_email = subscription.period != 'none'
    new_userthreads = []
    for thread in group_threads:
        new_userthreads += [
            UserThread(user=user,
                       thread=thread,
                       subscribed_email=subscribed_to_email,
                       read=True)
        ]

    # Create all the new userthreads
    UserThread.objects.bulk_create(new_userthreads)

    # Clear the user's 'groups joined' cache
    cache.delete(user.cache_key + 'groups_joined')

    # If necessary, notify the user they've been added to the group
    if notification:
        send_system_message.delay(user.pk, notification[0], notification[1])

    # Send a message to the group owners letting them know about the new member.
    subject = u'Your group {group} has a new member.'.format(
        group=group.group.name)
    message = (u'The user <a href="{user_link}">{user}</a>'
               u' has joined <a href="{group_link}">{group}</a>.'.format(
                   user_link=user.full_url,
                   user=user.get_full_name(),
                   group_link=group.full_url,
                   group=group.group.name))
    for owner in group.owners.filter(
            receive_group_join_notifications=True).iterator():
        send_system_message.delay(recipient=owner.pk,
                                  subject=subject,
                                  message_content=message)

    # Send a signal notifying that a group member was added
    group_member_added.send(Group, user=user, group=group)