class Memo(Base): __table__ = Table('memos', Base.metadata, Column('id', Integer, primary_key=True), Column('from_id', Integer, ForeignKey('identities.id'), nullable=False, index=True), Column('to_id', Integer, ForeignKey('identities.id'), nullable=False, index=True), Column('memo', IbidUnicodeText, nullable=False), Column('private', Boolean, nullable=False), Column('delivered', Boolean, nullable=False, index=True), Column('time', DateTime, nullable=False), useexisting=True) class MemoSchema(VersionedSchema): def upgrade_1_to_2(self): self.add_index(self.table.c.from_id) self.add_index(self.table.c.to_id) self.add_index(self.table.c.delivered) def upgrade_2_to_3(self): self.alter_column(Column('memo', IbidUnicodeText, nullable=False), force_rebuild=True) __table__.versioned_schema = MemoSchema(__table__, 3) def __init__(self, from_id, to_id, memo, private=False): self.from_id = from_id self.to_id = to_id self.memo = memo self.private = private self.delivered = False self.time = datetime.utcnow()
class FactoidValue(Base): __table__ = Table('factoid_values', Base.metadata, Column('id', Integer, primary_key=True), Column('value', IbidUnicodeText, nullable=False), Column('factoid_id', Integer, ForeignKey('factoids.id'), nullable=False, index=True), Column('identity_id', Integer, ForeignKey('identities.id'), index=True), Column('time', DateTime, nullable=False), Column('factpack', Integer, ForeignKey('factpacks.id'), index=True), useexisting=True) class FactoidValueSchema(VersionedSchema): def upgrade_1_to_2(self): self.add_column(Column('factpack', Integer, ForeignKey('factpacks.id'))) def upgrade_2_to_3(self): self.add_index(self.table.c.factoid_id) self.add_index(self.table.c.identity_id) self.add_index(self.table.c.factpack) def upgrade_3_to_4(self): self.alter_column(Column('value', IbidUnicodeText, nullable=False), force_rebuild=True) __table__.versioned_schema = FactoidValueSchema(__table__, 4) def __init__(self, value, identity_id, factoid_id=None, factpack=None): self.value = value self.factoid_id = factoid_id self.identity_id = identity_id self.time = datetime.utcnow() self.factpack = factpack def __repr__(self): return u'<FactoidValue %s %s>' % (self.factoid_id, self.value)
class URL(Base): __table__ = Table('urls', Base.metadata, Column('id', Integer, primary_key=True), Column('url', IbidUnicodeText, nullable=False), Column('channel', IbidUnicode(32, case_insensitive=True), nullable=False), Column('identity_id', Integer, ForeignKey('identities.id'), nullable=False, index=True), Column('time', DateTime, nullable=False), useexisting=True) class URLSchema(VersionedSchema): def upgrade_1_to_2(self): self.add_index(self.table.c.identity_id) def upgrade_2_to_3(self): self.alter_column(Column('url', IbidUnicodeText, nullable=False)) self.alter_column(Column('channel', IbidUnicode(32, case_insensitive=True), nullable=False), force_rebuild=True) __table__.versioned_schema = URLSchema(__table__, 3) def __init__(self, url, channel, identity_id): self.url = url self.channel = channel self.identity_id = identity_id self.time = datetime.utcnow()
class Factoid(Base): __table__ = Table('factoids', Base.metadata, Column('id', Integer, primary_key=True), Column('time', DateTime, nullable=False), Column('factpack', Integer, ForeignKey('factpacks.id'), index=True), useexisting=True) names = relation(FactoidName, cascade='all,delete', backref='factoid') values = relation(FactoidValue, cascade='all,delete', backref='factoid') class FactoidSchema(VersionedSchema): def upgrade_1_to_2(self): self.add_column( Column('factpack', Integer, ForeignKey('factpacks.id'))) def upgrade_2_to_3(self): self.add_index(self.table.c.factpack) __table__.versioned_schema = FactoidSchema(__table__, 3) def __init__(self, factpack=None): self.time = datetime.utcnow() self.factpack = factpack def __repr__(self): return u"<Factoid %s = %s>" % (', '.join([ name.name for name in self.names ]), ', '.join([value.value for value in self.values]))
class Sighting(Base): __table__ = Table('seen', Base.metadata, Column('id', Integer, primary_key=True), Column('identity_id', Integer, ForeignKey('identities.id'), nullable=False, index=True), Column('type', IbidUnicode(8), nullable=False, index=True), Column('channel', IbidUnicode(32)), Column('value', IbidUnicodeText), Column('time', DateTime, nullable=False), Column('count', Integer, nullable=False), UniqueConstraint('identity_id', 'type'), useexisting=True) class SightingSchema(VersionedSchema): def upgrade_1_to_2(self): self.add_index(self.table.c.identity_id) self.add_index(self.table.c.type) def upgrade_2_to_3(self): self.drop_index(self.table.c.type) self.alter_column(Column('type', IbidUnicode(8), nullable=False, index=True), force_rebuild=True) self.alter_column(Column('channel', IbidUnicode(32)), force_rebuild=True) self.alter_column(Column('value', IbidUnicodeText), force_rebuild=True) self.add_index(self.table.c.type) __table__.versioned_schema = SightingSchema(__table__, 3) identity = relation('Identity') def __init__(self, identity_id=None, type='message', channel=None, value=None): self.identity_id = identity_id self.type = type self.channel = channel self.value = value self.time = datetime.utcnow() self.count = 0 def __repr__(self): return u'<Sighting %s %s in %s at %s: %s>' % ( self.type, self.identity_id, self.channel, self.time, self.value)
class Item(Base): __table__ = Table('bucket_items', Base.metadata, Column('id', Integer, primary_key=True), Column('description', IbidUnicodeText(case_insensitive=True), nullable=False, index=True), Column('determiner', IbidUnicodeText(case_insensitive=True), index=True), Column('carried', Boolean, nullable=False, index=True), Column('giver_id', Integer, ForeignKey('identities.id'), nullable=False), useexisting=True) __table__.versioned_schema = VersionedSchema(__table__, 1) giver = relation(Identity) def __init__(self, description, determiner, giver): self.description = description self.determiner = determiner self.carried = True self.giver_id = giver @classmethod def carried_items(cls, session): return session.query(cls).filter_by(carried=True) @classmethod def take_item(cls, session): items = cls.carried_items(session) num = items.count() if num: item = items[randrange(0, num)] else: raise EmptyBucketException item.carried = False session.save_or_update(item) return item def __unicode__(self): if self.determiner: return self.determiner + u' ' + self.description else: return self.description
class Feed(Base): __table__ = Table('feeds', Base.metadata, Column('id', Integer, primary_key=True), Column('name', IbidUnicode(32, case_insensitive=True), unique=True, nullable=False, index=True), Column('url', IbidUnicodeText, nullable=False), Column('identity_id', Integer, ForeignKey('identities.id'), nullable=False, index=True), Column('time', DateTime, nullable=False), Column('source', IbidUnicode(32, case_insensitive=True), index=True), Column('target', IbidUnicode(32, case_insensitive=True), index=True), useexisting=True) class FeedSchema(VersionedSchema): def upgrade_1_to_2(self): self.add_index(self.table.c.name) self.add_index(self.table.c.identity_id) def upgrade_2_to_3(self): from ibid.db import IbidUnicode, Column self.add_column(Column('source', IbidUnicode(32), index=True)) self.add_column(Column('target', IbidUnicode(32), index=True)) def upgrade_3_to_4(self): self.drop_index(self.table.c.name) self.drop_index(self.table.c.source) self.drop_index(self.table.c.target) self.alter_column(Column('name', IbidUnicode(32, case_insensitive=True), unique=True, nullable=False, index=True), force_rebuild=True) self.alter_column(Column('url', IbidUnicodeText, nullable=False), force_rebuild=True) self.alter_column(Column('source', IbidUnicode(32, case_insensitive=True), index=True), force_rebuild=True) self.alter_column(Column('target', IbidUnicode(32, case_insensitive=True), index=True), force_rebuild=True) self.add_index(self.table.c.name) self.add_index(self.table.c.source) self.add_index(self.table.c.target) __table__.versioned_schema = FeedSchema(__table__, 4) feed = None entries = None def __init__(self, name, url, identity_id, source=None, target=None): self.name = name self.url = url self.identity_id = identity_id self.source = source self.target = target self.time = datetime.utcnow() self.update() def update(self, max_age=None): headers = {} if max_age: headers['Cache-Control'] = 'max-age=%i' % max_age feedfile = cacheable_download(self.url, "feeds/%s-%i.xml" % ( re.sub(r'\W+', '_', self.name), self.identity_id), headers) self.feed = feedparser.parse(feedfile) self.entries = self.feed['entries'] def __unicode__(self): if self.source is not None and self.target is not None: string = u'%s (notify %s on %s)' % ( self.name, self.target, self.source) if self.name in broken_feeds: string += ' [broken]' return string else: return self.name
def upgrade_1_to_2(self): self.add_column( Column('factpack', Integer, ForeignKey('factpacks.id')))
class FactoidName(Base): __table__ = Table('factoid_names', Base.metadata, Column('id', Integer, primary_key=True), Column('name', IbidUnicodeText(32, case_insensitive=True), key='_name', nullable=False, unique=True, index=True), Column('factoid_id', Integer, ForeignKey('factoids.id'), nullable=False, index=True), Column('identity_id', Integer, ForeignKey('identities.id'), index=True), Column('time', DateTime, nullable=False), Column('factpack', Integer, ForeignKey('factpacks.id'), index=True), Column('wild', Boolean, nullable=False, default=False, index=True), useexisting=True) class FactoidNameSchema(VersionedSchema): def upgrade_1_to_2(self): self.add_column( Column('factpack', Integer, ForeignKey('factpacks.id'))) def upgrade_2_to_3(self): self.add_index(self.table.c.name) def upgrade_3_to_4(self): self.add_index(self.table.c.name) self.add_index(self.table.c.factoid_id) self.add_index(self.table.c.identity_id) self.add_index(self.table.c.factpack) def upgrade_4_to_5(self): self.alter_column( Column('name', IbidUnicode(64), key='_name', nullable=False, unique=True, index=True)) def upgrade_5_to_6(self): self.alter_column( Column('name', IbidUnicodeText(32), key='_name', nullable=False, unique=True, index=True)) def upgrade_6_to_7(self): self.add_column( Column('wild', Boolean, PassiveDefault('0'), nullable=False, index=True, default=False)) for row in self.upgrade_session.query(FactoidName) \ .filter(FactoidName.name.like('%#_#%%', escape='#')) \ .all(): row.wild = True self.upgrade_session.add(row) def upgrade_7_to_8(self): self.drop_index(self.table.c._name) self.alter_column(Column('name', IbidUnicodeText(32, case_insensitive=True), key='_name', nullable=False, unique=True, index=True), force_rebuild=True) self.add_index(self.table.c._name) def upgrade_8_to_9(self): for row in self.upgrade_session.query(FactoidName) \ .filter_by(name=u'') \ .all(): self.upgrade_session.delete(row) if len(row.factoid.names) == 0: self.upgrade_session.delete(row.factoid) __table__.versioned_schema = FactoidNameSchema(__table__, 9) def __init__(self, name, identity_id, factoid_id=None, factpack=None): self.name = name self.factoid_id = factoid_id self.identity_id = identity_id self.time = datetime.utcnow() self.factpack = factpack def __repr__(self): return u'<FactoidName %s %s>' % (self.name, self.factoid_id) def _get_name(self): return unescape_name(self._name) def _set_name(self, name): self.wild = u'$arg' in name self._name = escape_name(name) name = synonym('_name', descriptor=property(_get_name, _set_name))