diff --git a/database/database.py b/database/database.py index 8d1eda4..6d79d69 100644 --- a/database/database.py +++ b/database/database.py @@ -12,10 +12,8 @@ SQLALCHEMY_DATABASE_URL = "postgresql://artnet_editor:G606Rm9sFEXe6wfTLxVu@[::1] engine = sqlalchemy.create_engine(SQLALCHEMY_DATABASE_URL, echo=True) SessionLocal = sqlalchemy.orm.sessionmaker(autocommit=False, autoflush=False, bind=engine) - Base = sqlalchemy.orm.declarative_base() - art_to_tag_table = Table('art_to_tag', Base.metadata, Column('art_id', Integer, ForeignKey('art.id')), Column('tag_id', Integer, ForeignKey('tag.tag_id'))) @@ -51,6 +49,7 @@ class DBArt(Base): path = Column(String, nullable=False, unique=True) title = Column(String, nullable=True) link = Column(String, nullable=True) + description = Column(String, nullable=True) tags = relationship("DBTag", secondary=art_to_tag_table, back_populates="art", viewonly=True) collections = relationship("DBCollection", secondary=art_to_art_collection, back_populates="art") @@ -129,7 +128,6 @@ Base.metadata.create_all(bind=engine) class Database: - __db: sqlalchemy.orm.Session = None def __get_db(self) -> sqlalchemy.orm.Session: @@ -144,7 +142,7 @@ class Database: Database.__db.close() Database.__db = None -# Art + # Art def get_art_list(self) -> List[DBArt]: return self.__get_db().query(DBArt).all() # TODO FIX StackOverflow @@ -159,7 +157,7 @@ class Database: if not (isinstance(art.path, str) and len(art.path) > 0): raise ValueError("New Art must contain a path!") - db_art = DBArt(title=art.title, md5_hash=art.hash, path=art.path, link=art.link) + db_art = DBArt(title=art.title, md5_hash=art.hash, path=art.path, link=art.link, description=art.description) if self.get_art_by_hash(art.hash) is not None: raise ValueError("New Art must not contain already known hashes! Is this really new?") @@ -193,6 +191,7 @@ class Database: db_art.link = art.link if art.link is not None else db_art.link db_art.md5_hash = art.hash if art.hash is not None else db_art.md5_hash db_art.path = art.path if art.path is not None else db_art.path + db_art.description = art.description if art.description is not None else db_art.description self.__get_db().commit() @@ -216,7 +215,8 @@ class Database: return result @DeprecationWarning # is this actually needed? superceeded by updating the art.presence field on art - def update_art_presences_by_id(self, art_id: int, presences: List[Presence]): # presences = [("name", "domain"),(...)] + def update_art_presences_by_id(self, art_id: int, + presences: List[Presence]): # presences = [("name", "domain"),(...)] """ Creates an art-presence relation for every presence listed :param art_id: @@ -263,7 +263,7 @@ class Database: return self.__get_db().query(DBPresence).all() # TODO fix StackOverflow def get_presence(self, name: str, domain: str) -> DBPresence: - result = self.__get_db().query(DBPresence)\ + result = self.__get_db().query(DBPresence) \ .filter(func.lower(DBPresence.name) == name.lower() and func.lower(DBPresence.domain) == domain.lower()).first() return result @@ -337,8 +337,8 @@ class Database: self.__get_db().delete(db_artist) self.__get_db().commit() # TODO FIX sqlalchemy.exc.IntegrityError: (psycopg2.errors.ForeignKeyViolation) - # TODO update or delete on table "artist" violates foreign key constraint "presence_artist_id_fkey" on table "presence" - # TODO DETAIL: Key (id)=(83) is still referenced from table "presence". + # TODO update or delete on table "artist" violates foreign key constraint "presence_artist_id_fkey" on table "presence" + # TODO DETAIL: Key (id)=(83) is still referenced from table "presence". # Topic diff --git a/database/models.py b/database/models.py index 536edec..e94b9ad 100644 --- a/database/models.py +++ b/database/models.py @@ -74,6 +74,7 @@ class ArtnoID(BaseModel): path: Optional[str] title: Optional[str] = None link: Optional[str] = None + description: Optional[str] presences: Optional[List[Presence]] tags: Optional[List[Tag]] diff --git a/main.py b/main.py index 3d9b4d3..80d3a5e 100644 --- a/main.py +++ b/main.py @@ -54,6 +54,7 @@ async def art(art: ArtnoID, id: int = None): updated_art.link = art.link updated_art.title = art.title updated_art.path = art.path + updated_art.description = art.description if art.presences is not None: updated_art.presences = art.presences diff --git a/tests/createAPI/create_delete_art.py b/tests/createAPI/create_delete_art.py index 075a96c..da3bb18 100644 --- a/tests/createAPI/create_delete_art.py +++ b/tests/createAPI/create_delete_art.py @@ -9,10 +9,13 @@ from tests.createAPI.create_delete_presence import test_presence_entries, list_p test_art_entries = [ {"hash": "hash1", "path": "artist1/image1", "title": "Image Title 1", "link": "http://localhost/artist1/image1.png", + "description": "description Nr. 1", "presences": [(test_presence_entries[0]["name"], test_presence_entries[0]["domain"])]}, {"hash": "hash2", "path": "artist1/image2", "title": "Image Title 2", "link": "http://localhost/artist1/image2.png", + "description": "description Nr. 2", "presences": [(test_presence_entries[1]["name"], test_presence_entries[1]["domain"])]}, {"hash": "hash3", "path": "artist2/image1", "title": "Image Title 3", "link": "http://localhost/artist2/image3.png", + "description": "description Nr. 3", "presences": [(test_presence_entries[0]["name"], test_presence_entries[0]["domain"]), (test_presence_entries[1]["name"], test_presence_entries[1]["domain"])]}, ] @@ -42,7 +45,7 @@ def create_art_entries(url: str, port: int): for i in range(len(test_art_entries)): r = create_art(url, port, md5_hash=test_art_entries[i]["hash"], path=test_art_entries[i]["path"], title=test_art_entries[i]["title"], link=test_art_entries[i]["link"], - presences=test_art_entries[i]["presences"]) + presences=test_art_entries[i]["presences"], description=test_art_entries[i]["description"]) if r.status_code != 200: print(f"Create Art Entry Test Nr.{i}: failed with {r.status_code} and reason {r.text}") raise Exception("Create Art Entry Test: FAILED") @@ -57,7 +60,8 @@ def update_art_entries(url: str, port: int): r = update_art(url, port, md5_hash=test_art_entries[i]["hash"]+"moreHash", path=test_art_entries[i]["path"]+"evenBetterPath", title=test_art_entries[i]["title"]+" updated", - presences=test_art_entries[i]["presences"], art_id=test_art_entries[i]["ID"]) + presences=test_art_entries[i]["presences"], art_id=test_art_entries[i]["ID"], + description=test_art_entries[i]["description"]+" evenBetterDescription") if r.status_code != 200: print(f"Update Art Entry Test Nr.{i}: failed with {r.status_code} and reason {r.text}") @@ -65,10 +69,13 @@ def update_art_entries(url: str, port: int): for i in range(len(test_art_entries)): r = requests.get(f"http://{url}:{port}/artnet/metadata/art?id={urllib.parse.quote(str(test_art_entries[i]['ID']))}") - if json.loads(r.text)["title"].split(" ")[-1] != "updated": + response = json.loads(r.text) + if response["title"].split(" ")[-1] != "updated": raise Exception("Update Art Entry Test: Failed (unexpected or no updated title)") - if json.loads(r.text)["link"] != test_art_entries[i]["link"]: + if response["link"] != test_art_entries[i]["link"]: raise Exception("Update Art Entry Test: Failed (unexpected link)") + if response["description"].split(" ")[-1] != "evenBetterDescription": + raise Exception("Update Art Entry Test: Failed (unexpected description)") print("Updated art entries: SUCCESS") @@ -88,16 +95,18 @@ def delete_art_entries(url: str, port: int): print("Deleted art entries: SUCCESS") -def create_art(url: str, port: int, md5_hash: str, path: str, title: str, link: str, presences: List[Tuple[str, str]]): +def create_art(url: str, port: int, md5_hash: str, path: str, title: str, link: str, description: str, + presences: List[Tuple[str, str]]): r = requests.post(f"http://{url}:{port}/artnet/metadata/art", json={"hash": md5_hash, "path": path, "title": title, "link": link, - "presences": [{"name": presence[0], "domain": presence[1]} for presence in presences]}) + "presences": [{"name": presence[0], "domain": presence[1]} for presence in presences], + "description": description}) return r def update_art(url: str, port: int, art_id: int, md5_hash: str = None, path: str = None, title: str = None, - link: str = None, presences: List[Tuple[str, str]] = None): + link: str = None, description: str = None, presences: List[Tuple[str, str]] = None): body = {} if md5_hash is not None: body["hash"] = md5_hash @@ -109,6 +118,8 @@ def update_art(url: str, port: int, art_id: int, md5_hash: str = None, path: str body["link"] = link if presences is not None: body["presences"] = [{"name": presence[0], "domain": presence[1]} for presence in presences] + if description is not None: + body["description"] = description r = requests.post(f"http://{url}:{port}/artnet/metadata/art?id={urllib.parse.quote(str(art_id))}", json=body) @@ -138,7 +149,7 @@ def run_art_test(url: str, port: int): print() print("----------------") - print(f"Starting presence test with " + print(f"Starting art test with " f"({len(list_artists(url, port))}) artists, ({len(list_presences(url, port))}) presences, " f"({len(list_art(url, port))}) art!") print("Setting up artist and presences for the art test ...")