# HG changeset patch # User Marcin Kuzminski # Date 2011-09-11 23:42:06 # Node ID ad5a543a9b2fba21544cde5c7d7484cda66c3941 # Parent ef31d0c6bae9bae5e1fb02dca19155a85b6f5a2d Code cleanup - extended repository models with scm functions - moved get_changeset_safe function to lib diff --git a/rhodecode/lib/__init__.py b/rhodecode/lib/__init__.py --- a/rhodecode/lib/__init__.py +++ b/rhodecode/lib/__init__.py @@ -140,7 +140,7 @@ def detect_mode(line, default): def generate_api_key(username, salt=None): """ - Generates unique API key for given username,if salt is not given + Generates unique API key for given username, if salt is not given it'll be generated from some random string :param username: username as string @@ -342,3 +342,23 @@ def credentials_filter(uri): return ''.join(uri) +def get_changeset_safe(repo, rev): + """ + Safe version of get_changeset if this changeset doesn't exists for a + repo it returns a Dummy one instead + + :param repo: + :param rev: + """ + from vcs.backends.base import BaseRepository + from vcs.exceptions import RepositoryError + if not isinstance(repo, BaseRepository): + raise Exception('You must pass an Repository ' + 'object as first argument got %s', type(repo)) + + try: + cs = repo.get_changeset(rev) + except RepositoryError: + from rhodecode.lib.utils import EmptyChangeset + cs = EmptyChangeset(requested_revision=rev) + return cs \ No newline at end of file diff --git a/rhodecode/lib/helpers.py b/rhodecode/lib/helpers.py --- a/rhodecode/lib/helpers.py +++ b/rhodecode/lib/helpers.py @@ -37,7 +37,7 @@ from webhelpers.html.tags import _set_in from vcs.utils.annotate import annotate_highlight from rhodecode.lib.utils import repo_name_slug -from rhodecode.lib import str2bool, safe_unicode, safe_str +from rhodecode.lib import str2bool, safe_unicode, safe_str,get_changeset_safe def _reset(name, value=None, id=NotGiven, type="reset", **attrs): """ @@ -275,21 +275,6 @@ def pygmentize_annotation(repo_name, fil return literal(annotate_highlight(filenode, url_func(repo_name), **kwargs)) -def get_changeset_safe(repo, rev): - from vcs.backends.base import BaseRepository - from vcs.exceptions import RepositoryError - if not isinstance(repo, BaseRepository): - raise Exception('You must pass an Repository ' - 'object as first argument got %s', type(repo)) - - try: - cs = repo.get_changeset(rev) - except RepositoryError: - from rhodecode.lib.utils import EmptyChangeset - cs = EmptyChangeset() - return cs - - def is_following_repo(repo_name, user_id): from rhodecode.model.scm import ScmModel return ScmModel().is_following_repo(repo_name, user_id) diff --git a/rhodecode/lib/utils.py b/rhodecode/lib/utils.py --- a/rhodecode/lib/utils.py +++ b/rhodecode/lib/utils.py @@ -313,14 +313,15 @@ class EmptyChangeset(BaseChangeset): an EmptyChangeset """ - def __init__(self, cs='0' * 40, repo=None): + def __init__(self, cs='0' * 40, repo=None,requested_revision=None): self._empty_cs = cs self.revision = -1 self.message = '' self.author = '' self.date = '' self.repository = repo - + self.requested_revision = requested_revision + @LazyProperty def raw_id(self): """Returns raw string identifying this changeset, useful for web diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -43,7 +43,7 @@ from vcs.utils.lazy import LazyProperty from vcs.nodes import FileNode from rhodecode.lib.exceptions import UsersGroupsAssignedException -from rhodecode.lib import str2bool, json, safe_str +from rhodecode.lib import str2bool, json, safe_str, get_changeset_safe from rhodecode.model.meta import Base, Session from rhodecode.model.caching_query import FromCache @@ -125,13 +125,13 @@ class BaseModel(object): @classmethod def get(cls, id_): return Session.query(cls).get(id_) - + @classmethod def delete(cls, id_): obj = Session.query(cls).get(id_) Session.delete(obj) Session.commit() - + class RhodeCodeSettings(Base, BaseModel): __tablename__ = 'rhodecode_settings' @@ -538,8 +538,7 @@ class Repository(Base, BaseModel): ret = Session.query(RhodeCodeUi)\ - .options(FromCache("sql_cache_short", - "repository_repo_ui")).all() + .options(FromCache("sql_cache_short", "repository_repo_ui")).all() hg_ui = ret for ui_ in hg_ui: @@ -551,6 +550,25 @@ class Repository(Base, BaseModel): return baseui #========================================================================== + # SCM PROPERTIES + #========================================================================== + + def get_changeset(self, rev): + return get_changeset_safe(self.scm_instance, rev) + + @property + def tip(self): + return self.get_changeset('tip') + + @property + def author(self): + return self.tip.author + + @property + def last_change(self): + return self.scm_instance.last_change + + #========================================================================== # SCM CACHE INSTANCE #========================================================================== @@ -580,7 +598,7 @@ class Repository(Base, BaseModel): Session.add(inv) Session.commit() - @property + @LazyProperty def scm_instance(self): return self.__get_instance()