Alter 2 files

Update `db.py`
Update `endpoints.py`
This commit is contained in:
akp 2022-10-29 17:53:04 +01:00
parent bffd014909
commit 6fa3a29264
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
2 changed files with 46 additions and 43 deletions

View file

@ -1,4 +1,48 @@
import sqlite3
from dataclasses import dataclass
from typing import *
import urllib
@dataclass
class Entry:
id: str
name: str
location_lat: float
location_long: float
votes: int
image_url: Optional[str]
def validate(self, only_populated_fields=False) -> Tuple[bool, str]:
if self.name == "" and not only_populated_fields:
return False, "name cannot be empty"
if self.votes < 0:
return False, "votes cannot be negative"
if self.image_url is not None:
try:
urllib.parse.urlparse(self.image_url)
except Exception:
return False, "invalid URL"
return True, ""
def as_dict(self) -> Dict:
res = {}
res["id"] = self.id
res["name"] = self.name
res["location"] = {
"lat": self.location_lat,
"long": self.location_long,
}
res["votes"] = self.votes
if self.image_url is not None:
res["imageURL"] = self.image_url
return res
class DB:
conn: sqlite3.Connection

View file

@ -9,47 +9,6 @@ import db
import paths
@dataclass
class Entry:
id: str
name: str
location_lat: float
location_long: float
votes: int
image_url: Optional[str]
def validate(self, only_populated_fields=False) -> Tuple[bool, str]:
if self.name == "" and not only_populated_fields:
return False, "name cannot be empty"
if self.votes < 0:
return False, "votes cannot be negative"
if self.image_url is not None:
try:
urllib.parse.urlparse(self.image_url)
except Exception:
return False, "invalid URL"
return True, ""
def as_dict(self) -> Dict:
res = {}
res["id"] = self.id
res["name"] = self.name
res["location"] = {
"lat": self.location_lat,
"long": self.location_long,
}
res["votes"] = self.votes
if self.image_url is not None:
res["imageURL"] = self.image_url
return res
class Endpoints:
db: db.DB
@ -64,8 +23,8 @@ class Endpoints:
def list_entries(self):
# TODO: populate from databaase
a = Entry("203fc6a0-9587-41a4-9862-e1b72039b98b", "Birmingham Duck Pond", -1.2345, 33.4567, 0, None)
b = Entry("b140e048-ea2c-4827-b670-ef41ba48c56d", "Northwich Duck Pond", -3.2345, 25.4567, 0, None)
a = db.Entry("203fc6a0-9587-41a4-9862-e1b72039b98b", "Birmingham Duck Pond", -1.2345, 33.4567, 0, None)
b = db.Entry("b140e048-ea2c-4827-b670-ef41ba48c56d", "Northwich Duck Pond", -3.2345, 25.4567, 0, None)
return flask.jsonify([a.as_dict(), b.as_dict()])