From b2e6fab294614c3e94b4a2e2929f33f6b1cf9cd2 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 17 Jan 2023 16:37:45 +0100 Subject: [PATCH] fixes --- app/orm.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/app/orm.py b/app/orm.py index 50ff3ce..bbabbbc 100644 --- a/app/orm.py +++ b/app/orm.py @@ -1,4 +1,5 @@ -import datetime +from datetime import datetime, timedelta +from dateutil.relativedelta import relativedelta from sqlalchemy import Column, VARCHAR, CHAR, ForeignKey, DATETIME, update, and_, inspect from sqlalchemy.ext.declarative import declarative_base @@ -81,7 +82,10 @@ class Lease(Base): def __repr__(self): return f'Lease(origin_ref={self.origin_ref}, lease_ref={self.lease_ref}, expires={self.lease_expires})' - def serialize(self, renewal_period: float, renewal_delta: datetime.timedelta) -> dict: + def serialize(self, renewal_period: float, renewal_delta: timedelta) -> dict: + lease_renewal = int(Lease.calculate_renewal(renewal_period, renewal_delta).total_seconds()) + lease_renewal = self.lease_created + relativedelta(seconds=lease_renewal) + return { 'lease_ref': self.lease_ref, 'origin_ref': self.origin_ref, @@ -89,7 +93,7 @@ class Lease(Base): 'lease_created': self.lease_created.isoformat(), 'lease_expires': self.lease_expires.isoformat(), 'lease_updated': self.lease_updated.isoformat(), - 'lease_renewal': Lease.calculate_renewal(renewal_period, renewal_delta), + 'lease_renewal': lease_renewal.isoformat(), } @staticmethod @@ -134,7 +138,7 @@ class Lease(Base): return entity @staticmethod - def renew(engine: Engine, lease: "Lease", lease_expires: datetime.datetime, lease_updated: datetime.datetime): + def renew(engine: Engine, lease: "Lease", lease_expires: datetime, lease_updated: datetime): session = sessionmaker(bind=engine)() x = dict(lease_expires=lease_expires, lease_updated=lease_updated) session.execute(update(Lease).where(and_(Lease.origin_ref == lease.origin_ref, Lease.lease_ref == lease.lease_ref)).values(**x)) @@ -158,16 +162,17 @@ class Lease(Base): return deletions @staticmethod - def calculate_renewal(renewal_period: float, delta: datetime.timedelta): + def calculate_renewal(renewal_period: float, delta: timedelta) -> timedelta: """ + import datetime LEASE_RENEWAL_PERIOD=0.2 # 20% delta = datetime.timedelta(days=1) renew = delta.total_seconds() * LEASE_RENEWAL_PERIOD - renew = timedelta(seconds=renew) + renew = datetime.timedelta(seconds=renew) expires = delta - renew # 19.2 """ renew = delta.total_seconds() * renewal_period - renew = datetime.timedelta(seconds=renew) + renew = timedelta(seconds=renew) return delta - renew