def insert(self, doc, title_or_id, increment=True, seperator="-"): """ Insert ``doc`` into the :term:`collection`. :param doc: A dictionary to be stored in the DB :param title_or_id: a string to be normalized for a URL and used as the _id for the document. :param increment: Whether to increment ``title_or_id`` if it already exists in the DB. **Default: ``True``** :param seperator: character to separate ``title_or_id`` incremental id. **Default: ```"-"``** """ ctime = mtime = datetime.datetime.utcnow().strftime(TS_FORMAT) doc["ctime"] = ctime doc["mtime"] = mtime doc["_id"] = normalize(title_or_id) if increment: suffix = 0 _id = doc["_id"] while True: try: oid = self._collection.insert(doc) break except self.duplicate_key_error: suffix += 1 _id_suffixed = seperator.join([_id, str(suffix)]) doc["_id"] = _id_suffixed else: oid = self._collection.insert(doc) return oid
def insert(self, doc, title_or_id, increment=True, seperator='-'): """ Insert ``doc`` into the :term:`collection`. :param doc: A dictionary to be stored in the DB :param title_or_id: a string to be normalized for a URL and used as the __name__ for the document. :param increment: Whether to increment ``title_or_id`` if it already exists in the DB. **Default: ``True``** :param seperator: character to separate ``title_or_id`` incremental id. **Default: ```"-"``** """ ctime = mtime = datetime.datetime.utcnow().strftime(TS_FORMAT) doc['ctime'] = ctime doc['mtime'] = mtime doc['__name__'] = normalize(title_or_id) if increment: suffix = 0 _id = doc['__name__'] while True: try: self._collection.insert(doc, safe=True) break except DuplicateKeyError as e: suffix += 1 __name___suffixed = seperator.join([_id, str(suffix)]) doc['__name__'] = __name___suffixed else: self._collection.insert(doc, safe=True) return {key: val for key, val in doc.items() if key in self._spec}
def make_one(self, title="testing title"): from lumin.util import normalize return normalize(title)