Changeset - 54bc7a89f090
[Not reviewed]
beta
0 3 0
Marcin Kuzminski - 13 years ago 2013-05-11 22:43:54
marcin@python-works.com
gists: add some API related code improvements
3 files changed with 50 insertions and 7 deletions:
0 comments (0 inline, 0 general)
rhodecode/model/db.py
Show inline comments
 
@@ -2149,18 +2149,61 @@ class Gist(Base, BaseModel):
 
        if not res:
 
            raise HTTPNotFound
 
        return res
 

	
 
    @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'
 
    __table_args__ = (
 
        {'extend_existing': True, 'mysql_engine': 'InnoDB',
 
         'mysql_charset': 'utf8'},
rhodecode/model/gist.py
Show inline comments
 
@@ -35,13 +35,12 @@ from rhodecode.lib.utils2 import safe_un
 
from rhodecode.lib.compat import json
 
from rhodecode.lib import helpers as h
 
from rhodecode.model import BaseModel
 
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__)
 

	
 
GIST_STORE_LOC = '.rc_gist_store'
 
GIST_METADATA_FILE = '.rc_gist_metadata'
 

	
 
@@ -65,22 +64,23 @@ class GistModel(BaseModel):
 
        """
 
        root_path = RepoModel().repos_path
 
        rm_path = os.path.join(root_path, GIST_STORE_LOC, gist.gist_access_id)
 
        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('/')]
 
        )
 

	
 
    def create(self, description, owner, gist_mapping,
 
               gist_type=Gist.GIST_PUBLIC, lifetime=-1):
rhodecode/tests/functional/test_admin_gists.py
Show inline comments
 
@@ -113,13 +113,13 @@ class TestGistsController(TestController
 
        response = self.app.delete(url('gist', id=1))
 

	
 
    def test_show(self):
 
        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>')
 

	
 
    def test_edit(self):
 
        self.skipTest('not implemented')
 
        response = self.app.get(url('edit_gist', id=1))
0 comments (0 inline, 0 general)