autocomplete_light_registry.py 3.69 KB
Newer Older
Bach Dániel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE 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, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE 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 CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

18
import autocomplete_light
19
from django.contrib.auth.models import User
20
from django.utils.html import escape
21 22 23
from django.utils.translation import ugettext as _

from .views import AclUpdateView
24
from .models import Profile
25 26


27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
def highlight(field, q, none_wo_match=True):
    """
    >>> highlight('<b>Akkount Krokodil', 'kro', False)
    u'&lt;b&gt;Akkount <span class="autocomplete-hl">Kro</span>kodil'
    """

    if not field:
        return None
    try:
        match = field.lower().index(q.lower())
    except ValueError:
        match = None
    if q and match is not None:
        match_end = match + len(q)
        return (escape(field[:match])
                + '<span class="autocomplete-hl">'
                + escape(field[match:match_end])
                + '</span>' + escape(field[match_end:]))
    elif none_wo_match:
        return None
    else:
        return escape(field)


51
class AclUserGroupAutocomplete(autocomplete_light.AutocompleteGenericBase):
52
    search_fields = (
53 54
        ('first_name', 'last_name', 'username', 'email', 'profile__org_id'),
        ('name', 'groupprofile__org_id'),
55
    )
56 57
    choice_html_format = (u'<span data-value="%s"><span style="display:none"'
                          u'>%s</span>%s</span>')
58

59 60
    def choice_displayed_text(self, choice):
        q = unicode(self.request.GET.get('q', ''))
61
        name = highlight(unicode(choice), q, False)
62
        if isinstance(choice, User):
63 64
            extra_fields = [highlight(choice.get_full_name(), q, False),
                            highlight(choice.email, q)]
65
            try:
66
                extra_fields.append(highlight(choice.profile.org_id, q))
67 68 69 70 71
            except Profile.DoesNotExist:
                pass
            return '%s (%s)' % (name, ', '.join(f for f in extra_fields
                                                if f))
        else:
72
            return _('%s (group)') % name
73

74
    def choice_html(self, choice):
75
        return self.choice_html_format % (
76 77
            self.choice_value(choice), self.choice_label(choice),
            self.choice_displayed_text(choice))
78 79 80 81 82

    def choices_for_request(self):
        user = self.request.user
        self.choices = (AclUpdateView.get_allowed_users(user),
                        AclUpdateView.get_allowed_groups(user))
83
        return super(AclUserGroupAutocomplete, self).choices_for_request()
84

85 86
    def autocomplete_html(self):
        html = []
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        for choice in self.choices_for_request():
            html.append(self.choice_html(choice))

        if not html:
            html = self.empty_html_format % _('no matches found').capitalize()

        return self.autocomplete_html_format % ''.join(html)


class AclUserAutocomplete(AclUserGroupAutocomplete):
    def choices_for_request(self):
        user = self.request.user
        self.choices = (AclUpdateView.get_allowed_users(user), )
        return super(AclUserGroupAutocomplete, self).choices_for_request()


autocomplete_light.register(AclUserGroupAutocomplete)
105
autocomplete_light.register(AclUserAutocomplete)