app/soc/modules/ghop/tasks/task_update.py
changeset 2817 c31428f08daa
child 2826 211783aa20d5
equal deleted inserted replaced
2816:bd2918172bfd 2817:c31428f08daa
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2009 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Appengine Tasks related to GHOP Task handling.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21     '"Madhusudan.C.S" <madhusudancs@gmail.com>'
       
    22   ]
       
    23 
       
    24 
       
    25 import logging
       
    26 import os
       
    27 
       
    28 from google.appengine.api.labs import taskqueue
       
    29 from google.appengine.ext import db
       
    30 
       
    31 from django import http
       
    32 from django.utils.translation import ugettext
       
    33 
       
    34 from soc.tasks.helper import error_handler
       
    35 from soc.views.helper import redirects
       
    36 
       
    37 from soc.modules.ghop.logic.models import task as ghop_task_logic
       
    38 
       
    39 
       
    40 DEF_TASK_UPDATE_SUBJECT_FMT = ugettext('[GHOP Task Update] %(title)s')
       
    41 
       
    42 
       
    43 def getDjangoURLPatterns():
       
    44   """Returns the URL patterns for the tasks in this module.
       
    45   """
       
    46 
       
    47   patterns = [
       
    48       (r'^tasks/ghop/task/update$',
       
    49        'soc.modules.ghop.tasks.task_update.updateGHOPTask'),
       
    50       (r'^tasks/ghop/task/update/student_status$',
       
    51        'soc.modules.ghop.tasks.task_update.updateTasksPostStudentSignUp')]
       
    52 
       
    53   return patterns
       
    54 
       
    55 
       
    56 def spawnUpdateTask(entity):
       
    57   """Spawns a task to update the state of the task. 
       
    58   """
       
    59 
       
    60   update_params = {
       
    61       'ghop_task_key': entity.key().name(),
       
    62       }
       
    63   update_url = '/tasks/ghop/task/update'
       
    64 
       
    65   new_task = taskqueue.Task(eta=entity.deadline, 
       
    66                             params=update_params,
       
    67                             url=update_url)
       
    68   new_task.add('ghop-update')
       
    69 
       
    70 
       
    71 def updateGHOPTask(request, *args, **kwargs):
       
    72   """Method executed by Task Queue API to update a GHOP Task to
       
    73   relevant state.
       
    74 
       
    75   Expects the ghop_task_key entry to be present in the POST dict.
       
    76 
       
    77   Args:
       
    78     request: the standard Django HTTP request object 
       
    79   """
       
    80 
       
    81   post_dict = request.POST
       
    82 
       
    83   key_name = post_dict.get('ghop_task_key')
       
    84 
       
    85   if not key_name:
       
    86     # invalid task data, log and return OK
       
    87     return error_handler.logErrorAndReturnOK(
       
    88         'Invalid updateGHOPTask data: %s' % post_dict)
       
    89 
       
    90   entity = ghop_task_logic.logic.getFromKeyNameOr404(key_name)
       
    91 
       
    92   entity, comment_entity, ws_entity = ghop_task_logic.logic.updateTaskStatus(
       
    93       entity)
       
    94 
       
    95   if entity:
       
    96     # TODO(madhusudan): does this really mean an unsuccessful update?
       
    97     # return OK
       
    98     return http.HttpResponse()
       
    99 
       
   100 
       
   101 def updateTasksPostStudentSignUp(request, *args, **kwargs):
       
   102   """Appengine task that updates the GHOP Tasks after the student signs up.
       
   103 
       
   104   Expects the following to be present in the POST dict:
       
   105     student_key: Specifies the student key name who registered
       
   106 
       
   107   Args:
       
   108     request: Django Request object
       
   109   """
       
   110   from soc.modules.ghop.logic.models import student as ghop_student_logic
       
   111 
       
   112   post_dict = request.POST
       
   113 
       
   114   student_key = post_dict.get('student_key')
       
   115 
       
   116   if not student_key:
       
   117     # invalid student data, log and return OK
       
   118     return error_handler.logErrorAndReturnOK(
       
   119         'Invalid Student data: %s' % post_dict)
       
   120 
       
   121   student_entity = ghop_student_logic.logic.getFromKeyNameOr404(student_key)
       
   122 
       
   123   # retrieve all tasks currently assigned to the user
       
   124   task_fields = {
       
   125       'user': student_entity.user,
       
   126       }
       
   127   task_entities = ghop_task_logic.logic.getForFields(task_fields)
       
   128 
       
   129   # TODO(madhusudan) move this to the Task Logic
       
   130   # Make sure the tasks store references to the student as well as
       
   131   # closing all tasks that are AwaitingRegistration.
       
   132   for task_entity in task_entities:
       
   133     task_entity.student = student_entity
       
   134     if task_entity.status == 'AwaitingRegistration':
       
   135       task_entities.remove(task_entity)
       
   136 
       
   137       properties = {'status': 'Closed'}
       
   138       changes = [ugettext('User-MelangeAutomatic'),
       
   139                  ugettext('Action-Student registered'),
       
   140                  ugettext('Status-%s' % (properties['status']))]
       
   141 
       
   142       comment_properties = {
       
   143           'parent': task_entity,
       
   144           'scope_path': task_entity.key().name(),
       
   145           'created_by': None,
       
   146           'changes': changes,
       
   147           'content': ugettext(
       
   148               '(The Melange Automated System has detected that the student '
       
   149               'has signed up for the program and hence has closed this task.'),
       
   150           }
       
   151 
       
   152       ghop_task_logic.logic.updateEntityPropertiesWithCWS(
       
   153           task_entity, properties, comment_properties)
       
   154 
       
   155   db.put(task_entities)
       
   156 
       
   157   # return OK
       
   158   return http.HttpResponse()