Mercurial > wikked
changeset 135:1030a6e8d416
Moved some database code up to the base class.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 05 Dec 2013 08:23:12 -0800 |
parents | f47b047c9414 |
children | 546a71fb2e37 |
files | wikked/db/base.py wikked/db/sql.py |
diffstat | 2 files changed, 35 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/wikked/db/base.py Thu Dec 05 08:22:55 2013 -0800 +++ b/wikked/db/base.py Thu Dec 05 08:23:12 2013 -0800 @@ -1,3 +1,4 @@ +from wikked.utils import PageNotFoundError class Database(object): @@ -27,8 +28,20 @@ def getPages(self, subdir=None, meta_query=None): raise NotImplementedError() - def getPage(self, url=None, path=None): - raise NotImplementedError() + def getPage(self, url=None, path=None, raise_if_none=True): + if not url and not path: + raise ValueError("Either URL or path need to be specified.") + if url and path: + raise ValueError("Can't specify both URL and path.") + if url: + page = self._getPageByUrl(url) + elif path: + page = self._getPageByPath(path) + else: + raise NotImplementedError() + if page is None and raise_if_none: + raise PageNotFoundError(url or path) + return page def pageExists(self, url=None, path=None): raise NotImplementedError() @@ -36,3 +49,9 @@ def getLinksTo(self, url): raise NotImplementedError() + def _getPageByUrl(self, url): + raise NotImplementedError() + + def _getPageByPath(self, path): + raise NotImplementedError() +
--- a/wikked/db/sql.py Thu Dec 05 08:22:55 2013 -0800 +++ b/wikked/db/sql.py Thu Dec 05 08:23:12 2013 -0800 @@ -246,29 +246,6 @@ for p in q.all(): yield SQLDatabasePage(self.wiki, db_obj=p) - def getPage(self, url=None, path=None, raise_if_none=True): - if not url and not path: - raise ValueError("Either URL or path need to be specified.") - if url and path: - raise ValueError("Can't specify both URL and path.") - if url: - q = self.session.query(SQLPage).filter_by(url=url) - page = q.first() - if page is None: - if raise_if_none: - raise PageNotFoundError(url) - return None - return SQLDatabasePage(self.wiki, db_obj=page) - if path: - q = self.session.query(SQLPage).filter_by(path=path) - page = q.first() - if page is None: - if raise_if_none: - raise PageNotFoundError(path) - return None - return SQLDatabasePage(self.wiki, db_obj=page) - raise NotImplementedError() - def pageExists(self, url=None, path=None): # TODO: replace with an `EXIST` query. return self.getPage(url, path, raise_if_none=False) is not None @@ -288,6 +265,20 @@ for p in q: yield SQLDatabasePage(self.wiki, db_obj=p) + def _getPageByUrl(self, url): + q = self.session.query(SQLPage).filter_by(url=url) + page = q.first() + if page is None: + return None + return SQLDatabasePage(self.wiki, db_obj=page) + + def _getPageByPath(self, path): + q = self.session.query(SQLPage).filter_by(path=path) + page = q.first() + if page is None: + return None + return SQLDatabasePage(self.wiki, db_obj=page) + def _createSchema(self): Base.metadata.drop_all(self.engine) Base.metadata.create_all(self.engine)