Skip to content
Snippets Groups Projects
Verified Commit a6a320f5 authored by Robert Metcalf's avatar Robert Metcalf
Browse files

add initial database with workspace support

parent f12e3550
No related branches found
No related tags found
1 merge request!4Add Flask server, templates and initial database support
__pycache__/ __pycache__/
.vscode/ .vscode/
database.db
import os
import sqlite3
from typing import List
DATABASE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "database.db")
class Connection:
def __init__(self):
self.connection = sqlite3.connect(DATABASE)
self.cursor = self.connection.cursor()
self.do_rollback = False
def commit(self):
self.connection.commit()
self.do_rollback = False
def execute(self, sql: str, tuple = ()):
self.do_rollback = True
self.cursor.execute(sql, tuple)
def fetch_all(self):
return self.cursor.fetchall()
def close(self):
if self.do_rollback:
self.connection.rollback()
self.connection.close()
def __enter__(self):
return self
def __exit__(self, *args):
self.close()
with Connection() as conn:
conn.execute('''CREATE TABLE IF NOT EXISTS Workspaces (
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
name text NOT NULL,
address text NOT NULL,
main_photo text NOT NULL,
description text NOT NULL,
website text NOT NULL,
email text NOT NULL,
phone_number text NOT NULL,
opening_hours text,
checkin_instructions text)''')
conn.execute('''CREATE TABLE IF NOT EXISTS AdditionalPhotos (
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
url text NOT NULL,
workspace_id integer NOT NULL,
FOREIGN KEY (workspace_id) REFERENCES Workspaces(id))''')
conn.commit()
class Workspace:
def __init__(self, name: str,
address: str, main_photo: str, additional_photos: List[str], description: str,
website: str, email: str, phone_number: str, opening_hours: str, checkin_instructions: str):
self.id = None
self.name = name
self.address = address
self.main_photo = main_photo
self.additional_photos = additional_photos
self.description = description
self.website = website
self.email = email
self.phone_number = phone_number
self.opening_hours = opening_hours
self.checkin_instructions = checkin_instructions
def from_query(conn, tuple):
(id, name, address, main_photo, description, website, email, phone_number, opening_hours, checkin_instructions) = tuple
additional_photos = []
conn.execute("SELECT url FROM AdditionalPhotos WHERE workspace_id = ?", ("1"))
for x in conn.cursor.fetchall():
additional_photos.append(x[0])
workspace = Workspace(name, address, main_photo, additional_photos, description, website, email, phone_number, opening_hours, checkin_instructions)
workspace.id = id
return workspace
def add_workspace(workspace: Workspace):
with Connection() as conn:
conn.execute(
"INSERT INTO Workspaces (name, address, main_photo, description, website, email, phone_number, opening_hours, checkin_instructions) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
(workspace.name, workspace.address, workspace.main_photo, workspace.description, workspace.website, workspace.email, workspace.phone_number, workspace.opening_hours, workspace.checkin_instructions)
)
id = conn.cursor.lastrowid
for url in workspace.additional_photos:
conn.execute("INSERT INTO AdditionalPhotos (url, workspace_id) VALUES (?, ?)", (url, id))
conn.commit()
return id
def get_workspaces():
with Connection() as conn:
conn.execute("SELECT * FROM Workspaces")
return [Workspace.from_query(conn, x) for x in conn.cursor.fetchall()]
print(get_workspaces())
\ No newline at end of file
import database
print(database.add_workspace(database.Workspace(
"CodeBase",
"CodeBase Edinburgh, 37a Castle Terrace, Edinburgh, EH1 2EL",
"https://images.squarespace-cdn.com/content/v1/55439320e4b0f92b5d6c4c8b/1505921023376-PAHUDHVOOKIYF4XQPHOO/5951229048_3e3d50fcb1_o.jpg?format=750w",
["https://images.squarespace-cdn.com/content/v1/55439320e4b0f92b5d6c4c8b/1636387943314-W9JWMS6ZX4DEZSPUV7T0/Copy+of+Unfiltered_square+%281%29.png?format=500w"],
"We've been exploring the world of startups...",
"https://www.thisiscodebase.com",
"hannah@thisiscodebase.com",
"+44 131 560 2003",
"Monday - Friday, 9am - 5pm",
"Tramsched members should contact Hannah using the email address listed."
)))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment