Changeset - 268fa0b6b2ef
[Not reviewed]
beta
0 2 0
Marcin Kuzminski - 15 years ago 2011-04-02 21:19:16
marcin@python-works.com
Added os.sep in models for better win support
fixed safe unicode function to not use str as param, and skip execution on unicode
2 files changed with 10 insertions and 5 deletions:
0 comments (0 inline, 0 general)
rhodecode/lib/__init__.py
Show inline comments
 
@@ -27,41 +27,44 @@
 

	
 
def str2bool(s):
 
    if s is None:
 
        return False
 
    if s in (True, False):
 
        return s
 
    s = str(s).strip().lower()
 
    return s in ('t', 'true', 'y', 'yes', 'on', '1')
 

	
 
def generate_api_key(username, salt=None):
 
    """
 
    Generates uniq API key for given username
 
    
 
    :param username: username as string
 
    :param salt: salt to hash generate KEY
 
    """
 
    from tempfile import _RandomNameSequence
 
    import hashlib
 

	
 
    if salt is None:
 
        salt = _RandomNameSequence().next()
 

	
 
    return hashlib.sha1(username + salt).hexdigest()
 

	
 
def safe_unicode(str):
 
def safe_unicode(_str):
 
    """
 
    safe unicode function. In case of UnicodeDecode error we try to return
 
    unicode with errors replace, if this fails we return unicode with 
 
    string_escape decoding 
 
    """
 

	
 
    if isinstance(_str, unicode):
 
        return _str
 

	
 
    try:
 
        u_str = unicode(str)
 
        u_str = unicode(_str)
 
    except UnicodeDecodeError:
 
        try:
 
            u_str = unicode(str, 'utf-8', 'replace')
 
            u_str = _str.decode('utf-8', 'replace')
 
        except UnicodeDecodeError:
 
            #incase we have a decode error just represent as byte string
 
            u_str = unicode(str(str).encode('string_escape'))
 
            u_str = unicode(_str.encode('string_escape'))
 

	
 
    return u_str
rhodecode/model/db.py
Show inline comments
 
@@ -3,48 +3,50 @@
 
    rhodecode.model.db
 
    ~~~~~~~~~~~~~~~~~~
 
    
 
    Database Models for RhodeCode
 
    
 
    :created_on: Apr 08, 2010
 
    :author: marcink
 
    :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>    
 
    :license: GPLv3, see COPYING for more details.
 
"""
 
# This program is free software; you can redistribute it and/or
 
# modify it under the terms of the GNU General Public License
 
# as published by the Free Software Foundation; version 2
 
# of the License or (at your opinion) any later version of the license.
 
# 
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU General Public License for more details.
 
# 
 
# 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 os
 
import logging
 
import datetime
 
from datetime import date
 

	
 
from sqlalchemy import *
 
from sqlalchemy.exc import DatabaseError
 
from sqlalchemy.orm import relationship, backref
 
from sqlalchemy.orm.interfaces import MapperExtension
 

	
 
from rhodecode.model.meta import Base, Session
 

	
 
log = logging.getLogger(__name__)
 

	
 
#==============================================================================
 
# MAPPER EXTENSIONS
 
#==============================================================================
 

	
 
class RepositoryMapper(MapperExtension):
 
    def after_update(self, mapper, connection, instance):
 
        pass
 

	
 

	
 
class RhodeCodeSettings(Base):
 
    __tablename__ = 'rhodecode_settings'
 
@@ -191,49 +193,49 @@ class Repository(Base):
 
    group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=False, default=None)
 

	
 

	
 
    user = relationship('User')
 
    fork = relationship('Repository', remote_side=repo_id)
 
    group = relationship('Group')
 
    repo_to_perm = relationship('RepoToPerm', cascade='all', order_by='RepoToPerm.repo_to_perm_id')
 
    users_group_to_perm = relationship('UsersGroupToPerm', cascade='all')
 
    stats = relationship('Statistics', cascade='all', uselist=False)
 

	
 
    followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all')
 

	
 
    logs = relationship('UserLog', cascade='all')
 

	
 
    def __repr__(self):
 
        return "<%s('%s:%s')>" % (self.__class__.__name__,
 
                                  self.repo_id, self.repo_name)
 

	
 
    @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]
 
        return self.repo_name.split(os.sep)[-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):
 
        return self.groups_with_parents, self.just_name
 

	
 

	
 
class Group(Base):
0 comments (0 inline, 0 general)