Skip to content

Commit

Permalink
depr: 3.12: fix datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
kerberizer committed Sep 13, 2024
1 parent 7e013cc commit 78e83c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
23 changes: 16 additions & 7 deletions src/rcapi/api/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pathlib import Path
import traceback
from datetime import datetime
from datetime import timezone as tz
import hashlib
import glob
import traceback
Expand All @@ -20,7 +21,7 @@
router = APIRouter()

config, UPLOAD_DIR, NEXUS_DIR, TEMPLATE_DIR = initialize_dirs()
DATE_FORMAT = "%a, %d %b %Y %H:%M:%S GMT"
DATE_FORMAT = "%a, %d %b %Y %H:%M:%S %z"

async def get_request(request: Request = Depends()):
return request
Expand Down Expand Up @@ -51,7 +52,7 @@ def generate_etag(data):
def get_last_modified(file_path):
try:
timestamp = os.path.getmtime(file_path)
last_modified = datetime.utcfromtimestamp(timestamp)
last_modified = datetime.fromtimestamp(timestamp, tz.utc)
return last_modified
except FileNotFoundError:
return None
Expand Down Expand Up @@ -229,8 +230,12 @@ async def get_template(request : Request, response : Response,
custom_headers = { "Last-Modified": last_modified_time.strftime(DATE_FORMAT) }
if format=="json":
# Check Last-Modified header
if if_modified_since and last_modified_time <= datetime.strptime(if_modified_since,DATE_FORMAT):
return JSONResponse(status_code=304, content=None)
if if_modified_since:
if_modified_since_dt = datetime.strptime(
if_modified_since.replace('GMT', '+0000'), DATE_FORMAT
)
if last_modified_time <= if_modified_since_dt:
return JSONResponse(status_code=304, content=None)

_etag = generate_etag(json_blueprint)
if if_none_match and if_none_match == str(_etag):
Expand Down Expand Up @@ -284,8 +289,12 @@ async def get_templates(request : Request,q:str = Query(None), response: Respons
list_of_json_files = glob.glob(os.path.join(TEMPLATE_DIR, '*.json'))
latest_json_file = max(list_of_json_files, key=os.path.getmtime)
last_modified_time = get_last_modified(latest_json_file)
if if_modified_since and last_modified_time <= datetime.strptime(if_modified_since,DATE_FORMAT):
return JSONResponse(status_code=304, content=None)
if if_modified_since:
if_modified_since_dt = datetime.strptime(
if_modified_since.replace('GMT', '+0000'), DATE_FORMAT
)
if last_modified_time <= if_modified_since_dt:
return JSONResponse(status_code=304, content=None)
except Exception as err:
print(err)
pass
Expand Down Expand Up @@ -370,4 +379,4 @@ def fetch_materials(project):
print("Failed to fetch materials:", response.status_code)
return []
except Exception as err:
return []
return []
15 changes: 8 additions & 7 deletions tests/test_api_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os.path
import shutil
from datetime import datetime, timedelta
from datetime import timezone as tz
import pandas as pd
from rcapi.api.templates import fetch_materials

Expand Down Expand Up @@ -74,10 +75,10 @@ def setup_template_dir(config_dict):
print(TEST_JSON_PATH)
file_path = os.path.join(TEMPLATE_DIR,"{}.json".format(TEMPLATE_UUID))
shutil.copy(TEST_JSON_PATH,file_path )
new_modified_date = datetime.now() - timedelta(hours=24)
new_modified_date = datetime.now(tz.utc) - timedelta(hours=24)
timestamp = new_modified_date.timestamp()
os.utime(file_path, times=(timestamp, timestamp))
now_date = datetime.utcnow() - timedelta(hours=12)
now_date = datetime.now(tz.utc) - timedelta(hours=12)
#headers = {"If-Modified-Since": now_date.strftime("%a, %d %b %Y %H:%M:%S GMT")}
assert new_modified_date <= now_date,new_modified_date
# Perform setup operations here, if any
Expand Down Expand Up @@ -140,14 +141,14 @@ def test_gettemplate(setup_template_dir):


def test_gettemplate_notmodified(setup_template_dir):
modified_date = datetime.utcnow() - timedelta(hours=12)
modified_date = datetime.now(tz.utc) - timedelta(hours=12)
headers = {"If-Modified-Since": modified_date.strftime("%a, %d %b %Y %H:%M:%S GMT")}
response_retrieve = client.get("/template",headers=headers)
assert response_retrieve.status_code == 304,response_retrieve.content


def test_gettemplateuuid_notmodified(setup_template_dir):
modified_date = datetime.utcnow() - timedelta(hours=12)
modified_date = datetime.now(tz.utc) - timedelta(hours=12)
headers = {"If-Modified-Since": modified_date.strftime("%a, %d %b %Y %H:%M:%S GMT")}
response_retrieve = client.get("/template/{}".format(TEMPLATE_UUID),headers=headers)
assert response_retrieve.status_code == 304,response_retrieve.content
Expand Down Expand Up @@ -194,7 +195,7 @@ def test_makecopy_finalized(setup_template_dir):


def test_doseresponse_excel(setup_template_dir):
modified_date = datetime.utcnow() - timedelta(hours=12)
modified_date = datetime.now(tz.utc) - timedelta(hours=12)
headers = {"If-Modified-Since": modified_date.strftime("%a, %d %b %Y %H:%M:%S GMT")}
#we ignore the header, want to generate the file on-the-fly
response_xlsx = client.get("/template/{}?format=xlsx&project=nanoreg".format(TEMPLATE_UUID),headers=headers)
Expand All @@ -207,7 +208,7 @@ def test_doseresponse_excel(setup_template_dir):
assert df.shape[0]>0,"materials"

def test_raman_excel(setup_template_dir):
modified_date = datetime.utcnow() - timedelta(hours=12)
modified_date = datetime.now(tz.utc) - timedelta(hours=12)
headers = {"If-Modified-Since": modified_date.strftime("%a, %d %b %Y %H:%M:%S GMT")}
#we ignore the header, want to generate the file on-the-fly
response_xlsx = client.get("/template/{}?format=xlsx&project=charisma".format(TEMPLATE_UUID_RAMAN),headers=headers)
Expand All @@ -220,7 +221,7 @@ def test_raman_excel(setup_template_dir):
assert df.shape[0]>0,"materials"

def test_doseresponse_nmparser(setup_template_dir):
modified_date = datetime.utcnow() - timedelta(hours=12)
modified_date = datetime.now(tz.utc) - timedelta(hours=12)
headers = {"If-Modified-Since": modified_date.strftime("%a, %d %b %Y %H:%M:%S GMT")}
#we ignore the header, want to generate the file on-the-fly
response_nmparser = client.get("/template/{}?format=nmparser".format(TEMPLATE_UUID),headers=headers)
Expand Down

0 comments on commit 78e83c1

Please sign in to comment.