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.py116
1 files changed, 89 insertions, 27 deletions
diff --git a/group_messaging/views.py b/group_messaging/views.py
index 9d324d62..289961ff 100644
--- a/group_messaging/views.py
+++ b/group_messaging/views.py
@@ -8,7 +8,10 @@ in order to render messages within the page.
Notice that :mod:`urls` module decorates all these functions
and turns them into complete views
"""
+import copy
+import datetime
from coffin.template.loader import get_template
+from django.contrib.auth.models import User
from django.forms import IntegerField
from django.http import HttpResponse
from django.http import HttpResponseNotAllowed
@@ -16,7 +19,9 @@ from django.http import HttpResponseForbidden
from django.utils import simplejson
from group_messaging.models import Message
from group_messaging.models import SenderList
+from group_messaging.models import LastVisitTime
from group_messaging.models import get_personal_group_by_user_id
+from group_messaging.models import get_personal_groups_for_users
class InboxView(object):
"""custom class-based view
@@ -35,9 +40,9 @@ class InboxView(object):
"""
if template_name is None:
template_name = self.template_name
- template = get_template(self.template_name)
+ template = get_template(template_name)
html = template.render(context)
- json = simplejson.dumps({'html': html})
+ json = simplejson.dumps({'html': html, 'success': True})
return HttpResponse(json, mimetype='application/json')
@@ -78,8 +83,7 @@ class InboxView(object):
class NewThread(InboxView):
"""view for creation of new thread"""
- template_name = 'create_thread.html'# contains new thread form
- http_method_list = ('GET', 'POST')
+ http_method_list = ('POST',)
def post(self, request):
"""creates a new thread on behalf of the user
@@ -87,18 +91,32 @@ class NewThread(InboxView):
need to go back to the thread listing view whose
content should be cached in the client'
"""
- username = IntegerField().clean(request.POST['to_username'])
- user = User.objects.get(username=username)
- recipient = get_personal_group_by_user_id(user.id)
- Message.objects.create_thread(
- sender=request.user,
- recipients=[recipient],
- text=request.POST['text']
- )
- return HttpResponse('', mimetype='application/json')
-
-
-class NewResponse(InboxView):
+ usernames = request.POST['to_usernames']
+ usernames = map(lambda v: v.strip(), usernames.split(','))
+ users = User.objects.filter(username__in=usernames)
+
+ missing = copy.copy(usernames)
+ for user in users:
+ if user.username in missing:
+ missing.remove(user.username)
+
+ result = dict()
+ if missing:
+ result['success'] = False
+ result['missing_users'] = missing
+ else:
+ recipients = get_personal_groups_for_users(users)
+ message = Message.objects.create_thread(
+ sender=request.user,
+ recipients=recipients,
+ text=request.POST['text']
+ )
+ result['success'] = True
+ result['message_id'] = message.id
+ return HttpResponse(simplejson.dumps(result), mimetype='application/json')
+
+
+class PostReply(InboxView):
"""view to create a new response"""
http_method_list = ('POST',)
@@ -110,25 +128,63 @@ class NewResponse(InboxView):
text=request.POST['text'],
parent=parent
)
+ last_visit = LastVisitTime.objects.get(
+ message=message.root,
+ user=request.user
+ )
+ last_visit.at = datetime.datetime.now()
+ last_visit.save()
return self.render_to_response(
- {'message': message}, template_name='stored_message.htmtl'
+ {'post': message, 'user': request.user},
+ template_name='group_messaging/stored_message.html'
)
class ThreadsList(InboxView):
"""shows list of threads for a given user"""
- template_name = 'threads_list.html'
+ template_name = 'group_messaging/threads_list.html'
http_method_list = ('GET',)
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)
- threads = threads.values('id', 'headline')
- return {'threads': threads}
+
+ sender_id = IntegerField().clean(request.GET.get('sender_id', '-1'))
+ if sender_id != -1:
+ threads = threads.filter(sender__id=sender_id)
+
+ #for each thread we need to know if there is something
+ #unread for the user - to mark "new" threads as bold
+ threads_data = dict()
+ for thread in threads:
+ thread_data = dict()
+ #determine status
+ thread_data['status'] = 'new'
+ #determine the senders info
+ senders_names = thread.senders_info.split(',')
+ if request.user.username in senders_names:
+ senders_names.remove(request.user.username)
+ thread_data['senders_info'] = ', '.join(senders_names)
+ thread_data['thread'] = thread
+ threads_data[thread.id] = thread_data
+
+ last_visit_times = LastVisitTime.objects.filter(
+ user=request.user,
+ message__in=threads
+ )
+ for last_visit in last_visit_times:
+ thread_data = threads_data[last_visit.message_id]
+ 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}
class SendersList(InboxView):
"""shows list of senders for a user"""
- template_name = 'senders_list.html'
+ template_name = 'group_messaging/senders_list.html'
http_method_names = ('GET',)
def get_context(self, request):
@@ -140,13 +196,19 @@ class SendersList(InboxView):
class ThreadDetails(InboxView):
"""shows entire thread in the unfolded form"""
- template_name = 'thread_details.html'
+ template_name = 'group_messaging/thread_details.html'
http_method_names = ('GET',)
- def get_context(self, request):
+ def get_context(self, request, thread_id=None):
"""shows individual thread"""
- thread_id = IntegerField().clean(request.GET['thread_id'])
#todo: assert that current thread is the root
- messages = Message.objects.filter(root__id=thread_id)
- messages = messages.values('html')
- return {'messages': messages}
+ root = Message.objects.get(id=thread_id)
+ responses = Message.objects.filter(root__id=thread_id)
+ last_visit, created = LastVisitTime.objects.get_or_create(
+ message=root,
+ user=request.user
+ )
+ if created is False:
+ last_visit.at = datetime.datetime.now()
+ last_visit.save()
+ return {'root_message': root, 'responses': responses, 'request': request}