diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-09-21 01:45:45 -0400 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-09-21 01:45:45 -0400 |
commit | a202578af77b8ecf66cb58a7f535481818db91e5 (patch) | |
tree | 362008396d0c9f67e4d8e306e9db29aed6580376 /group_messaging/views.py | |
parent | 5ac09a8bae3b095bd268866285e8a0a5bd35aa51 (diff) | |
download | askbot-a202578af77b8ecf66cb58a7f535481818db91e5.tar.gz askbot-a202578af77b8ecf66cb58a7f535481818db91e5.tar.bz2 askbot-a202578af77b8ecf66cb58a7f535481818db91e5.zip |
made possibility to display threads as new/seen
Diffstat (limited to 'group_messaging/views.py')
-rw-r--r-- | group_messaging/views.py | 72 |
1 files changed, 59 insertions, 13 deletions
diff --git a/group_messaging/views.py b/group_messaging/views.py index 9d324d62..8c06ff99 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 @@ -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,15 +91,29 @@ 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') + 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 NewResponse(InboxView): @@ -121,9 +139,37 @@ 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) - threads = threads.values('id', 'headline') - return {'threads': threads} + try: + last_visit = LastVisitTime.objects.get(user=request.user) + except LastVisitTime.DoesNotExist: + timestamp = datetime.datetime(2010, 3, 24)#day of askbot + last_visit = LastVisitTime(user=request.user, at=timestamp) + + + #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 + status = 'seen' + if thread.last_active_at > last_visit.at: + status = 'new' + thread_data['status'] = status + #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) + threads_data[thread.id] = thread_data + + #after we have all the data - update the last visit time + last_visit.at = datetime.datetime.now() + last_visit.save() + + return {'threads': threads, 'threads_data': threads_data} class SendersList(InboxView): |