Exemplo n.º 1
0
def new(request):
    selected_user = None

    if request.POST:
        form = NewMessageForm(request.user, request.POST)

        if form.is_valid():
            if form.cleaned_data['user']:
                m = Message(user=form.cleaned_data['user'],
                            author=request.user,
                            content=form.cleaned_data['content'],
                            subject=form.cleaned_data['subject'])
                m.save()
                send_new_message_notification.delay(m.pk)
            elif form.cleaned_data['team']:
                now = datetime.now()
                # TODO: Move this into a task for performance?
                language = form.cleaned_data['language']
                # We create messages using bulk_create, so that only one transaction is needed
                # But that means that we need to sort out the pk of newly created messages to
                # be able to send the notifications
                message_list = []
                for member in form.cleaned_data['team'].members.all():
                    if member.user != request.user:
                        if (len(language) == 0) or (language in set(
                                UserLanguage.objects.filter(
                                    user__exact=member.user).values_list(
                                        'language', flat=True))):
                            message_list.append(
                                Message(user=member.user,
                                        author=request.user,
                                        content=form.cleaned_data['content'],
                                        subject=form.cleaned_data['subject']))
                Message.objects.bulk_create(message_list)
                new_messages_ids = Message.objects.filter(
                    created__gt=now).values_list('pk', flat=True)
                for pk in new_messages_ids:
                    send_new_message_notification.delay(pk)
            messages.success(request, _(u'Message sent.'))
            return HttpResponseRedirect(reverse('messages:inbox'))
        else:
            if request.GET.get('user'):
                selected_user = User.objects.get(username=request.GET['user'])
    else:
        form = NewMessageForm(request.user)

        if request.GET.get('user'):
            selected_user = User.objects.get(username=request.GET['user'])

    return {
        'selected_user': selected_user,
        'user_info': request.user,
        'form': form,
    }
Exemplo n.º 2
0
    def send(self, rdata, user):
        if not user.is_authenticated():
            return {'error': _('You should be authenticated.')}

        form = SendMessageForm(user, rdata)
        if form.is_valid():
            m = form.save()
            
            send_new_message_notification.delay(m.pk)
        else:
            return dict(errors=form.get_errors())

        return {}
Exemplo n.º 3
0
def new(request):
    selected_user = None

    if request.POST:
        form = NewMessageForm(request.user, request.POST)

        if form.is_valid():
            if form.cleaned_data['user']:
                m = Message(user=form.cleaned_data['user'],
                            author=request.user,
                            content=form.cleaned_data['content'],
                            subject=form.cleaned_data['subject'])
                m.save()
                send_new_message_notification.delay(m.pk)
            elif form.cleaned_data['team']:
                # TODO: Move this into a task for performance?
                # Once we switch to Django 1.4, this should be replaced with
                # `bulk_create`.
                for member in form.cleaned_data['team'].members.all():
                    if member.user != request.user:
                        m = Message(user=member.user,
                                    author=request.user,
                                    content=form.cleaned_data['content'],
                                    subject=form.cleaned_data['subject'])
                        m.save()
                        send_new_message_notification.delay(m.pk)

            messages.success(request, _(u'Message sent.'))
            return HttpResponseRedirect(reverse('messages:inbox'))
        else:
            if request.GET.get('user'):
                selected_user = User.objects.get(username=request.GET['user'])
    else:
        form = NewMessageForm(request.user)

        if request.GET.get('user'):
            selected_user = User.objects.get(username=request.GET['user'])

    return {
        'selected_user': selected_user,
        'user_info': request.user,
        'form': form,
    }
Exemplo n.º 4
0
    def on_message_saved(self, sender, instance, created, *args, **kwargs):
        from messages.tasks import send_new_message_notification

        if created and instance.user.changes_notification:
            send_new_message_notification.delay(instance.pk)
