app/soc/modules/ghop/tasks/task_update.py
changeset 2826 211783aa20d5
parent 2817 c31428f08daa
child 2867 e8d86272e6ea
equal deleted inserted replaced
2825:e57a661a174d 2826:211783aa20d5
    45   """
    45   """
    46 
    46 
    47   patterns = [
    47   patterns = [
    48       (r'^tasks/ghop/task/update$',
    48       (r'^tasks/ghop/task/update$',
    49        'soc.modules.ghop.tasks.task_update.updateGHOPTask'),
    49        'soc.modules.ghop.tasks.task_update.updateGHOPTask'),
       
    50       (r'^tasks/ghop/task/mail/create$',
       
    51        'soc.modules.ghop.tasks.task_update.createNotificationMail'),
    50       (r'^tasks/ghop/task/update/student_status$',
    52       (r'^tasks/ghop/task/update/student_status$',
    51        'soc.modules.ghop.tasks.task_update.updateTasksPostStudentSignUp')]
    53        'soc.modules.ghop.tasks.task_update.updateTasksPostStudentSignUp')]
    52 
    54 
    53   return patterns
    55   return patterns
    54 
    56 
    94 
    96 
    95   if entity:
    97   if entity:
    96     # TODO(madhusudan): does this really mean an unsuccessful update?
    98     # TODO(madhusudan): does this really mean an unsuccessful update?
    97     # return OK
    99     # return OK
    98     return http.HttpResponse()
   100     return http.HttpResponse()
       
   101 
       
   102 
       
   103 def spawnCreateNotificationMail(entity):
       
   104   """Spawns a task to send mail to the user who has subscribed to the specific
       
   105   task.
       
   106 
       
   107   Args:
       
   108     entity: The Comment entity for which mails must be sent
       
   109   """
       
   110 
       
   111   task_params = {
       
   112       'comment_key': entity.key().id_or_name(),
       
   113       'task_key': entity.parent_key().id_or_name(),
       
   114       }
       
   115   task_url = '/tasks/ghop/task/mail/create'
       
   116 
       
   117   new_task = taskqueue.Task(params=task_params, url=task_url)
       
   118   new_task.add('mail')
       
   119 
       
   120 
       
   121 def createNotificationMail(request, *args, **kwargs):
       
   122   """Appengine task that sends mail to the subscribed users.
       
   123 
       
   124   Expects the following to be present in the POST dict:
       
   125     comment_key: Specifies the comment id for which to send the notifications
       
   126     task_key: Specifies the task key name for which the comment belongs to
       
   127 
       
   128   Args:
       
   129     request: Django Request object
       
   130   """
       
   131 
       
   132   from soc.modules.ghop.logic.helper import notifications as ghop_notifications
       
   133 
       
   134   from soc.modules.ghop.logic.models import comment as ghop_comment_logic
       
   135   from soc.modules.ghop.logic.models import task_subscription as \
       
   136       ghop_task_subscription_logic
       
   137 
       
   138   # set default batch size
       
   139   batch_size = 10
       
   140 
       
   141   post_dict = request.POST
       
   142 
       
   143   comment_key = post_dict.get('comment_key')
       
   144   task_key = post_dict.get('task_key')
       
   145 
       
   146   if not (comment_key and task_key):
       
   147     # invalid task data, log and return OK
       
   148     return error_handler.logErrorAndReturnOK(
       
   149         'Invalid createNotificationMail data: %s' % post_dict)
       
   150 
       
   151   comment_key = long(comment_key)
       
   152 
       
   153   # get the task entity under which the specified comment was made
       
   154   task_entity = ghop_task_logic.logic.getFromKeyName(task_key)
       
   155 
       
   156   # get the comment for the given id
       
   157   comment_entity = ghop_comment_logic.logic.getFromID(
       
   158       comment_key, task_entity)
       
   159 
       
   160   if not comment_entity:
       
   161     # invalid comment specified, log and return OK
       
   162     return error_handler.logErrorAndReturnOK(
       
   163         'Invalid comment specified: %s/%s' % (comment_key, task_key))
       
   164 
       
   165   # check and retrieve the subscriber_start_key that has been done last
       
   166   if 'subscriber_start_index' in post_dict:
       
   167     subscriber_start_index = post_dict['subscriber_start_index']
       
   168   else:
       
   169     subscriber_start_index = 0
       
   170 
       
   171   # get all subscribers to GHOP task
       
   172   fields = {
       
   173       'task': task_entity,
       
   174       }
       
   175 
       
   176   ts_entity = ghop_task_subscription_logic.logic.getForFields(
       
   177       fields, unique=True)
       
   178 
       
   179   subscribers = db.get(ts_entity.subscribers[
       
   180       subscriber_start_index:subscriber_start_index+batch_size])
       
   181 
       
   182   task_url = "http://%(host)s%(task)s" % {
       
   183                  'host': os.environ['HTTP_HOST'],
       
   184                  'task': redirects.getPublicRedirect(
       
   185                      task_entity, {'url_name': 'ghop/task'}),
       
   186                  }
       
   187 
       
   188   # create the data for the mail to be sent
       
   189   message_properties = {
       
   190       'task_url': task_url,
       
   191       'redirect_url': "%(task_url)s#c%(cid)d" % {
       
   192           'task_url': task_url,
       
   193           'cid': comment_entity.key().id_or_name()
       
   194           },
       
   195       'comment_entity': comment_entity,
       
   196       'task_entity': task_entity,
       
   197   }
       
   198 
       
   199   subject = DEF_TASK_UPDATE_SUBJECT_FMT % {
       
   200       'title': task_entity.title, 
       
   201       }
       
   202 
       
   203   for subscriber in subscribers:
       
   204     ghop_notifications.sendTaskUpdate(entity, subject, message_properties)
       
   205 
       
   206   if len(subscribers) == batch_size:
       
   207     # spawn task for sending out notifications to next set of subscribers
       
   208     next_start = subscriber_start_index + batch_size
       
   209 
       
   210     task_params = {
       
   211         'comment_key': comment_key,
       
   212         'task_key': task_key,
       
   213         'subscriber_start_index': next_start
       
   214         }
       
   215     task_url = '/tasks/ghop/task/mail/create'
       
   216 
       
   217     new_task = taskqueue.Task(params=task_params, url=task_url)
       
   218     new_task.add('mail')
       
   219 
       
   220   # return OK
       
   221   return http.HttpResponse()
    99 
   222 
   100 
   223 
   101 def updateTasksPostStudentSignUp(request, *args, **kwargs):
   224 def updateTasksPostStudentSignUp(request, *args, **kwargs):
   102   """Appengine task that updates the GHOP Tasks after the student signs up.
   225   """Appengine task that updates the GHOP Tasks after the student signs up.
   103 
   226