app/soc/tasks/updates/start_update.py
changeset 2925 d1de20978875
parent 2911 2ddd386d1dbd
child 2928 76d5782542dd
equal deleted inserted replaced
2924:6a096316f5c2 2925:d1de20978875
       
     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 """Version update Tasks starters.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22   '"Lennard de Rijk" <ljvderijk@gmail.com>',
       
    23   ]
       
    24 
       
    25 
       
    26 from django import http
       
    27 from django.template import loader
       
    28 from django.utils.translation import ugettext
       
    29 
       
    30 from soc.views.helper import responses
       
    31 
       
    32 
       
    33 def getDjangoURLPatterns():
       
    34   """Returns the URL patterns for the views in this module.
       
    35   """
       
    36 
       
    37   patterns = [
       
    38       (r'tasks/update/start$', 'soc.tasks.updates.start_update.startTasks'),
       
    39       (r'tasks/update/([a-z]+)$', 'soc.tasks.updates.start_update.runner')]
       
    40 
       
    41   return patterns
       
    42 
       
    43 
       
    44 def startTasks(request):
       
    45   """Presents a view that allows the user to start update tasks.
       
    46   """
       
    47 
       
    48   template = 'soc/tasks/start_update.html'
       
    49 
       
    50   context = responses.getUniversalContext(request)
       
    51 
       
    52   options = runner.getOptions()
       
    53 
       
    54   sorted_keys = []
       
    55   for key, option in options.iteritems():
       
    56     option['name'] = key
       
    57     sorted_keys.append(
       
    58         (option['from_version'], option['in_version_order'], key))
       
    59 
       
    60   # sort the keys
       
    61   sorted_keys.sort()
       
    62 
       
    63   # store only the true option
       
    64   sorted_options = []
       
    65 
       
    66   for key_tuple in sorted_keys:
       
    67     option_key = key_tuple[2]
       
    68     sorted_options.append(options[option_key])
       
    69 
       
    70   context.update(
       
    71       page_name='Update Tasks starter',
       
    72       options=sorted_options,
       
    73       )
       
    74 
       
    75   content = loader.render_to_string(template, dictionary=context)
       
    76   return http.HttpResponse(content)
       
    77 
       
    78 
       
    79 class TaskRunner(object):
       
    80   """Runs one of the supported task starters.
       
    81   """
       
    82 
       
    83   def __init__(self):
       
    84     """Initializes the TaskRunner.
       
    85     """
       
    86 
       
    87     self.options = {
       
    88         'program': self.programConversion(),
       
    89         'student': self.studentConversion(),
       
    90         'organization': self.orgConversion(),
       
    91     }
       
    92 
       
    93   def getOptions(self):
       
    94     """Returns the supported options.
       
    95     """
       
    96 
       
    97     return self.options
       
    98 
       
    99   def __call__(self, request, option):
       
   100     """Starts the specified task.
       
   101     """
       
   102 
       
   103     context = responses.getUniversalContext(request)
       
   104     context['page_name'] = 'Start Update Task'
       
   105 
       
   106     option = self.options.get(option)
       
   107     if not option:
       
   108       template = 'soc/error.html'
       
   109       context['message'] = 'Uknown option "%s".' % option
       
   110     else:
       
   111       template = 'soc/tasks/run_update.html'
       
   112       context['option'] = option
       
   113       context['success'] = option['updater'](request)
       
   114 
       
   115     content = loader.render_to_string(template, dictionary=context)
       
   116     return http.HttpResponse(content)
       
   117 
       
   118   def programConversion(self):
       
   119     """
       
   120     """
       
   121 
       
   122     description = ugettext('This converts the Program models to contain X,Y,Z. '
       
   123                            'Note that this conversion will only work after Q')
       
   124 
       
   125     # TODO(ljvderijk): implement this
       
   126     updater = lambda x:False
       
   127 
       
   128     conversion_information = {'from_version': 'V-1',
       
   129                               'in_version_order': 2,
       
   130                               'description': description,
       
   131                               'updater': updater}
       
   132 
       
   133     return conversion_information
       
   134 
       
   135   def studentConversion(self):
       
   136     """
       
   137     """
       
   138 
       
   139     description = ugettext('This converts the Student models to contain X,Y,Z.')
       
   140 
       
   141     # TODO(ljvderijk): implement this
       
   142     updater = lambda x:False
       
   143 
       
   144     conversion_information = {'from_version': 'V-1',
       
   145                               'in_version_order': 1,
       
   146                               'description': description,
       
   147                               'updater': updater}
       
   148 
       
   149     return conversion_information
       
   150 
       
   151   def orgConversion(self):
       
   152     """
       
   153     """
       
   154 
       
   155     description = ugettext('This converts the Organization models to contain X,Y,Z.')
       
   156 
       
   157     # TODO(ljvderijk): implement this
       
   158     updater = lambda x:False
       
   159 
       
   160     conversion_information = {'from_version': 'V-2',
       
   161                               'in_version_order': 1,
       
   162                               'description': description,
       
   163                               'updater': updater}
       
   164 
       
   165     return conversion_information
       
   166 
       
   167 runner = TaskRunner()