| 10 | |
| 11 | == Las acciones == |
| 12 | |
| 13 | Modificaremos 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 | ''' |
| 19 | Jobs plugin example |
| 20 | ''' |
| 21 | |
| 22 | from ..pluginbase import PluginBase |
| 23 | from view import UptimeView |
| 24 | |
| 25 | class 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 | |
| 33 | Y 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 |
| 37 | import web |
| 38 | import datetime |
| 39 | from jobs import GetUptimeJob |
| 40 | from decorators import pi18n |
| 41 | |
| 42 | class 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 | }}} |