Skip to content

Commit 4f393eb

Browse files
committed
fix orm to pydantic model conversion
1 parent 364c751 commit 4f393eb

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

jupyter_scheduler/models.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class NotificationsConfig(BaseModel):
4040
events: List[NotificationEvent] = []
4141
include_output: bool = False
4242

43-
def to_dict(self):
44-
return self.dict(exclude_none=True)
43+
class Config:
44+
orm_mode = True
4545

4646
@validator("send_to")
4747
def validate_send_to(cls, v):
@@ -155,12 +155,6 @@ def compute_input_filename(cls, values) -> Dict:
155155

156156
return values
157157

158-
@validator("notifications_config", pre=True, always=True)
159-
def convert_notifications_config(cls, v):
160-
if isinstance(v, NotificationsConfig):
161-
return v.to_dict()
162-
return v
163-
164158

165159
class JobFile(BaseModel):
166160
"""This model is used to describe the display value,

jupyter_scheduler/orm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def process_result_value(self, value, dialect):
7373
mapper_registry = registry()
7474

7575

76-
class NotificationsConfig(Base):
76+
class NotificationsConfigTable(Base):
7777
__tablename__ = "notifications_config"
7878
id = Column(String(36), primary_key=True, default=generate_uuid)
7979
include_output = Column(Boolean, default=False)
@@ -113,7 +113,7 @@ class Job(CommonColumns, Base):
113113
pid = Column(Integer)
114114
idempotency_token = Column(String(256))
115115
notifications_config_id = Column(String(36), ForeignKey("notifications_config.id"))
116-
notifications_config = relationship("NotificationsConfig", lazy="joined")
116+
notifications_config = relationship("NotificationsConfigTable", lazy="joined")
117117

118118

119119
class JobDefinition(CommonColumns, Base):
@@ -125,7 +125,7 @@ class JobDefinition(CommonColumns, Base):
125125
create_time = Column(Integer, default=get_utc_timestamp)
126126
active = Column(Boolean, default=True)
127127
notifications_config_id = Column(String(36), ForeignKey("notifications_config.id"))
128-
notifications_config = relationship("NotificationsConfig", lazy="joined")
128+
notifications_config = relationship("NotificationsConfigTable", lazy="joined")
129129

130130

131131
def create_tables(db_url, drop_tables=False):

jupyter_scheduler/scheduler.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
UpdateJob,
3939
UpdateJobDefinition,
4040
)
41-
from jupyter_scheduler.orm import Job, JobDefinition, create_session
41+
from jupyter_scheduler.orm import Job, JobDefinition, NotificationsConfigTable, create_session
4242
from jupyter_scheduler.utils import create_output_directory, create_output_filename
4343

4444

@@ -396,7 +396,17 @@ def create_job(self, model: CreateJob) -> str:
396396
if not model.output_formats:
397397
model.output_formats = []
398398

399-
job = Job(**model.dict(exclude_none=True, exclude={"input_uri"}))
399+
orm_notifications_config = None
400+
if model.notifications_config:
401+
orm_notifications_config = NotificationsConfigTable(
402+
**model.notifications_config.dict()
403+
)
404+
session.add(orm_notifications_config)
405+
406+
job_data = model.dict(exclude={"input_uri", "notifications_config"})
407+
job_data["notifications_config"] = orm_notifications_config
408+
409+
job = Job(**job_data)
400410
session.add(job)
401411
session.commit()
402412

0 commit comments

Comments
 (0)