Beispiel #1
0
def msg_reply(request, msg_id):
    """Show group message replies, and process message reply in ajax"""

    if not request.is_ajax():
        raise Http404

    content_type = "application/json; charset=utf-8"
    ctx = {}
    try:
        group_msg = GroupMessage.objects.get(id=msg_id)
    except GroupMessage.DoesNotExist:
        return HttpResponseBadRequest(content_type=content_type)

    if request.method == "POST":
        if not request.user.is_authenticated():
            return HttpResponseBadRequest(json.dumps({"error": "login required"}), content_type=content_type)

        form = MessageReplyForm(request.POST)
        r_status = request.GET.get("r_status")
        # TODO: invalid form
        if form.is_valid():
            msg = form.cleaned_data["message"]

            msg_reply = MessageReply()
            msg_reply.reply_to = group_msg
            msg_reply.from_email = request.user.username
            msg_reply.message = msg
            msg_reply.save()

            grpmsg_reply_added.send(
                sender=MessageReply,
                msg_id=msg_id,
                from_email=request.user.username,
                grpmsg_topic=group_msg.message,
                reply_msg=msg,
            )

            replies = MessageReply.objects.filter(reply_to=group_msg)
            r_num = len(replies)
            if r_num < 4 or r_status == "show":
                ctx["replies"] = replies
            else:
                ctx["replies"] = replies[r_num - 3 :]
            html = render_to_string("group/group_reply_list.html", ctx)
            serialized_data = json.dumps({"r_num": r_num, "html": html})
            return HttpResponse(serialized_data, content_type=content_type)

    else:
        replies = MessageReply.objects.filter(reply_to=group_msg)
        r_num = len(replies)
        ctx["replies"] = replies
        html = render_to_string("group/group_reply_list.html", ctx)
        serialized_data = json.dumps({"r_num": r_num, "html": html})
        return HttpResponse(serialized_data, content_type=content_type)
Beispiel #2
0
def msg_reply(request, msg_id):
    """Show group message replies, and process message reply in ajax"""

    if not request.is_ajax():
        raise Http404

    content_type = 'application/json; charset=utf-8'
    ctx = {}
    try:
        group_msg = GroupMessage.objects.get(id=msg_id)
    except GroupMessage.DoesNotExist:
        return HttpResponseBadRequest(content_type=content_type)

    if request.method == 'POST':
        if not request.user.is_authenticated():
            return HttpResponseBadRequest(json.dumps({
                    "error": "login required"}), content_type=content_type)

        form = MessageReplyForm(request.POST)
        r_status = request.GET.get('r_status')
        # TODO: invalid form
        if form.is_valid():
            msg = form.cleaned_data['message']

            msg_reply = MessageReply()
            msg_reply.reply_to = group_msg
            msg_reply.from_email = request.user.username
            msg_reply.message = msg
            msg_reply.save()

            grpmsg_reply_added.send(sender=MessageReply,
                                    msg_id=msg_id,
                                    from_email=request.user.username,
                                    grpmsg_topic=group_msg.message,
                                    reply_msg=msg)

            replies = MessageReply.objects.filter(reply_to=group_msg)
            r_num = len(replies)
            if r_num < 4 or r_status == 'show':
                ctx['replies'] = replies
            else:
                ctx['replies'] = replies[r_num - 3:]
            html = render_to_string("group/group_reply_list.html", ctx)
            serialized_data = json.dumps({"r_num": r_num, "html": html})
            return HttpResponse(serialized_data, content_type=content_type)

    else:
        replies = MessageReply.objects.filter(reply_to=group_msg)
        r_num = len(replies)
        ctx['replies'] = replies
        html = render_to_string("group/group_reply_list.html", ctx)
        serialized_data = json.dumps({"r_num": r_num, "html": html})
        return HttpResponse(serialized_data, content_type=content_type)
