def __unicode__(self): return self.name @classmethod def get_or_create( cls, name ): batch_job_type = cls.query.filter_by( name = name ).first() if not batch_job_type: batch_job_type = cls( name = name ) batch_job_type.flush() return batch_job_type class Admin(EntityAdmin): verbose_name = _('Batch job type') list_display = ['name', 'parent'] BatchJobType = documented_entity()( BatchJobType ) def hostname(): import socket return socket.gethostname() class BatchJob(Entity): """Information the batch job that is planned, running or has run""" using_options( tablename = 'batch_job', order_by=['-date'] ) date = Field(sqlalchemy.types.DateTime, required=True, default=datetime.datetime.now) host = Field(sqlalchemy.types.Unicode(256), required=True, default=hostname) type = ManyToOne('BatchJobType', required=True, ondelete = 'restrict', onupdate = 'cascade') status = Field(camelot.types.Enumeration([(-2, 'planned'), (-1, 'running'), (0, 'success'), (1,'warnings'), (2,'errors')]), required=True, default='planned' ) message = Field(camelot.types.RichText()) def add_exception_to_message(self, exception):
@classmethod def getOrCreate( cls, code, name ): country = Country.query.filter_by( code = code ).first() if not country: from elixir import session country = Country( code = code, name = name ) session.flush( [country] ) return country class Admin( EntityAdmin ): form_size = ( 700, 150 ) verbose_name = _('Country') verbose_name_plural = _('Countries') list_display = ['name', 'code'] Country = documented_entity()(Country) class City( GeographicBoundary ): """A subclass of GeographicBoundary used to store the name, the postal code and the Country of a city""" using_options( tablename = 'geographic_boundary_city', inheritance = 'multi' ) country = ManyToOne( 'Country', required = True, ondelete = 'cascade', onupdate = 'cascade' ) @classmethod def getOrCreate( cls, country, code, name ): city = City.query.filter_by( code = code, country = country ).first() if not city: from elixir import session city = City( code = code, name = name, country = country ) session.flush( [city] )