changeset 3842:54bc7a89f090 beta

gists: add some API related code improvements
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 11 May 2013 22:43:54 +0200
parents 979edf6a2990
children ad4a680113b7
files rhodecode/model/db.py rhodecode/model/gist.py rhodecode/tests/functional/test_admin_gists.py
diffstat 3 files changed, 50 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/model/db.py	Sat May 11 21:33:45 2013 +0200
+++ b/rhodecode/model/db.py	Sat May 11 22:43:54 2013 +0200
@@ -2152,12 +2152,55 @@
 
     @classmethod
     def get_by_access_id(cls, gist_access_id):
-        return cls.query().filter(cls.gist_access_id==gist_access_id).scalar()
+        return cls.query().filter(cls.gist_access_id == gist_access_id).scalar()
 
     def gist_url(self):
         from pylons import url
         return url('gist', id=self.gist_access_id, qualified=True)
 
+    @classmethod
+    def base_path(cls):
+        """
+        Returns base path when all gists are stored
+
+        :param cls:
+        """
+        from rhodecode.model.gist import GIST_STORE_LOC
+        q = Session().query(RhodeCodeUi)\
+            .filter(RhodeCodeUi.ui_key == URL_SEP)
+        q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
+        return os.path.join(q.one().ui_value, GIST_STORE_LOC)
+
+    def get_api_data(self):
+        """
+        Common function for generating gist related data for API
+        """
+        gist = self
+        data = dict(
+            gist_id=gist.gist_id,
+            type=gist.gist_type,
+            access_id=gist.gist_access_id,
+            description=gist.gist_description,
+            url=gist.gist_url(),
+            expires=gist.gist_expires,
+            created_on=gist.created_on,
+        )
+        return data
+
+    def __json__(self):
+        data = dict(
+        )
+        data.update(self.get_api_data())
+        return data
+    ## SCM functions
+
+    @property
+    def scm_instance(self):
+        from rhodecode.lib.vcs import get_repo
+        base_path = self.base_path()
+        return get_repo(os.path.join(*map(safe_str,
+                                          [base_path, self.gist_access_id])))
+
 
 class DbMigrateVersion(Base, BaseModel):
     __tablename__ = 'db_migrate_version'
--- a/rhodecode/model/gist.py	Sat May 11 21:33:45 2013 +0200
+++ b/rhodecode/model/gist.py	Sat May 11 22:43:54 2013 +0200
@@ -38,7 +38,6 @@
 from rhodecode.model.db import Gist
 from rhodecode.model.repo import RepoModel
 from rhodecode.model.scm import ScmModel
-from rhodecode.lib.vcs import get_repo
 
 log = logging.getLogger(__name__)
 
@@ -68,16 +67,17 @@
         log.info("Removing %s" % (rm_path))
         shutil.rmtree(rm_path)
 
+    def get_gist(self, gist):
+        return self._get_gist(gist)
+
     def get_gist_files(self, gist_access_id):
         """
         Get files for given gist
 
         :param gist_access_id:
         """
-        root_path = RepoModel().repos_path
-        r = get_repo(os.path.join(*map(safe_str,
-                                [root_path, GIST_STORE_LOC, gist_access_id])))
-        cs = r.get_changeset()
+        repo = Gist.get_by_access_id(gist_access_id)
+        cs = repo.scm_instance.get_changeset()
         return (
          cs, [n for n in cs.get_node('/')]
         )
--- a/rhodecode/tests/functional/test_admin_gists.py	Sat May 11 21:33:45 2013 +0200
+++ b/rhodecode/tests/functional/test_admin_gists.py	Sat May 11 22:43:54 2013 +0200
@@ -116,7 +116,7 @@
         gist = _create_gist('gist-show-me')
         response = self.app.get(url('gist', id=gist.gist_access_id))
         response.mustcontain('added file: gist-show-me<')
-        response.mustcontain('test_admin (RhodeCode Admin) - created just now')
+        response.mustcontain('test_admin (RhodeCode Admin) - created')
         response.mustcontain('gist-desc')
         response.mustcontain('<div class="ui-btn green badge">Public gist</div>')