Skip to content

Commit

Permalink
Merge pull request #109 from balsagoth/feature/query_improve
Browse files Browse the repository at this point in the history
add date__on to query and some other improvments
  • Loading branch information
martinrusev authored Nov 30, 2017
2 parents 6c11c75 + fe965e7 commit 7b1bda6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Usage
# Messages received after specific date
messages_from = imbox.messages(date__gt=datetime.date(2013, 7, 30))
# Messages received on a specific date
messages_from = imbox.messages(date__on=datetime.date(2013, 7, 30))
# Messages from a specific folder
messages_folder = imbox.messages(folder='Social')
Expand Down
21 changes: 10 additions & 11 deletions imbox/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

logger = logging.getLogger(__name__)

IMAP_MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]


def format_date(date):

return "%s-%s-%s" % (date.day, IMAP_MONTHS[date.month - 1], date.year)
if isinstance(date, datetime.date):
return date.strftime('%d-%b-%Y')
else:
return date


def build_search_query(**kwargs):
Expand All @@ -22,11 +21,8 @@ def build_search_query(**kwargs):
sent_from = kwargs.get('sent_from', False)
sent_to = kwargs.get('sent_to', False)
date__gt = kwargs.get('date__gt', False)
if type(date__gt) is datetime.date:
date__gt = format_date(date__gt)
date__lt = kwargs.get('date__lt', False)
if type(date__lt) is datetime.date:
date__lt = format_date(date__lt)
date__on = kwargs.get('date__on', False)
subject = kwargs.get('subject')

query = []
Expand All @@ -47,10 +43,13 @@ def build_search_query(**kwargs):
query.append('(TO "%s")' % sent_to)

if date__gt:
query.append('(SINCE "%s")' % date__gt)
query.append('(SINCE "%s")' % format_date(date__gt))

if date__lt:
query.append('(BEFORE "%s")' % date__lt)
query.append('(BEFORE "%s")' % format_date(date__lt))

if date__on:
query.append('(ON "%s")' % format_date(date__on))

if subject is not None:
query.append('(SUBJECT "%s")' % subject)
Expand Down
12 changes: 8 additions & 4 deletions tests/query_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@ def test_flagged(self):
def test_sent_from(self):

res = build_search_query(sent_from='test@example.com')
self.assertEqual(res, "(FROM \"test@example.com\")")
self.assertEqual(res, '(FROM "test@example.com")')

def test_sent_to(self):

res = build_search_query(sent_to='test@example.com')
self.assertEqual(res, "(TO \"test@example.com\")")
self.assertEqual(res, '(TO "test@example.com")')

def test_date__gt(self):

res = build_search_query(date__gt=date(2014, 12, 31))
self.assertEqual(res, "(SINCE \"31-Dec-2014\")")
self.assertEqual(res, '(SINCE "31-Dec-2014")')

def test_date__lt(self):

res = build_search_query(date__lt=date(2014, 1, 1))
self.assertEqual(res, "(BEFORE \"1-Jan-2014\")")
self.assertEqual(res, '(BEFORE "01-Jan-2014")')

def test_date__on(self):
res = build_search_query(date__on=date(2014, 1, 1))
self.assertEqual(res, '(ON "01-Jan-2014")')

0 comments on commit 7b1bda6

Please sign in to comment.