-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
68 lines (54 loc) · 1.9 KB
/
Copy pathmodels.py
File metadata and controls
68 lines (54 loc) · 1.9 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
import datetime
from typing import List
from sqlalchemy import (
String,
Integer,
Date,
create_engine,
ForeignKey,
UniqueConstraint,
select,
)
from sqlalchemy.orm import (
DeclarativeBase,
Mapped,
mapped_column,
relationship,
sessionmaker,
)
from flask_sqlalchemy import SQLAlchemy
class HRDBBase(DeclarativeBase):
def __repr__(self):
return f"{self.__class__.__name__}(id={self.id})"
class Employee(HRDBBase):
__tablename__ = "hr_employees"
__table_args__ = (UniqueConstraint("email"),)
id: Mapped[int] = mapped_column(primary_key=True)
last_name: Mapped[str] = mapped_column(String(50))
first_name: Mapped[str] = mapped_column(String(50))
email: Mapped[str] = mapped_column(String(50))
phone: Mapped[str] = mapped_column(String(50))
title_id: Mapped[int] = mapped_column(ForeignKey("hr_designations.id"))
title: Mapped["Designation"] = relationship(back_populates="employees")
class Designation(HRDBBase):
__tablename__ = "hr_designations"
__table_args__ = (UniqueConstraint("title"),)
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(50))
max_leaves: Mapped[int] = mapped_column(Integer)
employees: Mapped[List["Employee"]] = relationship(back_populates="title")
class Leave(HRDBBase):
__tablename__ = "hr_leaves"
__table_args__ = (UniqueConstraint("employee_id", "date"),)
id: Mapped[int] = mapped_column(primary_key=True)
date: Mapped[datetime.date] = mapped_column(Date())
employee_id: Mapped[int] = mapped_column(ForeignKey("hr_employees.id"))
reason: Mapped[str] = mapped_column(String(200))
def create_all(db_uri):
engine = create_engine(db_uri)
HRDBBase.metadata.create_all(engine)
def get_session(db_uri):
engine = create_engine(db_uri)
Session = sessionmaker(bind=engine)
session = Session()
return session