from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.shortcuts import get_object_or_404
from django.template import RequestContext

from user_messages.models import Thread, Message

@login_required
def inbox(request, template_name='user_messages/inbox.html'):
    threads = list(Thread.objects.inbox(request.user))
    threads.sort(key=lambda o: o.latest_message.sent_at, reversed=True)
    return render_to_response(template_name, {'threads': threads}, context_instance=RequestContext(request))

@login_required
def thread_detail(request, thread_id, 
    template_name='user_messages/thread_detail.html'):
    qs = Thread.objects.filter(Q(to_user=request.user) | Q(from_user=request.user))
    thread = get_object_or_404(qs, pk=thread_id)
    if request.user == thread.to_user:
        thread.to_user_unread = False
    else:
        thread.from_user_unread = False
    thread.save()
    return render_to_response(template_name, {'thread': thread}, context_instance=RequestContext(request))

@@ -67,26 +67,116 @@
 ngo.
-db.models import Q
+core.urlresolvers import reverse%0Afrom django.db.models import Q%0Afrom django.http import HttpResponseRedirect
 %0Afro
@@ -263,16 +263,65 @@
 ontext%0A%0A
+from user_messages.forms import MessageReplyForm%0A
 from use
@@ -920,16 +920,316 @@
 ead_id)%0A
+    if request.method == 'POST':%0A        form = MessageReplyForm(request.POST, user=requst.user, thread=thread)%0A        if form.is_valid():%0A            form.save()%0A            return HttpResponseRedirect(reverse(inbox))%0A    else:%0A        form = MessageReplyForm(user=request.user, thread=thread)%0A    
     if r
@@ -1259,16 +1259,20 @@
 o_user:%0A
+    
         
@@ -1297,24 +1297,28 @@
 ead = False%0A
+    
     else:%0A  
@@ -1319,24 +1319,28 @@
 se:%0A        
+    
 thread.from_
@@ -1355,24 +1355,28 @@
 ead = False%0A
+    
     thread.s
@@ -1423,24 +1423,33 @@
 late_name, %7B
+%0A        
 'thread': th
@@ -1452,16 +1452,43 @@
 : thread
+,%0A        'form': form%0A    
 %7D, conte

Add reply support to threads