autocomplete_light_registry.py 2.97 KB
Newer Older
1
import autocomplete_light
2
from django.contrib.auth.models import User
3
from django.utils.html import escape
4 5 6
from django.utils.translation import ugettext as _

from .views import AclUpdateView
7
from .models import Profile
8 9


10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
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)


34
class AclUserGroupAutocomplete(autocomplete_light.AutocompleteGenericBase):
35
    search_fields = (
36 37
        ('first_name', 'last_name', 'username', 'email', 'profile__org_id'),
        ('name', 'groupprofile__org_id'),
38
    )
39 40
    choice_html_format = (u'<span data-value="%s"><span style="display:none"'
                          u'>%s</span>%s</span>')
41

42 43
    def choice_displayed_text(self, choice):
        q = unicode(self.request.GET.get('q', ''))
44
        name = highlight(unicode(choice), q, False)
45
        if isinstance(choice, User):
46 47
            extra_fields = [highlight(choice.get_full_name(), q, False),
                            highlight(choice.email, q)]
48
            try:
49
                extra_fields.append(highlight(choice.profile.org_id, q))
50 51 52 53 54
            except Profile.DoesNotExist:
                pass
            return '%s (%s)' % (name, ', '.join(f for f in extra_fields
                                                if f))
        else:
55
            return _('%s (group)') % name
56

57
    def choice_html(self, choice):
58
        return self.choice_html_format % (
59 60
            self.choice_value(choice), self.choice_label(choice),
            self.choice_displayed_text(choice))
61 62 63 64 65

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

68 69
    def autocomplete_html(self):
        html = []
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        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)
88
autocomplete_light.register(AclUserAutocomplete)