api/tracking: add POST route
This commit is contained in:
parent
d3850cce2e
commit
0a60389152
3 changed files with 20 additions and 10 deletions
|
|
@ -3,7 +3,7 @@
|
|||
"""basic (generic) routes and views for tracks (records)"""
|
||||
|
||||
from datetime import datetime
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Body, Depends
|
||||
from fastapi.responses import HTMLResponse
|
||||
from pydantic import BaseModel, Field
|
||||
import transaction
|
||||
|
|
@ -14,11 +14,6 @@ from scopes.storage import tracking
|
|||
from scopes.storage.common import Storage
|
||||
|
||||
class Track(BaseModel):
|
||||
|
||||
@classmethod
|
||||
def create(cls, obj: Any) -> Self:
|
||||
return cls(**obj.asDict())
|
||||
|
||||
uid: str = None
|
||||
head: dict[str, str]
|
||||
data: dict[str, Any]
|
||||
|
|
@ -34,7 +29,15 @@ type Storage = Annotated[Storage, Depends(deps.storage)]
|
|||
async def get_tracks(storage: Storage):
|
||||
tracks = storage.create(tracking.Container)
|
||||
result = [tr.asDict() for tr in tracks.query()]
|
||||
#result = [Track.create(tr) for tr in tracks.query()]
|
||||
transaction.commit()
|
||||
return result
|
||||
|
||||
@router.post('/tracks', response_model=Track)
|
||||
async def post_track(track: Annotated[Track, Body()], storage: Storage):
|
||||
tracks = storage.create(tracking.Container)
|
||||
hd = track.head
|
||||
tr = tracking.Track(hd['taskId'], hd['userName'], data=track.data)
|
||||
tracks.save(tr)
|
||||
transaction.commit()
|
||||
return tr.asDict()
|
||||
|
||||
|
|
|
|||
|
|
@ -122,9 +122,10 @@ class Container:
|
|||
def insert(self, track, withTrackId=False):
|
||||
t = self.table
|
||||
values = self.setupValues(track, withTrackId)
|
||||
stmt = t.insert().values(**values).returning(t.c.trackid)
|
||||
trackId = self.session.execute(stmt).first()[0]
|
||||
stmt = t.insert().values(**values).returning(t.c.trackid, t.c.timestamp)
|
||||
trackId, ts = self.session.execute(stmt).first()
|
||||
track.trackId = trackId
|
||||
track.timeStamp = ts
|
||||
self.storage.mark_changed()
|
||||
return trackId
|
||||
|
||||
|
|
|
|||
|
|
@ -52,5 +52,11 @@ def test_tracking(storage, caplog):
|
|||
tr['timeStamp'] = '...'
|
||||
assert tr == {'uid': 'rec-1', 'head': {'taskId': 't01', 'userName': 'john'},
|
||||
'data': {'activity': 'testing'}, 'timeStamp': '...'}
|
||||
resp = clt.post('/tracks', json={'head': {'taskId': 't02', 'userName': 'jim'},
|
||||
'data': {'activity': 'implement post track'}})
|
||||
assert resp.status_code == 200
|
||||
tr02 = resp.json()
|
||||
assert tr02['uid'] == 'rec-2'
|
||||
#print('\n*** Logs: ', caplog.text)
|
||||
#print('*** tr02', tr02)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue