Esempio n. 1
0
    def save(self, *args, **kwargs):
        "Set Resolution if selected"
        instance = super(TicketRecordForm, self).save(*args, **kwargs)
        ticket = self.ticket
        if 'resolution' in self.cleaned_data and self.cleaned_data['resolution']:
            ticket.resolution = self.cleaned_data['body']
            ticket.save()
        
        # Send update if notify clicked
        if 'notify' in self.cleaned_data and self.cleaned_data['notify'] and ticket.caller:
            toaddr = ticket.caller.get_email()
            if ticket.message or toaddr:
                reply = Message()
                try:
                    reply.author = Contact.objects.get(name=settings.SERVICE_MAIL_FROM)
                except Exception:
                    reply.author = instance.sender
                reply.body = instance.body
                reply.auto_notify = False
                if ticket.message:
                    reply.stream = ticket.message.stream
                    reply.reply_to = ticket.message
                else:
                    reply.stream = ticket.queue.message_stream if ticket.queue else None
                    reply.title = "[#%s] %s" % (ticket.reference, ticket.name)
                reply.save()
                if not ticket.message:
                    ticket.message = reply
                reply.recipients.add(ticket.caller)
                email = EmailMessage(reply)
                email.send_email()

        return instance
Esempio n. 2
0
    def save(self, *args, **kwargs):
        "Set Resolution if selected"
        instance = super(TicketRecordForm, self).save(*args, **kwargs)
        ticket = self.ticket
        if "resolution" in self.cleaned_data and self.cleaned_data["resolution"]:
            ticket.resolution = self.cleaned_data["body"]
            ticket.save()

        # Send update if notify clicked
        if "notify" in self.cleaned_data and self.cleaned_data["notify"] and ticket.caller:
            toaddr = ticket.caller.get_email()
            if ticket.message or toaddr:
                reply = Message()
                reply.author = instance.sender
                reply.body = instance.body
                reply.auto_notify = False
                if ticket.message:
                    reply.stream = ticket.message.stream
                    reply.reply_to = ticket.message
                else:
                    reply.stream = ticket.queue.message_stream if ticket.queue else None
                    reply.title = "[#%s] %s" % (ticket.reference, ticket.name)
                reply.save()
                if not ticket.message:
                    ticket.message = reply
                reply.recipients.add(ticket.caller)
                email = EmailMessage(reply)
                email.send_email()

        return instance
Esempio n. 3
0
    def save(self, *args, **kwargs):
        "Set Resolution if selected"
        instance = super(TicketRecordForm, self).save(*args, **kwargs)
        ticket = self.ticket
        if 'resolution' in self.cleaned_data and self.cleaned_data[
                'resolution']:
            ticket.resolution = self.cleaned_data['body']
            ticket.save()

        # Send update if notify clicked
        if 'notify' in self.cleaned_data and self.cleaned_data[
                'notify'] and ticket.caller:
            toaddr = ticket.caller.get_email()
            if ticket.message or toaddr:
                reply = Message()
                reply.author = instance.sender
                reply.body = instance.body
                reply.auto_notify = False
                if ticket.message:
                    reply.stream = ticket.message.stream
                    reply.reply_to = ticket.message
                else:
                    reply.stream = ticket.queue.message_stream if ticket.queue else None
                    reply.title = "[#%s] %s" % (ticket.reference, ticket.name)
                reply.save()
                if not ticket.message:
                    ticket.message = reply
                reply.recipients.add(ticket.caller)
                email = EmailMessage(reply)
                email.send_email()

        return instance
Esempio n. 4
0
File: views.py Progetto: 5n1p/treeio
def messaging_compose(request, response_format='html'):
    "New message page"

    user = request.user.get_profile()

    if request.POST:
        if not 'cancel' in request.POST:
            message = Message()
            message.author = user.get_contact()
            if not message.author:
                return user_denied(request,
                                   message="You can't send message without a Contact Card assigned to you.",
                                   response_format=response_format)

            form = MessageForm(
                request.user.get_profile(), None, None, request.POST, instance=message)
            if form.is_valid():
                message = form.save()
                message.recipients.add(user.get_contact())
                message.set_user_from_request(request)
                message.read_by.add(user)
                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(
                                'treeio.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)
                                message.recipients.add(contact)
                                if created:
                                    contact.set_user_from_request(request)
                except:
                    pass
                # send email to all recipients
                message.send_email()

                return HttpResponseRedirect(reverse('messaging'))
        else:
            return HttpResponseRedirect(reverse('messaging'))

    else:
        form = MessageForm(request.user.get_profile(), None)

    context = _get_default_context(request)
    context.update({'form': form})

    return render_to_response('messaging/message_compose', context,
                              context_instance=RequestContext(request), response_format=response_format)
Esempio n. 5
0
    def create(self, request, *args, **kwargs):
        "Send email to some recipients"

        user = request.user.get_profile()

        if request.data is None:
            return rc.BAD_REQUEST

        if request.data.has_key('stream'):
            stream = getOrNone(MessageStream, request.data['stream'])
            if stream and not user.has_permission(stream, mode='x'):
                return rc.FORBIDDEN

        message = Message()
        message.author = user.get_contact()
        if not message.author:
            return rc.FORBIDDEN

        form = MessageForm(user, None, None, request.data, instance=message)
        if form.is_valid():
            message = form.save()
            message.recipients.add(user.get_contact())
            message.set_user_from_request(request)
            message.read_by.add(user)
            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(
                            'treeio.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)
                            message.recipients.add(contact)
                            if created:
                                contact.set_user_from_request(request)
            except:
                pass
            # send email to all recipients
            message.send_email()
            return message
        else:
            self.status = 400
            return form.errors
Esempio n. 6
0
    def create(self, request, *args, **kwargs):
        "Send email to some recipients"

        user = request.user.get_profile()

        if request.data is None:
            return rc.BAD_REQUEST

        if request.data.has_key('stream'):
            stream = getOrNone(MessageStream, request.data['stream'])
            if stream and not user.has_permission(stream, mode='x'):
                return rc.FORBIDDEN

        message = Message()
        message.author = user.get_contact()
        if not message.author:
            return rc.FORBIDDEN

        form = MessageForm(user, None, None, request.data, instance=message)
        if form.is_valid():
            message = form.save()
            message.recipients.add(user.get_contact())
            message.set_user_from_request(request)
            message.read_by.add(user)
            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(
                            'treeio.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)
                            message.recipients.add(contact)
                            if created:
                                contact.set_user_from_request(request)
            except:
                pass
            # send email to all recipients
            message.send_email()
            return message
        else:
            self.status = 400
            return form.errors
Esempio n. 7
0
def mlist_view(request, mlist_id, response_format='html'):
    "Mailing List view page"

    user = request.user.get_profile()

    mlist = get_object_or_404(MailingList, pk=mlist_id)
    if not request.user.get_profile().has_permission(mlist):
        return user_denied(
            request,
            message="You don't have access to this Mailing List",
            response_format=response_format)

    if request.user.get_profile().has_permission(mlist, mode='x'):
        if request.POST:
            message = Message()
            message.author = request.user.get_profile().get_contact()
            if not message.author:
                return user_denied(
                    request,
                    message=
                    "You can't send message without a Contact Card assigned to you.",
                    response_format=response_format)
            form = MessageForm(request.user.get_profile(),
                               mlist_id,
                               None,
                               request.POST,
                               instance=message)
            if form.is_valid():
                message = form.save()
                message.set_user_from_request(request)
                message.read_by.add(user)
                return HttpResponseRedirect(
                    reverse('messaging_mlist_view', args=[mlist.id]))
        else:
            form = MessageForm(request.user.get_profile(), mlist_id)

    else:
        form = None

    messages = Object.filter_by_request(
        request,
        Message.objects.filter(reply_to__isnull=True,
                               mlist=mlist).order_by('-date_created'))
    context = _get_default_context(request)
    context.update({'messages': messages, 'form': form, 'mlist': mlist})

    return render_to_response('messaging/mlist_view',
                              context,
                              context_instance=RequestContext(request),
                              response_format=response_format)
Esempio n. 8
0
def mlist_view(request, mlist_id, response_format='html'):
    "Mailing List view page"
    
    user = request.user.get_profile()
    
    mlist = get_object_or_404(MailingList, pk=mlist_id)
    if not request.user.get_profile().has_permission(mlist):
        return user_denied(request, message="You don't have access to this Mailing List", 
                           response_format=response_format)
        

    if request.user.get_profile().has_permission(mlist, mode='x'):
        if request.POST:
            message = Message()
            message.author = request.user.get_profile().get_contact()
            if not message.author:
                return user_denied(request, 
                                   message="You can't send message without a Contact Card assigned to you.", 
                                   response_format=response_format)
            form = MessageForm(request.user.get_profile(), mlist_id, None, request.POST, instance=message)
            if form.is_valid():
                message = form.save()
                message.set_user_from_request(request)
                message.read_by.add(user)
                return HttpResponseRedirect(reverse('messaging_mlist_view', args=[mlist.id]))
        else:   
            form = MessageForm(request.user.get_profile(), mlist_id)

    else:
        form = None
        
    messages = Object.filter_by_request(request, 
                                        Message.objects.filter(reply_to__isnull=True, 
                                                               mlist=mlist).order_by('-date_created'))
    context = _get_default_context(request)
    context.update({'messages':messages,
                    'form': form,
                    'mlist':mlist})
    
    return render_to_response('messaging/mlist_view', context,
                              context_instance=RequestContext(request),
                              response_format=response_format)
Esempio n. 9
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.get_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('treeio.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.get_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)
Esempio n. 10
0
def stream_view(request, stream_id, response_format='html'):
    "Stream view page"
    
    user = request.user.get_profile()
    
    stream = get_object_or_404(MessageStream, pk=stream_id)
    if not request.user.get_profile().has_permission(stream):
        return user_denied(request, message="You don't have access to this Stream", 
                           response_format=response_format)
        
    if request.user.get_profile().has_permission(stream, mode='x'):
        if request.POST:
            message = Message()
            message.author = user.get_contact()
            if not message.author:
                return user_denied(request, 
                                   message="You can't send message without a Contact Card assigned to you.", 
                                   response_format=response_format)
    
            form = MessageForm(request.user.get_profile(), None, None, request.POST, instance=message)
            if form.is_valid():
                message = form.save()
                message.recipients.add(user.get_contact())
                message.set_user_from_request(request)
                message.read_by.add(user)
                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('treeio.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)
                                message.recipients.add(contact)
                                if created:
                                    contact.set_user_from_request(request)
                except:
                    pass
                # send email to all recipients
                message.send_email()
                
                return HttpResponseRedirect(reverse('messaging_stream_view', args=[stream.id]))
        else:   
            form = MessageForm(request.user.get_profile(), stream_id)

    else:
        form = None
        
    messages = Object.filter_by_request(request, 
                                        Message.objects.filter(reply_to__isnull=True, 
                                                               stream=stream).order_by('-date_created'))
    context = _get_default_context(request)
    context.update({'messages':messages,
                    'form': form,
                    'stream':stream})
    
    return render_to_response('messaging/stream_view', context,
                              context_instance=RequestContext(request),
                              response_format=response_format)
Esempio n. 11
0
    def update(self, request, *args, **kwargs):
        "Reply to message"

        if request.data is None:
            return rc.BAD_REQUEST

        pkfield = kwargs.get(self.model._meta.pk.name) or request.data.get(
            self.model._meta.pk.name)

        if not pkfield:
            return rc.BAD_REQUEST

        user = request.user.get_profile()

        try:
            message = self.model.objects.get(pk=pkfield)
        except ObjectDoesNotExist:
            return rc.NOT_FOUND

        if not user.has_permission(message):
            return rc.FORBIDDEN

        reply = Message()
        reply.author = user.get_contact()
        if not reply.author:
            return rc.FORBIDDEN

        reply.reply_to = message
        form = MessageReplyForm(
            user, message.stream_id, message, request.data, 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(
                            'treeio.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 reply

        else:
            self.status = 400
            return form.errors
Esempio n. 12
0
    def update(self, request, *args, **kwargs):
        "Reply to message"

        if request.data is None:
            return rc.BAD_REQUEST

        pkfield = kwargs.get(self.model._meta.pk.name) or request.data.get(
            self.model._meta.pk.name)

        if not pkfield:
            return rc.BAD_REQUEST

        user = request.user.get_profile()

        try:
            message = self.model.objects.get(pk=pkfield)
        except ObjectDoesNotExist:
            return rc.NOT_FOUND

        if not user.has_permission(message):
            return rc.FORBIDDEN

        reply = Message()
        reply.author = user.get_contact()
        if not reply.author:
            return rc.FORBIDDEN

        reply.reply_to = message
        form = MessageReplyForm(user,
                                message.stream_id,
                                message,
                                request.data,
                                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(
                            'treeio.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 reply

        else:
            self.status = 400
            return form.errors
Esempio n. 13
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.get_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(
                            'treeio.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.get_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)
Esempio n. 14
0
def messaging_compose(request, response_format='html'):
    "New message page"

    user = request.user.get_profile()

    if request.POST:
        if not 'cancel' in request.POST:
            message = Message()
            message.author = user.get_contact()
            if not message.author:
                return user_denied(
                    request,
                    message=
                    "You can't send message without a Contact Card assigned to you.",
                    response_format=response_format)

            form = MessageForm(request.user.get_profile(),
                               None,
                               None,
                               request.POST,
                               instance=message)
            if form.is_valid():
                message = form.save()
                message.recipients.add(user.get_contact())
                message.set_user_from_request(request)
                message.read_by.add(user)
                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(
                                'treeio.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)
                                message.recipients.add(contact)
                                if created:
                                    contact.set_user_from_request(request)
                except:
                    pass
                # send email to all recipients
                message.send_email()

                return HttpResponseRedirect(reverse('messaging'))
        else:
            return HttpResponseRedirect(reverse('messaging'))

    else:
        form = MessageForm(request.user.get_profile(), None)

    context = _get_default_context(request)
    context.update({'form': form})

    return render_to_response('messaging/message_compose',
                              context,
                              context_instance=RequestContext(request),
                              response_format=response_format)
Esempio n. 15
0
def stream_view(request, stream_id, response_format='html'):
    "Stream view page"

    user = request.user.get_profile()

    stream = get_object_or_404(MessageStream, pk=stream_id)
    if not request.user.get_profile().has_permission(stream):
        return user_denied(request,
                           message="You don't have access to this Stream",
                           response_format=response_format)

    if request.user.get_profile().has_permission(stream, mode='x'):
        if request.POST:
            message = Message()
            message.author = user.get_contact()
            if not message.author:
                return user_denied(
                    request,
                    message=
                    "You can't send message without a Contact Card assigned to you.",
                    response_format=response_format)

            form = MessageForm(request.user.get_profile(),
                               None,
                               None,
                               request.POST,
                               instance=message)
            if form.is_valid():
                message = form.save()
                message.recipients.add(user.get_contact())
                message.set_user_from_request(request)
                message.read_by.add(user)
                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(
                                'treeio.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)
                                message.recipients.add(contact)
                                if created:
                                    contact.set_user_from_request(request)
                except:
                    pass
                # send email to all recipients
                message.send_email()

                return HttpResponseRedirect(
                    reverse('messaging_stream_view', args=[stream.id]))
        else:
            form = MessageForm(request.user.get_profile(), stream_id)

    else:
        form = None

    objects = Object.filter_by_request(
        request,
        Message.objects.filter(reply_to__isnull=True,
                               stream=stream).order_by('-date_created'))
    context = _get_default_context(request)
    context.update({'messages': objects, 'form': form, 'stream': stream})

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