Changes between Version 2 and Version 3 of Version2/Tutoriales/Consola_Web/Tutorial_6_Subjobs


Ignore:
Timestamp:
May 5, 2011, 12:00:42 PM (13 years ago)
Author:
edulix
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Version2/Tutoriales/Consola_Web/Tutorial_6_Subjobs

    v2 v3  
    88
    99En este sexto tutorial modificaremos el código del tutorial 4 para poder ejecutar el comando uptime sobre una unidad organizativa, enviándose a todos los ordenadores dentro de la misma. Para ello crearemos un Job padre que agrupe la tarea conjunta, y un job hijo para cada ordenador donde se vaya a ejecutar el comando.
     10
     11== Las acciones ==
     12
     13Modificaremos el código del tutorial 4 para que la acción uptime aparezca también en las unidades organizativas:
     14
     15
     16{{{
     17#!python
     18'''
     19Jobs plugin example
     20'''
     21
     22from ..pluginbase import PluginBase
     23from view import UptimeView
     24
     25class Plugin(PluginBase):
     26    '''
     27    '''
     28    def enable(self):
     29        self.actions_for_url = ('navigator/(computer|ou)/(.*)', 'get_uptime')
     30        self.urls = ('self.get_action_url('get_uptime')', UptimeView)
     31}}}
     32
     33Y el código de la vista para que tenga en cuenta que debe comprobar si estamos tratando con un ordenador o una unidad organizativa, y crear y el job correspondiente:
     34
     35{{{
     36#!python
     37import web
     38import datetime
     39from jobs import GetUptimeJob
     40from decorators import pi18n
     41
     42class UptimeView:
     43    @pi18n
     44    def GET(self, entity_type, entity_name):
     45        orm = web.ctx.orm
     46        if entity_type == 'computer':
     47            computer = orm.query(Computer).filter(Computer.name == name).first()
     48            if not computer:
     49                self.error(_('Computer "%s" not found') % name)
     50            if not computer.online:
     51                # makes no sense to send uptime to an offline computer
     52                self.error(_('Computer "%s" not online') % name)
     53            parent_job = UptimeJob(computer)
     54            paren_job.send()
     55
     56        elif entity_type == 'ou':
     57            ou = orm.query(OrganizativeUnit).filter(OrganizativeUnit.name == name).first()
     58            if not ou:
     59                self.error(_('Organizative Unit "%s" not found') % name)
     60            parent_job = Job()
     61            for computer in ou.get_all_descendant_computers():
     62                # makes no sense to send uptime to an offline computer
     63                if not computer.online:
     64                    continue
     65                UptimeJob(computer, parent_job)
     66            parent_job.send()
     67        else:
     68            self.error(_('This command is only for computers or ous'))
     69
     70        web.ctx.notify(_('Uptime command sent to %s') % name, 'notification')
     71        raise web.seeother('/jobs/%s' % parent_job.id)
     72}}}