Changeset - 37a9421f32a0
[Not reviewed]
beta
0 2 0
Marcin Kuzminski - 15 years ago 2011-03-18 22:04:21
marcin@python-works.com
Added handy methods to Repository model for fetching groups with parents
2 files changed with 25 insertions and 4 deletions:
0 comments (0 inline, 0 general)
rhodecode/model/__init__.py
Show inline comments
 
@@ -41,23 +41,25 @@
 
# You should have received a copy of the GNU General Public License
 
# along with this program; if not, write to the Free Software
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
# MA  02110-1301, USA.
 

	
 
import logging
 

	
 
from rhodecode.model import meta
 

	
 
log = logging.getLogger(__name__)
 

	
 
def init_model(engine):
 
    """Initializes db session, bind the engine with the metadata,
 
    Call this before using any of the tables or classes in the model, preferably
 
    once in application start
 
    
 
    :param engine: engine to bind to
 
    """
 
    log.info("initializing db models for %s", engine)
 
    log.info("initializing db for %s", engine)
 
    meta.Base.metadata.bind = engine
 

	
 
class BaseModel(object):
 
    """Base Model for all RhodeCode models, it adds sql alchemy session
 
    into instance of model
 
    
rhodecode/model/db.py
Show inline comments
 
@@ -208,16 +208,35 @@ class Repository(Base):
 

	
 
    @classmethod
 
    def by_repo_name(cls, repo_name):
 
        return Session.query(cls).filter(cls.repo_name == repo_name).one()
 

	
 
    @property
 
    def just_name(self):
 
        return self.repo_name.split('/')[-1]
 

	
 
    @property
 
    def groups_with_parents(self):
 
        groups = []
 
        if self.group is None:
 
            return groups
 

	
 
        cur_gr = self.group
 
        groups.insert(0, cur_gr)
 
        while 1:
 
            gr = getattr(cur_gr, 'parent_group', None)
 
            cur_gr = cur_gr.parent_group
 
            if gr is None:
 
                break
 
            groups.insert(0, gr)
 

	
 
        return groups
 

	
 
    @property
 
    def groups_and_repo(self):
 
        just_name = self.repo_name.split('/')[-1]
 

	
 
        return self.group, just_name
 
        return self.groups_with_parents, self.just_name
 

	
 

	
 
class Group(Base):
 
    __tablename__ = 'groups'
 
    __table_args__ = (UniqueConstraint('group_name'), {'useexisting':True},)
 

	
0 comments (0 inline, 0 general)