Skip to content

Commit

Permalink
Merge pull request #32 from whit537/relax-id-search
Browse files Browse the repository at this point in the history
Relax _id matching
  • Loading branch information
chadwhitacre authored Jul 26, 2016
2 parents fa7b43d + f79644d commit abdd5a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
21 changes: 15 additions & 6 deletions mongs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ def commaize(n, places=1):
return out


def get_single_document_filter(_id):
filter = {"_id": {"$in": [_id]}}
try:
# Under some combination of MongoDB/PyMongo versions, this is
# necessary.
object_id = ObjectId(_id)
except InvalidId:
pass
else:
filter['_id']['$in'].append(object_id)
return filter


def get_value(request):
"""Given a request object, return a value. Use for *.txt and *.json.
"""
Expand All @@ -34,12 +47,8 @@ def get_value(request):
key = request.line.uri.path['value'] # derp

db = pymongo.MongoClient(server)[database][collection]

try:
_id = ObjectId(_id)
except InvalidId:
pass
document = db.find_one(_id)
filter = get_single_document_filter(_id)
document = db.find_one(filter)
return document[key]


Expand Down
7 changes: 1 addition & 6 deletions www/%server/%database/%collection/%filter/index.html.spt
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,7 @@ else: # /server/database/collection/_id/
# Convert a request for a specific _id into a filter with one page.
page = 1
base = '..'
_id = filter
try:
_id = ObjectId(_id)
except InvalidId:
pass
filter = {"_id": _id}
filter = mongs.get_single_document_filter(filter)


# Sort.
Expand Down

0 comments on commit abdd5a8

Please sign in to comment.