-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from rbw0/branch0.3.1
Branch0.3.1
- Loading branch information
Showing
6 changed files
with
86 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
``pysnow.Query`` --- Creates a new Query object | ||
=================================================== | ||
``pysnow.QueryBuilder`` --- Creates a new QueryBuilder object | ||
============================================================= | ||
|
||
.. automodule:: pysnow | ||
.. autoclass:: Query | ||
.. autoclass:: QueryBuilder | ||
:members: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
import inspect | ||
|
||
__author__ = "Robert Wikman <[email protected]>" | ||
__version__ = "0.3.0" | ||
__version__ = "0.3.1" | ||
|
||
|
||
class UnexpectedResponse(Exception): | ||
|
@@ -70,24 +70,24 @@ class QueryMultipleExpressions(Exception): | |
pass | ||
|
||
|
||
class Query(object): | ||
class QueryBuilder(object): | ||
def __init__(self): | ||
"""The Query builder""" | ||
"""Query builder - used for building complex queries""" | ||
self._query = [] | ||
self.current_field = None | ||
self.c_oper = None | ||
self.l_oper = None | ||
|
||
def AND(self): | ||
"""AND operator for use between expressions""" | ||
"""Operator for use between expressions""" | ||
return self._add_logical_operator('^') | ||
|
||
def OR(self): | ||
"""OR operator for use between expressions""" | ||
"""Operator for use between expressions""" | ||
return self._add_logical_operator('^OR') | ||
|
||
def NQ(self): | ||
"""NQ operator for use between expressions""" | ||
"""Operator for use between expressions""" | ||
return self._add_logical_operator('^NQ') | ||
|
||
def field(self, field): | ||
|
@@ -129,11 +129,21 @@ def not_equals(self, value): | |
|
||
def greater_than(self, value): | ||
"""Query records with the given field greater than the value specified""" | ||
return self._add_condition('>', value, types=[int]) | ||
if hasattr(value, 'strftime'): | ||
value = value.strftime('%Y-%m-%d %H:%M:%S') | ||
elif isinstance(value, str): | ||
raise QueryTypeError('Expected value of type `int` or instance of `datetime`, not %s' % type(value)) | ||
|
||
return self._add_condition('>', value, types=[int, str]) | ||
|
||
def less_than(self, value): | ||
"""Query records with the given field less than the value specified""" | ||
return self._add_condition('<', value, types=[int]) | ||
if hasattr(value, 'strftime'): | ||
value = value.strftime('%Y-%m-%d %H:%M:%S') | ||
elif isinstance(value, str): | ||
raise QueryTypeError('Expected value of type `int` or instance of `datetime`, not %s' % type(value)) | ||
|
||
return self._add_condition('<', value, types=[int, str]) | ||
|
||
def between(self, start, end): | ||
"""Query records in a start and end range | ||
|
@@ -224,6 +234,11 @@ def __str__(self): | |
return str().join(self._query) | ||
|
||
|
||
# For backwards compatibility | ||
class Query(QueryBuilder): | ||
pass | ||
|
||
|
||
class Client(object): | ||
def __init__(self, instance, user, password, raise_on_empty=True, default_payload=None): | ||
"""Sets configuration and creates a session object used in `Request` later on | ||
|
@@ -540,7 +555,7 @@ def _get_formatted_query(self, fields): | |
:return: ServiceNow query | ||
""" | ||
|
||
if isinstance(self.query, Query): | ||
if isinstance(self.query, QueryBuilder): | ||
sysparm_query = str(self.query) | ||
elif isinstance(self.query, dict): # Dict-type query | ||
try: | ||
|
@@ -552,7 +567,7 @@ def _get_formatted_query(self, fields): | |
elif isinstance(self.query, str): # String-type query | ||
sysparm_query = self.query | ||
else: | ||
raise InvalidUsage("You must pass a query using either a dictionary or string (for advanced queries)") | ||
raise InvalidUsage("Query must be instance of %s, %s or %s" % (QueryBuilder, str, dict)) | ||
|
||
result = {'sysparm_query': sysparm_query} | ||
result.update(self.default_payload) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters