Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api.add_variable() add parameter keyword #304

Merged
merged 5 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions metacatalog/api/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
return add_record(session=session, tablename='units', **attrs)


def add_variable(session: 'Session', name: str, symbol: str, column_names: List[str], unit: Union[int, str]) -> models.Variable:
def add_variable(session: 'Session', name: str, symbol: str, column_names: List[str], unit: Union[int, str], keyword: Union[int, str] = None) -> models.Variable:
r"""
Add a new variable to the database.

Expand All @@ -142,6 +142,12 @@
unit : int, str
Either the id or **full** name of the unit to be
linked to this variable.
keyword: int, str
.. versionadded:: 0.8.4
Either the id or **full** path of the keyword to be
linked to this variable.
It is strongly recommended to add a keyword from a controlled thesaurus to a
newly created variable to improve the findability of the variable.

Returns
-------
Expand All @@ -159,9 +165,20 @@
unit = api.find_unit(session=session,name=unit, return_iterator=True).first()
else:
raise AttributeError('unit has to be of type integer or string.')

attrs['unit_id'] = unit.id

# get the keyword
if keyword:
if isinstance(keyword, int):
keyword = api.find_keyword(session=session, id=keyword, return_iterator=True).one()
elif isinstance(keyword, str):
keyword = api.find_keyword(session=session, full_path=keyword, return_iterator=True).first()

Check warning on line 176 in metacatalog/api/add.py

View check run for this annotation

Codecov / codecov/patch

metacatalog/api/add.py#L173-L176

Added lines #L173 - L176 were not covered by tests
else:
raise AttributeError('keyword has to be of type integer or string.')

Check warning on line 178 in metacatalog/api/add.py

View check run for this annotation

Codecov / codecov/patch

metacatalog/api/add.py#L178

Added line #L178 was not covered by tests
AlexDo1 marked this conversation as resolved.
Show resolved Hide resolved

attrs['keyword_id'] = keyword.id

Check warning on line 180 in metacatalog/api/add.py

View check run for this annotation

Codecov / codecov/patch

metacatalog/api/add.py#L180

Added line #L180 was not covered by tests

# add the variable
return add_record(session=session, tablename='variables', **attrs)

Expand Down
8 changes: 7 additions & 1 deletion metacatalog/api/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
def find_keyword(return_iterator: Literal[False] = False) -> List['Keyword']: ...
@overload
def find_keyword(return_iterator: Literal[True] = False) -> 'Query': ...
def find_keyword(session: 'Session', id: Optional[int] = None, uuid: Optional[str] = None, value: Optional[str] = None, thesaurus_name: Optional[str] = None, return_iterator: bool = False) -> List['Keyword'] | 'Query':
def find_keyword(session: 'Session', id: Optional[int] = None, uuid: Optional[str] = None, value: Optional[str] = None, full_path: Optional[str] = None, thesaurus_name: Optional[str] = None, return_iterator: bool = False) -> List['Keyword'] | 'Query':
"""
Return one or many keyword entries from the database on
exact matches.
Expand All @@ -103,6 +103,10 @@
value : str
Value of the requested keyword(s). Multiple record
return is possible.
full_path : str
.. versionadded:: 0.8.4

Full path of the requested keyword.
thesaurus_name : str
.. versionadded:: 0.1.10

Expand Down Expand Up @@ -133,6 +137,8 @@
# add needed filter
if id is not None:
query = query.filter(models.Keyword.id==id)
if full_path is not None:
query = query.filter(models.Keyword.full_path==full_path)

Check warning on line 141 in metacatalog/api/find.py

View check run for this annotation

Codecov / codecov/patch

metacatalog/api/find.py#L140-L141

Added lines #L140 - L141 were not covered by tests
AlexDo1 marked this conversation as resolved.
Show resolved Hide resolved
if value is not None:
query = query.filter(_match(models.Keyword.value, value))
if thesaurus_name is not None:
Expand Down
Loading