fixed timezone handling

This commit is contained in:
Oscar Krause 2023-01-03 21:49:24 +01:00
parent f199800040
commit f35a4c6145
3 changed files with 22 additions and 8 deletions

View File

@ -27,6 +27,8 @@ from orm import Origin, Lease, init as db_init, migrate
logger = logging.getLogger() logger = logging.getLogger()
load_dotenv('../version.env') load_dotenv('../version.env')
TZ = datetime.now().astimezone().tzinfo
VERSION, COMMIT, DEBUG = env('VERSION', 'unknown'), env('COMMIT', 'unknown'), bool(env('DEBUG', False)) VERSION, COMMIT, DEBUG = env('VERSION', 'unknown'), env('COMMIT', 'unknown'), bool(env('DEBUG', False))
config = dict(openapi_url='/-/openapi.json', docs_url='/-/docs', redoc_url='/-/redoc') config = dict(openapi_url='/-/openapi.json', docs_url='/-/docs', redoc_url='/-/redoc')
@ -101,6 +103,7 @@ async def _config():
'LEASE_EXPIRE_DELTA': str(LEASE_EXPIRE_DELTA), 'LEASE_EXPIRE_DELTA': str(LEASE_EXPIRE_DELTA),
'LEASE_RENEWAL_PERIOD': str(LEASE_RENEWAL_PERIOD), 'LEASE_RENEWAL_PERIOD': str(LEASE_RENEWAL_PERIOD),
'CORS_ORIGINS': str(CORS_ORIGINS), 'CORS_ORIGINS': str(CORS_ORIGINS),
'TZ': str(TZ),
}) })

View File

@ -1,4 +1,4 @@
import datetime from datetime import datetime, timezone
from sqlalchemy import Column, VARCHAR, CHAR, ForeignKey, DATETIME, update, and_, inspect from sqlalchemy import Column, VARCHAR, CHAR, ForeignKey, DATETIME, update, and_, inspect
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
@ -86,9 +86,9 @@ class Lease(Base):
'lease_ref': self.lease_ref, 'lease_ref': self.lease_ref,
'origin_ref': self.origin_ref, 'origin_ref': self.origin_ref,
# 'scope_ref': self.scope_ref, # 'scope_ref': self.scope_ref,
'lease_created': self.lease_created.isoformat(), 'lease_created': self.lease_created.replace(tzinfo=timezone.utc).isoformat(),
'lease_expires': self.lease_expires.isoformat(), 'lease_expires': self.lease_expires.replace(tzinfo=timezone.utc).isoformat(),
'lease_updated': self.lease_updated.isoformat(), 'lease_updated': self.lease_updated.replace(tzinfo=timezone.utc).isoformat(),
} }
@staticmethod @staticmethod
@ -133,7 +133,7 @@ class Lease(Base):
return entity return entity
@staticmethod @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)() session = sessionmaker(bind=engine)()
x = dict(lease_expires=lease_expires, lease_updated=lease_updated) 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)) session.execute(update(Lease).where(and_(Lease.origin_ref == lease.origin_ref, Lease.lease_ref == lease.lease_ref)).values(**x))

View File

@ -49,6 +49,17 @@ async function fetchOriginsWithLeases(element) {
} }
async function fetchLeases(element) { async function fetchLeases(element) {
// datetime config
const dtc = {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
timeZoneName: "short"
}
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.open("GET", '/-/leases?origin=true', true); xhr.open("GET", '/-/leases?origin=true', true);
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
@ -74,9 +85,9 @@ async function fetchLeases(element) {
let row = document.createElement('tr'); let row = document.createElement('tr');
row.innerHTML = ` row.innerHTML = `
<td><code>${o.lease_ref}</code></td> <td><code>${o.lease_ref}</code></td>
<td>${o.lease_created}</td> <td>${new Date(o.lease_created).toLocaleDateString('system', dtc)}</td>
<td>${o.lease_updated}</td> <td>${new Date(o.lease_updated).toLocaleDateString('system', dtc)}</td>
<td>${o.lease_expires}</td> <td>${new Date(o.lease_expires).toLocaleDateString('system', dtc)}</td>
<td><code>${o.origin_ref}</code></td>` <td><code>${o.origin_ref}</code></td>`
tbody.appendChild(row); tbody.appendChild(row);
}) })