# HG changeset patch # User RhodeCode Admin # Date 2023-03-28 08:50:10 # Node ID bed423f3eb3f439d9fec775e5d6d0adfc3a952b4 # Parent 4102d6ca14c4ead9dbe989cb96fdbfb54ac15cb4 db-models: use consistent __str__ for all models, removed safe_unicode calls diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -53,12 +53,13 @@ from zope.cachedescriptors.property impo from pyramid.threadlocal import get_current_request from webhelpers2.text import remove_formatting +from rhodecode.lib.str_utils import safe_bytes from rhodecode.translation import _ from rhodecode.lib.vcs import get_vcs_instance, VCSError from rhodecode.lib.vcs.backends.base import ( EmptyCommit, Reference, unicode_to_reference, reference_to_unicode) from rhodecode.lib.utils2 import ( - str2bool, safe_str, get_commit_safe, safe_unicode, sha1_safe, + str2bool, safe_str, get_commit_safe, sha1_safe, time_to_datetime, aslist, Optional, safe_int, get_clone_url, AttributeDict, glob2re, StrictAttributeDict, cleaned_uri, datetime_to_time) from rhodecode.lib.jsonalchemy import MutationObj, MutationList, JsonType, \ @@ -316,7 +317,7 @@ class RhodeCodeSetting(Base, BaseModel): SETTINGS_TYPES = { 'str': safe_str, 'int': safe_int, - 'unicode': safe_unicode, + 'unicode': safe_str, 'bool': str2bool, 'list': functools.partial(aslist, sep=',') } @@ -360,7 +361,7 @@ class RhodeCodeSetting(Base, BaseModel): :param val: """ - val = safe_unicode(val) + val = safe_str(val) # encode the encrypted value if 'encrypted' in self.app_settings_type: cipher = EncryptedTextValue() @@ -436,7 +437,7 @@ class RhodeCodeUi(Base, BaseModel): ui_active = Column( "ui_active", Boolean(), nullable=True, unique=None, default=True) - def __repr__(self): + def __str__(self): return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.ui_section, self.ui_key, self.ui_value) @@ -494,7 +495,7 @@ class RepoRhodeCodeSetting(Base, BaseMod :param val: """ - self._app_settings_value = safe_unicode(val) + self._app_settings_value = safe_str(val) @hybrid_property def app_settings_type(self): @@ -508,8 +509,8 @@ class RepoRhodeCodeSetting(Base, BaseMod % (SETTINGS_TYPES.keys(), val)) self._app_settings_type = val - def __unicode__(self): - return u"<%s('%s:%s:%s[%s]')>" % ( + def __str__(self): + return "<%s('%s:%s:%s[%s]')>" % ( self.__class__.__name__, self.repository.repo_name, self.app_settings_name, self.app_settings_value, self.app_settings_type @@ -542,7 +543,7 @@ class RepoRhodeCodeUi(Base, BaseModel): repository = relationship('Repository') - def __repr__(self): + def __str__(self): return '<%s[%s:%s]%s=>%s]>' % ( self.__class__.__name__, self.repository.repo_name, self.ui_section, self.ui_key, self.ui_value) @@ -626,9 +627,9 @@ class User(Base, BaseModel): # no cascade, set NULL scope_artifacts = relationship('FileStore', primaryjoin='FileStore.scope_user_id==User.user_id') - def __unicode__(self): - return u"<%s('id:%s:%s')>" % (self.__class__.__name__, - self.user_id, self.username) + + def __str__(self): + return f"<{self.__class__.__name__}('id:{self.user_id}:{self.username}')>" @hybrid_property def email(self): @@ -1177,8 +1178,8 @@ class UserApiKeys(Base, BaseModel): user = relationship('User', lazy='joined') - def __unicode__(self): - return u"<%s('%s')>" % (self.__class__.__name__, self.role) + def __str__(self): + return f"<{self.__class__.__name__}('{self.role}')>" def __json__(self): data = { @@ -1318,9 +1319,8 @@ class UserIpMap(Base, BaseModel): 'ip_range': self._get_ip_range(self.ip_addr), } - def __unicode__(self): - return u"<%s('user_id:%s=>%s')>" % (self.__class__.__name__, - self.user_id, self.ip_addr) + def __str__(self): + return "<%s('user_id:%s=>%s')>" % (self.__class__.__name__, self.user_id, self.ip_addr) class UserSshKeys(Base, BaseModel): @@ -1382,9 +1382,9 @@ class UserLog(Base, BaseModel): user_data = Column('user_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT())))) action_data = Column('action_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT())))) - def __unicode__(self): - return u"<%s('id:%s:%s')>" % ( - self.__class__.__name__, self.repository_name, self.action) + + def __str__(self): + return f"<{self.__class__.__name__}('id:{self.repository_name}:{self.action}')>" def __json__(self): return { @@ -1473,10 +1473,8 @@ class UserGroup(Base, BaseModel): def sync(self): return self._load_sync(self.group_data) - def __unicode__(self): - return u"<%s('id:%s:%s')>" % (self.__class__.__name__, - self.users_group_id, - self.users_group_name) + def __str__(self): + return f"<{self.__class__.__name__}('id:{self.users_group_id}:{self.users_group_name}')>" @classmethod def get_by_group_name(cls, group_name, cache=False, @@ -1783,9 +1781,9 @@ class Repository(Base, BaseModel): # no cascade, set NULL artifacts = relationship('FileStore', primaryjoin='FileStore.scope_repo_id==Repository.repo_id') - def __unicode__(self): - return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id, - safe_unicode(self.repo_name)) + + def __str__(self): + return "<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id, self.repo_name) @hybrid_property def description_safe(self): @@ -2012,7 +2010,7 @@ class Repository(Base, BaseModel): # names in the database, but that eventually needs to be converted # into a valid system path p += self.repo_name.split(self.NAME_SEP) - return os.path.join(*map(safe_unicode, p)) + return os.path.join(*map(safe_str, p)) @property def cache_keys(self): @@ -2699,8 +2697,8 @@ class RepoGroup(Base, BaseModel): self.group_name = group_name self.parent_group = parent_group - def __unicode__(self): - return u"<%s('id:%s:%s')>" % ( + def __str__(self): + return "<%s('id:%s:%s')>" % ( self.__class__.__name__, self.group_id, self.group_name) @hybrid_property @@ -3233,8 +3231,8 @@ class Permission(Base, BaseModel): permission_name = Column("permission_name", String(255), nullable=True, unique=None, default=None) permission_longname = Column("permission_longname", String(255), nullable=True, unique=None, default=None) - def __unicode__(self): - return u"<%s('%s:%s')>" % ( + def __str__(self): + return "<%s('%s:%s')>" % ( self.__class__.__name__, self.permission_id, self.permission_name ) @@ -3422,8 +3420,8 @@ class UserRepoToPerm(Base, BaseModel): Session().add(n) return n - def __unicode__(self): - return u'<%s => %s >' % (self.user, self.repository) + def __str__(self): + return f'<{self.user} => {self.repository} >' class UserUserGroupToPerm(Base, BaseModel): @@ -3451,8 +3449,8 @@ class UserUserGroupToPerm(Base, BaseMode Session().add(n) return n - def __unicode__(self): - return u'<%s => %s >' % (self.user, self.user_group) + def __str__(self): + return f'<{self.user} => {self.user_group} >' class UserToPerm(Base, BaseModel): @@ -3469,8 +3467,8 @@ class UserToPerm(Base, BaseModel): user = relationship('User') permission = relationship('Permission', lazy='joined') - def __unicode__(self): - return u'<%s => %s >' % (self.user, self.permission) + def __str__(self): + return f'<{self.user} => {self.permission} >' class UserGroupRepoToPerm(Base, BaseModel): @@ -3499,8 +3497,8 @@ class UserGroupRepoToPerm(Base, BaseMode Session().add(n) return n - def __unicode__(self): - return u' %s >' % (self.users_group, self.repository) + def __str__(self): + return ' %s >' % (self.users_group, self.repository) class UserGroupUserGroupToPerm(Base, BaseModel): @@ -3529,8 +3527,8 @@ class UserGroupUserGroupToPerm(Base, Bas Session().add(n) return n - def __unicode__(self): - return u' %s >' % (self.target_user_group, self.user_group) + def __str__(self): + return ' %s >' % (self.target_user_group, self.user_group) class UserGroupToPerm(Base, BaseModel): @@ -3599,8 +3597,8 @@ class UserGroupRepoGroupToPerm(Base, Bas Session().add(n) return n - def __unicode__(self): - return u' %s >' % (self.users_group, self.group) + def __str__(self): + return ' %s >' % (self.users_group, self.group) class Statistics(Base, BaseModel): @@ -3670,8 +3668,8 @@ class CacheKey(Base, BaseModel): # first key should be same for all entries, since all workers should share it self.cache_state_uid = cache_state_uid or self.generate_new_state_uid() - def __unicode__(self): - return u"<%s('%s:%s[%s]')>" % ( + def __str__(self): + return "<%s('%s:%s[%s]')>" % ( self.__class__.__name__, self.cache_id, self.cache_key, self.cache_active) @@ -3912,11 +3910,11 @@ class ChangesetComment(Base, BaseModel): if self.status_change: return self.status_change[0].status_lbl - def __repr__(self): + def __str__(self): if self.comment_id: - return '' % self.comment_id + return f'' else: - return '' % id(self) + return f'' def get_api_data(self): comment = self @@ -4009,11 +4007,9 @@ class ChangesetStatus(Base, BaseModel): comment = relationship('ChangesetComment', lazy='select') pull_request = relationship('PullRequest', lazy='select') - def __unicode__(self): - return u"<%s('%s[v%s]:%s')>" % ( - self.__class__.__name__, - self.status, self.version, self.author - ) + + def __str__(self): + return f"<{self.__class__.__name__}('{self.status}[v{self.version}]:{self.author}')>" @classmethod def get_status_lbl(cls, value): @@ -4143,7 +4139,7 @@ class _PullRequestBase(BaseModel): if len(parts) != 3: raise ValueError( 'Invalid reference format given: {}, expected X:Y:Z'.format(val)) - self._source_ref = safe_unicode(val) + self._source_ref = safe_str(val) _target_ref = Column('other_ref', Unicode(255), nullable=False) @@ -4157,7 +4153,7 @@ class _PullRequestBase(BaseModel): if len(parts) != 3: raise ValueError( 'Invalid reference format given: {}, expected X:Y:Z'.format(val)) - self._target_ref = safe_unicode(val) + self._target_ref = safe_str(val) @declared_attr def target_repo_id(cls): @@ -4290,7 +4286,7 @@ class _PullRequestBase(BaseModel): PullRequestModel().merge_status(pull_request) merge_state = { 'status': merge_status, - 'message': safe_unicode(msg), + 'message': safe_str(msg), } else: merge_state = {'status': 'not_available', @@ -4375,12 +4371,10 @@ class PullRequest(Base, _PullRequestBase pull_request_id = Column( 'pull_request_id', Integer(), nullable=False, primary_key=True) - def __repr__(self): + def __str__(self): if self.pull_request_id: - return '' % self.pull_request_id + return f'' else: - return '' % id(self) - reviewers = relationship('PullRequestReviewers', cascade="all, delete-orphan") statuses = relationship('ChangesetStatus', cascade="all, delete-orphan") comments = relationship('ChangesetComment', cascade="all, delete-orphan") @@ -4412,8 +4406,9 @@ class PullRequest(Base, _PullRequestBase raise AttributeError( '%s object has no attribute %s' % (self, item)) - def __repr__(self): - return '' % self.attrs.get('pull_request_id') + def __str__(self): + pr_id = self.attrs.get('pull_request_id') + return f'' def versions(self): return pull_request_obj.versions.order_by( @@ -4546,15 +4541,16 @@ class PullRequestVersion(Base, _PullRequ ForeignKey('pull_requests.pull_request_id'), nullable=False) pull_request = relationship('PullRequest') - def __repr__(self): + def __str__(self): if self.pull_request_version_id: - return '' % self.pull_request_version_id + return f'' else: - return '' % id(self) + return f'' @property def reviewers(self): return self.pull_request.reviewers + @property def reviewers(self): return self.pull_request.reviewers @@ -4648,9 +4644,8 @@ class PullRequestReviewers(Base, BaseMod return qry.all() - def __unicode__(self): - return u"<%s('id:%s')>" % (self.__class__.__name__, - self.pull_requests_reviewers_id) + def __str__(self): + return f"<{self.__class__.__name__}('id:{self.pull_requests_reviewers_id}')>" class Notification(Base, BaseModel): @@ -4821,8 +4816,8 @@ class Gist(Base, BaseModel): owner = relationship('User') - def __repr__(self): - return '' % (self.gist_type, self.gist_access_id) + def __str__(self): + return f'' @hybrid_property def description_safe(self): @@ -5005,7 +5000,7 @@ class Integration(Base, BaseModel): return 'root_repos' return 'global' - def __repr__(self): + def __str__(self): return '' % (self.integration_type, self.scope) @@ -5248,7 +5243,7 @@ class RepoReviewRule(Base, BaseModel): rules.append(user_group) return rules - def __repr__(self): + def __str__(self): return '' % ( self.repo_review_rule_id, self.repo) @@ -5366,7 +5361,7 @@ class ScheduleEntry(Base, BaseModel): def kwargs_raw(self, indent=None): return self._as_raw(self.task_kwargs, indent) - def __repr__(self): + def __str__(self): return ''.format( self.schedule_entry_id, self.schedule_name) @@ -5442,9 +5437,8 @@ class UserToRepoBranchPermission(Base, _ _branch_pattern = Column('branch_pattern', UnicodeText().with_variant(UnicodeText(2048), 'mysql'), default=u'*') # glob _branch_hash = Column('branch_hash', UnicodeText().with_variant(UnicodeText(2048), 'mysql')) - def __unicode__(self): - return u' %r)>' % ( - self.user_repo_to_perm, self.branch_pattern) + def __str__(self): + return f' {self.branch_pattern!r})>' class UserGroupToRepoBranchPermission(Base, _BaseBranchPerms): @@ -5468,9 +5462,8 @@ class UserGroupToRepoBranchPermission(Ba _branch_pattern = Column('branch_pattern', UnicodeText().with_variant(UnicodeText(2048), 'mysql'), default=u'*') # glob _branch_hash = Column('branch_hash', UnicodeText().with_variant(UnicodeText(2048), 'mysql')) - def __unicode__(self): - return u' %r)>' % ( - self.user_group_repo_to_perm, self.branch_pattern) + def __str__(self): + return f' {self.branch_pattern!r})>' class UserBookmark(Base, BaseModel): @@ -5518,8 +5511,8 @@ class UserBookmark(Base, BaseModel): return bookmarks.all() - def __unicode__(self): - return u'' % (self.position, self.redirect_url) + def __str__(self): + return f'' class FileStore(Base, BaseModel): @@ -5672,8 +5665,8 @@ class FileStore(Base, BaseModel): return data - def __repr__(self): - return ''.format(self.file_store_id) + def __str__(self): + return f'' class FileStoreMetadata(Base, BaseModel): @@ -5687,7 +5680,7 @@ class FileStoreMetadata(Base, BaseModel) SETTINGS_TYPES = { 'str': safe_str, 'int': safe_int, - 'unicode': safe_unicode, + 'unicode': safe_str, 'bool': str2bool, 'list': functools.partial(aslist, sep=',') } @@ -5753,7 +5746,7 @@ class FileStoreMetadata(Base, BaseModel) # decode the encrypted value if it's encrypted field type if '.encrypted' in self._file_store_meta_value_type: cipher = EncryptedTextValue() - val = safe_unicode(cipher.process_result_value(val, None)) + val = safe_str(cipher.process_result_value(val, None)) # do final type conversion converter = self.SETTINGS_TYPES.get(_type) or self.SETTINGS_TYPES['unicode'] val = converter(val) @@ -5762,11 +5755,11 @@ class FileStoreMetadata(Base, BaseModel) @file_store_meta_value.setter def file_store_meta_value(self, val): - val = safe_unicode(val) + val = safe_str(val) # encode the encrypted value if '.encrypted' in self.file_store_meta_value_type: cipher = EncryptedTextValue() - val = safe_unicode(cipher.process_bind_param(val, None)) + val = safe_str(cipher.process_bind_param(val, None)) self._file_store_meta_value = val @hybrid_property @@ -5789,7 +5782,7 @@ class FileStoreMetadata(Base, BaseModel) return data - def __repr__(self): + def __str__(self): return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.file_store_meta_section, self.file_store_meta_key, self.file_store_meta_value) @@ -5820,8 +5813,8 @@ class DbSession(Base, BaseModel): base_table_args, ) - def __repr__(self): - return ''.format(self.id) + def __str__(self): + return f'' id = Column('id', Integer()) namespace = Column('namespace', String(255), primary_key=True)