diff options
author | Daniel Schadt <kingdread@gmx.de> | 2023-03-25 15:42:11 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2023-03-31 19:38:40 +0200 |
commit | c4708163ac9e8157f9c875fb201cfd05f6419c70 (patch) | |
tree | 4ca3e15e18038806867f5a11dedbe7599f38add0 | |
parent | 233c688fed5abf3db150052801db7d6732daa44b (diff) | |
download | fietsboek-c4708163ac9e8157f9c875fb201cfd05f6419c70.tar.gz fietsboek-c4708163ac9e8157f9c875fb201cfd05f6419c70.tar.bz2 fietsboek-c4708163ac9e8157f9c875fb201cfd05f6419c70.zip |
stop sqlite3 DB creation if it doesnt exist
This would otherwise happen if e.g. the user has the page open, the
SQLite file is deleted, the user then activates the overlay layer, and
the sqlite3.connect() creates the database.
-rw-r--r-- | fietsboek/views/profile.py | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/fietsboek/views/profile.py b/fietsboek/views/profile.py index a08fad7..9f68d2f 100644 --- a/fietsboek/views/profile.py +++ b/fietsboek/views/profile.py @@ -1,6 +1,7 @@ """Endpoints for the user profile pages.""" import datetime import sqlite3 +import urllib.parse from dataclasses import dataclass from typing import Optional @@ -192,7 +193,16 @@ def user_tile(request: Request) -> Response: if path is None: return HTTPNotFound() - connection = sqlite3.connect(path) + # See + # https://docs.python.org/3/library/sqlite3.html#how-to-work-with-sqlite-uris + # https://stackoverflow.com/questions/10205744/opening-sqlite3-database-from-python-in-read-only-mode + # https://stackoverflow.com/questions/17170202/dont-want-to-create-a-new-database-if-it-doesnt-already-exists + sqlite_uri = urllib.parse.urlunparse(("file", "", str(path), "", "mode=ro", "")) + try: + connection = sqlite3.connect(sqlite_uri, uri=True) + except sqlite3.OperationalError: + return HTTPNotFound() + cursor = connection.cursor() result = cursor.execute( "SELECT data FROM tiles WHERE zoom = ? AND x = ? AND y = ?;", |