Added Task for sending out emails to subscribed users about a Comment.
authorMadhusudan.C.S <madhusudancs@gmail.com>
Fri, 28 Aug 2009 13:13:05 +0200
changeset 2826 211783aa20d5
parent 2825 e57a661a174d
child 2827 58e2a02e35b4
Added Task for sending out emails to subscribed users about a Comment. Reviewed by: Lennard de Rijk
app/soc/modules/ghop/tasks/task_update.py
--- a/app/soc/modules/ghop/tasks/task_update.py	Fri Aug 28 12:57:12 2009 +0200
+++ b/app/soc/modules/ghop/tasks/task_update.py	Fri Aug 28 13:13:05 2009 +0200
@@ -47,6 +47,8 @@
   patterns = [
       (r'^tasks/ghop/task/update$',
        'soc.modules.ghop.tasks.task_update.updateGHOPTask'),
+      (r'^tasks/ghop/task/mail/create$',
+       'soc.modules.ghop.tasks.task_update.createNotificationMail'),
       (r'^tasks/ghop/task/update/student_status$',
        'soc.modules.ghop.tasks.task_update.updateTasksPostStudentSignUp')]
 
@@ -98,6 +100,127 @@
     return http.HttpResponse()
 
 
+def spawnCreateNotificationMail(entity):
+  """Spawns a task to send mail to the user who has subscribed to the specific
+  task.
+
+  Args:
+    entity: The Comment entity for which mails must be sent
+  """
+
+  task_params = {
+      'comment_key': entity.key().id_or_name(),
+      'task_key': entity.parent_key().id_or_name(),
+      }
+  task_url = '/tasks/ghop/task/mail/create'
+
+  new_task = taskqueue.Task(params=task_params, url=task_url)
+  new_task.add('mail')
+
+
+def createNotificationMail(request, *args, **kwargs):
+  """Appengine task that sends mail to the subscribed users.
+
+  Expects the following to be present in the POST dict:
+    comment_key: Specifies the comment id for which to send the notifications
+    task_key: Specifies the task key name for which the comment belongs to
+
+  Args:
+    request: Django Request object
+  """
+
+  from soc.modules.ghop.logic.helper import notifications as ghop_notifications
+
+  from soc.modules.ghop.logic.models import comment as ghop_comment_logic
+  from soc.modules.ghop.logic.models import task_subscription as \
+      ghop_task_subscription_logic
+
+  # set default batch size
+  batch_size = 10
+
+  post_dict = request.POST
+
+  comment_key = post_dict.get('comment_key')
+  task_key = post_dict.get('task_key')
+
+  if not (comment_key and task_key):
+    # invalid task data, log and return OK
+    return error_handler.logErrorAndReturnOK(
+        'Invalid createNotificationMail data: %s' % post_dict)
+
+  comment_key = long(comment_key)
+
+  # get the task entity under which the specified comment was made
+  task_entity = ghop_task_logic.logic.getFromKeyName(task_key)
+
+  # get the comment for the given id
+  comment_entity = ghop_comment_logic.logic.getFromID(
+      comment_key, task_entity)
+
+  if not comment_entity:
+    # invalid comment specified, log and return OK
+    return error_handler.logErrorAndReturnOK(
+        'Invalid comment specified: %s/%s' % (comment_key, task_key))
+
+  # check and retrieve the subscriber_start_key that has been done last
+  if 'subscriber_start_index' in post_dict:
+    subscriber_start_index = post_dict['subscriber_start_index']
+  else:
+    subscriber_start_index = 0
+
+  # get all subscribers to GHOP task
+  fields = {
+      'task': task_entity,
+      }
+
+  ts_entity = ghop_task_subscription_logic.logic.getForFields(
+      fields, unique=True)
+
+  subscribers = db.get(ts_entity.subscribers[
+      subscriber_start_index:subscriber_start_index+batch_size])
+
+  task_url = "http://%(host)s%(task)s" % {
+                 'host': os.environ['HTTP_HOST'],
+                 'task': redirects.getPublicRedirect(
+                     task_entity, {'url_name': 'ghop/task'}),
+                 }
+
+  # create the data for the mail to be sent
+  message_properties = {
+      'task_url': task_url,
+      'redirect_url': "%(task_url)s#c%(cid)d" % {
+          'task_url': task_url,
+          'cid': comment_entity.key().id_or_name()
+          },
+      'comment_entity': comment_entity,
+      'task_entity': task_entity,
+  }
+
+  subject = DEF_TASK_UPDATE_SUBJECT_FMT % {
+      'title': task_entity.title, 
+      }
+
+  for subscriber in subscribers:
+    ghop_notifications.sendTaskUpdate(entity, subject, message_properties)
+
+  if len(subscribers) == batch_size:
+    # spawn task for sending out notifications to next set of subscribers
+    next_start = subscriber_start_index + batch_size
+
+    task_params = {
+        'comment_key': comment_key,
+        'task_key': task_key,
+        'subscriber_start_index': next_start
+        }
+    task_url = '/tasks/ghop/task/mail/create'
+
+    new_task = taskqueue.Task(params=task_params, url=task_url)
+    new_task.add('mail')
+
+  # return OK
+  return http.HttpResponse()
+
+
 def updateTasksPostStudentSignUp(request, *args, **kwargs):
   """Appengine task that updates the GHOP Tasks after the student signs up.