-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmodels.py
More file actions
132 lines (101 loc) · 3.57 KB
/
models.py
File metadata and controls
132 lines (101 loc) · 3.57 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import datetime
from sqlalchemy import (
Column,
DECIMAL,
String,
Integer,
Date,
create_engine,
ForeignKey,
Table
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
engine = create_engine(
"mysql+pymysql://root:qwerty@127.0.0.1:33061/startup"
)
Base = declarative_base(bind=engine)
employees_departments = Table(
"employees_departments",
Base.metadata,
Column("employee_id", ForeignKey("employees.pesel"), primary_key=True),
Column("department_id", ForeignKey("departments.id"), primary_key=True),
Column("date", Date, default=datetime.datetime.now)
)
managers_departments = Table(
"managers_departments",
Base.metadata,
Column("manager_id", ForeignKey("employees.pesel"), primary_key=True),
Column("department_id", ForeignKey("departments.id"), primary_key=True),
Column("date", Date, default=datetime.datetime.now)
)
employees_positions = Table(
"employees_positions",
Base.metadata,
Column("employee_id", ForeignKey("employees.pesel"), primary_key=True),
Column("position_id", ForeignKey("positions.id"), primary_key=True),
Column("date", Date, default=datetime.datetime.now)
)
class Position(Base):
__tablename__ = "positions"
id = Column(Integer, primary_key=True)
name = Column(String(20), nullable=False)
amount = Column(DECIMAL(precision=10, scale=2), nullable=False)
employees = relationship(
"Employee",
secondary=employees_positions,
back_populates="positions"
)
def __repr__(self) -> str:
return f"Position({self.id}, {self.name}, {self.amount})"
class Salary(Base):
__tablename__ = "salaries"
id = Column(Integer, primary_key=True)
amount = Column(DECIMAL(precision=10, scale=2), nullable=False)
pesel = Column(String(20), ForeignKey("employees.pesel"), nullable=False)
date = Column(Date, nullable=False, default=datetime.datetime.now)
employee = relationship("Employee", back_populates="salaries")
def __repr__(self) -> str:
return f"Salary({self.pesel}, {self.amount}, {self.date})"
class Employee(Base):
__tablename__ = "employees"
pesel = Column(String(20), primary_key=True)
first_name = Column(String(50), nullable=False)
last_name = Column(String(50), nullable=False)
email = Column(String(50), nullable=False, unique=True)
birth = Column(Date, nullable=False)
phone = Column(String(50), nullable=False)
home_address = Column(String(100), nullable=False)
salaries = relationship("Salary")
departments = relationship(
"Department",
secondary=employees_departments,
back_populates="employees"
)
positions = relationship(
"Position",
secondary=employees_positions,
back_populates="employees"
)
def pay(self, amount: float, date: str = None) -> None:
self.salaries.append(
Salary(amount=amount, date=date, pesel=self.pesel)
)
def __repr__(self) -> str:
return f"Employee({self.first_name}, {self.last_name})"
class Department(Base):
__tablename__ = "departments"
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False, unique=True)
address = Column(String(50))
employees = relationship(
"Employee",
secondary=employees_departments,
back_populates="departments"
)
managers = relationship(
"Employee",
secondary=managers_departments
)
def __repr__(self) -> str:
return f"Department({self.name}, {self.manager_id})"