diff --git a/geomagio/api/db/metadata_table.py b/geomagio/api/db/metadata_table.py index 9b1571a21c1abf54a7aed5e644e0974d7c001c99..367e59368f83b3c353f72e9d7095e2610a81f7e1 100644 --- a/geomagio/api/db/metadata_table.py +++ b/geomagio/api/db/metadata_table.py @@ -83,8 +83,8 @@ async def create_metadata(meta: Metadata) -> Metadata: query = metadata.insert() values = meta.datetime_dict(exclude={"id"}, exclude_none=True) query = query.values(**values) - metadata.id = await database.execute(query) - return metadata + meta.id = await database.execute(query) + return meta async def delete_metadata(id: int) -> None: @@ -102,21 +102,24 @@ async def get_metadata( category: MetadataCategory = None, starttime: datetime = None, endtime: datetime = None, + created_after: datetime = None, + created_before: datetime = None, data_valid: bool = None, metadata_valid: bool = None, ): query = metadata.select() - if id is not None: + if id: query = query.where(metadata.c.id == id) if category: query = query.where(metadata.c.category == category) - if network or station or channel or location: - query = ( - query.where(metadata.c.network.like(network or "%")) - .where(metadata.c.station.like(station or "%")) - .where(metadata.c.channel.like(channel or "%")) - .where(metadata.c.location.like(location or "%")) - ) + if network: + query = query.where(metadata.c.network == network) + if station: + query = query.where(metadata.c.station == station) + if channel: + query = query.where(metadata.c.channel.like(channel)) + if location: + query = query.where(metadata.c.location.like(location)) if starttime: query = query.where( or_(metadata.c.endtime == None, metadata.c.endtime > starttime) @@ -125,6 +128,10 @@ async def get_metadata( query = query.where( or_(metadata.c.starttime == None, metadata.c.starttime < endtime) ) + if created_after: + query = query.where(metadata.c.created_time > created_after) + if created_before: + query = query.where(metadata.c.created_time < created_before) if data_valid is not None: query = query.where(metadata.c.data_valid == data_valid) if metadata_valid is not None: diff --git a/geomagio/api/secure/MetadataQuery.py b/geomagio/api/secure/MetadataQuery.py index 629ad014907dbf7d35c76acd4781caad98ea184e..1d92c67d86e11deba40b19a6185c69df45a3c389 100644 --- a/geomagio/api/secure/MetadataQuery.py +++ b/geomagio/api/secure/MetadataQuery.py @@ -12,6 +12,8 @@ class MetadataQuery(BaseModel): category: MetadataCategory = None starttime: UTCDateTime = None endtime: UTCDateTime = None + created_after: UTCDateTime = None + created_before: UTCDateTime = None network: str = None station: str = None channel: str = None @@ -21,11 +23,7 @@ class MetadataQuery(BaseModel): def datetime_dict(self, **kwargs): values = self.dict(**kwargs) - for key in ["starttime", "endtime"]: + for key in ["starttime", "endtime", "created_after", "created_before"]: if key in values and values[key] is not None: values[key] = values[key].datetime.replace(tzinfo=timezone.utc) return values - - @validator("starttime") - def set_default_starttime(cls, starttime: UTCDateTime = None) -> UTCDateTime: - return starttime or UTCDateTime() - 30 * 86400 diff --git a/geomagio/api/secure/metadata.py b/geomagio/api/secure/metadata.py index fb0dfc4cfeed3981e07d37f501062975c5055420..7126a1ace52f5f74be13c8d699ad0ef9eb98b118 100644 --- a/geomagio/api/secure/metadata.py +++ b/geomagio/api/secure/metadata.py @@ -36,7 +36,7 @@ async def create_metadata( user: User = Depends(require_user()), ): metadata = await metadata_table.create_metadata(metadata) - return Response(metadata, status_code=201, media_type="application/json") + return Response(metadata.json(), status_code=201, media_type="application/json") @router.delete("/metadata/{id}") @@ -51,6 +51,8 @@ async def get_metadata( category: MetadataCategory = None, starttime: UTCDateTime = None, endtime: UTCDateTime = None, + created_after: UTCDateTime = None, + created_before: UTCDateTime = None, network: str = None, station: str = None, channel: str = None, @@ -62,6 +64,8 @@ async def get_metadata( category=category, starttime=starttime, endtime=endtime, + created_after=created_after, + created_before=created_before, network=network, station=station, channel=channel,