Package Camelot :: Package camelot :: Package model :: Module batch_job |
[frames] | no frames]

# Source Code for Module Camelot.camelot.model.batch_job

     1  """
     2  Classes to store the result of batch jobs to enable the user to
     3  review or plan them
     4  """
     5
     6  from elixir.entity import Entity
     7  from elixir.options import using_options
     8  from elixir.fields import Field
     9  import sqlalchemy.types
    10  from elixir.relationships import ManyToOne
    11
    12  from camelot.core.utils import ugettext_lazy as _
    13  from camelot.model import metadata
    14  from camelot.view import filters
    15  from camelot.admin.entity_admin import EntityAdmin
    16  import camelot.types
    17
    18  import datetime
    19
    20  __metadata__ = metadata
    21
    22 -class BatchJobType(Entity):
    23      using_options( tablename = 'batch_job_type' )
    24      name   = Field(sqlalchemy.types.Unicode(256), required=True)
    25      parent = ManyToOne('BatchJobType')
    26
    27 -    def __unicode__(self):
    28          return self.name
    29
    30      @classmethod
    31 -    def get_or_create( cls, name ):
    32          batch_job_type = cls.query.filter_by( name = name ).first()
    33          if not batch_job_type:
    34              batch_job_type = cls( name = name )
    35              batch_job_type.flush()
    36          return batch_job_type
    37
    38 -    class Admin(EntityAdmin):
    39          verbose_name = _('Batch job type')
    40          list_display = ['name', 'parent']
    41
    42 -def hostname():
    43      import socket
    44      return socket.gethostname()
    45
    46 -class BatchJob(Entity):
    47      using_options( tablename = 'batch_job', order_by=['-date'] )
    48      date    = Field(sqlalchemy.types.DateTime, required=True, default=datetime.datetime.now)
    49      host    = Field(sqlalchemy.types.Unicode(256), required=True, default=hostname)
    50      type    = ManyToOne('BatchJobType', required=True, ondelete = 'restrict', onupdate = 'cascade')
    51      status  = Field(camelot.types.Enumeration([(-2, 'planned'), (-1, 'running'), (0, 'success'), (1,'warnings'), (2,'errors')]), required=True, default='planned' )
    52      message = Field(camelot.types.RichText())
    53
    54 -    def add_exception_to_message(self, exception):
    55          import traceback, cStringIO
    56          sio = cStringIO.StringIO()
    57          traceback.print_exc(file=sio)
    58          traceback_print = sio.getvalue()
    59          sio.close()
    60          self.message = (self.message or '') + '<br/>' + unicode(exception) + '<br/>' +  traceback_print.replace('\n', '<br/>')
    61
    62 -    def add_strings_to_message(self, strings):
    63          """:param strings: a list or generator of strings"""
    64          self.message = (self.message or '') + '<br/>'.join(list(strings))
    65
    66 -    class Admin(EntityAdmin):
    67          verbose_name = _('Batch job')
    68          list_display = ['date', 'host', 'type', 'status']
    69          list_filter = ['status', filters.ComboBoxFilter('host')]
    70          form_display = list_display + ['message']
    71