app/soc/tasks/updates/start_update.py
changeset 2931 e89022c0657f
parent 2928 76d5782542dd
child 2932 666d31bd43bd
equal deleted inserted replaced
2930:74d4875b4922 2931:e89022c0657f
    26 from django import http
    26 from django import http
    27 from django.template import loader
    27 from django.template import loader
    28 from django.utils.translation import ugettext
    28 from django.utils.translation import ugettext
    29 
    29 
    30 from soc.tasks.helper import error_handler
    30 from soc.tasks.helper import error_handler
       
    31 from soc.tasks.updates import student_school_type
    31 from soc.views.helper import responses
    32 from soc.views.helper import responses
    32 
    33 
    33 
    34 
    34 def getDjangoURLPatterns():
    35 def getDjangoURLPatterns():
    35   """Returns the URL patterns for the views in this module.
    36   """Returns the URL patterns for the views in this module.
    36   """
    37   """
    37 
    38 
    38   patterns = [
    39   patterns = [
    39       (r'tasks/update/start$', 'soc.tasks.updates.start_update.startTasks'),
    40       (r'tasks/update/start$', 'soc.tasks.updates.start_update.startTasks'),
    40       (r'tasks/update/start/([a-z]+)$',
    41       (r'tasks/update/start/([0-9_a-z]+)$',
    41        'soc.tasks.updates.start_update.start_task'),
    42        'soc.tasks.updates.start_update.start_task'),
    42       (r'tasks/update/run/([a-z]+)$',
    43       (r'tasks/update/run/([0-9_a-z]+)$',
    43        'soc.tasks.updates.start_update.run_task')]
    44        'soc.tasks.updates.start_update.run_task')]
    44 
    45 
    45   return patterns
    46   return patterns
    46 
    47 
    47 
    48 
    82 
    83 
    83 class TaskRunner(object):
    84 class TaskRunner(object):
    84   """Runs one of the supported tasks.
    85   """Runs one of the supported tasks.
    85   """
    86   """
    86 
    87 
    87   ORG_CONVERSION = {
    88   STUDENT_SCHOOL_TYPE = {
    88       'from_version': 'V-2',
    89       'from_version': '0-5-20090914',
    89       'in_version_order': 1,
    90       'in_version_order': 1,
    90       'description': ugettext('This converts the Organization models to contain X,Y,Z.'),
    91       'description': ugettext(
    91       'starter': lambda x:False,
    92           'Updates due to changes in the Student model. Sets all school_type '
    92       'runner': lambda x,**kwargs:http.HttpResponse('TEST OK'),
    93           'entries to University since that was the first type of Student that '
       
    94           'was supported.'),
       
    95       'starter': student_school_type.startSchoolTypeUpdate,
       
    96       'runner': student_school_type.runSchoolTypeUpdate,
    93       }
    97       }
    94 
    98 
    95 
    99 
    96   def __init__(self):
   100   def __init__(self):
    97     """Initializes the TaskRunner.
   101     """Initializes the TaskRunner.
    98     """
   102     """
    99 
   103 
   100     self.options = {
   104     self.options = {
   101         'organization': self.ORG_CONVERSION,
   105         'student_school_type': self.STUDENT_SCHOOL_TYPE,
   102     }
   106     }
   103 
   107 
   104   def getOptions(self):
   108   def getOptions(self):
   105     """Returns the supported options.
   109     """Returns the supported options.
   106     """
   110     """
   107 
   111 
   108     return self.options
   112     return self.options
   109 
   113 
   110   def startTask(self, request, option):
   114   def startTask(self, request, option_name):
   111     """Starts the specified Task for the given option.
   115     """Starts the specified Task for the given option.
   112     """
   116     """
   113 
   117 
   114     context = responses.getUniversalContext(request)
   118     context = responses.getUniversalContext(request)
   115     context['page_name'] = 'Start Update Task'
   119     context['page_name'] = 'Start Update Task'
   116 
   120 
   117     option = self.options.get(option)
   121     option = self.options.get(option_name)
   118     if not option:
   122     if not option:
   119       template = 'soc/error.html'
   123       template = 'soc/error.html'
   120       context['message'] = 'Uknown option "%s".' % option
   124       context['message'] = 'Uknown option "%s".' % option_name
   121     else:
   125     else:
   122       template = 'soc/tasks/run_update.html'
   126       template = 'soc/tasks/run_update.html'
   123       context['option'] = option
   127       context['option'] = option
   124       context['success'] = option['starter'](request)
   128       context['success'] = option['starter'](request, self._getRunUpdateURL(option_name))
   125 
   129 
   126     content = loader.render_to_string(template, dictionary=context)
   130     content = loader.render_to_string(template, dictionary=context)
   127     return http.HttpResponse(content)
   131     return http.HttpResponse(content)
   128 
   132 
   129   def runTask(self, request, option, **kwargs):
   133   def _getRunUpdateURL(self, option):
       
   134     """Returns the URL to run a specific update.
       
   135 
       
   136     Args:
       
   137       option: the update option for which the URL should returned
       
   138     """
       
   139     return '/tasks/update/run/%s' % option
       
   140 
       
   141   def runTask(self, request, option_name, **kwargs):
   130     """Runs the specified Task for the given option.
   142     """Runs the specified Task for the given option.
   131     """
   143     """
   132 
   144 
   133     option = self.options.get(option)
   145     option = self.options.get(option_name)
   134 
   146 
   135     if not option:
   147     if not option:
   136       error_handler('Uknown Updater option "%s".' % option)
   148       error_handler('Uknown Updater option "%s".' % option_name)
   137     else:
   149     else:
   138       return option['runner'](request, **kwargs)
   150       return option['runner'](request, **kwargs)
   139 
   151 
   140 
   152 
   141 task_runner = TaskRunner()
   153 task_runner = TaskRunner()