Created
May 2, 2010 16:09
-
-
Save thruflo/387245 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MultiDBModel(Document): | |
| """Overwrites methods of the base ``Document`` class that | |
| prescribe a fixed mapping between class and database. | |
| Can be used like normal but can also pass ``db=...`` | |
| to methods as necessary. | |
| """ | |
| def save(self, db=None, **params): | |
| """ Save document in database. | |
| """ | |
| self.validate() | |
| if self._db is None and db is None: | |
| raise TypeError("doc database required to save document") | |
| else: | |
| _db = self._db and self._db or db | |
| doc = self.to_json() | |
| _db.save_doc(doc, **params) | |
| if '_id' in doc and '_rev' in doc: | |
| self._doc.update(doc) | |
| elif '_id' in doc: | |
| self._doc.update({'_id': doc['_id']}) | |
| @classmethod | |
| def bulk_save(cls, docs, use_uuids=True, all_or_nothing=False): | |
| """ Save multiple documents in database. | |
| """ | |
| if cls._db is None and db is None: | |
| raise TypeError("doc database required to save document") | |
| else: | |
| _db = cls._db and cls._db or db | |
| docs_to_save= [doc._doc for doc in docs if doc._doc_type == cls._doc_type] | |
| if not len(docs_to_save) == len(docs): | |
| raise ValueError("one of your documents does not have the correct type") | |
| _db.bulk_save( | |
| docs_to_save, | |
| use_uuids=use_uuids, | |
| all_or_nothing=all_or_nothing | |
| ) | |
| @classmethod | |
| def get(cls, docid, rev=None, db=None, dynamic_properties=True): | |
| """Get document with `docid` | |
| """ | |
| if cls._db is None and db is None: | |
| raise TypeError("doc database required to save document") | |
| else: | |
| _db = cls._db and cls._db or db | |
| cls._allow_dynamic_properties = dynamic_properties | |
| return _db.get(docid, rev=rev, wrapper=cls.wrap) | |
| @classmethod | |
| def get_or_create(cls, docid=None, db=None, dynamic_properties=True, **params): | |
| """Get or create document with `docid` | |
| """ | |
| if cls._db is None and db is None: | |
| raise TypeError("doc database required to save document") | |
| else: | |
| _db = cls._db and cls._db or db | |
| cls._allow_dynamic_properties = dynamic_properties | |
| if docid is None: | |
| obj = cls() | |
| obj.save(db=_db, **params) | |
| return obj | |
| rev = params.pop('rev', None) | |
| try: | |
| return _db.get(docid, rev=rev, wrapper=cls.wrap, **params) | |
| except ResourceNotFound: | |
| obj = cls() | |
| obj._id = docid | |
| obj.save(db=_db, **params) | |
| return obj | |
| def delete(self, db=None): | |
| """Delete document from the database. | |
| """ | |
| if self._db is None and db is None: | |
| raise TypeError("doc database required to delete document") | |
| else: | |
| _db = self._db and self._db or db | |
| if self.new_document: | |
| raise TypeError("the document is not saved") | |
| _db.delete_doc(self._id) | |
| # reinit document | |
| del self._doc['_id'] | |
| del self._doc['_rev'] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment