incremental sequences to print short unique IDs

This commit is contained in:
Davide Alberani 2015-05-01 16:27:22 +02:00
parent dcbab3000c
commit d3ca5f342e
2 changed files with 25 additions and 2 deletions

View file

@ -32,7 +32,8 @@ class EventManDB(object):
'update': '$set', 'update': '$set',
'append': '$push', 'append': '$push',
'appendUnique': '$addToSet', 'appendUnique': '$addToSet',
'delete': '$pull' 'delete': '$pull',
'increment': '$inc'
} }
def __init__(self, url=None, dbName='eventman'): def __init__(self, url=None, dbName='eventman'):
@ -189,7 +190,7 @@ class EventManDB(object):
:type _id_or_query: str or :class:`~bson.objectid.ObjectId` or iterable :type _id_or_query: str or :class:`~bson.objectid.ObjectId` or iterable
:param data: the updated information to store :param data: the updated information to store
:type data: dict :type data: dict
:param operation: operation used to update the document or a portion of it, like a list (update, append, appendUnique, delete) :param operation: operation used to update the document or a portion of it, like a list (update, append, appendUnique, delete, increment)
:type operation: str :type operation: str
:param updateList: if set, it's considered the name of a list (the first matching element will be updated) :param updateList: if set, it's considered the name of a list (the first matching element will be updated)
:type updateList: str :type updateList: str

View file

@ -100,6 +100,26 @@ class CollectionHandler(BaseHandler):
# set of documents we're managing (a collection in MongoDB or a table in a SQL database) # set of documents we're managing (a collection in MongoDB or a table in a SQL database)
collection = None collection = None
# set of documents used to store incremental sequences
counters_collection = 'counters'
def get_next_seq(self, seq):
"""Increment and return the new value of a ever-incrementing counter.
:param seq: unique name of the sequence
:type seq: str
:return: the next value of the sequence
:rtype: int
"""
if not self.db.query(self.counters_collection, {'seq_name': seq}):
self.db.add(self.counters_collection, {'seq_name': seq, 'seq': 0})
merged, doc = self.db.update(self.counters_collection,
{'seq_name': seq},
{'seq': 1},
operation='increment')
return doc.get('seq', 0)
def _filter_results(self, results, params): def _filter_results(self, results, params):
"""Filter a list using keys and values from a dictionary. """Filter a list using keys and values from a dictionary.
@ -329,6 +349,8 @@ class EventsHandler(CollectionHandler):
def handle_post_persons(self, id_, person_id, data): def handle_post_persons(self, id_, person_id, data):
# Add a person to the list of persons registered at this event. # Add a person to the list of persons registered at this event.
data['seq'] = self.get_next_seq('event_%s_persons' % id_)
data['seq_hex'] = '%06X' % data['seq']
doc = self.db.query('events', doc = self.db.query('events',
{'_id': id_, 'persons.person_id': person_id}) {'_id': id_, 'persons.person_id': person_id})
if '_id' in data: if '_id' in data: