summaryrefslogtreecommitdiffstats
path: root/group_messaging/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'group_messaging/views.py')
-rw-r--r--group_messaging/views.py90
1 files changed, 81 insertions, 9 deletions
diff --git a/group_messaging/views.py b/group_messaging/views.py
index 289961ff..d4d646ec 100644
--- a/group_messaging/views.py
+++ b/group_messaging/views.py
@@ -12,12 +12,14 @@ import copy
import datetime
from coffin.template.loader import get_template
from django.contrib.auth.models import User
+from django.db import models
from django.forms import IntegerField
from django.http import HttpResponse
from django.http import HttpResponseNotAllowed
from django.http import HttpResponseForbidden
from django.utils import simplejson
from group_messaging.models import Message
+from group_messaging.models import MessageMemo
from group_messaging.models import SenderList
from group_messaging.models import LastVisitTime
from group_messaging.models import get_personal_group_by_user_id
@@ -104,7 +106,12 @@ class NewThread(InboxView):
if missing:
result['success'] = False
result['missing_users'] = missing
- else:
+
+ if request.user.username in usernames:
+ result['success'] = False
+ result['self_message'] = True
+
+ if result.get('success', True):
recipients = get_personal_groups_for_users(users)
message = Message.objects.create_thread(
sender=request.user,
@@ -139,6 +146,7 @@ class PostReply(InboxView):
template_name='group_messaging/stored_message.html'
)
+
class ThreadsList(InboxView):
"""shows list of threads for a given user"""
template_name = 'group_messaging/threads_list.html'
@@ -147,11 +155,24 @@ class ThreadsList(InboxView):
def get_context(self, request):
"""returns thread list data"""
#get threads and the last visit time
- threads = Message.objects.get_threads_for_user(request.user)
+ sender_id = IntegerField().clean(request.REQUEST.get('sender_id', '-1'))
+ if sender_id == -2:
+ threads = Message.objects.get_threads(
+ recipient=request.user,
+ deleted=True
+ )
+ elif sender_id == -1:
+ threads = Message.objects.get_threads(recipient=request.user)
+ elif sender_id == request.user.id:
+ threads = Message.objects.get_sent_threads(sender=request.user)
+ else:
+ sender = User.objects.get(id=sender_id)
+ threads = Message.objects.get_threads(
+ recipient=request.user,
+ sender=sender
+ )
- sender_id = IntegerField().clean(request.GET.get('sender_id', '-1'))
- if sender_id != -1:
- threads = threads.filter(sender__id=sender_id)
+ threads = threads.order_by('-last_active_at')
#for each thread we need to know if there is something
#unread for the user - to mark "new" threads as bold
@@ -168,6 +189,17 @@ class ThreadsList(InboxView):
thread_data['thread'] = thread
threads_data[thread.id] = thread_data
+ ids = [thread.id for thread in threads]
+ counts = Message.objects.filter(
+ id__in=ids
+ ).annotate(
+ responses_count=models.Count('descendants')
+ ).values('id', 'responses_count')
+ for count in counts:
+ thread_id = count['id']
+ responses_count = count['responses_count']
+ threads_data[thread_id]['responses_count'] = responses_count
+
last_visit_times = LastVisitTime.objects.filter(
user=request.user,
message__in=threads
@@ -177,9 +209,49 @@ class ThreadsList(InboxView):
if thread_data['thread'].last_active_at <= last_visit.at:
thread_data['status'] = 'seen'
- #after we have all the data - update the last visit time
- last_visit_times.update(at=datetime.datetime.now())
- return {'threads': threads, 'threads_data': threads_data}
+ return {
+ 'threads': threads,
+ 'threads_count': threads.count(),
+ 'threads_data': threads_data,
+ 'sender_id': sender_id
+ }
+
+
+class DeleteOrRestoreThread(ThreadsList):
+ """subclassing :class:`ThreadsList`, because deletion
+ or restoring of thread needs subsequent refreshing
+ of the threads list"""
+
+ http_method_list = ('POST',)
+
+ def post(self, request, thread_id=None):
+ """process the post request:
+ * delete or restore thread
+ * recalculate the threads list and return it for display
+ by reusing the threads list "get" function
+ """
+ #part of the threads list context
+ sender_id = IntegerField().clean(request.POST['sender_id'])
+
+ #a little cryptic, but works - sender_id==-2 means deleted post
+ if sender_id == -2:
+ action = 'restore'
+ else:
+ action = 'delete'
+
+ thread = Message.objects.get(id=thread_id)
+ memo, created = MessageMemo.objects.get_or_create(
+ user=request.user,
+ message=thread
+ )
+ if action == 'delete':
+ memo.status = MessageMemo.ARCHIVED
+ else:
+ memo.status = MessageMemo.SEEN
+ memo.save()
+
+ context = self.get_context(request)
+ return self.render_to_response(context)
class SendersList(InboxView):
@@ -191,7 +263,7 @@ class SendersList(InboxView):
"""get data about senders for the user"""
senders = SenderList.objects.get_senders_for_user(request.user)
senders = senders.values('id', 'username')
- return {'senders': senders}
+ return {'senders': senders, 'request_user_id': request.user.id}
class ThreadDetails(InboxView):