Skip to content

Commit 85b565c

Browse files
authored
Expiration (#64)
* Added Expiration Date and Handling backend stores expiration date and proper handling * fixed small formatting issues * Changes to Handling Expired URLs Removed purge deletes and changed to checking if url is expired and if so, deletes and displays 404 * fixed expiration datetime format comment * Accept Dates in ISOString Format Added small formatting to accept ISOStrings * Multiline comment for parsed date formats * small comment format fix * Sorting feature for Expires At column --------- Co-authored-by: johnnyn <125083987+johnnyn7@users.noreply.github.com>
1 parent d14ddb9 commit 85b565c

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

modules/sqlite_helpers.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def insert_url(sqlite_file: str, url: str, alias: str, expiration_date: typing.U
4343
cursor = db.cursor()
4444
timestamp = datetime.now()
4545
if expiration_date is not None:
46+
if isinstance(expiration_date, str) and expiration_date.endswith('Z'):
47+
expiration_date = expiration_date[:-1] + '+00:00'
4648
expiration_date = datetime.fromisoformat(expiration_date)
4749
try:
4850
sql = "INSERT INTO urls(url, alias, created_at, expires_at) VALUES (?, ?, ?, ?)"
@@ -83,6 +85,7 @@ def get_urls(sqlite_file, page=0, search=None, sort_by="created_at", order="DESC
8385
"alias": row[2],
8486
"created_at": row[3],
8587
"used": row[4],
88+
"expires_at": row[5]
8689
}
8790
url_array.append(url_data)
8891
except KeyError:
@@ -126,9 +129,14 @@ def maybe_delete_expired_url(sqlite_file, sqlite_row) -> bool: #returns True if
126129
utc_tz = ZoneInfo('UTC')
127130

128131
expiration_datetime = None
129-
# sqlite_row[5] represents the expiration datetime e.g., "2024-11-04 18:05:24.006593"
132+
# sqlite_row[5] represents the expiration datetime in UTC timezone e.g., "2024-11-04 05:09:00+00:00"
133+
# The following date and datetime formats are now supported:
134+
# 2024-11-04
135+
# 2024-11-04T18:05:24
136+
# 2024-11-04T18:05:24.123456
137+
# 2024-11-04T18:05:24+02:00
130138
if sqlite_row[5] is not None:
131-
expiration_datetime = datetime.strptime(sqlite_row[5], "%Y-%m-%d %H:%M:%S.%f")
139+
expiration_datetime = datetime.fromisoformat(sqlite_row[5])
132140
expiration_datetime = expiration_datetime.replace(tzinfo=utc_tz)
133141

134142
now = datetime.now(tz=utc_tz)

server.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async def create_url(request: Request):
6868
alias = generate_alias(urljson["url"])
6969
if not alias.isalnum():
7070
raise ValueError("alias must only contain alphanumeric characters")
71-
expiration_date = urljson.get("expiration_date")
71+
expiration_date = urljson.get("expires_at")
7272

7373
with MetricsHandler.query_time.labels("create").time():
7474
response = sqlite_helpers.insert_url(
@@ -99,7 +99,7 @@ async def get_urls(
9999
sort_by: str = "created_at",
100100
order: str = "DESC",
101101
):
102-
valid_sort_attributes = {"id", "url", "alias", "created_at", "used"}
102+
valid_sort_attributes = {"id", "url", "alias", "created_at", "expires_at", "used"}
103103
if order not in {"DESC", "ASC"}:
104104
raise HTTPException(status_code=400, detail="Invalid order")
105105
if sort_by not in valid_sort_attributes:
@@ -128,6 +128,10 @@ async def get_url(alias: str):
128128
logging.debug(f"/find called with alias: {alias}")
129129
url_output = cache.find(alias) # try to find url in cache
130130
if url_output is not None:
131+
valid = sqlite_helpers.get_url(DATABASE_FILE, alias)
132+
if valid is None:
133+
cache.delete(alias)
134+
raise HTTPException(status_code=HttpResponse.NOT_FOUND.code)
131135
alias_queue.put(alias)
132136
return RedirectResponse(url_output)
133137

0 commit comments

Comments
 (0)