Beispiel #3
0
def msg_reply(request, msg_id):
    """Show group message replies, and process message reply in ajax"""
    
    content_type = 'application/json; charset=utf-8'
    if request.is_ajax():
        ctx = {}
        try:
            group_msg = GroupMessage.objects.get(id=msg_id)
        except GroupMessage.DoesNotExist:
            return HttpResponseBadRequest(content_type=content_type)

        if request.method == 'POST':
            form = MessageReplyForm(request.POST)
            # TODO: invalid form
            if form.is_valid():
                msg = form.cleaned_data['message']

                msg_reply = MessageReply()
                msg_reply.reply_to = group_msg
                msg_reply.from_email = request.user.username
                msg_reply.message = msg
                msg_reply.save()

                # send signal if reply other's message
                if group_msg.from_email != request.user.username:
                    grpmsg_reply_added.send(sender=MessageReply,
                                            msg_id=msg_id,
                                            from_email=request.user.username)
                replies = MessageReply.objects.filter(reply_to=group_msg)
                r_num = len(replies)
                if r_num < 4:
                    ctx['replies'] = replies
                else:
                    ctx['replies'] = replies[r_num - 3:]
                html = render_to_string("group/group_reply_list.html", ctx)
                serialized_data = json.dumps({"r_num": r_num, "html": html})
                return HttpResponse(serialized_data, content_type=content_type)

        else:
            replies = MessageReply.objects.filter(reply_to=group_msg)
            r_num = len(replies)
            ctx['replies'] = replies
            html = render_to_string("group/group_reply_list.html", ctx)
            serialized_data = json.dumps({"r_num": r_num, "html": html})
            return HttpResponse(serialized_data, content_type=content_type)
    else:
        return HttpResponseBadRequest(content_type=content_type)
Beispiel #4
0
def msg_reply(request, msg_id):
    """Show group message replies, and process message reply in ajax"""

    content_type = "application/json; charset=utf-8"
    if request.is_ajax():
        ctx = {}
        if request.method == "POST":
            form = MessageReplyForm(request.POST)

            # TODO: invalid form
            if form.is_valid():
                msg = form.cleaned_data["message"]
                try:
                    group_msg = GroupMessage.objects.get(id=msg_id)
                except GroupMessage.DoesNotExist:
                    return HttpResponseBadRequest(content_type=content_type)

                msg_reply = MessageReply()
                msg_reply.reply_to = group_msg
                msg_reply.from_email = request.user.username
                msg_reply.message = msg
                msg_reply.save()

                # send signal if reply other's message
                if group_msg.from_email != request.user.username:
                    grpmsg_reply_added.send(sender=MessageReply, msg_id=msg_id, from_email=request.user.username)

                ctx["reply"] = msg_reply
                html = render_to_string("group/group_reply_new.html", ctx)

        else:
            try:
                msg = GroupMessage.objects.get(id=msg_id)
            except GroupMessage.DoesNotExist:
                raise HttpResponse(status=400)

            replies = MessageReply.objects.filter(reply_to=msg)
            ctx["replies"] = replies
            html = render_to_string("group/group_reply_list.html", ctx)

        serialized_data = json.dumps({"html": html})
        return HttpResponse(serialized_data, content_type=content_type)
    else:
        return HttpResponseBadRequest(content_type=content_type)
