# HG changeset patch # User Mads Kiilerich # Date 1437748660 -7200 # Node ID 63bed817308c7cd9829fede0036ef2a640a744bb # Parent 72e0fe2e3278ca20749157c6c0d0e10c1fefd2ab cleanup: check for None object identity in cases where that is what the 'contract' says Avoid applying bool() on complex objects - it might return something unexpected such as the key (which might be 0 and thus be false). Checking for None is more safe and faster. diff -r 72e0fe2e3278 -r 63bed817308c kallithea/controllers/admin/auth_settings.py --- a/kallithea/controllers/admin/auth_settings.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/controllers/admin/auth_settings.py Fri Jul 24 16:37:40 2015 +0200 @@ -76,7 +76,7 @@ c.defaults[fullname] = v["default"] # Current values will be the default on the form, if there are any setting = Setting.get_by_name(fullname) - if setting: + if setting is not None: c.defaults[fullname] = setting.app_settings_value # we want to show , separated list of enabled plugins c.defaults['auth_plugins'] = ','.join(c.enabled_plugins) diff -r 72e0fe2e3278 -r 63bed817308c kallithea/controllers/admin/my_account.py --- a/kallithea/controllers/admin/my_account.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/controllers/admin/my_account.py Fri Jul 24 16:37:40 2015 +0200 @@ -259,7 +259,7 @@ user_id = self.authuser.user_id if request.POST.get('del_api_key_builtin'): user = User.get(user_id) - if user: + if user is not None: user.api_key = generate_api_key() Session().add(user) Session().commit() diff -r 72e0fe2e3278 -r 63bed817308c kallithea/controllers/admin/notifications.py --- a/kallithea/controllers/admin/notifications.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/controllers/admin/notifications.py Fri Jul 24 16:37:40 2015 +0200 @@ -160,7 +160,7 @@ # if this association to user is not valid, we don't want to show # this message - if unotification: + if unotification is not None: if not unotification.read: unotification.mark_as_read() Session().commit() diff -r 72e0fe2e3278 -r 63bed817308c kallithea/controllers/admin/repos.py --- a/kallithea/controllers/admin/repos.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/controllers/admin/repos.py Fri Jul 24 16:37:40 2015 +0200 @@ -217,7 +217,7 @@ h.url('summary_home', repo_name=repo.repo_name)) fork = repo.fork - if fork: + if fork is not None: fork_name = fork.repo_name h.flash(h.literal(_('Forked repository %s as %s') % (fork_name, repo_url)), category='success') diff -r 72e0fe2e3278 -r 63bed817308c kallithea/controllers/admin/users.py --- a/kallithea/controllers/admin/users.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/controllers/admin/users.py Fri Jul 24 16:37:40 2015 +0200 @@ -316,7 +316,7 @@ api_key = request.POST.get('del_api_key') if request.POST.get('del_api_key_builtin'): user = User.get(c.user.user_id) - if user: + if user is not None: user.api_key = generate_api_key() Session().add(user) Session().commit() diff -r 72e0fe2e3278 -r 63bed817308c kallithea/controllers/api/api.py --- a/kallithea/controllers/api/api.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/controllers/api/api.py Fri Jul 24 16:37:40 2015 +0200 @@ -463,7 +463,7 @@ if time_: _api_data = r.get_api_data() # if we use userfilter just show the locks for this user - if user: + if user is not None: if safe_int(userid) == user.user_id: ret.append(_api_data) else: diff -r 72e0fe2e3278 -r 63bed817308c kallithea/controllers/changeset.py --- a/kallithea/controllers/changeset.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/controllers/changeset.py Fri Jul 24 16:37:40 2015 +0200 @@ -398,7 +398,7 @@ data = { 'target_id': h.safeid(h.safe_unicode(request.POST.get('f_path'))), } - if comment: + if comment is not None: data.update(comment.get_dict()) data.update({'rendered_text': render('changeset/changeset_comment_block.html')}) diff -r 72e0fe2e3278 -r 63bed817308c kallithea/controllers/pullrequests.py --- a/kallithea/controllers/pullrequests.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/controllers/pullrequests.py Fri Jul 24 16:37:40 2015 +0200 @@ -751,7 +751,7 @@ data = { 'target_id': h.safeid(h.safe_unicode(request.POST.get('f_path'))), } - if comment: + if comment is not None: c.comment = comment data.update(comment.get_dict()) data.update({'rendered_text': diff -r 72e0fe2e3278 -r 63bed817308c kallithea/model/__init__.py --- a/kallithea/model/__init__.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/model/__init__.py Fri Jul 24 16:37:40 2015 +0200 @@ -94,7 +94,7 @@ elif isinstance(instance, (int, long)) or safe_str(instance).isdigit(): return cls.get(instance) else: - if instance: + if instance is not None: if callback is None: raise Exception( 'given object must be int, long or Instance of %s ' diff -r 72e0fe2e3278 -r 63bed817308c kallithea/model/api_key.py --- a/kallithea/model/api_key.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/model/api_key.py Fri Jul 24 16:37:40 2015 +0200 @@ -65,7 +65,7 @@ """ api_key = UserApiKeys.query().filter(UserApiKeys.api_key == api_key) - if user: + if user is not None: user = self._get_user(user) api_key = api_key.filter(UserApiKeys.user_id == user.user_id) diff -r 72e0fe2e3278 -r 63bed817308c kallithea/model/comment.py --- a/kallithea/model/comment.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/model/comment.py Fri Jul 24 16:37:40 2015 +0200 @@ -181,9 +181,9 @@ comment.f_path = f_path comment.line_no = line_no - if revision: + if revision is not None: comment.revision = revision - elif pull_request: + elif pull_request is not None: pull_request = self.__get_pull_request(pull_request) comment.pull_request = pull_request else: diff -r 72e0fe2e3278 -r 63bed817308c kallithea/model/db.py --- a/kallithea/model/db.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/model/db.py Fri Jul 24 16:37:40 2015 +0200 @@ -130,7 +130,7 @@ raise HTTPNotFound res = cls.query().get(id_) - if not res: + if res is None: raise HTTPNotFound return res @@ -232,7 +232,7 @@ @classmethod def get_by_name_or_create(cls, key, val='', type='unicode'): res = cls.get_by_name(key) - if not res: + if res is None: res = cls(key, val, type) return res @@ -248,7 +248,7 @@ :return: """ res = cls.get_by_name(key) - if not res: + if res is None: val = Optional.extract(val) type = Optional.extract(type) res = cls(key, val, type) @@ -270,7 +270,7 @@ if cache: ret = ret.options(FromCache("sql_cache_short", "get_hg_settings")) - if not ret: + if ret is None: raise Exception('Could not get application settings !') settings = {} for each in ret: @@ -624,12 +624,12 @@ _email = email(author) if _email: user = cls.get_by_email(_email, case_insensitive=True) - if user: + if user is not None: return user # Maybe we can match by username? _author = author_name(author) user = cls.get_by_username(_author, case_insensitive=True) - if user: + if user is not None: return user def update_lastlogin(self): @@ -2488,7 +2488,7 @@ @classmethod def get_or_404(cls, id_): res = cls.query().filter(cls.gist_access_id == id_).scalar() - if not res: + if res is None: raise HTTPNotFound return res diff -r 72e0fe2e3278 -r 63bed817308c kallithea/model/notification.py --- a/kallithea/model/notification.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/model/notification.py Fri Jul 24 16:37:40 2015 +0200 @@ -53,7 +53,7 @@ elif isinstance(notification, (int, long)): return Notification.get(notification) else: - if notification: + if notification is not None: raise Exception('notification must be int, long or Instance' ' of Notification got %s' % type(notification)) @@ -85,7 +85,7 @@ if recipients: for u in recipients: obj = self._get_user(u) - if obj: + if obj is not None: recipients_objs.append(obj) else: # TODO: inform user that requested operation couldn't be completed diff -r 72e0fe2e3278 -r 63bed817308c kallithea/model/repo.py --- a/kallithea/model/repo.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/model/repo.py Fri Jul 24 16:37:40 2015 +0200 @@ -535,7 +535,7 @@ if not cur_user: cur_user = getattr(get_current_authuser(), 'username', None) repo = self._get_repo(repo) - if repo: + if repo is not None: if forks == 'detach': for r in repo.forks: r.fork = None @@ -602,7 +602,7 @@ .filter(UserRepoToPerm.repository == repo) \ .filter(UserRepoToPerm.user == user) \ .scalar() - if obj: + if obj is not None: self.sa.delete(obj) log.debug('Revoked perm on %s on %s' % (repo, user)) @@ -652,7 +652,7 @@ .filter(UserGroupRepoToPerm.repository == repo) \ .filter(UserGroupRepoToPerm.users_group == group_name) \ .scalar() - if obj: + if obj is not None: self.sa.delete(obj) log.debug('Revoked perm to %s on %s' % (repo, group_name)) @@ -666,7 +666,7 @@ try: obj = self.sa.query(Statistics) \ .filter(Statistics.repository == repo).scalar() - if obj: + if obj is not None: self.sa.delete(obj) except Exception: log.error(traceback.format_exc()) diff -r 72e0fe2e3278 -r 63bed817308c kallithea/model/repo_group.py --- a/kallithea/model/repo_group.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/model/repo_group.py Fri Jul 24 16:37:40 2015 +0200 @@ -485,7 +485,7 @@ .filter(UserRepoGroupToPerm.user == user)\ .filter(UserRepoGroupToPerm.group == repo_group)\ .scalar() - if obj: + if obj is not None: self.sa.delete(obj) log.debug('Revoked perm on %s on %s' % (repo_group, user)) @@ -537,6 +537,6 @@ .filter(UserGroupRepoGroupToPerm.group == repo_group)\ .filter(UserGroupRepoGroupToPerm.users_group == group_name)\ .scalar() - if obj: + if obj is not None: self.sa.delete(obj) log.debug('Revoked perm to %s on %s' % (repo_group, group_name)) diff -r 72e0fe2e3278 -r 63bed817308c kallithea/model/scm.py --- a/kallithea/model/scm.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/model/scm.py Fri Jul 24 16:37:40 2015 +0200 @@ -247,7 +247,7 @@ return cls.get(instance) elif isinstance(instance, basestring): return cls.get_by_repo_name(instance) - elif instance: + elif instance is not None: raise Exception('given object must be int, basestr or Instance' ' of %s got %s' % (type(cls), type(instance))) @@ -340,7 +340,7 @@ """ CacheInvalidation.set_invalidate(repo_name, delete=delete) repo = Repository.get_by_repo_name(repo_name) - if repo: + if repo is not None: repo.update_changeset_cache() def toggle_following_repo(self, follow_repo_id, user_id): @@ -807,7 +807,7 @@ repo = self.__get_repo(repo) hist_l.append(['rev:tip', _('latest tip')]) choices.append('rev:tip') - if not repo: + if repo is None: return choices, hist_l repo = repo.scm_instance diff -r 72e0fe2e3278 -r 63bed817308c kallithea/model/user.py --- a/kallithea/model/user.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/model/user.py Fri Jul 24 16:37:40 2015 +0200 @@ -249,7 +249,7 @@ return user def delete(self, user, cur_user=None): - if not cur_user: + if cur_user is None: cur_user = getattr(get_current_authuser(), 'username', None) user = self._get_user(user) @@ -287,7 +287,7 @@ user_email = data['email'] user = User.get_by_email(user_email) - if user: + if user is not None: log.debug('password reset user found %s' % user) link = h.canonical_url('reset_password_confirmation', key=user.api_key) @@ -316,7 +316,7 @@ user = User.get_by_email(user_email) new_passwd = auth.PasswordGenerator().gen_password( 8, auth.PasswordGenerator.ALPHABETS_BIG_SMALL) - if user: + if user is not None: user.password = auth.get_crypt_password(new_passwd) Session().add(user) Session().commit() @@ -433,7 +433,7 @@ """ user = self._get_user(user) obj = UserEmailMap.query().get(email_id) - if obj: + if obj is not None: self.sa.delete(obj) def add_extra_ip(self, user, ip): diff -r 72e0fe2e3278 -r 63bed817308c kallithea/model/user_group.py --- a/kallithea/model/user_group.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/model/user_group.py Fri Jul 24 16:37:40 2015 +0200 @@ -253,7 +253,7 @@ obj = UserGroupToPerm.query()\ .filter(UserGroupToPerm.users_group == user_group)\ .filter(UserGroupToPerm.permission == perm).scalar() - if obj: + if obj is not None: self.sa.delete(obj) def grant_user_permission(self, user_group, user, perm): @@ -302,7 +302,7 @@ .filter(UserUserGroupToPerm.user == user)\ .filter(UserUserGroupToPerm.user_group == user_group)\ .scalar() - if obj: + if obj is not None: self.sa.delete(obj) log.debug('Revoked perm on %s on %s' % (user_group, user)) @@ -351,7 +351,7 @@ .filter(UserGroupUserGroupToPerm.target_user_group == target_user_group)\ .filter(UserGroupUserGroupToPerm.user_group == user_group)\ .scalar() - if obj: + if obj is not None: self.sa.delete(obj) log.debug('Revoked perm on %s on %s' % (target_user_group, user_group)) diff -r 72e0fe2e3278 -r 63bed817308c kallithea/model/validators.py --- a/kallithea/model/validators.py Fri Jul 24 16:37:40 2015 +0200 +++ b/kallithea/model/validators.py Fri Jul 24 16:37:40 2015 +0200 @@ -228,7 +228,7 @@ .filter(RepoGroup.group_parent_id == group_parent_id)\ .scalar() - if gr: + if gr is not None: msg = M(self, 'group_exists', state, group_name=slug) raise formencode.Invalid(msg, value, state, error_dict=dict(group_name=msg) @@ -239,7 +239,7 @@ .filter(Repository.repo_name == slug)\ .scalar() - if repo: + if repo is not None: msg = M(self, 'repo_exists', state, group_name=slug) raise formencode.Invalid(msg, value, state, error_dict=dict(group_name=msg) @@ -715,7 +715,7 @@ def validate_python(self, value, state): if (old_data.get('email') or '').lower() != value: user = User.get_by_email(value, case_insensitive=True) - if user: + if user is not None: msg = M(self, 'email_taken', state) raise formencode.Invalid(msg, value, state, error_dict=dict(email=msg)