summaryrefslogtreecommitdiffstats
path: root/group_messaging
diff options
context:
space:
mode:
authorEvgeny Fadeev <evgeny.fadeev@gmail.com>2012-09-19 01:58:49 -0400
committerEvgeny Fadeev <evgeny.fadeev@gmail.com>2012-09-19 01:58:49 -0400
commit8adb7e1b244d07e27d040d14e1cfd5e5280ce40b (patch)
tree4401a630ffbf68eeccc4b974cad4fd0a929dfb8f /group_messaging
parent524a7d83c93ff4b671fe128efbb592b3a11f9507 (diff)
downloadaskbot-8adb7e1b244d07e27d040d14e1cfd5e5280ce40b.tar.gz
askbot-8adb7e1b244d07e27d040d14e1cfd5e5280ce40b.tar.bz2
askbot-8adb7e1b244d07e27d040d14e1cfd5e5280ce40b.zip
started building front end for private messaging
Diffstat (limited to 'group_messaging')
-rw-r--r--group_messaging/urls.py22
-rw-r--r--group_messaging/views.py135
2 files changed, 91 insertions, 66 deletions
diff --git a/group_messaging/urls.py b/group_messaging/urls.py
new file mode 100644
index 00000000..eb033751
--- /dev/null
+++ b/group_messaging/urls.py
@@ -0,0 +1,22 @@
+"""url configuration for the group_messaging application"""
+from django.conf.urls.defaults import patterns
+from django.conf.urls.defaults import url
+from group_messaging import views
+
+urlpatterns = patterns('',
+ url(
+ '^threads/$',
+ views.ThreadsList().as_view(),
+ name='get_threads'
+ ),
+ url(
+ '^threads/create/$',
+ views.NewThread().as_view(),
+ name='create_thread'
+ ),
+ url(
+ '^senders/$',
+ views.SendersList().as_view(),
+ name='get_senders'
+ )
+)
diff --git a/group_messaging/views.py b/group_messaging/views.py
index d24833f2..c2e9af93 100644
--- a/group_messaging/views.py
+++ b/group_messaging/views.py
@@ -8,92 +8,98 @@ in order to render messages within the page.
Notice that :mod:`urls` module decorates all these functions
and turns them into complete views
"""
-import functools
-from django.contrib.auth.models import Group
-from django.core.exceptions import PermissionDenied
+from coffin.template.loader import get_template
from django.forms import IntegerField
from django.http import HttpResponse
-from django.shortcuts import render_to_response
-from django.template import RequestContext
-from django.views.decorators.http import require_GET
-from django.views.decorators.http import require_POST
+from django.http import HttpResponseNotAllowed
+from django.http import HttpResponseForbidden
from django.utils import simplejson
-from group_messages.models import Message
-from group_messages.models import MessageMemo
-from group_messages.models import SenderList
-from group_messages.models import get_personal_group_by_id
+from group_messaging.models import Message
+from group_messaging.models import SenderList
+from group_messaging.models import get_personal_group_by_user_id
class InboxView(object):
"""custom class-based view
to be used for pjax use and for generation
- of content in the traditional way"""
- template_name = None #this needs to be set
+ of content in the traditional way, where
+ the only the :method:`get_context` would be used.
+ """
+ template_name = None #used only for the "GET" method
+ http_method_names = ('GET', 'POST')
+
+ def render_to_response(self, context, template_name=None):
+ """like a django's shortcut, except will use
+ template_name from self, if `template_name` is not given.
+ Also, response is packaged as json with an html fragment
+ for the pjax consumption
+ """
+ if template_name is None:
+ template_name = self.template_name
+ template = get_template(self.template_name)
+ html = template.render(context)
+ json = simplejson.dumps({'html': html})
+ return HttpResponse(json, mimetype='application/json')
+
def get(self, request, *args, **kwargs):
+ """view function for the "GET" method"""
context = self.get_context(request, *args, **kwargs)
- #todo: load template with Coffin and render it
- return HttpResponse(json, mimetype='application/json')
+ return self.render_to_response(context)
+
+ def post(self, request, *args, **kwargs):
+ """view function for the "POST" method"""
+ pass
+
+ def dispatch(self, request, *args, **kwargs):
+ """checks that the current request method is allowed
+ and calls the corresponding view function"""
+ if request.method not in self.http_method_names:
+ return HttpResponseNotAllowed()
+ view_func = getattr(self, request.method.lower())
+ return view_func(request, *args, **kwargs)
def get_context(self, request, *args, **kwargs):
- """Should return the context dictionary"""
+ """Returns the context dictionary for the "get"
+ method only"""
return {}
- def as_pjax(self):
+ def as_view(self):
"""returns the view function - for the urls.py"""
def view_function(request, *args, **kwargs):
"""the actual view function"""
- if request.user.is_anonymous():
- raise PermissionDenied()
- if request.is_ajax() is False:
- raise PermissionDenied()
-
- if request.method == 'GET':
- return self.get(request, *args, **kwargs)
- elif request.method == 'POST':
- return self.post(request, *args, **kwargs)
+ if request.user.is_authenticated() and request.is_ajax():
+ view_method = getattr(self, request.method.lower())
+ return view_method(request, *args, **kwargs)
else:
- raise NotImplementedError
- return view_function
-
-
-def require_login(view_func):
- @functools.wraps(view_func)
- def wrapped(request, *args, **kwargs):
- if request.user.is_authenticated():
- return view_func(request, *args, **kwargs)
- else:
- raise PermissionDenied()
- return wrapped
+ return HttpResponseForbidden()
-
-def ajax(view_func):
- @functools.wraps(view_func)
- def wrapped(request, *args, **kwargs):
- if request.is_ajax():
- result = view_func(request, *args, **kwargs)
- json = simplejson.dumps(result)
- return HttpResponse(json, mimetype='application/json')
- else:
- raise PermissionDenied()
+ return view_function
class NewThread(InboxView):
- template_name = 'new_thread.html'
+ """view for creation of new thread"""
+ template_name = 'create_thread.html'# contains new thread form
+ http_method_list = ('GET', 'POST')
def post(self, request):
+ """creates a new thread on behalf of the user
+ response is blank, because on the client side we just
+ need to go back to the thread listing view whose
+ content should be cached in the client'
+ """
recipient_id = IntegerField().clean(request.POST['recipient_id'])
- recipient = get_personal_group_by_id(recipient_id)
- message = Message.objects.create_thread(
+ recipient = get_personal_group_by_user_id(recipient_id)
+ Message.objects.create_thread(
sender=request.user,
recipients=[recipient],
text=request.POST['text']
)
- return {'message_id': message.id}
+ return HttpResponse('', mimetype='application/json')
class NewResponse(InboxView):
- def get(self, request):
- raise PermissionDenied()
+ """view to create a new response"""
+ http_method_list = ('POST',)
def post(self, request):
parent_id = IntegerField().clean(request.POST['parent_id'])
@@ -103,30 +109,29 @@ class NewResponse(InboxView):
text=request.POST['text'],
parent=parent
)
+ return self.render_to_response(
+ {'message': message}, template_name='stored_message.htmtl'
+ )
class ThreadsList(InboxView):
"""shows list of threads for a given user"""
template_name = 'threads_list.html'
+ http_method_list = ('GET',)
def get_context(self, request):
"""returns thread list data"""
- if request.method != 'GET':
- raise PermissionDenied()
-
threads = Message.objects.get_threads_for_user(request.user)
- threads = threads.values('id', 'headline', 'is_read')
+ threads = threads.values('id', 'headline')
return {'threads': threads}
class SendersList(InboxView):
"""shows list of senders for a user"""
template_name = 'senders_list.html'
+ http_method_names = ('GET',)
- def get_context(request):
+ def get_context(self, request):
"""get data about senders for the user"""
- if request.method != 'GET':
- raise PermissionDenied()
-
senders = SenderList.objects.get_senders_for_user(request.user)
senders = senders.values('id', 'username')
return {'senders': senders}
@@ -135,12 +140,10 @@ class SendersList(InboxView):
class ThreadDetails(InboxView):
"""shows entire thread in the unfolded form"""
template_name = 'thread_details.html'
+ http_method_names = ('GET',)
- def get_context(request):
+ def get_context(self, request):
"""shows individual thread"""
- if request.method != 'GET':
- raise PermissionDenied()
-
thread_id = IntegerField().clean(request.GET['thread_id'])
#todo: assert that current thread is the root
messages = Message.objects.filter(root__id=thread_id)