-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
47 lines (37 loc) · 1.88 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
import enum
from sqlalchemy import Column, Integer, String, Boolean, Enum, DateTime, ForeignKey, UniqueConstraint
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from db import Base
# Enum for review status
class ReviewStatus(enum.Enum):
IN_REVIEW = "IN_REVIEW"
DONE = "DONE"
def __len__(self) -> int:
return len(self.value)
# User model
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
name = Column(String, nullable=False)
slack_id = Column(String, nullable=False)
channel_id = Column(String, nullable=False)
is_reviewer = Column(Boolean, default=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
# Relationships
reviews_given = relationship("Review", foreign_keys="[Review.reviewer_id]", back_populates="reviewer")
reviews_received = relationship("Review", foreign_keys="[Review.user_id]", back_populates="user")
__table_args__ = (UniqueConstraint("slack_id", "channel_id", name="_slack_channel_uc"),)
class Review(Base):
__tablename__ = "reviews"
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
url = Column(String, nullable=False)
reviewer_id = Column(Integer, ForeignKey("users.id"), nullable=False)
status = Column(Enum(ReviewStatus), nullable=False, default=ReviewStatus.IN_REVIEW)
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
# Relationships
reviewer = relationship("User", foreign_keys=[reviewer_id], back_populates="reviews_given")
user = relationship("User", foreign_keys=[user_id], back_populates="reviews_received")