Advertisement
SetazeR

Untitled

Jan 23rd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.86 KB | None | 0 0
  1. from sqlalchemy import create_engine
  2. from sqlalchemy import Column, Integer, String, Sequence, UniqueConstraint, PrimaryKeyConstraint
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from sqlalchemy.orm import sessionmaker
  5. from contextlib import contextmanager
  6. Base = declarative_base()
  7.  
  8. class User(Base):
  9.     __tablename__ = 'users'
  10.     user_id = Column(Integer, primary_key=True)
  11.     access = Column(Integer)
  12.  
  13.     def __repr__(self):
  14.         return "<User(user_id='{user_id}', access='{access}')>".format(user_id=self.user_id, access=self.access)
  15.  
  16.  
  17. class Tag(Base):
  18.     __tablename__ = 'tags'
  19.     service = Column(String(15), nullable=False)
  20.     tag = Column(String(50),nullable=False)
  21.     last_check = Column(Integer)
  22.     missing_times = Column(Integer)
  23.     __table_args__ = (PrimaryKeyConstraint('service', 'tag', name='tag_pkey'),)
  24.     def __repr__(self):
  25.         return "<Tag(tag='{tag}', last_check='{last_check}, missing_times='{missing_times}')>".format(tag=self.tag,
  26.                                                                                                       last_check=self.last_check,
  27.                                                                                                       missing_times=self.missing_times)
  28.  
  29.  
  30. class Setting(Base):
  31.     __tablename__ = 'settings'
  32.     setting = Column(String(30), primary_key=True)
  33.     value = Column(String(30), nullable=False)
  34.  
  35.     def __repr__(self):
  36.         return "<Setting(setting='{setting}', value='{value}')>".format(setting=self.setting, value=self.value)
  37.  
  38.  
  39. class QueueItem(Base):
  40.     __tablename__ = 'queue'
  41.     id = Column(Integer, Sequence('queue_id_seq'), primary_key=True)
  42.     sender = Column(Integer, nullable=False)
  43.     service = Column(String(15), nullable=False)
  44.     post_id = Column(String(15), nullable=False)
  45.     pic_name = Column(String(30))
  46.     authors = Column(String(300))
  47.     chars = Column(String)
  48.     copyright = Column(String)
  49.     __table_args__ = (UniqueConstraint('service', 'post_id', name='queue_service_post_id_key'),)
  50.  
  51.     def __repr__(self):
  52.         return "<QueueItem(id='{id}', sender='{sender}', service='{service}', post_id='{post_id}', pic_name='{pic_name}', authors='{authors}', chars='{chars}', copyright='{copyright}')>".format(
  53.             id=self.id, sender=self.sender, service=self.service, post_id=self.post_id, pic_name=self.pic_name,
  54.             authors=self.authors, chars=self.chars, copyright=self.copyright)
  55.  
  56.  
  57. class HistoryItem(Base):
  58.     __tablename__ = 'history'
  59.     service = Column(String(15), nullable=False)
  60.     post_id = Column(String(15), nullable=False)
  61.     wall_id = Column(Integer)
  62.     __table_args__ = (PrimaryKeyConstraint('service', 'post_id', name='history_pkey'),)
  63.  
  64.     def __repr__(self):
  65.         return "<Setting(service='{service}', post_id='{post_id}', wall_id='{wall_id}')>".format(service=self.service,
  66.                                                                                                  post_id=self.post_id,
  67.                                                                                                  wall_id=self.wall_id)
  68.  
  69.  
  70. # logging.basicConfig()
  71. # logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
  72. conn_string = "postgresql://{user}:{password}@{host}:{port}/{db}".format(user=DB_USER, password=DB_PASSWORD,
  73.                                                                          host=DB_HOST,
  74.                                                                          port=DB_PORT, db=DB_NAME)
  75. engine = create_engine(conn_string)
  76. Base.metadata.create_all(engine)
  77. Session = sessionmaker(bind=engine)
  78.  
  79. @contextmanager
  80. def session_scope():
  81.     """Provide a transactional scope around a series of operations."""
  82.  
  83.     session = Session()
  84.     try:
  85.         yield session
  86.         session.commit()
  87.     except:
  88.         session.rollback()
  89.         raise
  90.     finally:
  91.         session.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement