Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add date__on to query and some other improvments #109

Merged
merged 1 commit into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")')