You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.5 KiB
Python
91 lines
2.5 KiB
Python
7 years ago
|
from configparser import ConfigParser
|
||
|
|
||
7 years ago
|
import bcrypt
|
||
7 years ago
|
from sqlalchemy.ext.declarative import declarative_base
|
||
|
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Boolean, Numeric
|
||
|
from sqlalchemy.orm import relationship, backref
|
||
|
from sqlalchemy.sql import expression
|
||
|
from sqlalchemy.ext.compiler import compiles
|
||
|
from sqlalchemy.types import DateTime as DateTimeType
|
||
|
|
||
7 years ago
|
cfg = ConfigParser()
|
||
7 years ago
|
cfg.read('config.ini')
|
||
7 years ago
|
|
||
7 years ago
|
BCRYPT_LOG_ROUNDS = int(cfg.get('user', 'BCRYPT_LOG_ROUNDS'))
|
||
7 years ago
|
|
||
7 years ago
|
|
||
7 years ago
|
# can be moved to models util?
|
||
7 years ago
|
class utcnow(expression.FunctionElement):
|
||
|
type = DateTimeType()
|
||
|
|
||
7 years ago
|
|
||
7 years ago
|
# Can be moved to the models util?
|
||
7 years ago
|
@compiles(utcnow, 'postgresql')
|
||
|
def pg_utcnow(element, compiler, **kw):
|
||
|
return "TIMEZONE('utc', CURRENT_TIMESTAMP)"
|
||
|
|
||
|
|
||
|
Base = declarative_base()
|
||
|
|
||
|
|
||
|
class DBMixin:
|
||
7 years ago
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||
7 years ago
|
created_date = Column(DateTime, server_default=utcnow())
|
||
|
modified_date = Column(DateTime, server_default=utcnow(), onupdate=utcnow())
|
||
|
|
||
7 years ago
|
def to_dict(self):
|
||
|
d = self.__dict__
|
||
|
d.pop('_sa_instance_state')
|
||
|
return d
|
||
|
|
||
7 years ago
|
|
||
|
def ReferenceCol(tablename, nullable=False, **kw):
|
||
|
return Column(ForeignKey('{}.id'.format(tablename)), nullable=nullable, **kw)
|
||
|
|
||
|
|
||
|
class Cookie(Base, DBMixin):
|
||
|
__tablename__ = 'cookies'
|
||
|
|
||
|
name = Column(String(50), index=True)
|
||
|
recipe_url = Column(String(255))
|
||
|
sku = Column(String(55))
|
||
|
qoh = Column(Integer)
|
||
|
unit_cost = Column(Numeric(12, 2))
|
||
|
|
||
|
|
||
|
class User(DBMixin, Base):
|
||
|
__tablename__ = 'users'
|
||
|
|
||
7 years ago
|
email = Column(String(255), nullable=False, unique=True)
|
||
7 years ago
|
password = Column(String(255))
|
||
7 years ago
|
admin = Column(Boolean, nullable=False, default=False)
|
||
|
|
||
|
def __init__(self, email, password, admin=False):
|
||
|
self.email = email
|
||
7 years ago
|
self.password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt(BCRYPT_LOG_ROUNDS)).decode()
|
||
7 years ago
|
self.admin = admin
|
||
7 years ago
|
|
||
7 years ago
|
def check_password(self, password):
|
||
|
return bcrypt.checkpw(password.encode('utf-8'), self.password.encode('utf-8'))
|
||
|
|
||
7 years ago
|
|
||
|
class Order(DBMixin, Base):
|
||
|
__tablename__ = 'orders'
|
||
|
|
||
|
user_id = ReferenceCol('users')
|
||
|
shipped = Column(Boolean, default=False)
|
||
|
|
||
|
user = relationship('User', backref=backref('orders'))
|
||
|
|
||
|
|
||
|
class LineItem(DBMixin, Base):
|
||
|
__tablename__ = 'line_items'
|
||
|
|
||
|
order_id = ReferenceCol('orders')
|
||
|
cookie_id = ReferenceCol('cookies')
|
||
|
quantity = Column(Integer)
|
||
|
extended_cost = Column(Numeric(12, 2))
|
||
|
|
||
|
order = relationship('Order', backref=backref('line_items'))
|
||
|
cookie = relationship('Cookie', uselist=False)
|