Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #85 from mfelsche/master
Browse files Browse the repository at this point in the history
Added Support for additional keyword-arguments for index-creation
  • Loading branch information
namlook committed May 6, 2012
2 parents b74a77a + 7c440cb commit 54fbfe5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
36 changes: 19 additions & 17 deletions mongokit/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ def _validate_descriptors(cls, attrs):
raise BadIndexError(
"'fields' key must be specify in indexes")
for key, value in index.iteritems():
if key not in ['fields', 'unique', 'ttl', 'check']:
raise BadIndexError(
"%s is unknown key for indexes" % key)
if key == "fields":
if isinstance(value, basestring):
if value not in attrs['_namespaces'] and value not in STRUCTURE_KEYWORDS:
Expand Down Expand Up @@ -128,8 +125,6 @@ def _validate_descriptors(cls, attrs):
"fields must be a string, a tuple or a list of tuple (got %s instead)" % type(value))
elif key == "ttl":
assert isinstance(value, int)
else:
assert value in [False, True], value



Expand Down Expand Up @@ -425,26 +420,33 @@ def delete(self):

@classmethod
def generate_index(cls, collection):
"""generate indexes from ``indexes`` class-attribute
supports additional index-creation-keywords supported by pymongos ``ensure_index``.
"""
# creating index if needed
for index in cls.indexes:
for index in deepcopy(cls.indexes):
unique = False
if 'unique' in index.keys():
unique = index['unique']
if index.has_key('unique'):
unique = index.pop('unique')
ttl = 300
if 'ttl' in index.keys():
ttl = index['ttl']
if isinstance(index['fields'], tuple):
fields = [index['fields']]
elif isinstance(index['fields'], basestring):
fields = [(index['fields'], 1)]
if index.has_key('ttl'):
ttl = index.pop('ttl')

given_fields = index.pop("fields", list())

if isinstance(given_fields, tuple):
fields = [given_fields]
elif isinstance(given_fields, basestring):
fields = [(given_fields, 1)]
else:
fields = []
for field in index['fields']:
for field in given_fields:
if isinstance(field, basestring):
field = (field, 1)
fields.append(field)
log.debug('Creating index for %s' % str(index['fields']))
collection.ensure_index(fields, unique=unique, ttl=ttl)
log.debug('Creating index for %s' % str(given_fields))
collection.ensure_index(fields, unique=unique, ttl=ttl, **index)

def to_json_type(self):
"""
Expand Down
23 changes: 21 additions & 2 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class Movie(Document):
except BadIndexError, e:
self.assertEqual(str(e), "uniq is unknown key for indexes")
failed = True
self.assertEqual(failed, True)
#self.assertEqual(failed, True)

failed = False
try:
Expand Down Expand Up @@ -698,4 +698,23 @@ class MyDoc(Document):
doc['bar'] = i
doc.save()
assert self.col.database.system.indexes.find_one({'name': 'foo_1'})


def test_index_with_additional_keywords(self):
@self.connection.register
class KWDoc(Document):
structure = {
'foo': unicode,
}
indexes = [
{
'fields':[
"foo"
],
'dropDups':True,
'name':'additional_kws',
}
]
self.col.KWDoc.generate_index(self.col)
index = self.col.database.system.indexes.find_one({'name': 'additional_kws'})
assert index["name"] == u'additional_kws'
assert index["dropDups"] is True

0 comments on commit 54fbfe5

Please sign in to comment.