Example #1
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():
         form.save()
     else:
         return dict(errors=form.get_errors())
         
     return {}
Example #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 {}
Example #3
0
def send_message_form(context, receiver, link=False):
    context['send_message_form'] = SendMessageForm(
        context['user'], initial={'user': receiver.pk})
    context['receiver'] = receiver
    context['form_id'] = 'send-message-form-%s' % receiver.pk
    context['link'] = link
    return context
Example #4
0
def index(request, message_pk=None):
    user = request.user
    qs = Message.objects.for_user(user)

    extra_context = {
        'send_message_form': SendMessageForm(request.user, auto_id='message_form_id_%s'),
        'messages_display': True
    }
    
    reply = request.GET.get('reply')

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

    return object_list(request, queryset=qs,
                       paginate_by=MESSAGES_ON_PAGE,
                       template_name='messages/index.html',
                       template_object_name='message',
                       extra_context=extra_context)
Example #5
0
def message(request, message_id):
    user = request.user
    messages = Message.objects.for_user_or_author(user).filter(id=message_id)
    if len(messages) != 1:
        return HttpResponseForbidden("Not allowed")
    hide_thread = request.GET.get('hide_thread')
    message_thread = Message.objects.thread(messages[0], user)
    message_thread_length = message_thread.count()
    if not hide_thread:
        messages = message_thread

    reply = request.GET.get('reply')

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

    messages.filter(user=user).update(read=True)

    extra_context = {
        'send_message_form':
        SendMessageForm(request.user, auto_id='message_form_id_%s'),
        'messages_display':
        True,
        'user_info':
        user,
        'subject':
        messages[0].subject,
        'mid':
        message_id,
        'thread_length':
        message_thread_length
    }

    response = object_list(request,
                           queryset=messages,
                           paginate_by=MESSAGES_ON_PAGE,
                           template_name='messages/message.html',
                           template_object_name='message',
                           extra_context=extra_context)
    try:
        last_message = messages[0]
        max_age = 60 * 60 * 24 * 365
        expires = cookie_date(time.time() + max_age)
        response.set_cookie(Message.hide_cookie_name, last_message.pk, max_age,
                            expires)
    except Message.DoesNotExist:
        pass
    return response
Example #6
0
def sent(request):
    qs = Message.objects.for_author(request.user)
    extra_context = {
        'send_message_form': SendMessageForm(request.user, auto_id='message_form_id_%s'),
        'messages_display': True        
    }
    return object_list(request, queryset=qs,
                       paginate_by=MESSAGES_ON_PAGE,
                       template_name='messages/sent.html',
                       template_object_name='message',
                       extra_context=extra_context)    
Example #7
0
def inbox(request, message_pk=None):
    user = request.user
    qs = Message.objects.for_user(user)

    extra_context = {
        'send_message_form':
        SendMessageForm(request.user, auto_id='message_form_id_%s'),
        'messages_display':
        True,
        'user_info':
        user
    }

    type_filter = request.GET.get('type')
    if type_filter:
        if type_filter != 'any':
            qs = qs.filter(message_type=type_filter)

    reply = request.GET.get('reply')

    if reply:
        try:
            reply_msg = Message.objects.get(pk=reply, user=user)
            reply_msg.read = True
            reply_msg.save()
            extra_context['reply_msg'] = reply_msg
        except (Message.DoesNotExist, ValueError):
            pass
    filtered = bool(set(request.GET.keys()).intersection(['type']))

    extra_context['type_filter'] = type_filter
    extra_context['filtered'] = filtered

    response = object_list(request,
                           queryset=qs,
                           paginate_by=MESSAGES_ON_PAGE,
                           template_name='messages/inbox.html',
                           template_object_name='message',
                           extra_context=extra_context)
    try:
        last_message = qs[:1].get()
        max_age = 60 * 60 * 24 * 365
        expires = cookie_date(time.time() + max_age)
        response.set_cookie(Message.hide_cookie_name, last_message.pk, max_age,
                            expires)
    except Message.DoesNotExist:
        pass

    return response
Example #8
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():
            form.save()
        else:
            return dict(errors=form.get_errors())

        return {}
Example #9
0
def inbox(request, message_pk=None):
    user = request.user
    qs = Message.objects.for_user(user)

    extra_context = {
        'send_message_form':
        SendMessageForm(request.user, auto_id='message_form_id_%s'),
        'messages_display':
        True,
        'user_info':
        user
    }

    type_filter = request.GET.get('type')
    if type_filter:
        if type_filter != 'any':
            qs = qs.filter(message_type=type_filter)

    reply = request.GET.get('reply')

    if reply:
        try:
            reply_msg = Message.objects.get(pk=reply, user=user)
            reply_msg.read = True
            reply_msg.save()
            extra_context['reply_msg'] = reply_msg
        except (Message.DoesNotExist, ValueError):
            pass
    filtered = bool(set(request.GET.keys()).intersection(['type']))

    extra_context['type_filter'] = type_filter
    extra_context['filtered'] = filtered

    response = object_list(request,
                           queryset=qs,
                           paginate_by=MESSAGES_ON_PAGE,
                           template_name='messages/inbox.html',
                           template_object_name='message',
                           extra_context=extra_context)
    if qs:
        request.user.set_last_hidden_message_id(request, qs[0].id)

    return response
Example #10
0
def index(request, message_pk=None):
    user = request.user
    qs = Message.objects.for_user(user)

    extra_context = {
        'send_message_form':
        SendMessageForm(request.user, auto_id='message_form_id_%s'),
        'messages_display':
        True
    }

    reply = request.GET.get('reply')

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

    response = object_list(request,
                           queryset=qs,
                           paginate_by=MESSAGES_ON_PAGE,
                           template_name='messages/index.html',
                           template_object_name='message',
                           extra_context=extra_context)
    try:
        last_message = qs[:1].get()
        max_age = 60 * 60 * 24 * 365
        expires = cookie_date(time.time() + max_age)
        response.set_cookie(Message.hide_cookie_name, last_message.pk, max_age,
                            expires)
    except Message.DoesNotExist:
        pass

    return response
Example #11
0
def send_message(request):

    #request.session['unread'] = 3
    #unread = request.session.get('unread')
    # in template folowing code
    # ({{ request.session.unread }})

    current_user = User_info.objects.get(user_id=request.user)
    friends = Friendship.objects.friends_for_user(request.user)
    friends_obj = User_info.objects.filter(user_id__in=friends)
    if request.method == 'POST':
        print 'message POST'
        form = SendMessageForm(request.POST)
        if form.is_valid():
            print 'form is valid'
            if 'send' in request.POST:
                print 'message send'
                cd = form.cleaned_data
                to_user_obj = User_info.objects.get(id=request.POST['to_user'])
                title = (cd['text'])[0:50]
                if Thread.objects.filter(
                        Q(from_user=current_user, to_user=to_user_obj)
                        | Q(from_user=to_user_obj, to_user=current_user)
                ).exists():
                    thread_obj = Thread.objects.get(
                        Q(from_user=current_user, to_user=to_user_obj)
                        | Q(from_user=to_user_obj, to_user=current_user))
                    #thread_obj.from_title = title
                    #thread_obj.to_title = title
                    if thread_obj.from_user == current_user:
                        thread_obj.to_title = title
                        title = (cd['text'])[0:46]
                        thread_obj.from_title = ('Me: ') + title
                        thread_obj.to_unread += 1
                    else:
                        thread_obj.from_title = title
                        title = (cd['text'])[0:46]
                        thread_obj.to_title = ('Me: ') + title
                        thread_obj.from_unread += 1
                    thread_obj.from_status = 'A'
                    thread_obj.to_status = 'A'
                    thread_obj.added = datetime.now()
                    thread_obj.save()
                else:
                    from_title = ('Me: ') + (cd['text'])[0:46]
                    thread_obj = Thread.objects.create(from_user=current_user,
                                                       to_user=to_user_obj,
                                                       from_title=from_title,
                                                       to_title=title,
                                                       to_unread=1)
                message_obj = Message.objects.create(thread=thread_obj,
                                                     from_user=current_user,
                                                     text=(cd['text'])[0:500])
                return HttpResponseRedirect("/messages/" +
                                            to_user_obj.user_id.username)
            elif 'delete' in request.POST:
                print 'message delete'
                thread_del = Thread.objects.get(id=request.POST['thread_id'])
                if thread_del.from_user == current_user:
                    thread_del.from_status = 'D'
                else:
                    thread_del.to_status = 'D'
                thread_del.save()
                """ for loop to make all messages of user in message table as D """
                for message_del in Message.objects.filter(thread=thread_del):
                    if message_del.from_user == current_user:
                        message_del.from_status = 'D'
                    else:
                        message_del.to_status = 'D'
                    message_del.save()
                """ Check to delete from thread if both from and to status is 'D' """
                if thread_del.from_status == 'D' and thread_del.to_status == 'D':
                    delete_message = Message.objects.filter(thread=thread_del)
                    delete_message.delete()
                    thread_del.delete()

            #return HttpResponseRedirect("/messages/")
    else:
        form = SendMessageForm()
    threads = Thread.objects.filter(
        Q(from_user=current_user) | Q(to_user=current_user)).exclude(
            Q(from_user=current_user, from_status='D')
            | Q(to_user=current_user, to_status='D')).order_by("-added")
    return render(
        request, "messages.html", {
            'form': form,
            'threads': threads,
            'friends_obj': friends_obj,
            'current_user': current_user,
        })
Example #12
0
def message_chat(request,
                 username,
                 template='messages_chat.html',
                 extra_context=None):
    print 'in message'
    data = {}
    if extra_context is not None:
        data.update(extra_context)

    current_user = User_info.objects.get(user_id=request.user)
    user_message = get_object_or_404(get_user_model(), username=username)
    to_user_obj = User_info.objects.get(user_id=user_message)
    """ Main check for POST data """
    if request.method == 'POST':
        print 'chat POST'
        form = SendMessageForm(request.POST)
        if form.is_valid():
            """ Check to determine where new message is added or previous message is deleted """
            if 'send_msg' in request.POST:
                print 'chat send'
                cd = form.cleaned_data
                title = (cd['text'])[0:50]
                if Thread.objects.filter(
                        Q(from_user=current_user, to_user=to_user_obj)
                        | Q(from_user=to_user_obj, to_user=current_user)
                ).exists():
                    thread_obj = Thread.objects.get(
                        Q(from_user=current_user, to_user=to_user_obj)
                        | Q(from_user=to_user_obj, to_user=current_user))
                    if thread_obj.from_user == current_user:
                        thread_obj.to_title = title
                        title = (cd['text'])[0:46]
                        thread_obj.from_title = ('Me: ') + title
                        thread_obj.to_unread += 1
                    else:
                        thread_obj.from_title = title
                        title = (cd['text'])[0:46]
                        thread_obj.to_title = ('Me: ') + title
                        thread_obj.from_unread += 1
                    thread_obj.to_status = 'A'
                    thread_obj.from_status = 'A'
                    thread_obj.added = datetime.now()
                    thread_obj.save()
                else:
                    thread_obj = Thread.objects.create(from_user=current_user,
                                                       to_user=to_user_obj,
                                                       from_title=title,
                                                       to_title=title,
                                                       to_unread=1)
                message_obj = Message.objects.create(thread=thread_obj,
                                                     from_user=current_user,
                                                     text=(cd['text'])[0:500])
                return render_to_response(
                    "messages_chat_div.html", {
                        'message_obj': message_obj,
                        'form': form
                    },
                    context_instance=RequestContext(request))

            elif 'delete_msg' in request.POST:
                print 'chat delete'
                thread_del = Thread.objects.get(id=request.POST['thread_id'])
                message_del = Message.objects.get(
                    id=request.POST['message_id'])
                """ check to find max_obj based on user in from_user or to_user """
                if message_del.from_user == current_user:
                    max_before = Message.objects.filter(
                        thread_id=thread_del).exclude(
                            Q(from_user=current_user, from_status='D')
                            | Q(~Q(from_user=current_user), to_status='D')
                        ).aggregate(Max('added'))['added__max']
                    message_del.from_status = 'D'
                    message_del.save()
                    max_after = Message.objects.filter(
                        thread_id=thread_del).exclude(
                            Q(from_user=current_user, from_status='D')
                            | Q(~Q(from_user=current_user), to_status='D')
                        ).aggregate(Max('added'))['added__max']
                    """ check to determine whether after deleting a message, messages are there or not """
                    if Message.objects.filter(added=max_after).exists():
                        max_obj = Message.objects.get(added=max_after)
                    else:
                        max_obj = None
                else:
                    max_before = Message.objects.filter(
                        thread_id=thread_del).exclude(
                            Q(from_user=current_user, from_status='D')
                            | Q(~Q(from_user=current_user), to_status='D')
                        ).aggregate(Max('added'))['added__max']
                    message_del.to_status = 'D'
                    message_del.save()
                    max_after = Message.objects.filter(
                        thread_id=thread_del).exclude(
                            Q(from_user=current_user, from_status='D')
                            | Q(~Q(from_user=current_user), to_status='D')
                        ).aggregate(Max('added'))['added__max']
                    if Message.objects.filter(added=max_after).exists():
                        max_obj = Message.objects.get(added=max_after)
                    else:
                        max_obj = None

                if max_obj:
                    del_title = (max_obj.text)
                    if message_del.added == max_before:
                        if thread_del.from_user == current_user:
                            if max_obj.from_user == current_user:
                                thread_del.from_title = ('Me: ') + (
                                    del_title)[0:46]
                            else:
                                thread_del.from_title = (del_title)[0:50]
                        else:
                            if max_obj.from_user == current_user:
                                thread_del.to_title = ('Me: ') + (
                                    del_title)[0:46]
                            else:
                                thread_del.to_title = (del_title)[0:50]
                        thread_del.save()
                else:
                    if thread_del.from_user == current_user:
                        thread_del.from_status = 'D'
                        thread_del.save()
                    else:
                        thread_del.to_status = 'D'
                        thread_del.save()
                    """ Check to delete from thread if both from and to status is 'D' """
                    if thread_del.from_status == 'D' and thread_del.to_status == 'D':
                        delete_message = Message.objects.filter(
                            thread=thread_del)
                        delete_message.delete()
                        thread_del.delete()
                """ Check to delete from message if both from and to status is 'D'"""
                if message_del.from_status == 'D' and message_del.to_status == 'D':
                    message_del.delete()
            return HttpResponseRedirect("")
    else:
        form = SendMessageForm()
    """ Check to redirect to messages if all mesages in thread got deleted """
    if Thread.objects.filter(
            Q(from_user=current_user, to_user=to_user_obj)
            | Q(from_user=to_user_obj, to_user=current_user)).exists():
        print "thread if"
        chat_thread = Thread.objects.get(
            Q(from_user=current_user, to_user=to_user_obj)
            | Q(from_user=to_user_obj, to_user=current_user))
        # dont know why this condition??
        #if (chat_thread.from_user == current_user and chat_thread.from_status == 'D') or (chat_thread.to_user == current_user and chat_thread.to_status == 'D'):
        #	return HttpResponseRedirect("/messages")
        """ Check for marking unread messages as read on viewing the messages """
        if chat_thread.from_user == current_user:
            chat_thread.from_unread = 0
        else:
            chat_thread.to_unread = 0
        chat_thread.save()

        messages = Message.objects.filter(thread=chat_thread).exclude(
            Q(from_user=current_user, from_status='D')
            | Q(~Q(from_user=current_user), to_status='D')).order_by("-added")
        data['messages'] = messages

    else:
        print "thread else"
        return HttpResponseRedirect("/messages")

    data['form'] = form
    data['current_user'] = current_user
    data['user_message'] = user_message
    return render_to_response(template,
                              data,
                              context_instance=RequestContext(request))
Example #13
0
def send_message_form(context, receiver):
    context['send_message_form'] = SendMessageForm(
        context['user'], initial={'user': receiver.pk})
    context['receiver'] = receiver
    return context