11import fastapi
22import db_models
33import api_models
4- import uuid
54import typing
65import base64
76import pymongo
7+ from bson import ObjectId
88import pydantic
99
1010
@@ -97,15 +97,14 @@ def route_submit(params: RunSubmitSimpleParams, db: pymongo.database.Database =
9797 fields of request body; `id` will be real id of this run.
9898 """
9999
100- run_uuid = uuid .uuid4 ()
101- user_id = uuid .UUID ('12345678123456781234567812345678' )
102- doc_main = db_models .RunMainProj (id = run_uuid , toolchain_name = params .toolchain ,
100+ user_id = ObjectId ('507f1f77bcf86cd799439011' )
101+ doc_main = db_models .RunMainProj (toolchain_name = params .toolchain ,
103102 problem_name = params .problem , user_id = user_id , contest_name = params .contest , phase = str (db_models .RunPhase .QUEUED ))
104103 doc_source = db_models .RunSourceProj (
105104 source = base64 .b64decode (params .code ))
106105 doc = {** dict (doc_main ), ** dict (doc_source )}
107- db .runs .insert_one (doc )
108- return api_models .Run (id = run_uuid , toolchain_name = params .toolchain , problem_name = params .problem , user_id = user_id , contest_name = params .contest )
106+ result = db .runs .insert_one (doc )
107+ return api_models .Run (id = str ( result . inserted_id ) , toolchain_name = params .toolchain , problem_name = params .problem , user_id = str ( user_id ) , contest_name = params .contest )
109108
110109 @app .get ('/runs' , response_model = typing .List [api_models .Run ],
111110 operation_id = 'listRuns' )
@@ -122,13 +121,12 @@ def route_list_runs(db: pymongo.database.Database = fastapi.Depends(db_connect))
122121 return runs
123122
124123 @app .get ('/runs/{run_id}' , response_model = api_models .Run , operation_id = 'getRun' )
125- def route_get_run (run_id : uuid . UUID , db : pymongo .database .Database = fastapi .Depends (db_connect )):
124+ def route_get_run (run_id : str , db : pymongo .database .Database = fastapi .Depends (db_connect )):
126125 """
127126 Loads run by id
128127 """
129-
130128 run = db .runs .find_one (projection = db_models .RunMainProj .FIELDS , filter = {
131- 'id ' : run_id
129+ '_id ' : ObjectId ( run_id )
132130 })
133131 if run is None :
134132 raise fastapi .HTTPException (404 , detail = 'RunNotFound' )
@@ -139,13 +137,13 @@ def route_get_run(run_id: uuid.UUID, db: pymongo.database.Database = fastapi.Dep
139137 'description' : "Run source is not available"
140138 }
141139 })
142- def route_get_run_source (run_id : uuid . UUID , db : pymongo .database .Database = fastapi .Depends (db_connect )):
140+ def route_get_run_source (run_id : str , db : pymongo .database .Database = fastapi .Depends (db_connect )):
143141 """
144142 Returns run source as base64-encoded JSON string
145143 """
146144
147145 doc = db .runs .find_one (projection = ['source' ], filter = {
148- 'id ' : run_id
146+ '_id ' : ObjectId ( run_id )
149147 })
150148 if doc is None :
151149 raise fastapi .HTTPException (404 , detail = 'RunNotFound' )
@@ -154,7 +152,7 @@ def route_get_run_source(run_id: uuid.UUID, db: pymongo.database.Database = fast
154152 return base64 .b64encode (doc ['source' ])
155153
156154 @app .patch ('/runs/{run_id}' , response_model = api_models .Run , operation_id = 'patchRun' )
157- def route_run_patch (run_id : uuid . UUID , patch : RunPatch , db : pymongo .database .Database = fastapi .Depends (db_connect )):
155+ def route_run_patch (run_id : str , patch : RunPatch , db : pymongo .database .Database = fastapi .Depends (db_connect )):
158156 """
159157 Modifies existing run
160158
@@ -177,7 +175,7 @@ def route_run_patch(run_id: uuid.UUID, patch: RunPatch, db: pymongo.database.Dat
177175 "RunPatch.status[*] must have length exactly 2" )
178176 p ['$set' ][f"status.{ status_to_add [0 ]} " ] = status_to_add [1 ]
179177 updated_run = db .runs .find_one_and_update (
180- {'id ' : run_id }, p , projection = db_models .RunMainProj .FIELDS , return_document = pymongo .ReturnDocument .AFTER )
178+ {'_id ' : ObjectId ( run_id ) }, p , projection = db_models .RunMainProj .FIELDS , return_document = pymongo .ReturnDocument .AFTER )
181179 if updated_run is None :
182180 raise fastapi .HTTPException (404 , 'RunNotFound' )
183181 return updated_run
0 commit comments