##// END OF EJS Templates
db-models: use consistent __str__ for all models, removed safe_unicode calls
super-admin -
r5010:bed423f3 default
parent child Browse files
Show More
@@ -53,12 +53,13 b' from zope.cachedescriptors.property impo'
53 53 from pyramid.threadlocal import get_current_request
54 54 from webhelpers2.text import remove_formatting
55 55
56 from rhodecode.lib.str_utils import safe_bytes
56 57 from rhodecode.translation import _
57 58 from rhodecode.lib.vcs import get_vcs_instance, VCSError
58 59 from rhodecode.lib.vcs.backends.base import (
59 60 EmptyCommit, Reference, unicode_to_reference, reference_to_unicode)
60 61 from rhodecode.lib.utils2 import (
61 str2bool, safe_str, get_commit_safe, safe_unicode, sha1_safe,
62 str2bool, safe_str, get_commit_safe, sha1_safe,
62 63 time_to_datetime, aslist, Optional, safe_int, get_clone_url, AttributeDict,
63 64 glob2re, StrictAttributeDict, cleaned_uri, datetime_to_time)
64 65 from rhodecode.lib.jsonalchemy import MutationObj, MutationList, JsonType, \
@@ -316,7 +317,7 b' class RhodeCodeSetting(Base, BaseModel):'
316 317 SETTINGS_TYPES = {
317 318 'str': safe_str,
318 319 'int': safe_int,
319 'unicode': safe_unicode,
320 'unicode': safe_str,
320 321 'bool': str2bool,
321 322 'list': functools.partial(aslist, sep=',')
322 323 }
@@ -360,7 +361,7 b' class RhodeCodeSetting(Base, BaseModel):'
360 361
361 362 :param val:
362 363 """
363 val = safe_unicode(val)
364 val = safe_str(val)
364 365 # encode the encrypted value
365 366 if 'encrypted' in self.app_settings_type:
366 367 cipher = EncryptedTextValue()
@@ -436,7 +437,7 b' class RhodeCodeUi(Base, BaseModel):'
436 437 ui_active = Column(
437 438 "ui_active", Boolean(), nullable=True, unique=None, default=True)
438 439
439 def __repr__(self):
440 def __str__(self):
440 441 return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.ui_section,
441 442 self.ui_key, self.ui_value)
442 443
@@ -494,7 +495,7 b' class RepoRhodeCodeSetting(Base, BaseMod'
494 495
495 496 :param val:
496 497 """
497 self._app_settings_value = safe_unicode(val)
498 self._app_settings_value = safe_str(val)
498 499
499 500 @hybrid_property
500 501 def app_settings_type(self):
@@ -508,8 +509,8 b' class RepoRhodeCodeSetting(Base, BaseMod'
508 509 % (SETTINGS_TYPES.keys(), val))
509 510 self._app_settings_type = val
510 511
511 def __unicode__(self):
512 return u"<%s('%s:%s:%s[%s]')>" % (
512 def __str__(self):
513 return "<%s('%s:%s:%s[%s]')>" % (
513 514 self.__class__.__name__, self.repository.repo_name,
514 515 self.app_settings_name, self.app_settings_value,
515 516 self.app_settings_type
@@ -542,7 +543,7 b' class RepoRhodeCodeUi(Base, BaseModel):'
542 543
543 544 repository = relationship('Repository')
544 545
545 def __repr__(self):
546 def __str__(self):
546 547 return '<%s[%s:%s]%s=>%s]>' % (
547 548 self.__class__.__name__, self.repository.repo_name,
548 549 self.ui_section, self.ui_key, self.ui_value)
@@ -626,9 +627,9 b' class User(Base, BaseModel):'
626 627 # no cascade, set NULL
627 628 scope_artifacts = relationship('FileStore', primaryjoin='FileStore.scope_user_id==User.user_id')
628 629
629 def __unicode__(self):
630 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
631 self.user_id, self.username)
630
631 def __str__(self):
632 return f"<{self.__class__.__name__}('id:{self.user_id}:{self.username}')>"
632 633
633 634 @hybrid_property
634 635 def email(self):
@@ -1177,8 +1178,8 b' class UserApiKeys(Base, BaseModel):'
1177 1178
1178 1179 user = relationship('User', lazy='joined')
1179 1180
1180 def __unicode__(self):
1181 return u"<%s('%s')>" % (self.__class__.__name__, self.role)
1181 def __str__(self):
1182 return f"<{self.__class__.__name__}('{self.role}')>"
1182 1183
1183 1184 def __json__(self):
1184 1185 data = {
@@ -1318,9 +1319,8 b' class UserIpMap(Base, BaseModel):'
1318 1319 'ip_range': self._get_ip_range(self.ip_addr),
1319 1320 }
1320 1321
1321 def __unicode__(self):
1322 return u"<%s('user_id:%s=>%s')>" % (self.__class__.__name__,
1323 self.user_id, self.ip_addr)
1322 def __str__(self):
1323 return "<%s('user_id:%s=>%s')>" % (self.__class__.__name__, self.user_id, self.ip_addr)
1324 1324
1325 1325
1326 1326 class UserSshKeys(Base, BaseModel):
@@ -1382,9 +1382,9 b' class UserLog(Base, BaseModel):'
1382 1382 user_data = Column('user_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT()))))
1383 1383 action_data = Column('action_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT()))))
1384 1384
1385 def __unicode__(self):
1386 return u"<%s('id:%s:%s')>" % (
1387 self.__class__.__name__, self.repository_name, self.action)
1385
1386 def __str__(self):
1387 return f"<{self.__class__.__name__}('id:{self.repository_name}:{self.action}')>"
1388 1388
1389 1389 def __json__(self):
1390 1390 return {
@@ -1473,10 +1473,8 b' class UserGroup(Base, BaseModel):'
1473 1473 def sync(self):
1474 1474 return self._load_sync(self.group_data)
1475 1475
1476 def __unicode__(self):
1477 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
1478 self.users_group_id,
1479 self.users_group_name)
1476 def __str__(self):
1477 return f"<{self.__class__.__name__}('id:{self.users_group_id}:{self.users_group_name}')>"
1480 1478
1481 1479 @classmethod
1482 1480 def get_by_group_name(cls, group_name, cache=False,
@@ -1783,9 +1781,9 b' class Repository(Base, BaseModel):'
1783 1781 # no cascade, set NULL
1784 1782 artifacts = relationship('FileStore', primaryjoin='FileStore.scope_repo_id==Repository.repo_id')
1785 1783
1786 def __unicode__(self):
1787 return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id,
1788 safe_unicode(self.repo_name))
1784
1785 def __str__(self):
1786 return "<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id, self.repo_name)
1789 1787
1790 1788 @hybrid_property
1791 1789 def description_safe(self):
@@ -2012,7 +2010,7 b' class Repository(Base, BaseModel):'
2012 2010 # names in the database, but that eventually needs to be converted
2013 2011 # into a valid system path
2014 2012 p += self.repo_name.split(self.NAME_SEP)
2015 return os.path.join(*map(safe_unicode, p))
2013 return os.path.join(*map(safe_str, p))
2016 2014
2017 2015 @property
2018 2016 def cache_keys(self):
@@ -2699,8 +2697,8 b' class RepoGroup(Base, BaseModel):'
2699 2697 self.group_name = group_name
2700 2698 self.parent_group = parent_group
2701 2699
2702 def __unicode__(self):
2703 return u"<%s('id:%s:%s')>" % (
2700 def __str__(self):
2701 return "<%s('id:%s:%s')>" % (
2704 2702 self.__class__.__name__, self.group_id, self.group_name)
2705 2703
2706 2704 @hybrid_property
@@ -3233,8 +3231,8 b' class Permission(Base, BaseModel):'
3233 3231 permission_name = Column("permission_name", String(255), nullable=True, unique=None, default=None)
3234 3232 permission_longname = Column("permission_longname", String(255), nullable=True, unique=None, default=None)
3235 3233
3236 def __unicode__(self):
3237 return u"<%s('%s:%s')>" % (
3234 def __str__(self):
3235 return "<%s('%s:%s')>" % (
3238 3236 self.__class__.__name__, self.permission_id, self.permission_name
3239 3237 )
3240 3238
@@ -3422,8 +3420,8 b' class UserRepoToPerm(Base, BaseModel):'
3422 3420 Session().add(n)
3423 3421 return n
3424 3422
3425 def __unicode__(self):
3426 return u'<%s => %s >' % (self.user, self.repository)
3423 def __str__(self):
3424 return f'<{self.user} => {self.repository} >'
3427 3425
3428 3426
3429 3427 class UserUserGroupToPerm(Base, BaseModel):
@@ -3451,8 +3449,8 b' class UserUserGroupToPerm(Base, BaseMode'
3451 3449 Session().add(n)
3452 3450 return n
3453 3451
3454 def __unicode__(self):
3455 return u'<%s => %s >' % (self.user, self.user_group)
3452 def __str__(self):
3453 return f'<{self.user} => {self.user_group} >'
3456 3454
3457 3455
3458 3456 class UserToPerm(Base, BaseModel):
@@ -3469,8 +3467,8 b' class UserToPerm(Base, BaseModel):'
3469 3467 user = relationship('User')
3470 3468 permission = relationship('Permission', lazy='joined')
3471 3469
3472 def __unicode__(self):
3473 return u'<%s => %s >' % (self.user, self.permission)
3470 def __str__(self):
3471 return f'<{self.user} => {self.permission} >'
3474 3472
3475 3473
3476 3474 class UserGroupRepoToPerm(Base, BaseModel):
@@ -3499,8 +3497,8 b' class UserGroupRepoToPerm(Base, BaseMode'
3499 3497 Session().add(n)
3500 3498 return n
3501 3499
3502 def __unicode__(self):
3503 return u'<UserGroupRepoToPerm:%s => %s >' % (self.users_group, self.repository)
3500 def __str__(self):
3501 return '<UserGroupRepoToPerm:%s => %s >' % (self.users_group, self.repository)
3504 3502
3505 3503
3506 3504 class UserGroupUserGroupToPerm(Base, BaseModel):
@@ -3529,8 +3527,8 b' class UserGroupUserGroupToPerm(Base, Bas'
3529 3527 Session().add(n)
3530 3528 return n
3531 3529
3532 def __unicode__(self):
3533 return u'<UserGroupUserGroup:%s => %s >' % (self.target_user_group, self.user_group)
3530 def __str__(self):
3531 return '<UserGroupUserGroup:%s => %s >' % (self.target_user_group, self.user_group)
3534 3532
3535 3533
3536 3534 class UserGroupToPerm(Base, BaseModel):
@@ -3599,8 +3597,8 b' class UserGroupRepoGroupToPerm(Base, Bas'
3599 3597 Session().add(n)
3600 3598 return n
3601 3599
3602 def __unicode__(self):
3603 return u'<UserGroupRepoGroupToPerm:%s => %s >' % (self.users_group, self.group)
3600 def __str__(self):
3601 return '<UserGroupRepoGroupToPerm:%s => %s >' % (self.users_group, self.group)
3604 3602
3605 3603
3606 3604 class Statistics(Base, BaseModel):
@@ -3670,8 +3668,8 b' class CacheKey(Base, BaseModel):'
3670 3668 # first key should be same for all entries, since all workers should share it
3671 3669 self.cache_state_uid = cache_state_uid or self.generate_new_state_uid()
3672 3670
3673 def __unicode__(self):
3674 return u"<%s('%s:%s[%s]')>" % (
3671 def __str__(self):
3672 return "<%s('%s:%s[%s]')>" % (
3675 3673 self.__class__.__name__,
3676 3674 self.cache_id, self.cache_key, self.cache_active)
3677 3675
@@ -3912,11 +3910,11 b' class ChangesetComment(Base, BaseModel):'
3912 3910 if self.status_change:
3913 3911 return self.status_change[0].status_lbl
3914 3912
3915 def __repr__(self):
3913 def __str__(self):
3916 3914 if self.comment_id:
3917 return '<DB:Comment #%s>' % self.comment_id
3915 return f'<DB:Comment #{self.comment_id}>'
3918 3916 else:
3919 return '<DB:Comment at %#x>' % id(self)
3917 return f'<DB:Comment at {id(self)!r}>'
3920 3918
3921 3919 def get_api_data(self):
3922 3920 comment = self
@@ -4009,11 +4007,9 b' class ChangesetStatus(Base, BaseModel):'
4009 4007 comment = relationship('ChangesetComment', lazy='select')
4010 4008 pull_request = relationship('PullRequest', lazy='select')
4011 4009
4012 def __unicode__(self):
4013 return u"<%s('%s[v%s]:%s')>" % (
4014 self.__class__.__name__,
4015 self.status, self.version, self.author
4016 )
4010
4011 def __str__(self):
4012 return f"<{self.__class__.__name__}('{self.status}[v{self.version}]:{self.author}')>"
4017 4013
4018 4014 @classmethod
4019 4015 def get_status_lbl(cls, value):
@@ -4143,7 +4139,7 b' class _PullRequestBase(BaseModel):'
4143 4139 if len(parts) != 3:
4144 4140 raise ValueError(
4145 4141 'Invalid reference format given: {}, expected X:Y:Z'.format(val))
4146 self._source_ref = safe_unicode(val)
4142 self._source_ref = safe_str(val)
4147 4143
4148 4144 _target_ref = Column('other_ref', Unicode(255), nullable=False)
4149 4145
@@ -4157,7 +4153,7 b' class _PullRequestBase(BaseModel):'
4157 4153 if len(parts) != 3:
4158 4154 raise ValueError(
4159 4155 'Invalid reference format given: {}, expected X:Y:Z'.format(val))
4160 self._target_ref = safe_unicode(val)
4156 self._target_ref = safe_str(val)
4161 4157
4162 4158 @declared_attr
4163 4159 def target_repo_id(cls):
@@ -4290,7 +4286,7 b' class _PullRequestBase(BaseModel):'
4290 4286 PullRequestModel().merge_status(pull_request)
4291 4287 merge_state = {
4292 4288 'status': merge_status,
4293 'message': safe_unicode(msg),
4289 'message': safe_str(msg),
4294 4290 }
4295 4291 else:
4296 4292 merge_state = {'status': 'not_available',
@@ -4375,12 +4371,10 b' class PullRequest(Base, _PullRequestBase'
4375 4371 pull_request_id = Column(
4376 4372 'pull_request_id', Integer(), nullable=False, primary_key=True)
4377 4373
4378 def __repr__(self):
4374 def __str__(self):
4379 4375 if self.pull_request_id:
4380 return '<DB:PullRequest #%s>' % self.pull_request_id
4376 return f'<DB:PullRequest #{self.pull_request_id}>'
4381 4377 else:
4382 return '<DB:PullRequest at %#x>' % id(self)
4383
4384 4378 reviewers = relationship('PullRequestReviewers', cascade="all, delete-orphan")
4385 4379 statuses = relationship('ChangesetStatus', cascade="all, delete-orphan")
4386 4380 comments = relationship('ChangesetComment', cascade="all, delete-orphan")
@@ -4412,8 +4406,9 b' class PullRequest(Base, _PullRequestBase'
4412 4406 raise AttributeError(
4413 4407 '%s object has no attribute %s' % (self, item))
4414 4408
4415 def __repr__(self):
4416 return '<DB:PullRequestDisplay #%s>' % self.attrs.get('pull_request_id')
4409 def __str__(self):
4410 pr_id = self.attrs.get('pull_request_id')
4411 return f'<DB:PullRequestDisplay #{pr_id}>'
4417 4412
4418 4413 def versions(self):
4419 4414 return pull_request_obj.versions.order_by(
@@ -4546,15 +4541,16 b' class PullRequestVersion(Base, _PullRequ'
4546 4541 ForeignKey('pull_requests.pull_request_id'), nullable=False)
4547 4542 pull_request = relationship('PullRequest')
4548 4543
4549 def __repr__(self):
4544 def __str__(self):
4550 4545 if self.pull_request_version_id:
4551 return '<DB:PullRequestVersion #%s>' % self.pull_request_version_id
4546 return f'<DB:PullRequestVersion #{self.pull_request_version_id}>'
4552 4547 else:
4553 return '<DB:PullRequestVersion at %#x>' % id(self)
4548 return f'<DB:PullRequestVersion at {id(self)!r}>'
4554 4549
4555 4550 @property
4556 4551 def reviewers(self):
4557 4552 return self.pull_request.reviewers
4553
4558 4554 @property
4559 4555 def reviewers(self):
4560 4556 return self.pull_request.reviewers
@@ -4648,9 +4644,8 b' class PullRequestReviewers(Base, BaseMod'
4648 4644
4649 4645 return qry.all()
4650 4646
4651 def __unicode__(self):
4652 return u"<%s('id:%s')>" % (self.__class__.__name__,
4653 self.pull_requests_reviewers_id)
4647 def __str__(self):
4648 return f"<{self.__class__.__name__}('id:{self.pull_requests_reviewers_id}')>"
4654 4649
4655 4650
4656 4651 class Notification(Base, BaseModel):
@@ -4821,8 +4816,8 b' class Gist(Base, BaseModel):'
4821 4816
4822 4817 owner = relationship('User')
4823 4818
4824 def __repr__(self):
4825 return '<Gist:[%s]%s>' % (self.gist_type, self.gist_access_id)
4819 def __str__(self):
4820 return f'<Gist:[{self.gist_type}]{self.gist_access_id}>'
4826 4821
4827 4822 @hybrid_property
4828 4823 def description_safe(self):
@@ -5005,7 +5000,7 b' class Integration(Base, BaseModel):'
5005 5000 return 'root_repos'
5006 5001 return 'global'
5007 5002
5008 def __repr__(self):
5003 def __str__(self):
5009 5004 return '<Integration(%r, %r)>' % (self.integration_type, self.scope)
5010 5005
5011 5006
@@ -5248,7 +5243,7 b' class RepoReviewRule(Base, BaseModel):'
5248 5243 rules.append(user_group)
5249 5244 return rules
5250 5245
5251 def __repr__(self):
5246 def __str__(self):
5252 5247 return '<RepoReviewerRule(id=%r, repo=%r)>' % (
5253 5248 self.repo_review_rule_id, self.repo)
5254 5249
@@ -5366,7 +5361,7 b' class ScheduleEntry(Base, BaseModel):'
5366 5361 def kwargs_raw(self, indent=None):
5367 5362 return self._as_raw(self.task_kwargs, indent)
5368 5363
5369 def __repr__(self):
5364 def __str__(self):
5370 5365 return '<DB:ScheduleEntry({}:{})>'.format(
5371 5366 self.schedule_entry_id, self.schedule_name)
5372 5367
@@ -5442,9 +5437,8 b' class UserToRepoBranchPermission(Base, _'
5442 5437 _branch_pattern = Column('branch_pattern', UnicodeText().with_variant(UnicodeText(2048), 'mysql'), default=u'*') # glob
5443 5438 _branch_hash = Column('branch_hash', UnicodeText().with_variant(UnicodeText(2048), 'mysql'))
5444 5439
5445 def __unicode__(self):
5446 return u'<UserBranchPermission(%s => %r)>' % (
5447 self.user_repo_to_perm, self.branch_pattern)
5440 def __str__(self):
5441 return f'<UserBranchPermission({self.user_repo_to_perm} => {self.branch_pattern!r})>'
5448 5442
5449 5443
5450 5444 class UserGroupToRepoBranchPermission(Base, _BaseBranchPerms):
@@ -5468,9 +5462,8 b' class UserGroupToRepoBranchPermission(Ba'
5468 5462 _branch_pattern = Column('branch_pattern', UnicodeText().with_variant(UnicodeText(2048), 'mysql'), default=u'*') # glob
5469 5463 _branch_hash = Column('branch_hash', UnicodeText().with_variant(UnicodeText(2048), 'mysql'))
5470 5464
5471 def __unicode__(self):
5472 return u'<UserBranchPermission(%s => %r)>' % (
5473 self.user_group_repo_to_perm, self.branch_pattern)
5465 def __str__(self):
5466 return f'<UserBranchPermission({self.user_group_repo_to_perm} => {self.branch_pattern!r})>'
5474 5467
5475 5468
5476 5469 class UserBookmark(Base, BaseModel):
@@ -5518,8 +5511,8 b' class UserBookmark(Base, BaseModel):'
5518 5511
5519 5512 return bookmarks.all()
5520 5513
5521 def __unicode__(self):
5522 return u'<UserBookmark(%s @ %r)>' % (self.position, self.redirect_url)
5514 def __str__(self):
5515 return f'<UserBookmark({self.position} @ {self.redirect_url!r})>'
5523 5516
5524 5517
5525 5518 class FileStore(Base, BaseModel):
@@ -5672,8 +5665,8 b' class FileStore(Base, BaseModel):'
5672 5665
5673 5666 return data
5674 5667
5675 def __repr__(self):
5676 return '<FileStore({})>'.format(self.file_store_id)
5668 def __str__(self):
5669 return f'<FileStore({self.file_store_id})>'
5677 5670
5678 5671
5679 5672 class FileStoreMetadata(Base, BaseModel):
@@ -5687,7 +5680,7 b' class FileStoreMetadata(Base, BaseModel)'
5687 5680 SETTINGS_TYPES = {
5688 5681 'str': safe_str,
5689 5682 'int': safe_int,
5690 'unicode': safe_unicode,
5683 'unicode': safe_str,
5691 5684 'bool': str2bool,
5692 5685 'list': functools.partial(aslist, sep=',')
5693 5686 }
@@ -5753,7 +5746,7 b' class FileStoreMetadata(Base, BaseModel)'
5753 5746 # decode the encrypted value if it's encrypted field type
5754 5747 if '.encrypted' in self._file_store_meta_value_type:
5755 5748 cipher = EncryptedTextValue()
5756 val = safe_unicode(cipher.process_result_value(val, None))
5749 val = safe_str(cipher.process_result_value(val, None))
5757 5750 # do final type conversion
5758 5751 converter = self.SETTINGS_TYPES.get(_type) or self.SETTINGS_TYPES['unicode']
5759 5752 val = converter(val)
@@ -5762,11 +5755,11 b' class FileStoreMetadata(Base, BaseModel)'
5762 5755
5763 5756 @file_store_meta_value.setter
5764 5757 def file_store_meta_value(self, val):
5765 val = safe_unicode(val)
5758 val = safe_str(val)
5766 5759 # encode the encrypted value
5767 5760 if '.encrypted' in self.file_store_meta_value_type:
5768 5761 cipher = EncryptedTextValue()
5769 val = safe_unicode(cipher.process_bind_param(val, None))
5762 val = safe_str(cipher.process_bind_param(val, None))
5770 5763 self._file_store_meta_value = val
5771 5764
5772 5765 @hybrid_property
@@ -5789,7 +5782,7 b' class FileStoreMetadata(Base, BaseModel)'
5789 5782
5790 5783 return data
5791 5784
5792 def __repr__(self):
5785 def __str__(self):
5793 5786 return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.file_store_meta_section,
5794 5787 self.file_store_meta_key, self.file_store_meta_value)
5795 5788
@@ -5820,8 +5813,8 b' class DbSession(Base, BaseModel):'
5820 5813 base_table_args,
5821 5814 )
5822 5815
5823 def __repr__(self):
5824 return '<DB:DbSession({})>'.format(self.id)
5816 def __str__(self):
5817 return f'<DB:DbSession({self.id})>'
5825 5818
5826 5819 id = Column('id', Integer())
5827 5820 namespace = Column('namespace', String(255), primary_key=True)
General Comments 0
You need to be logged in to leave comments. Login now