@ -1,3 +1,4 @@
from urllib . parse import unquote
from fastapi import FastAPI , HTTPException
import uvicorn
from typing import List , Tuple
@ -11,9 +12,8 @@ db = Database()
@app.get ( " /artnet/metadata/art " )
async def art ( id : int = None , hash : str = None , tag_id : int = None ) :
hash = unquote ( hash ) if hash is not None else None
print ( f " Received GET on /artnet/metadata/art (id= { id } , hash= { hash } , tag_id= { tag_id } ) " )
#if id is None and hash is None and tag_id is None:
#raise HTTPException(status_code=406, detail="requires id or hash param")
result = None
if id is not None :
@ -38,46 +38,59 @@ async def art(art: ArtnoID, id: int = None):
if art . presences is None : # tried to create art without any presence
raise HTTPException ( status_code = 422 , detail = " No presences were listed " )
try :
for presence in art . presences :
r = db . get_presence ( name = presence . name , domain = presence . domain )
#if db.get_presence(name=presence.name, domain=presence.domain) is None:
# raise HTTPException(status_code=422,
# detail=f"Presence ({presence.name}, {presence.domain}) is unknown!")
#db.create # create presence-art relationship
# TODO validate & create presence-art relationship
new_id = db . create_art_by_model ( art )
new_id = db . create_art_by_model ( art ) . id
return { " id " : new_id }
except ValueError as e :
raise HTTPException ( status_code = 422 , detail = str ( e ) )
else : # update existing art entry
if db . get_art_by_id ( id ) is None :
r eturn HTTPException ( status_code = 404 , detail = " The specified art could not be found! " )
raise HTTPException ( status_code = 404 , detail = " The specified art could not be found! " )
# TODO implement linking to one or multiple presences
# TODO enforcing at least one presence
updated_art = Art ( id = id )
updated_art . hash = art . hash
updated_art . link = art . link
updated_art . title = art . title
updated_art . path = art . path
if art . presences is not None :
updated_art . presences = art . presences
db . update_art_by_model ( updated_art )
return True
@app.delete ( " /artnet/metadata/art " )
async def art ( id : int ) :
print ( f " Received DELETE on /artnet/metadata/art (id= { id } ) " )
async def art ( id : int , presence_name : str = None , presence_domain : str = None ) :
presence_name = unquote ( presence_name ) if presence_name is not None else None
presence_domain = unquote ( presence_domain ) if presence_domain is not None else None
print ( f " Received DELETE on /artnet/metadata/art (id= { id } , presence_name= { presence_name } , "
f " presence_domain= { presence_domain } ) " )
print ( " All art available is " , [ a . id for a in db . get_art_list ( ) ] )
if db . get_art_by_id ( id ) is None :
raise HTTPException ( status_code = 404 , detail = " Art has not been found! " )
db . delete_art_by_id ( id )
if id is not None and presence_name is None and presence_domain is None :
db . delete_art_by_id ( id )
return
elif id is not None and presence_name is not None and presence_domain is not None :
try :
db . delete_art_presences_by_id ( art_id = id , presence_name = presence_name , presence_domain = presence_domain )
except ValueError :
raise HTTPException ( status_code = 404 , detail = " The art-presence relation could not be found! " )
return
raise HTTPException ( status_code = 422 , detail = " Unknown parameter combination! " )
@app.get ( " /artnet/metadata/presence " )
async def presence ( name : str = None , domain : str = None , artist_id : int = None , art_id : int = None ,
art_md5 : str = None ) :
name = unquote ( name ) if name is not None else None
domain = unquote ( domain ) if domain is not None else None
art_md5 = unquote ( art_md5 ) if art_md5 is not None else None
print ( f " Received GET on /artnet/metadata/presence (name= { name } , domain= { domain } , artist_id= { artist_id } "
f " , art_id= { art_id } , art_md5= { art_md5 } ) " )
result = None
@ -127,18 +140,19 @@ async def presence(name: str, domain: str):
@app.get ( " /artnet/metadata/artist " )
async def artist ( id : int = None , topic_id : int = None , description : str = None ) :
print ( f " Received GET on /artnet/metadata/artist (id= { id } , topic_id= { topic_id } , description= { description } ) " )
async def artist ( id : int = None , topic_id : int = None , name : str = None ) :
name = unquote ( name ) if name is not None else None
print ( f " Received GET on /artnet/metadata/artist (id= { id } , topic_id= { topic_id } , name= { name } ) " )
result = None
if id is None and topic_id is None and description is None :
if id is None and topic_id is None and name is None :
result = db . get_artist_list ( )
if id is not None :
result = db . get_artist ( id )
elif topic_id is not None :
result = db . get_topic_artists ( topic_id )
elif description is not None :
result = db . search_artist ( description )
elif name is not None :
result = db . search_artist ( name )
if result is not None :
return result
@ -149,12 +163,12 @@ async def artist(id: int = None, topic_id: int = None, description: str = None):
async def artist ( artist : ArtistNoId , id : int = None ) :
print ( f " Received POST on /artnet/metadata/artist (id= { id } ) body: artist= { artist } " )
if id is None : # create new artist
db_artist = db . create_artist ( name = artist . description , topics = artist . topics )
db_artist = db . create_artist ( name = artist . name , topics = artist . topics )
return db_artist . id
else :
if db . get_artist ( id ) is not None :
db . update_artist ( artist_id = id , name = artist . description , topics = artist . topics )
db . update_artist ( artist_id = id , name = artist . name , topics = artist . topics )
else :
raise HTTPException ( status_code = 404 , detail = " Tried to edit unknown artist. ID was not found! " )
@ -167,9 +181,9 @@ async def artist(id: int):
db . delete_artist ( id )
@app.get ( " /artnet/metadata/topic " )
async def topic ( name : str = None , id : int = None , artist_id : int = None ) :
name = unquote ( name ) if name is not None else None
print ( f " Received GET on /artnet/metadata/topic (name= { name } , id= { id } , artist_id= { artist_id } ) " )
if name is None and id is None and artist_id is None :
raise HTTPException ( status_code = 406 )
@ -191,6 +205,8 @@ async def topic(name: str = None, id: int = None, artist_id: int = None):
@app.get ( " /artnet/metadata/tag " )
async def tag ( id : int = None , name : str = None , fuzzy : bool = False , category : int = None ) :
name = unquote ( name ) if name is not None else None
print ( f " Received GET on /artnet/metadata/tag (name= { name } , id= { id } , name= { category } , fuzzy= { fuzzy } ) " )
if id is None and name is None and category is None :
raise HTTPException ( status_code = 406 )
@ -216,8 +232,11 @@ async def tag(id: int = None, name: str = None, fuzzy: bool = False, category: i
@app.get ( " /artnet/metadata/category " )
async def category ( name : str = None , id : int = None ) :
name = unquote ( name ) if name is not None else None
print ( f " Received GET on /artnet/metadata/category (name= { name } , id= { id } ) " )
if name is None and id is None :
raise HTTPException ( status_code = 406 )
result = db . get_category_list ( )
return result
result = None
if id is not None :