Commit b61a5728 by Bach Dániel

dashboard: improve autocomplete

parent c79a3431
import autocomplete_light import autocomplete_light
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from .views import AclUpdateView from .views import AclUpdateView
from .models import Profile
class AclUserGroupAutocomplete(autocomplete_light.AutocompleteGenericBase): class AclUserGroupAutocomplete(autocomplete_light.AutocompleteGenericBase):
search_fields = ( search_fields = (
('^first_name', 'last_name', 'username', '^email', 'profile__org_id'), ('first_name', 'last_name', 'username', 'email', 'profile__org_id'),
('^name', 'groupprofile__org_id'), ('name', 'groupprofile__org_id'),
) )
autocomplete_js_attributes = {'placeholder': _("Name of group or user")} choice_html_format = (u'<span data-value="%s"><span style="display:none"'
choice_html_format = u'<span data-value="%s"><span>%s</span> %s</span>' u'>%s</span>%s</span>')
def choice_html(self, choice): def highlight(self, field, q, none_wo_match=True):
if not field:
return None
try: try:
name = choice.get_full_name() match = field.lower().index(q.lower())
except AttributeError: except ValueError:
name = _('group') match = None
if name: if q and match is not None:
name = u'(%s)' % name match_end = match + len(q)
return (field[:match] + '<span class="autocomplete-hl">' +
field[match:match_end] + '</span>' + field[match_end:])
else:
return None if none_wo_match else field
def choice_displayed_text(self, choice):
q = unicode(self.request.GET.get('q', ''))
name = self.highlight(unicode(choice), q, False)
if isinstance(choice, User):
extra_fields = [self.highlight(choice.get_full_name(), q, False),
self.highlight(choice.email, q)]
try:
extra_fields.append(self.highlight(choice.profile.org_id, q))
except Profile.DoesNotExist:
pass
return '%s (%s)' % (name, ', '.join(f for f in extra_fields
if f))
else:
return '%s (%s)' % (name, _('group'))
def choice_html(self, choice):
return self.choice_html_format % ( return self.choice_html_format % (
self.choice_value(choice), self.choice_label(choice), name) self.choice_value(choice), self.choice_label(choice),
self.choice_displayed_text(choice))
def choices_for_request(self): def choices_for_request(self):
user = self.request.user user = self.request.user
......
...@@ -960,3 +960,12 @@ textarea[name="list-new-namelist"] { ...@@ -960,3 +960,12 @@ textarea[name="list-new-namelist"] {
#vm-activity-state { #vm-activity-state {
margin-bottom: 15px; margin-bottom: 15px;
} }
.autocomplete-hl {
color: #b20000;
font-weight: bold;
}
.hilight .autocomplete-hl {
color: orange;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment