-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
63 lines (48 loc) · 1.97 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from sqlalchemy import Column, Integer, String, Float, ForeignKey, DateTime
from sqlalchemy.orm import relationship, declarative_base
class BaseModel:
__abstract__ = True
def keyvalgen(self):
""" Generate attr name/val pairs, filtering out SQLA attrs."""
excl = ('_sa_adapter', '_sa_instance_state')
for k, v in vars(self).items():
if not k.startswith('_') and not any(hasattr(v, a) for a in excl):
yield k, v
def __repr__(self):
params = ', '.join(f'{k}={v}' for k, v in self.keyvalgen())
return f"{self.__class__.__name__}({params})"
def __str__(self):
return self.__repr__()
Base = declarative_base(cls=BaseModel)
class Brewery(Base):
__tablename__ = "breweries"
id = Column(Integer, primary_key=True)
name = Column(String)
slug = Column(String, unique=True)
beers = relationship("Beer", back_populates="brewery")
class Beer(Base):
__tablename__ = "beers"
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
slug = Column(String, nullable=False, unique=True)
alc_percentage = Column(Float)
brewery_id = Column(Integer, ForeignKey("breweries.id"))
brewery = relationship("Brewery", back_populates="beers")
reviews = relationship("Review", back_populates="beer")
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
slug = Column(String, unique=True)
description = Column(String)
reviews = relationship("Review", back_populates="user")
class Review(Base):
__tablename__ = "reviews"
id = Column(Integer, primary_key=True)
percentage = Column(Float)
comment = Column(String)
date = Column(DateTime)
beer_id = Column(Integer, ForeignKey("beers.id"))
user_id = Column(Integer, ForeignKey("users.id"))
beer = relationship("Beer", back_populates="reviews")
user = relationship("User", back_populates="reviews")