app/soc/views/models/cron.py
changeset 2211 f7497180d037
child 2212 4095892a3c99
equal deleted inserted replaced
2210:1095b52ed667 2211:f7497180d037
       
     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 """Views for Cron.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22   ]
       
    23 
       
    24 
       
    25 from django import forms
       
    26 from django import http
       
    27 
       
    28 from soc.logic import cleaning
       
    29 from soc.logic import dicts
       
    30 from soc.logic.models.priority_group import logic as priority_group_logic
       
    31 from soc.logic.models.job import logic as job_logic
       
    32 from soc.views.helper import access
       
    33 from soc.views.helper import decorators
       
    34 from soc.views.helper import dynaform
       
    35 from soc.views.helper import widgets
       
    36 from soc.views.models import base
       
    37 
       
    38 import soc.cron.job
       
    39 
       
    40 
       
    41 class View(base.View):
       
    42   """View methods for the Cron model.
       
    43   """
       
    44 
       
    45   def __init__(self, params=None):
       
    46     """Defines the fields and methods required for the base View class
       
    47     to provide the user with list, public, create, edit and delete views.
       
    48 
       
    49     Params:
       
    50       params: a dict with params for this View
       
    51     """
       
    52 
       
    53     rights = access.Checker(params)
       
    54 
       
    55     new_params = {}
       
    56     new_params['rights'] = rights
       
    57     new_params['logic'] = priority_group_logic
       
    58 
       
    59     new_params['name'] = "Cron"
       
    60 
       
    61     new_params['django_patterns_defaults'] = [
       
    62         (r'^%(url_name)s/(?P<access_type>add)$',
       
    63           'soc.views.models.%(module_name)s.add', 'Add %(name_short)s'),
       
    64         (r'^%(url_name)s/(?P<access_type>poke)$',
       
    65           'soc.views.models.%(module_name)s.poke', 'Poke %(name_short)s'),
       
    66         ]
       
    67 
       
    68     params = dicts.merge(params, new_params)
       
    69 
       
    70     super(View, self).__init__(params=params)
       
    71 
       
    72   def add(self, request, access_type, page_name):
       
    73     group = priority_group_logic.getGroup(priority_group_logic.EMAIL)
       
    74 
       
    75     fields = {
       
    76         'priority_group': group,
       
    77         'task_name': 'sendAcceptanceEmail',
       
    78         'text_data': "O HI THAR",
       
    79         }
       
    80 
       
    81     job_logic.updateOrCreateFromFields(fields)
       
    82 
       
    83     return http.HttpResponse("Done")
       
    84 
       
    85   def poke(self, request, access_type, page_name):
       
    86     """
       
    87     """
       
    88 
       
    89     order = ['-priority']
       
    90     query = priority_group_logic.getQueryForFields(order=order)
       
    91     groups = priority_group_logic.getAll(query)
       
    92     handler = soc.cron.job.handler
       
    93 
       
    94     groups_completed = 0
       
    95     jobs_completed = 0
       
    96 
       
    97     for group in groups:
       
    98       filter = {
       
    99           'priority_group': group,
       
   100           'status': 'waiting',
       
   101           }
       
   102 
       
   103       query = job_logic.getQueryForFields(filter=filter)
       
   104       jobs = job_logic.getAll(query)
       
   105 
       
   106       for job in jobs:
       
   107         job_key = job.key().id()
       
   108         good = handler.handle(job_key)
       
   109 
       
   110         if not good:
       
   111           break
       
   112 
       
   113         jobs_completed += 1
       
   114 
       
   115       groups_completed += 1
       
   116 
       
   117     response = 'Completed %d jobs and %d groups completed.' % (
       
   118         jobs_completed, groups_completed)
       
   119 
       
   120     return http.HttpResponse(response)
       
   121 
       
   122 
       
   123 view = View()
       
   124 
       
   125 add = view.add
       
   126 poke = view.poke