forked from serhii-korobchenko/Python_Web_Team2_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
115 lines (90 loc) · 4.09 KB
/
models.py
File metadata and controls
115 lines (90 loc) · 4.09 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from datetime import datetime
from sqlalchemy import Column, Integer, String, Boolean, DateTime
from sqlalchemy.orm import relationship
from sqlalchemy.sql.schema import ForeignKey, Table
from db import Base, engine, db_session
#таблица для связи many-to-many между таблицами records и email
record_m2m_phone = Table(
"record_m2m_phone",
Base.metadata,
Column("id", Integer, primary_key=True),
Column("record", Integer, ForeignKey("records.id")),
Column("phone", Integer, ForeignKey("phones.id")),
)
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String(120), nullable=False)
email = Column(String(120), unique=True, nullable=False)
hash = Column(String(255), nullable=False)
token_cookie = Column(String(255), nullable=True, default=None)
records = relationship('Record', back_populates='user')
notes = relationship('Note', back_populates='user')
# isDeleted = default False
def __repr__(self):
return f"User({self.id}, {self.username}, {self.email})"
# Таблица Record
class Record(Base):
__tablename__ = "records"
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
created = Column(DateTime, default=datetime.now())
emails = relationship("Email", cascade="all, delete", backref="records")
adresses = relationship("Adress", cascade="all, delete", backref="records")
phones = relationship("Phone", cascade="all, delete", backref="records")
birthdays = relationship("Birthday", cascade="all, delete", backref="records")
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
user = relationship('User', cascade='all, delete', back_populates='records')
# phones = relationship("Phone", secondary=record_m2m_phone, cascade="all, delete", backref="records")
# Таблица Email
class Email(Base):
__tablename__ = "emails"
id = Column(Integer, primary_key=True)
email_name = Column(String(100), nullable=True)
rec_id = Column(Integer, ForeignKey("records.id", ondelete="CASCADE"))
user_id = Column(Integer, nullable=False)
# Таблица Adress
class Adress(Base):
__tablename__ = "adresses"
id = Column(Integer, primary_key=True)
adress_name = Column(String(250), nullable=True)
user_id = Column(Integer, nullable=False)
rec_id = Column(Integer, ForeignKey("records.id", ondelete="CASCADE"))
# Таблица Phone
class Phone(Base):
__tablename__ = "phones"
id = Column(Integer, primary_key=True)
phone_name = Column(String(20), nullable=True)
user_id = Column(Integer, nullable=False)
rec_id = Column(Integer, ForeignKey("records.id", ondelete="CASCADE"))
class Birthday(Base):
__tablename__ = "birthdays"
id = Column(Integer, primary_key=True)
birthday_date = Column('birthday_date', DateTime, default=datetime.now())
user_id = Column(Integer, nullable=False)
rec_id = Column(Integer, ForeignKey("records.id", ondelete="CASCADE"))
class Note(Base):
__tablename__ = "notes"
id = Column(Integer, primary_key=True)
note_title = Column(String(50), nullable=False)
note_text = Column(String(250), nullable=False)
created = Column(DateTime, default=datetime.now())
tags = relationship("Tag", cascade="all, delete", backref="notes")
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
user = relationship('User', cascade='all, delete', back_populates='notes')
class Tag(Base):
__tablename__ = "tags"
id = Column(Integer, primary_key=True)
tag_text = Column(String(50), nullable=False)
user_id = Column(Integer, nullable=False)
note_id = Column(Integer, ForeignKey("notes.id", ondelete="cascade"))
#alembic revision --autogenerate -m 'Init'
#alembic upgrade head
class News(Base):
__tablename__ = "news"
id = Column(Integer, primary_key=True)
headline = Column(String(250), nullable=False)
ful_ref = Column(String(250), nullable=False)
created = Column(DateTime, default=datetime.now())
if __name__ == "__main__":
Base.metadata.create_all(engine)