Skip to content

Commit

Permalink
[#38] making SearchQuery model fields optional
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdash committed Oct 16, 2023
1 parent c4c22db commit 486dba1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
20 changes: 14 additions & 6 deletions api/adapters/hydroshare.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from typing import List, Optional, Union
from pydantic import BaseModel, EmailStr, HttpUrl

from api.adapters.base import AbstractRepositoryMetadataAdapter, AbstractRepositoryRequestHandler
from api.adapters.base import (
AbstractRepositoryMetadataAdapter,
AbstractRepositoryRequestHandler,
)
from api.adapters.utils import RepositoryType, register_adapter
from api.exceptions import RepositoryException
from api.models import schema
Expand Down Expand Up @@ -93,7 +96,9 @@ def to_dataset_spatial_coverage(self):
place.name = self.name

place.geo = schema.GeoShape.model_construct()
place.geo.box = f"{self.northlimit} {self.eastlimit} {self.southlimit} {self.westlimit}"
place.geo.box = (
f"{self.northlimit} {self.eastlimit} {self.southlimit} {self.westlimit}"
)
return place


Expand Down Expand Up @@ -143,7 +148,7 @@ def to_dataset_part_relation(self, relation_type: str):
else:
return relation

description, url = self.value.rsplit(',', 1)
description, url = self.value.rsplit(",", 1)
relation.description = description.strip()
relation.url = url.strip()
relation.name = self.value
Expand All @@ -162,15 +167,16 @@ def to_dataset_license(self):


class _HydroshareRequestHandler(AbstractRepositoryRequestHandler):

def get_metadata(self, record_id: str):
hs_meta_url = self.settings.hydroshare_meta_read_url % record_id
hs_file_url = self.settings.hydroshare_file_read_url % record_id

def make_request(url, file_list=False) -> Union[dict, List[dict]]:
response = requests.get(url)
if response.status_code != 200:
raise RepositoryException(status_code=response.status_code, detail=response.text)
raise RepositoryException(
status_code=response.status_code, detail=response.text
)
if not file_list:
return response.json()

Expand All @@ -180,7 +186,9 @@ def make_request(url, file_list=False) -> Union[dict, List[dict]]:
while response.json()["next"]:
response = requests.get(response.json()["next"])
if response.status_code != 200:
raise RepositoryException(status_code=response.status_code, detail=response.text)
raise RepositoryException(
status_code=response.status_code, detail=response.text
)
content_files.extend(response.json()["results"])
return content_files

Expand Down
39 changes: 20 additions & 19 deletions api/routes/discovery.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
from datetime import datetime
from typing import Optional

from fastapi import APIRouter, Request, Depends
from pydantic import BaseModel, field_validator, model_validator, FieldValidationInfo
from pydantic import BaseModel, field_validator, model_validator, ValidationInfo

router = APIRouter()


class SearchQuery(BaseModel):
term: str = None
sortBy: str = None
term: Optional[str] = None
sortBy: Optional[str] = None
reverseSort: bool = True
contentType: str = None
providerName: str = None
creatorName: str = None
dataCoverageStart: int = None
dataCoverageEnd: int = None
publishedStart: int = None
publishedEnd: int = None
hasPartName: str = None
isPartOfName: str = None
associatedMediaName: str = None
fundingGrantName: str = None
fundingFunderName: str = None
creativeWorkStatus: str = None
contentType: Optional[str] = None
providerName: Optional[str] = None
creatorName: Optional[str] = None
dataCoverageStart: Optional[int] = None
dataCoverageEnd: Optional[int] = None
publishedStart: Optional[int] = None
publishedEnd: Optional[int] = None
hasPartName: Optional[str] = None
isPartOfName: Optional[str] = None
associatedMediaName: Optional[str] = None
fundingGrantName: Optional[str] = None
fundingFunderName: Optional[str] = None
creativeWorkStatus: Optional[str] = None
pageNumber: int = 1
pageSize: int = 30

@field_validator('*')
def empty_str_to_none(cls, v, info: FieldValidationInfo):
def empty_str_to_none(cls, v, info: ValidationInfo):
if info.field_name == 'term' and v:
return v.strip()

Expand All @@ -36,7 +37,7 @@ def empty_str_to_none(cls, v, info: FieldValidationInfo):
return v

@field_validator('dataCoverageStart', 'dataCoverageEnd', 'publishedStart', 'publishedEnd')
def validate_year(cls, v, info: FieldValidationInfo):
def validate_year(cls, v, info: ValidationInfo):
if v is None:
return v
try:
Expand All @@ -54,7 +55,7 @@ def validate_date_range(self):
raise ValueError('publishedEnd must be greater or equal to publishedStart')

@field_validator('pageNumber', 'pageSize')
def validate_page(cls, v, info: FieldValidationInfo):
def validate_page(cls, v, info: ValidationInfo):
if v <= 0:
raise ValueError(f'{info.field_name} must be greater than 0')
return v
Expand Down

0 comments on commit 486dba1

Please sign in to comment.