Skip to content

Commit

Permalink
Merge branch 'feature/remaining-icat-endpoints-#145' into feature/isi…
Browse files Browse the repository at this point in the history
…s-specific-endpoints-icat-#146
  • Loading branch information
MRichards99 committed Oct 12, 2020
2 parents a089db4 + 3702123 commit eb2382d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
4 changes: 2 additions & 2 deletions common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from datetime import datetime, timedelta

from icat.entities import getTypeMap
from icat.exception import (
ICATSessionError,
ICATValidationError,
Expand All @@ -13,7 +14,6 @@
from common.exceptions import (
AuthenticationError,
BadRequestError,
FilterError,
MissingRecordError,
PythonICATError,
)
Expand Down Expand Up @@ -320,7 +320,7 @@ def get_count_with_filters(client, table_name, filters):
)

selected_entity_name = get_python_icat_entity_name(client, table_name)
query = icat_query(client, selected_entity_name, aggregate="COUNT")
query = ICATQuery(client, selected_entity_name, aggregate="COUNT")

filter_handler = FilterOrderHandler()
filter_handler.manage_icat_filters(filters, query.query)
Expand Down
42 changes: 40 additions & 2 deletions common/icat/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from icat.query import Query
from icat.exception import ICATValidationError, ICATInternalError

from common.exceptions import PythonICATError
from common.exceptions import PythonICATError, FilterError
from common.date_handler import DateHandler
from common.constants import Constants

log = logging.getLogger()
Expand All @@ -16,6 +17,43 @@ class ICATQuery:
def __init__(
self, client, entity_name, conditions=None, aggregate=None, includes=None
):
"""
Create a Query object within Python ICAT
:param client: ICAT client containing an authenticated user
:type client: :class:`icat.client.Client`
:param entity_name: Name of the entity to get data from
:type entity_name: :class:`suds.sax.text.Text`
:param conditions: Constraints used when an entity is queried
:type conditions: :class:`dict`
:param aggregate: Name of the aggregate function to apply. Operations such as
counting the number of records. See `icat.query.setAggregate` for valid
values.
:type aggregate: :class:`str`
:param includes: List of related entity names to add to the query so related
entities (and their data) can be returned with the query result
:type includes: :class:`str` or iterable of :class:`str`
:return: Query object from Python ICAT
:raises PythonICATError: If a ValueError is raised when creating a Query(), 500
will be returned as a response
"""

try:
log.info("Creating ICATQuery for entity: %s", entity_name)
self.query = Query(
client,
entity_name,
conditions=conditions,
aggregate=aggregate,
includes=includes,
)
except ValueError:
raise PythonICATError(
"An issue has occurred while creating a Python ICAT Query object,"
" suggesting an invalid argument"
)

def execute_query(self, client, return_json_formattable=False):
"""
Execute the ICAT Query object and return in the format specified by the
return_json_formattable flag
Expand Down Expand Up @@ -296,4 +334,4 @@ def flatten_query_included_fields(self, includes):
ICAT query
"""

return [m for n in (field.split(".") for field in includes) for m in n]
return [m for n in (field.split(".") for field in includes) for m in n]

0 comments on commit eb2382d

Please sign in to comment.