-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
94 lines (76 loc) · 3.61 KB
/
models.py
File metadata and controls
94 lines (76 loc) · 3.61 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from sqlalchemy import Column, Integer, String, Boolean, ForeignKey, Table, Text, DateTime
from sqlalchemy.orm import relationship
from database import Base
from datetime import datetime
# Association table for followers (many-to-many)
followers = Table(
'followers',
Base.metadata,
Column('follower_id', Integer, ForeignKey('users.id', ondelete='CASCADE')),
Column('followed_id', Integer, ForeignKey('users.id', ondelete='CASCADE'))
)
# Association table for post likes (many-to-many)
post_likes = Table(
'post_likes',
Base.metadata,
Column('user_id', Integer, ForeignKey('users.id', ondelete='CASCADE')),
Column('post_id', Integer, ForeignKey('posts.id', ondelete='CASCADE'))
)
# Association table for comment likes (many-to-many)
comment_likes = Table(
'comment_likes',
Base.metadata,
Column('user_id', Integer, ForeignKey('users.id', ondelete='CASCADE')),
Column('comment_id', Integer, ForeignKey('comments.id', ondelete='CASCADE'))
)
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
username = Column(String(50), unique=True, index=True, nullable=False)
email = Column(String(100), unique=True, index=True, nullable=False)
hashed_password = Column(String(255), nullable=False)
full_name = Column(String(100))
bio = Column(Text)
created_at = Column(DateTime, default=datetime.utcnow)
is_active = Column(Boolean, default=True)
# Relationships
posts = relationship("Post", back_populates="author", cascade="all, delete-orphan")
comments = relationship("Comment", back_populates="author", cascade="all, delete-orphan")
# Users this user is following
following = relationship(
"User",
secondary=followers,
primaryjoin=(followers.c.follower_id == id),
secondaryjoin=(followers.c.followed_id == id),
backref="followers_list"
)
# Posts this user has liked
liked_posts = relationship("Post", secondary=post_likes, back_populates="liked_by")
# Comments this user has liked
liked_comments = relationship("Comment", secondary=comment_likes, back_populates="liked_by")
class Post(Base):
__tablename__ = "posts"
id = Column(Integer, primary_key=True, index=True)
title = Column(String(200), nullable=False)
content = Column(Text, nullable=False)
image_url = Column(String(500))
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
published = Column(Boolean, default=True)
author_id = Column(Integer, ForeignKey('users.id', ondelete='CASCADE'), nullable=False)
# Relationships
author = relationship("User", back_populates="posts")
comments = relationship("Comment", back_populates="post", cascade="all, delete-orphan")
liked_by = relationship("User", secondary=post_likes, back_populates="liked_posts")
class Comment(Base):
__tablename__ = "comments"
id = Column(Integer, primary_key=True, index=True)
content = Column(Text, nullable=False)
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
author_id = Column(Integer, ForeignKey('users.id', ondelete='CASCADE'), nullable=False)
post_id = Column(Integer, ForeignKey('posts.id', ondelete='CASCADE'), nullable=False)
# Relationships
author = relationship("User", back_populates="comments")
post = relationship("Post", back_populates="comments")
liked_by = relationship("User", secondary=comment_likes, back_populates="liked_comments")