Skip to content

Commit

Permalink
Merge pull request #141 from SevanSSP/feature/contains_filter
Browse files Browse the repository at this point in the history
Feature/contains filter
  • Loading branch information
Per Erlend Voie authored Sep 28, 2021
2 parents 78896eb + 5c37e15 commit 9b5badc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This example will filter by campaign name and description and sort ascending by
The filter object enables use of Pythonic syntax


```python hl_lines="8-10"
```python hl_lines="7-10"
--8<--- "filter_and_sort.py"
```

Expand Down
25 changes: 14 additions & 11 deletions modeltestSDK/query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""
Classes and functions enabling advanced API queries
"""
try:
from typing_extensions import Literal # Python 3.7
except ImportError:
from typing import Literal # Python 3.8/3.9
query_extension_types = Literal["sort", "filter"]


class FilterAttribute:
Expand All @@ -22,6 +27,9 @@ def __gt__(self, other):
def __ge__(self, other):
return dict(name=self.name, op='gte', val=other)

def contains(self, item):
return dict(name=self.name, op='co', val=item)


class SortAttribute:
def __init__(self, name):
Expand All @@ -44,23 +52,23 @@ def desc(self):
return self.descending


def class_factory(name: str, method_spec: str, attribute_list: list):
def class_factory(name: str, method_spec: query_extension_types, attribute_list: list):
def init():
pass

attr_dict = {'__init__': init}
if method_spec == 'sort' or method_spec == 'both':
if method_spec == 'sort':
for attr in attribute_list:
attr_dict[attr] = SortAttribute(attr)
if method_spec == 'filter' or method_spec == 'both':
if method_spec == 'filter':
for attr in attribute_list:
attr_dict[attr] = FilterAttribute(attr)

return type(name, (object,), attr_dict)


class Query:
def __init__(self, method_spec: str = 'both'):
def __init__(self, method_spec: query_extension_types):
self.campaign = class_factory(name='Campaign', method_spec=method_spec,
attribute_list=['name',
'description',
Expand Down Expand Up @@ -154,7 +162,7 @@ def __init__(self, method_spec: str = 'both'):
'description',
'characteristic_length',
'campaign_id',
' draft',
'draft',
'read_only',
'id'])

Expand Down Expand Up @@ -183,11 +191,6 @@ def create_query_parameters(filter_expressions: list, sorting_expressions: list)
query_filter['val'] = str(query_filter['val'])
filter_s = filter_s + query_filter['name'] + '[' + query_filter['op'] + ']' + '=' + query_filter['val']

if sorting_expressions == [] or filter_expressions == []:
sort_filter = ''
else:
sort_filter = ''

sort_str = ''
for sort_parameter in sorting_expressions:
if not sort_str == '':
Expand All @@ -196,7 +199,7 @@ def create_query_parameters(filter_expressions: list, sorting_expressions: list)

parameters = dict()
if filter_s != "":
parameters["filter_by"] = filter_s + sort_filter
parameters["filter_by"] = filter_s

if sort_str != "":
parameters["sort_by"] = sort_str
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "modeltestSDK"
version = "2.1.0"
version = "2.2.0"
description = "SDK for use with modeltest API"
authors = ["Jørgen Engelsen <jen@sevanssp.com>","Per Voie <pev@sevanssp.com>", "Einar Glomnes <ebg@sevanssp.com>"]
license = "MIT"
Expand Down

0 comments on commit 9b5badc

Please sign in to comment.