예제 #1
0
파일: node.py 프로젝트: reedobrien/lumin
    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
예제 #2
0
파일: node.py 프로젝트: joshfinnie/lumin
    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}
예제 #3
0
    def make_one(self, title="testing title"):
        from lumin.util import normalize

        return normalize(title)