-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
90 lines (65 loc) · 2.6 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
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
from sqlalchemy import (
Table,
Column,
Integer,
BigInteger,
String,
DateTime,
Boolean,
ForeignKey,
)
from sqlalchemy.orm import (
scoped_session,
sessionmaker,
relationship,
backref
)
from sqlalchemy.sql import (
func,
expression
)
from sqlalchemy.ext.declarative import declarative_base
DBSession = scoped_session(sessionmaker(expire_on_commit=False))
Base = declarative_base()
# TODO __user_tablename__ = 'user' etc
tweet_note_assc = Table('tweet_note_association', Base.metadata,
Column('note_id', Integer, ForeignKey('note.id', ondelete='CASCADE')),
Column('tweet_id', BigInteger, ForeignKey('tweet.id', ondelete='CASCADE'))
)
category_note_assc = Table('category_note_association', Base.metadata,
Column('note_id', Integer, ForeignKey('note.id', ondelete='CASCADE')),
Column('category_id', Integer, ForeignKey('category.id', ondelete='CASCADE'))
)
class Tweet(Base):
__tablename__ = 'tweet'
id = Column(BigInteger, primary_key=True)
url = Column(String(10000))
text = Column(String(10000))
# image
class Category(Base):
__tablename__ = 'category'
id = Column(Integer, primary_key=True)
name = Column(String(10000))
created_by_id = Column(BigInteger, ForeignKey('user.id', ondelete='CASCADE'))
# created_by = relationship("User")
created_at = Column(DateTime, server_default=func.now())
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
class Note(Base):
__tablename__ = 'note'
id = Column(Integer, primary_key=True)
text = Column(String(10000))
is_tweet = Column(Boolean, server_default=expression.false())
created_at = Column(DateTime, server_default=func.now())
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
created_by_id = Column(BigInteger, ForeignKey('user.id', ondelete='CASCADE'))
tweets = relationship(Tweet, secondary=tweet_note_assc, backref=backref('tweet', lazy='dynamic'))
categories = relationship(Category, secondary=category_note_assc, backref=backref('category', lazy='dynamic'))
# created_by = relationship("User")
# users_mentioned = Column(Integer, ForeignKey('user.id'))
class User(Base):
__tablename__ = 'user'
id = Column(BigInteger, primary_key=True)
email = Column(String(256))
# TODO Insert twitter user info here
notes = relationship(Note, backref="created_by")
categories = relationship(Category, backref="created_by")