Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sqlalchemy import create_engine
- from sqlalchemy import Column, Integer, String, Sequence, UniqueConstraint, PrimaryKeyConstraint
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy.orm import sessionmaker
- from contextlib import contextmanager
- Base = declarative_base()
- class User(Base):
- __tablename__ = 'users'
- user_id = Column(Integer, primary_key=True)
- access = Column(Integer)
- def __repr__(self):
- return "<User(user_id='{user_id}', access='{access}')>".format(user_id=self.user_id, access=self.access)
- class Tag(Base):
- __tablename__ = 'tags'
- service = Column(String(15), nullable=False)
- tag = Column(String(50),nullable=False)
- last_check = Column(Integer)
- missing_times = Column(Integer)
- __table_args__ = (PrimaryKeyConstraint('service', 'tag', name='tag_pkey'),)
- def __repr__(self):
- return "<Tag(tag='{tag}', last_check='{last_check}, missing_times='{missing_times}')>".format(tag=self.tag,
- last_check=self.last_check,
- missing_times=self.missing_times)
- class Setting(Base):
- __tablename__ = 'settings'
- setting = Column(String(30), primary_key=True)
- value = Column(String(30), nullable=False)
- def __repr__(self):
- return "<Setting(setting='{setting}', value='{value}')>".format(setting=self.setting, value=self.value)
- class QueueItem(Base):
- __tablename__ = 'queue'
- id = Column(Integer, Sequence('queue_id_seq'), primary_key=True)
- sender = Column(Integer, nullable=False)
- service = Column(String(15), nullable=False)
- post_id = Column(String(15), nullable=False)
- pic_name = Column(String(30))
- authors = Column(String(300))
- chars = Column(String)
- copyright = Column(String)
- __table_args__ = (UniqueConstraint('service', 'post_id', name='queue_service_post_id_key'),)
- def __repr__(self):
- return "<QueueItem(id='{id}', sender='{sender}', service='{service}', post_id='{post_id}', pic_name='{pic_name}', authors='{authors}', chars='{chars}', copyright='{copyright}')>".format(
- id=self.id, sender=self.sender, service=self.service, post_id=self.post_id, pic_name=self.pic_name,
- authors=self.authors, chars=self.chars, copyright=self.copyright)
- class HistoryItem(Base):
- __tablename__ = 'history'
- service = Column(String(15), nullable=False)
- post_id = Column(String(15), nullable=False)
- wall_id = Column(Integer)
- __table_args__ = (PrimaryKeyConstraint('service', 'post_id', name='history_pkey'),)
- def __repr__(self):
- return "<Setting(service='{service}', post_id='{post_id}', wall_id='{wall_id}')>".format(service=self.service,
- post_id=self.post_id,
- wall_id=self.wall_id)
- # logging.basicConfig()
- # logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
- conn_string = "postgresql://{user}:{password}@{host}:{port}/{db}".format(user=DB_USER, password=DB_PASSWORD,
- host=DB_HOST,
- port=DB_PORT, db=DB_NAME)
- engine = create_engine(conn_string)
- Base.metadata.create_all(engine)
- Session = sessionmaker(bind=engine)
- @contextmanager
- def session_scope():
- """Provide a transactional scope around a series of operations."""
- session = Session()
- try:
- yield session
- session.commit()
- except:
- session.rollback()
- raise
- finally:
- session.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement