Skip to content

Commit

Permalink
Add option for call direction and status filtering
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Čihař <michal@cihar.com>
  • Loading branch information
nijel committed Jul 13, 2015
1 parent 779e4d8 commit 858baeb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
6 changes: 5 additions & 1 deletion docs/python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,18 @@ Python API

Initiates callback.

.. method:: calls(from_date, to_date, line=None):
.. method:: calls(from_date, to_date, line=None, status=None, direction=None):

:param from_date: Starting date
:type from_date: datetime.datetime
:param to_date: Ending date
:type to_date: datetime.datetime
:param line: Line to use for listing
:type line: string or None
:param status: Call status, one of 'answered', 'missed'
:type status: string
:param direction: Call direction, one of 'in', 'out', 'redirected'
:type direction: string
:rtype: list

Returns list of calls in given interval. Optionally filtered for given
Expand Down
6 changes: 5 additions & 1 deletion odorik/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,18 @@ def send_sms(self, recipient, message, sender='5517'):
self._check_response(response)
return response

def calls(self, from_date, to_date, line=None):
def calls(self, from_date, to_date, line=None, status=None, direction=None):
"""Return list of calls."""
args = {
'from': from_date.isoformat(),
'to': to_date.isoformat()
}
if line is not None:
args['line'] = line
if status is not None:
args['status'] = status
if direction is not None:
args['direction'] = direction
return self.get_json('calls.json', args)

def sms(self, from_date, to_date, line=None):
Expand Down
18 changes: 18 additions & 0 deletions odorik/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ def print(self, value):
"""Print value."""
header = None
if isinstance(value, list):
if len(value) == 0:
return
header = sorted(value[0].keys(), key=sort_key)

if self.args.format == 'json':
Expand Down Expand Up @@ -582,15 +584,31 @@ def add_parser(cls, subparser):
parser = super(Calls, cls).add_parser(subparser)
cls.add_list_option(parser)
cls.add_line_option(parser)
parser.add_argument(
'--direction',
choices=('in', 'out', 'redirected'),
help='Direction of call',
)
parser.add_argument(
'--status',
choices=('answered', 'missed'),
help='Status of call',
)
return parser

def run(self):
"""Main execution of the command."""
from_date, to_date = self.get_interval()
args = {}
if self.args.status:
args['status'] = self.args.status
if self.args.direction:
args['direction'] = self.args.direction
calls = self.odorik.calls(
from_date,
to_date,
self.resolve('lines', self.args.line),
**args
)
if self.args.list:
self.print(calls)
Expand Down
9 changes: 9 additions & 0 deletions odorik/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,15 @@ def test_calls_list(self):
output = execute(['calls', '--list'])
self.assertIn('554.03', output)

@httpretty.activate
def test_calls_filter(self):
"""Test getting calls list."""
register_uris()
output = execute(
['calls', '--list', '--direction', 'in', '--status', 'answered']
)
self.assertIn('554.03', output)

@httpretty.activate
def test_calls_line(self):
"""Test getting calls summary for line."""
Expand Down

0 comments on commit 858baeb

Please sign in to comment.