51 lines
1.9 KiB
Python
51 lines
1.9 KiB
Python
# combo - content management system
|
|
# Copyright (C) 2014-2016 Entr'ouvert
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU Affero General Public License as published
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program 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 Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import json
|
|
|
|
from django.http import HttpResponse
|
|
from django.contrib.auth.models import User
|
|
from django.db.models import Q
|
|
from django.core import serializers
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from .models import UserSearchCell
|
|
|
|
|
|
def ajax_usersearch(request):
|
|
# allow access if the user can see at least one UserSearch cell
|
|
for cell in UserSearchCell.objects.all():
|
|
if cell.is_visible(request.user) and cell.page.is_visible(request.user):
|
|
break
|
|
else:
|
|
raise PermissionDenied
|
|
query = request.GET.get('q')
|
|
if query:
|
|
users = User.objects.all()
|
|
# pseudo full-text search
|
|
for elem in query.split():
|
|
users = users.filter(Q(username__icontains=elem)
|
|
|Q(first_name__icontains=elem)
|
|
|Q(last_name__icontains=elem)
|
|
|Q(email__icontains=elem))
|
|
users = json.loads(serializers.serialize(
|
|
'json', users[:10], fields=('username', 'first_name', 'last_name', 'email')))
|
|
else:
|
|
users = []
|
|
response = HttpResponse(content_type='application/json')
|
|
json.dump(users, response, indent=2)
|
|
return response
|