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