image-host-server/db/database.py

41 lines
1.2 KiB
Python

import os
from datetime import datetime
from sqlalchemy import create_engine, Column, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
user = os.environ['DB_USER']
password = os.environ['DB_PASSWORD']
host = os.environ['DB_HOST']
port = os.environ['DB_PORT']
database = os.environ['DB_DATABASE']
Base = declarative_base()
class Image(Base):
__tablename__ = "images"
id = Column('id', Integer(), primary_key=True, autoincrement=True)
location = Column('location', String(25), nullable=False)
description = Column('copyright', String(), nullable=False)
timestamp = Column('date_added', DateTime, default=datetime.now())
def __init__(self, location, description):
self.location = location
self.description = description
def get_connection():
return create_engine(
url="mysql+pymysql://{0}:{1}@{2}:{3}/{4}".format(
user, password, host, port, database
)
)
engine = create_engine
Base.metadata.create_all(engine)
Session = scoped_session(sessionmaker(bind=engine,
autocommit=False, autoflush=False))