forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
55 lines (40 loc) · 2.04 KB
/
models.py
File metadata and controls
55 lines (40 loc) · 2.04 KB
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
from datetime import datetime
from sqlalchemy import Column, String, Integer, DateTime, ForeignKey, UniqueConstraint
from sqlalchemy.orm import relationship
from database import Base
import secrets
import string
def generate_id(prefix: str) -> str:
"""Generate a random ID with the given prefix."""
chars = string.ascii_letters + string.digits + "_-"
random_part = ''.join(secrets.choice(chars) for _ in range(8))
return f"{prefix}_{random_part}"
class Post(Base):
__tablename__ = "posts"
id = Column(String, primary_key=True, default=lambda: generate_id("p"))
username = Column(String, nullable=False)
content = Column(String, nullable=False)
createdAt = Column(DateTime, nullable=False, default=datetime.utcnow)
updatedAt = Column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
likes = Column(Integer, nullable=False, default=0)
comments = relationship("Comment", back_populates="post", cascade="all, delete-orphan")
liked_by = relationship("Like", back_populates="post", cascade="all, delete-orphan")
class Comment(Base):
__tablename__ = "comments"
id = Column(String, primary_key=True, default=lambda: generate_id("c"))
postId = Column(String, ForeignKey("posts.id"), nullable=False)
username = Column(String, nullable=False)
content = Column(String, nullable=False)
createdAt = Column(DateTime, nullable=False, default=datetime.utcnow)
updatedAt = Column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
post = relationship("Post", back_populates="comments")
class Like(Base):
__tablename__ = "likes"
id = Column(Integer, primary_key=True, autoincrement=True)
postId = Column(String, ForeignKey("posts.id"), nullable=False)
username = Column(String, nullable=False)
createdAt = Column(DateTime, nullable=False, default=datetime.utcnow)
post = relationship("Post", back_populates="liked_by")
__table_args__ = (
UniqueConstraint('postId', 'username', name='unique_post_user_like'),
)