Skip to content

Commit

Permalink
Offset/limit mongo support
Browse files Browse the repository at this point in the history
  • Loading branch information
ayakimov committed Jun 20, 2017
1 parent 8de0435 commit f09e544
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions lathermail/storage/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ def message_handler(*args, **kwargs):
mongo.db.messages.insert(msg)


def find_messages(password, inbox=None, fields=None, limit=0, include_attachment_bodies=False):
messages = list(_iter_messages(password, inbox, fields, limit, include_attachment_bodies))
def find_messages(password, inbox=None, fields=None, offset=0, limit=0, include_attachment_bodies=False):
messages = list(_iter_messages(password, inbox, fields, offset, limit, include_attachment_bodies))
if messages:
ids = [m["_id"] for m in messages]
mongo.db.messages.update({"_id": {"$in": ids}, "read": False},
{"$set": {"read": True}}, multi=True)
return messages


def _iter_messages(password, inbox=None, fields=None, limit=0, include_attachment_bodies=False):
def _iter_messages(password, inbox=None, fields=None, offset=0, limit=0, include_attachment_bodies=False):
query = _prepare_query(password, inbox, fields)
for message in mongo.db.messages.find(query).sort("created_at", DESCENDING).limit(limit):

for message in mongo.db.messages.find(query).skip(offset).sort("created_at", DESCENDING).limit(limit):
yield expand_message_fields(message, include_attachment_bodies)


Expand Down
4 changes: 2 additions & 2 deletions lathermail/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def iso_date(value):
parser.add_argument("created_at_lt", type=iso_date)
parser.add_argument("created_at_gt", type=iso_date)
parser.add_argument("read", type=types.boolean)
parser.add_argument("X-Mail-Offset", type=int, dest="offset", location="headers")
parser.add_argument("X-Mail-Limit", type=int, dest="limit", location="headers")
parser.add_argument("X-Mail-Offset", type=int, dest="offset", location="headers", default=0)
parser.add_argument("X-Mail-Limit", type=int, dest="limit", location="headers", default=0)

0 comments on commit f09e544

Please sign in to comment.