Beispiel #5
0
def messaging_view(request, message_id, response_format='html'):
    "Single message page"

    message = get_object_or_404(Message, pk=message_id)
    user = request.user.profile

    if not user.has_permission(message):
        return user_denied(request,
                           message="You don't have access to this Message",
                           response_format=response_format)

    message.read_by.add(user)

    if request.POST and request.POST.get('body', False):
        "Unread message"

        reply = Message()
        reply.author = user.get_contact()
        if not reply.author:
            return user_denied(
                request,
                message=
                "You can't send message without a Contact Card assigned to you.",
                response_format=response_format)
        reply.reply_to = message
        form = MessageReplyForm(user,
                                message.stream_id,
                                message,
                                request.POST,
                                instance=reply)
        if form.is_valid():
            reply = form.save()
            reply.set_user_from_request(request)
            # Add author to recipients
            reply.recipients.add(reply.author)
            message.read_by.clear()

            try:
                # if email entered create contact and add to recipients
                if 'multicomplete_recipients' in request.POST and request.POST[
                        'multicomplete_recipients']:
                    try:
                        conf = ModuleSetting.get_for_module(
                            'anaf.messaging', 'default_contact_type')[0]
                        default_contact_type = ContactType.objects.get(
                            pk=long(conf.value))
                    except Exception:
                        default_contact_type = None
                    emails = request.POST['multicomplete_recipients'].split(
                        ',')
                    for email in emails:
                        emailstr = unicode(email).strip()
                        if re.match(
                                '[a-zA-Z0-9+_\-\.]+@[0-9a-zA-Z][.-0-9a-zA-Z]*.[a-zA-Z]+',
                                emailstr):
                            contact, created = Contact.get_or_create_by_email(
                                emailstr, contact_type=default_contact_type)
                            reply.recipients.add(contact)
                            if created:
                                contact.set_user_from_request(request)
            except:
                pass

            # Add each recipient of the reply to the original message
            for recipient in reply.recipients.all():
                message.recipients.add(recipient)

            # send email to all recipients
            reply.send_email()

            return HttpResponseRedirect(
                reverse('messaging_message_view', args=[message.id]))

    else:
        form = MessageReplyForm(request.user.profile, message.stream_id,
                                message)

    replies = Object.filter_by_request(
        request,
        Message.objects.filter(reply_to=message).order_by('date_created'))

    context = _get_default_context(request)
    context.update({'message': message, 'messages': replies, 'form': form})

    return render_to_response('messaging/message_view',
                              context,
                              context_instance=RequestContext(request),
                              response_format=response_format)
Beispiel #6
0
def messaging_view(request, message_id, response_format='html'):
    "Single message page"

    message = get_object_or_404(Message, pk=message_id)
    user = request.user.profile

    if not user.has_permission(message):
        return user_denied(request, message="You don't have access to this Message",
                           response_format=response_format)

    message.read_by.add(user)

    if request.POST and request.POST.get('body', False):
        "Unread message"

        reply = Message()
        reply.author = user.get_contact()
        if not reply.author:
            return user_denied(request,
                               message="You can't send message without a Contact Card assigned to you.",
                               response_format=response_format)
        reply.reply_to = message
        form = MessageReplyForm(
            user, message.stream_id, message, request.POST, instance=reply)
        if form.is_valid():
            reply = form.save()
            reply.set_user_from_request(request)
            # Add author to recipients
            reply.recipients.add(reply.author)
            message.read_by.clear()

            try:
                # if email entered create contact and add to recipients
                if 'multicomplete_recipients' in request.POST and request.POST['multicomplete_recipients']:
                    try:
                        conf = ModuleSetting.get_for_module(
                            'anaf.messaging', 'default_contact_type')[0]
                        default_contact_type = ContactType.objects.get(
                            pk=long(conf.value))
                    except Exception:
                        default_contact_type = None
                    emails = request.POST[
                        'multicomplete_recipients'].split(',')
                    for email in emails:
                        emailstr = unicode(email).strip()
                        if re.match('[a-zA-Z0-9+_\-\.]+@[0-9a-zA-Z][.-0-9a-zA-Z]*.[a-zA-Z]+', emailstr):
                            contact, created = Contact.get_or_create_by_email(
                                emailstr, contact_type=default_contact_type)
                            reply.recipients.add(contact)
                            if created:
                                contact.set_user_from_request(request)
            except:
                pass

            # Add each recipient of the reply to the original message
            for recipient in reply.recipients.all():
                message.recipients.add(recipient)

            # send email to all recipients
            reply.send_email()

            return HttpResponseRedirect(reverse('messaging_message_view', args=[message.id]))

    else:
        form = MessageReplyForm(
            request.user.profile, message.stream_id, message)

    replies = Object.filter_by_request(request,
                                       Message.objects.filter(reply_to=message).order_by('date_created'))

    context = _get_default_context(request)
    context.update({'message': message,
                    'messages': replies,
                    'form': form})

    return render_to_response('messaging/message_view', context,
                              context_instance=RequestContext(request),
                              response_format=response_format)