app/soc/tasks/grading_survey_group.py
changeset 2647 69ac7307bb50
parent 2644 0eb27bb0122d
equal deleted inserted replaced
2646:f1e7cf929d02 2647:69ac7307bb50
    43   patterns = [(
    43   patterns = [(
    44       r'tasks/grading_survey_group/update_records$',
    44       r'tasks/grading_survey_group/update_records$',
    45       'soc.tasks.grading_survey_group.updateOrCreateRecordsForSurveyGroup'),
    45       'soc.tasks.grading_survey_group.updateOrCreateRecordsForSurveyGroup'),
    46               (
    46               (
    47       r'tasks/grading_survey_group/update_projects$',
    47       r'tasks/grading_survey_group/update_projects$',
    48       'soc.tasks.grading_survey_group.updateProjectsForSurveyGroup')]
    48       'soc.tasks.grading_survey_group.updateProjectsForSurveyGroup'),
       
    49               (
       
    50       r'tasks/grading_survey_group/mail_result$',
       
    51       'soc.tasks.grading_survey_group.sendMailAboutGradingRecordResult')]
    49 
    52 
    50   return patterns
    53   return patterns
    51 
    54 
    52 
    55 
    53 def updateOrCreateRecordsForSurveyGroup(request, *args, **kwargs):
    56 def updateOrCreateRecordsForSurveyGroup(request, *args, **kwargs):
   136 
   139 
   137   Expects the following to be present in the POST dict:
   140   Expects the following to be present in the POST dict:
   138     group_key: Specifies the GradingSurveyGroup key name.
   141     group_key: Specifies the GradingSurveyGroup key name.
   139     record_key: Optional, specifies the key of the last processed
   142     record_key: Optional, specifies the key of the last processed
   140                 GradingRecord.
   143                 GradingRecord.
       
   144     send_mail: Optional, if this string evaluates to True mail will be send
       
   145                for each GradingRecord that's processed.
   141 
   146 
   142   Args:
   147   Args:
   143     request: Django Request object
   148     request: Django Request object
   144   """
   149   """
   145 
   150 
   188   record_entities = grading_record_logic.getForFields(fields,
   193   record_entities = grading_record_logic.getForFields(fields,
   189                                                       limit=DEF_BATCH_SIZE)
   194                                                       limit=DEF_BATCH_SIZE)
   190 
   195 
   191   student_project_logic.updateProjectsForGradingRecords(record_entities)
   196   student_project_logic.updateProjectsForGradingRecords(record_entities)
   192 
   197 
       
   198   # check if we need to send an email for each GradingRecord
       
   199   send_mail = post_dict.get('send_mail', '')
       
   200 
       
   201   if send_mail:
       
   202     # enqueue a task to send mail for each GradingRecord
       
   203     for record_entity in record_entities:
       
   204       # pass along these params as POST to the new task
       
   205       task_params = {'record_key': record_entity.key().id_or_name()}
       
   206       task_url = '/tasks/grading_survey_group/mail_result'
       
   207 
       
   208       mail_task = taskqueue.Task(params=task_params, url=task_url)
       
   209       mail_task.add('mail')
       
   210 
   193   if len(record_entities) == DEF_BATCH_SIZE:
   211   if len(record_entities) == DEF_BATCH_SIZE:
   194     # spawn new task starting from the last
   212     # spawn new task starting from the last
   195     new_record_start = record_entities[DEF_BATCH_SIZE-1].key().id_or_name()
   213     new_record_start = record_entities[DEF_BATCH_SIZE-1].key().id_or_name()
   196 
   214 
   197     # pass along these params as POST to the new task
   215     # pass along these params as POST to the new task
   198     task_params = {'group_key': group_key,
   216     task_params = {'group_key': group_key,
   199                    'record_key': new_record_start}
   217                    'record_key': new_record_start,
       
   218                    'send_mail': send_mail}
   200     task_url = '/tasks/grading_survey_group/update_projects'
   219     task_url = '/tasks/grading_survey_group/update_projects'
   201 
   220 
   202     new_task = taskqueue.Task(params=task_params, url=task_url)
   221     new_task = taskqueue.Task(params=task_params, url=task_url)
   203     new_task.add()
   222     new_task.add()
   204 
   223 
   205   # task completed, return OK
   224   # task completed, return OK
   206   return http.HttpResponse('OK')
   225   return http.HttpResponse('OK')
       
   226 
       
   227 
       
   228 def sendMailAboutGradingRecordResult(request, *args, **kwargs):
       
   229   """Sends out a mail about the result of one GradingRecord.
       
   230 
       
   231   Expects the following to be present in the POST dict:
       
   232     record_key: Specifies the key for the record to process.
       
   233 
       
   234   Args:
       
   235     request: Django Request object
       
   236   """
       
   237 
       
   238   from soc.logic import mail_dispatcher
       
   239   from soc.logic.models.grading_record import logic as grading_record_logic
       
   240   from soc.logic.models.org_admin import logic as org_admin_logic
       
   241   from soc.logic.models.site import logic as site_logic
       
   242 
       
   243   post_dict = request.POST
       
   244 
       
   245   # check and retrieve the record_key that has been done last
       
   246   if 'record_key' in post_dict and post_dict['record_key'].isdigit():
       
   247     record_key = int(post_dict['record_key'])
       
   248   else:
       
   249     record_key = None
       
   250 
       
   251   if not record_key:
       
   252     # no GradingRecord key specified, log and return OK
       
   253     error_handler.logErrorAndReturnOK(
       
   254         'No valid record_key specified in POST data: %s' % request.POST)
       
   255 
       
   256   record_entity = grading_record_logic.getFromID(record_key)
       
   257 
       
   258   if not record_entity:
       
   259     # no valid GradingRecord key specified, log and return OK
       
   260     error_handler.logErrorAndReturnOK(
       
   261         'No valid GradingRecord key specified: %s' % record_key)
       
   262 
       
   263   survey_group_entity = record_entity.grading_survey_group
       
   264   project_entity = record_entity.project
       
   265   student_entity = project_entity.student
       
   266   mentor_entity = project_entity.mentor
       
   267   org_entity = project_entity.scope
       
   268   site_entity = site_logic.getSingleton()
       
   269 
       
   270   mail_context = {
       
   271     'survey_group': survey_group_entity,
       
   272     'grading_record': record_entity,
       
   273     'project': project_entity,
       
   274     'organization': org_entity,
       
   275     'site_name': site_entity.site_name,
       
   276     'to_name': student_entity.name()
       
   277   }
       
   278 
       
   279   # set the sender
       
   280   (sender, sender_address) = mail_dispatcher.getDefaultMailSender()
       
   281   mail_context['sender'] = sender_address
       
   282 
       
   283   # set the receiver and subject
       
   284   mail_context['to'] = student_entity.email
       
   285   mail_context['cc'] = [mentor_entity.email]
       
   286   mail_context['subject'] = '%s results processed for %s' %(
       
   287       survey_group_entity.name, project_entity.title)
       
   288 
       
   289   # find all org admins for the project's organization
       
   290   fields = {'scope': org_entity,
       
   291             'status': 'active'}
       
   292   org_admin_entities = org_admin_logic.getForFields(fields)
       
   293 
       
   294   # collect email addresses for all found org admins
       
   295   org_admin_addresses = []
       
   296 
       
   297   for org_admin_entity in org_admin_entities:
       
   298     org_admin_addresses.append(org_admin_entity.email)
       
   299 
       
   300   if org_admin_addresses:
       
   301     mail_context['cc'].extend(org_admin_addresses)
       
   302 
       
   303   # send out the email using a template
       
   304   mail_template = 'soc/grading_record/mail/result.html'
       
   305   mail_dispatcher.sendMailFromTemplate(mail_template, mail_context)
       
   306 
       
   307   # return OK
       
   308   return http.HttpResponse()