-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
82 lines (66 loc) · 2.09 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
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///phones.sqlite', echo=False)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String(250))
phones = relationship("Phone", back_populates="user", cascade="all, delete-orphan")
def __str__(self):
return self.name
@classmethod
def add(cls, name):
user = cls(name=name)
session.add(user)
session.commit()
return user
@classmethod
def delete(cls, name):
user = cls(name=name)
session.query(User).filter(User.name == name).delete()
session.commit()
return user
@classmethod
def update(cls, old_name, new_name):
users = session.query(User).all()
for i in users:
if i.name == old_name:
i.name = new_name
session.commit()
return User.name
@classmethod
def all(cls):
return session.query(cls).all()
class Phone(Base):
__tablename__ = 'phones'
id = Column(Integer, primary_key=True)
phone = Column(String(250), nullable=False)
user_id = Column(Integer, ForeignKey('users.id'))
user = relationship("User", back_populates="phones")
def __str__(self):
return self.phone
@classmethod
def add(cls, phone, user):
phone = cls(phone=phone, user=user)
session.add(phone)
session.commit()
return phone
@classmethod
def all(cls):
return session.query(cls).all()
@classmethod
def update(cls, old_phone, new_phone):
phones = session.query(Phone).all()
for i in phones:
if i.phone == old_phone:
i.phone = new_phone
session.commit()
return Phone.phone
Base.metadata.create_all(engine)