app/soc/tasks/grading_survey_group.py
changeset 2618 6a3c464f871f
child 2631 f0d3d24a609b
equal deleted inserted replaced
2617:274efd5c769d 2618:6a3c464f871f
       
     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 """Tasks related to Grading Survey Groups.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Lennard de Rijk" <ljvderijk@gmail.com>',
       
    22   ]
       
    23 
       
    24 
       
    25 import datetime
       
    26 import logging
       
    27 
       
    28 from google.appengine.api.labs import taskqueue
       
    29 
       
    30 from django import http
       
    31 
       
    32 from soc.tasks.helper import error_handler
       
    33 
       
    34 
       
    35 # batch size to use when going through StudentProjects
       
    36 DEF_BATCH_SIZE = 10
       
    37 
       
    38 
       
    39 def getDjangoURLPatterns():
       
    40   """Returns the URL patterns for the tasks in this module.
       
    41   """
       
    42 
       
    43   patterns = [(
       
    44       r'tasks/grading_survey_group/update_records$',
       
    45       'soc.tasks.grading_survey_group.updateOrCreateRecordsForSurveyGroup'),
       
    46               (
       
    47       r'tasks/grading_survey_group/update_projects$',
       
    48       'soc.tasks.grading_survey_group.updateProjectsForSurveyGroup')]
       
    49 
       
    50   return patterns
       
    51 
       
    52 
       
    53 def updateOrCreateRecordsForSurveyGroup(request, *args, **kwargs):
       
    54   """Updates or creates GradingRecords for the given GradingSurveyGroup.
       
    55 
       
    56   Expects the following to be present in the POST dict:
       
    57     group_key: Specifies the GradingSurveyGroup key name.
       
    58     project_key: optional to specify which project was the last for which this
       
    59                  task was run
       
    60   Args:
       
    61     request: Django Request object
       
    62   """
       
    63 
       
    64   from soc.logic.models.grading_record import logic as grading_record_logic
       
    65   from soc.logic.models.grading_survey_group import logic as survey_group_logic
       
    66   from soc.logic.models.student_project import logic as student_project_logic
       
    67 
       
    68   post_dict = request.POST
       
    69 
       
    70   group_key = post_dict.get('group_key')
       
    71 
       
    72   if not group_key:
       
    73     # invalid task data, log and return OK
       
    74     return error_handler.logErrorAndReturnOK(
       
    75         'Invalid updateRecordForSurveyGroup data: %s' % post_dict)
       
    76 
       
    77   # get the GradingSurveyGroup for the given keyname
       
    78   survey_group_entity = survey_group_logic.getFromKeyName(group_key)
       
    79 
       
    80   if not survey_group_entity:
       
    81     # invalid GradingSurveyGroup specified, log and return OK
       
    82     return error_handler.logErrorAndReturnOK(
       
    83         'Invalid GradingSurveyGroup specified: %s' % group_key)
       
    84 
       
    85   # check and retrieve the project_key that has been done last
       
    86   if 'project_key' in post_dict:
       
    87     project_start_key = post_dict['project_key']
       
    88   else:
       
    89     project_start_key = None
       
    90 
       
    91   # get all valid StudentProjects from starting key
       
    92   fields = {'program': survey_group_entity.scope,
       
    93             'status': ['accepted', 'failed', 'completed', 'withdrawn']}
       
    94 
       
    95   if project_start_key:
       
    96     # retrieve the last project that was done
       
    97     project_start = student_project_logic.getFromKeyName(project_start_key)
       
    98 
       
    99     if not project_start:
       
   100       # invalid starting project key specified, log and return OK
       
   101       return error_handler.logErrorAndReturnOK(
       
   102           'Invalid Student Project Key specified: %s' %(project_start_key))
       
   103 
       
   104     fields['__key__ >'] = project_start.key()
       
   105 
       
   106   # get the first batch_size number of StudentProjects
       
   107   project_entities = student_project_logic.getForFields(fields,
       
   108                                                         limit=DEF_BATCH_SIZE)
       
   109 
       
   110   # update/create and batch put the new GradingRecords
       
   111   grading_record_logic.updateOrCreateRecordsFor(survey_group_entity,
       
   112                                                 project_entities)
       
   113 
       
   114   if len(project_entities) == DEF_BATCH_SIZE:
       
   115     # spawn new task starting from the last
       
   116     new_project_start = project_entities[DEF_BATCH_SIZE-1].key().id_or_name()
       
   117 
       
   118     # pass along these params as POST to the new task
       
   119     task_params = {'group_key': group_key,
       
   120                    'project_key': new_project_start}
       
   121     task_url = '/tasks/grading_survey_group/update_records'
       
   122 
       
   123     new_task = taskqueue.Task(params=task_params, url=task_url)
       
   124     new_task.add()
       
   125   else:
       
   126     # task completed, update timestamp for last update complete
       
   127     fields = {'last_update_complete': datetime.datetime.now()}
       
   128     survey_group_logic.updateEntityProperties(survey_group_entity, fields)
       
   129 
       
   130   # task completed, return OK
       
   131   return http.HttpResponse('OK')
       
   132 
       
   133 def updateProjectsForSurveyGroup(request, *args, **kwargs):
       
   134   """Updates each StudentProject for which a GradingRecord is found.
       
   135 
       
   136   Expects the following to be present in the POST dict:
       
   137     group_key: Specifies the GradingSurveyGroup key name.
       
   138 
       
   139   Args:
       
   140     request: Django Request object
       
   141   """
       
   142   # TODO(ljvderijk) implement this method
       
   143   return http.HttpResponse('OK')