75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
from datetime import datetime
|
|
import typing
|
|
from uuid import UUID, uuid4
|
|
|
|
from database import Database
|
|
|
|
class PetPicture:
|
|
uuid: str
|
|
title: str
|
|
contributor: str
|
|
created_at: int
|
|
posted: bool
|
|
filepath: str
|
|
description: str
|
|
|
|
def __init__(self, title, contributor, created_at, filepath, description, uuid=None, posted=False):
|
|
self.uuid = str(uuid4()) if uuid is None else uuid
|
|
self.title = title
|
|
self.contributor = contributor
|
|
self.created_at = created_at
|
|
self.posted = posted
|
|
self.filepath = filepath
|
|
self.description = description
|
|
|
|
def get_json(self):
|
|
return {
|
|
"uuid": self.uuid,
|
|
"title": self.title,
|
|
"contributor": self.contributor,
|
|
"description": self.description,
|
|
"created_at": self.created_at,
|
|
"posted": self.posted,
|
|
"filepath": self.filepath,
|
|
}
|
|
|
|
def upsert(self):
|
|
conn = Database.get_connection()
|
|
c = conn.cursor()
|
|
save_string = """INSERT INTO PetPictures (uuid, title, contributor, description, created_at, posted, filepath)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT (uuid) DO UPDATE SET title = excluded.title, contributor = excluded.contributor, created_at = excluded.created_at, posted = excluded.posted, filepath = excluded.filepath, description = excluded.description"""
|
|
c.execute(save_string, (self.uuid, self.title, self.contributor, self.description, self.created_at, self.posted, self.filepath,))
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
@staticmethod
|
|
def from_row(db_row):
|
|
return PetPicture(uuid=db_row[0], title=db_row[1], contributor=db_row[2], description=db_row[3], created_at=db_row[4], filepath=db_row[6], posted=db_row[5])
|
|
|
|
@staticmethod
|
|
def get(uuid: str):
|
|
conn = Database.get_connection()
|
|
c = conn.cursor()
|
|
FETCH_QUERY = "SELECT * FROM PetPictures WHERE UUID=?"
|
|
c.execute(FETCH_QUERY, (uuid,))
|
|
row = c.fetchone()
|
|
return PetPicture.from_row(row)
|
|
|
|
@staticmethod
|
|
def get_all():
|
|
conn = Database.get_connection()
|
|
c = conn.cursor()
|
|
FETCH_QUERY = "SELECT * FROM PetPictures ORDER BY created_at DESC"
|
|
c.execute(FETCH_QUERY)
|
|
rows = c.fetchall()
|
|
conn.close()
|
|
return [
|
|
PetPicture.from_row(row) for row in rows
|
|
]
|
|
|
|
|
|
|
|
|
|
|