Skip to content

Commit

Permalink
use sqlite3 for storing bookmarks and searches
Browse files Browse the repository at this point in the history
  • Loading branch information
diversen7 committed Oct 3, 2024
1 parent cb68ee2 commit dc8e87d
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 17 deletions.
65 changes: 65 additions & 0 deletions bin/create_db_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
stadsarkiv-client exec -c example-config-aarhus -s bin/create_db_tables.py
"""

from stadsarkiv_client.core.dynamic_settings import init_settings
import sqlite3
import os
from stadsarkiv_client.core.logging import get_log

init_settings()

log = get_log()

DATABASE_URL = str(os.getenv("DATABASE_URL"))
conn = sqlite3.connect(DATABASE_URL)
cursor = conn.cursor()

sql_statements = []

create_booksmarks_query = """
CREATE TABLE IF NOT EXISTS bookmarks (
id INTEGER PRIMARY KEY,
bookmark VARCHAR(128),
user_id VARCHAR(128),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
"""

create_booksmarks_index_query = """
-- Generate index on user_id
CREATE INDEX idx_bookmarks_user_id ON bookmarks (user_id);
"""

create_searches_query = """
CREATE TABLE IF NOT EXISTS searches (
id INTEGER PRIMARY KEY,
search VARCHAR(1024),
title VARCHAR(256),
user_id VARCHAR(128),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
"""

create_searches_index_query = """
-- Generate index on user_id
CREATE INDEX idx_searches_user_id ON searches (user_id);
"""

sql_statements.append(create_booksmarks_query)
sql_statements.append(create_booksmarks_index_query)
sql_statements.append(create_searches_query)
sql_statements.append(create_searches_index_query)


# Create the table
def create_tables():
for sql in sql_statements:
cursor.execute(sql)
conn.commit()
log.debug("SQL executed")


create_tables()

conn.close()
52 changes: 35 additions & 17 deletions stadsarkiv_client/endpoints/endpoints_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,51 @@
"""

from starlette.requests import Request
from starlette.responses import JSONResponse, RedirectResponse
from starlette.responses import JSONResponse
from stadsarkiv_client.core.logging import get_log
from stadsarkiv_client.core.context import get_context
from stadsarkiv_client.core.templates import templates
import random
import sqlite3
import os
import typing


DATABASE_URL = str(os.getenv("DATABASE_URL"))


log = get_log()


async def get_db_connection() -> sqlite3.Connection:
connection = sqlite3.connect(DATABASE_URL)
connection.row_factory = sqlite3.Row
connection.execute("PRAGMA journal_mode=WAL;")
return connection


async def test_get(request: Request):
# https://placehold.co/600x400
# generate a context containing 10 images with random sizes.
# But min width is 200px and max width is 360px
# The height maybe between 50px and 600px

# random_images is a list of strings
random_images = []
for _ in range(20):
width = random.randint(200, 360)
height = random.randint(50, 600)
random_images.append(f"https://placehold.co/{width}x{height}")

context_values = {"title": "Base test page", "random_images": random_images}
context = await get_context(request, context_values=context_values)

return templates.TemplateResponse(request, "testing/test.html", context)
await insert_bookmark("Test note", "UUID-1234")
return JSONResponse({"message": "Test note inserted"})


async def insert_bookmark(bookmark, user_id) -> typing.Any:

connection = await get_db_connection()
cursor = connection.cursor()

try:

values = {"user_id": user_id, "bookmark": "bookmark"}
query = "INSERT INTO bookmarks (user_id, bookmark) VALUES (:user_id, :bookmark)"
cursor.execute(query, values)
connection.commit()

except sqlite3.Error as e:
log.error(f"Failed to insert note: {e}")
connection.rollback()
finally:
cursor.close()


async def test_page(request: Request):
Expand Down

0 comments on commit dc8e87d

Please sign in to comment.