@@ -39,68 +39,73 @@ class HTTPError(BaseModel):
3939@router .get (
4040 "/query" ,
4141 tags = ["filing" ],
42- status_code = 201 ,
42+ status_code = 200 ,
4343)
44- async def query_filer (
44+ async def query_filing (
4545 cik : str , access_number : str , background : BackgroundTasks = BackgroundTasks
4646):
4747 filing = database .find_filing (cik , access_number , {"_id" : 1 })
48+
4849 if not filing :
49- try :
50- sec_data = sec_filer_search (cik )
51- except Exception as e :
52- logging .error (e )
53- raise HTTPException (404 , detail = "CIK not found." )
50+
51+ update , last_report = web .check_new (cik )
52+ company = database .find_filer (cik )
53+
54+ if not update :
55+ return update_filing (company , last_report , background )
56+
57+ if not company :
58+ raise HTTPException (404 , detail = "Filer not found." )
59+
60+ filings = database .find_filings (cik , {"access_number" : 1 })
61+ access_numbers = [filing ["access_number" ] for filing in filings ]
62+
63+ if access_number not in access_numbers :
64+ raise HTTPException (404 , detail = "Filing not found. Invalid access number." )
5465
5566 if production_environment :
56- worker .create_filer .delay (cik , sec_data )
67+ worker .repair_filer .delay (cik )
5768 else :
58- background .add_task (filer .create_filer , cik , sec_data )
69+ background .add_task (filer .repair_filer , cik )
5970
60- return {"status" : "Filing creation started." }
61- else :
71+ raise HTTPException (201 , detail = "Filing found but not queried. Repair started." )
6272
63- if filing ["form" ] not in database .holding_forms :
64- raise HTTPException (404 , detail = "Filing type not supported." )
73+ else :
6574
66- return update_filing ({ "cik " : cik })
75+ return { "description " : "Filing already queried." }
6776
6877
6978# Exact functionality as `update_filer`, so changes need to be synced
7079
7180
72- def update_filing (filing , background : BackgroundTasks = BackgroundTasks ):
73- cik = filing ["cik" ]
81+ def update_filing (
82+ company , last_report : str , background : BackgroundTasks = BackgroundTasks
83+ ):
84+ cik = company ["cik" ]
7485 time = datetime .now ().timestamp ()
7586
7687 operation = database .find_log (cik )
7788 if operation is None :
78- raise HTTPException (404 , detail = "Filing log not found." )
89+ raise HTTPException (404 , detail = "Filer log not found." )
7990 elif (
8091 production_environment and operation ["status" ] == 2 or operation ["status" ] == 1
8192 ):
8293 raise HTTPException ( # @IgnoreException
83- 302 , detail = "Filing is partially building."
94+ 302 , detail = "Filer is partially building."
8495 )
8596 elif operation ["status" ] >= 2 :
86- raise HTTPException (409 , detail = "Filing still building." )
87-
88- update , last_report = web .check_new (cik )
89- if not update :
90- raise HTTPException (
91- 200 , detail = "Filing is already up to date."
92- ) # @IgnoreException
97+ raise HTTPException (409 , detail = "Filer still building." )
9398
9499 database .edit_status (cik , 1 )
95100 database .edit_filer ({"cik" : cik }, {"$set" : {"last_report" : last_report }})
96101
97102 stamp = {"name" : company ["name" ], "start" : time }
98103 if production_environment :
99- worker .create_historical .delay (cik , company , stamp )
104+ worker .create_recent .delay (cik , company , stamp )
100105 else :
101- background .add_task (create_historical , cik , company , stamp )
106+ background .add_task (filer . create_recent , cik , company , stamp )
102107
103- return {"description" : "Filer update started." }
108+ return {"description" : "Filing update started." }
104109
105110
106111@cache (24 )
@@ -148,6 +153,9 @@ async def record_filing_csv(cik: str, access_number: str, headers: str = None):
148153 file_name = f"wallstreetlocal-{ cik } -{ access_number } .csv"
149154
150155 filing = database .find_filing (cik , access_number )
156+ if filing is None :
157+ raise HTTPException (404 , detail = "Filing not found." )
158+
151159 stock_dict = filing ["stocks" ]
152160 stock_list = [stock_dict [cusip ] for cusip in stock_dict ]
153161
@@ -157,15 +165,27 @@ async def record_filing_csv(cik: str, access_number: str, headers: str = None):
157165 )
158166
159167
160- @router .get ("/info " , status_code = 200 )
161- async def query_filings (cik : str ):
168+ @router .get ("/filer " , status_code = 200 )
169+ async def filings_info (cik : str ):
162170 pipeline = [
163- {"$match" : {"cik" : cik , "form" : "13F-HR" }},
164- {"$project" : {"stocks" : 0 , "_id" : 0 }},
171+ {"$match" : {"cik" : cik , "form" : { "$in" : database . holding_forms } }},
172+ {"$project" : {"cik" : 0 , " stocks" : 0 , "_id" : 0 }},
165173 ]
166174 cursor = database .search_filings (pipeline )
167175 if not cursor :
168176 raise HTTPException (detail = "Filer not found." , status_code = 404 )
169177 filings = [result for result in cursor ]
170178
171179 return {"filings" : filings }
180+
181+
182+ @router .get ("/info" , status_code = 200 )
183+ async def filing_info (cik : str , access_number : str ):
184+ filing = database .find_filing (cik , access_number , {"_id" : 0 , "cik" : 0 , "stocks" : 0 })
185+ if filing is None :
186+ raise HTTPException (detail = "Filing not found." , status_code = 404 )
187+
188+ status = database .find_log (cik , {"status" : 1 , "_id" : 0 })
189+ filing ["status" ] = status ["status" ]
190+
191+ return {"description" : "Filing found." , "filing" : filing }
0 commit comments