Changeset - 3971715302b1
[Not reviewed]
default
0 1 0
Thomas De Schampheleire - 8 years ago 2018-02-02 15:52:53
thomas.de_schampheleire@nokia.com
home: make sure users and group autocomplete is case insensitive

Both SQLite and MySQL have a case-insensitive LIKE operator by default, but
PostgreSQL does not. As a result, a query for 'john' does not match the user
'John Doe'.

As case-insensitivity is most user-friendly in the context of autocompletion
of users and groups, switch to the ilike() method of SQLAlchemy rather than
like().
1 file changed with 4 insertions and 4 deletions:
0 comments (0 inline, 0 general)
kallithea/controllers/home.py
Show inline comments
 
@@ -128,85 +128,85 @@ class HomeController(BaseController):
 
                'children': [{'id': rev, 'text': name, 'type': 'closed-branch'} for name, rev in _closed_branches]
 
            })
 
        _tags = repo.tags.items()
 
        if _tags:
 
            res.append({
 
                'text': _('Tag'),
 
                'children': [{'id': rev, 'text': name, 'type': 'tag'} for name, rev in _tags]
 
            })
 
        _bookmarks = repo.bookmarks.items()
 
        if _bookmarks:
 
            res.append({
 
                'text': _('Bookmark'),
 
                'children': [{'id': rev, 'text': name, 'type': 'book'} for name, rev in _bookmarks]
 
            })
 
        data = {
 
            'more': False,
 
            'results': res
 
        }
 
        return data
 

	
 
    @LoginRequired(allow_default_user=True)
 
    @jsonify
 
    def users_and_groups_data(self):
 
        """
 
        Returns 'results' with a list of users and user groups.
 

	
 
        You can either use the 'key' GET parameter to get a user by providing
 
        the exact user key or you can use the 'query' parameter to
 
        search for users by user key, first name and last name.
 
        'types' defaults to just 'users' but can be set to 'users,groups' to
 
        get both users and groups.
 
        No more than 500 results (of each kind) will be returned.
 
        """
 
        types = request.GET.get('types', 'users').split(',')
 
        key = request.GET.get('key', '')
 
        query = request.GET.get('query', '')
 
        results = []
 
        if 'users' in types:
 
            user_list = []
 
            if key:
 
                u = User.get_by_username(key)
 
                if u:
 
                    user_list = [u]
 
            elif query:
 
                user_list = User.query() \
 
                    .filter(User.is_default_user == False) \
 
                    .filter(User.active == True) \
 
                    .filter(or_(
 
                        User.username.like("%%"+query+"%%"),
 
                        User.name.like("%%"+query+"%%"),
 
                        User.lastname.like("%%"+query+"%%"),
 
                        User.username.ilike("%%"+query+"%%"),
 
                        User.name.ilike("%%"+query+"%%"),
 
                        User.lastname.ilike("%%"+query+"%%"),
 
                    )) \
 
                    .order_by(User.username) \
 
                    .limit(500) \
 
                    .all()
 
            for u in user_list:
 
                results.append({
 
                    'type': 'user',
 
                    'id': u.user_id,
 
                    'nname': u.username,
 
                    'fname': u.name,
 
                    'lname': u.lastname,
 
                    'gravatar_lnk': h.gravatar_url(u.email, size=28, default='default'),
 
                    'gravatar_size': 14,
 
                })
 
        if 'groups' in types:
 
            grp_list = []
 
            if key:
 
                grp = UserGroup.get_by_group_name(key)
 
                if grp:
 
                    grp_list = [grp]
 
            elif query:
 
                grp_list = UserGroup.query() \
 
                    .filter(UserGroup.users_group_name.like("%%"+query+"%%")) \
 
                    .filter(UserGroup.users_group_name.ilike("%%"+query+"%%")) \
 
                    .filter(UserGroup.users_group_active == True) \
 
                    .order_by(UserGroup.users_group_name) \
 
                    .limit(500) \
 
                    .all()
 
            for g in UserGroupList(grp_list, perm_level='read'):
 
                results.append({
 
                    'type': 'group',
 
                    'id': g.users_group_id,
 
                    'grname': g.users_group_name,
 
                })
 
        return dict(results=results)
0 comments (0 inline, 0 general)