Exemplo n.º 5
0
def new(request):
    selected_user = None

    if request.POST:
        form = NewMessageForm(request.user, request.POST)

        if form.is_valid():
            if form.cleaned_data['user']:
                m = Message(user=form.cleaned_data['user'],
                            author=request.user,
                            message_type='M',
                            content=form.cleaned_data['content'],
                            subject=form.cleaned_data['subject'])
                m.save()
                send_new_message_notification.delay(m.pk)
            elif form.cleaned_data['team']:
                now = datetime.now()
                # TODO: Move this into a task for performance?
                language = form.cleaned_data['language']
                # We create messages using bulk_create, so that only one transaction is needed
                # But that means that we need to sort out the pk of newly created messages to
                # be able to send the notifications
                message_list = []
                members = []
                if len(language) == 0:
                    members = map(
                        lambda member: member.user,
                        form.cleaned_data['team'].members.all().exclude(
                            user__exact=request.user).select_related('user'))
                else:
                    members = map(
                        lambda member: member.user,
                        UserLanguage.objects.filter(
                            user__in=form.cleaned_data['team'].members.values(
                                'user')).filter(language__exact=language).
                        exclude(
                            user__exact=request.user).select_related('user'))
                for member in members:
                    message_list.append(
                        Message(user=member,
                                author=request.user,
                                message_type='M',
                                content=form.cleaned_data['content'],
                                subject=form.cleaned_data['subject']))
                Message.objects.bulk_create(message_list, batch_size=500)
                new_messages_ids = Message.objects.filter(
                    created__gt=now).values_list('pk', flat=True)
                # Creating a bunch of reasonably-sized tasks
                batch = 0
                batch_size = 1000
                while batch < len(new_messages_ids):
                    send_new_messages_notifications.delay(
                        new_messages_ids[batch:batch + batch_size])
                    batch += batch_size

            messages.success(request, _(u'Message sent.'))
            return HttpResponseRedirect(reverse('messages:inbox'))
        else:
            if request.GET.get('user'):
                selected_user = User.objects.get(username=request.GET['user'])
    else:
        form = NewMessageForm(request.user)

        if request.GET.get('user'):
            selected_user = User.objects.get(username=request.GET['user'])

    return {
        'selected_user': selected_user,
        'user_info': request.user,
        'form': form,
    }
Exemplo n.º 6
0
    def on_message_saved(self, sender, instance, created, *args, **kwargs):
        from messages.tasks import send_new_message_notification

        if created and instance.user.changes_notification:
            send_new_message_notification.delay(instance.pk)
Exemplo n.º 7
0
def new(request):
    selected_user = None
    reply_msg = None
    reply = request.GET.get('reply')

    if reply:
        try:
            reply_msg = Message.objects.get(pk=reply, user=request.user)
            reply_msg.read = True
            reply_msg.save()
        except (Message.DoesNotExist, ValueError):
            pass

    if request.POST:
        form = NewMessageForm(request.user, request.POST)

        if form.is_valid():
            if form.cleaned_data['user']:
                m = Message(user=form.cleaned_data['user'],
                            author=request.user,
                            message_type='M',
                            content=form.cleaned_data['content'],
                            subject=form.cleaned_data['subject'])
                m.save()
                request.user.sent_message()
                send_new_message_notification.delay(m.pk)
            elif form.cleaned_data['team']:
                # TODO: Move this into a task for performance?
                language = form.cleaned_data['language']
                # We create messages using bulk_create, so that only one transaction is needed
                # But that means that we need to sort out the pk of newly created messages to
                # be able to send the notifications
                message_list = []
                members = []
                if len(language) == 0:
                    members = map(
                        lambda member: member.user,
                        form.cleaned_data['team'].members.all().exclude(
                            user__exact=request.user).select_related('user'))
                else:
                    members = map(
                        lambda member: member.user,
                        UserLanguage.objects.filter(
                            user__in=form.cleaned_data['team'].members.values(
                                'user')).filter(language__exact=language).
                        exclude(
                            user__exact=request.user).select_related('user'))
                for member in members:
                    message_list.append(
                        Message.objects.create(
                            user=member,
                            author=request.user,
                            message_type='M',
                            content=form.cleaned_data['content'],
                            subject=form.cleaned_data['subject']))
                # Creating a bunch of reasonably-sized tasks
                batch = 0
                batch_size = 1000
                while batch < len(message_list):
                    send_new_messages_notifications.delay(
                        [m.pk for m in message_list[batch:batch + batch_size]])
                    batch += batch_size

            messages.success(request, _(u'Message sent.'))
            return HttpResponseRedirect(reverse('messages:inbox'))
        else:
            if request.GET.get('user'):
                selected_user = User.objects.get(username=request.GET['user'])
    else:
        initial = {}
        if reply_msg:
            initial['subject'] = 'RE: {}'.format(reply_msg.subject)
        form = NewMessageForm(request.user, initial=initial)

        if request.GET.get('user'):
            selected_user = User.objects.get(username=request.GET['user'])

    if not selected_user and reply_msg:
        selected_user = reply_msg.author

    return {
        'selected_user': selected_user,
        'user_info': request.user,
        'form': form,
    }