~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to django_messages_wl/views.py

  • Committer: kaputtnik
  • Date: 2019-05-30 18:20:02 UTC
  • mto: This revision was merged to the branch mainline in revision 540.
  • Revision ID: kaputtnik-20190530182002-g7l91m1xo28clghv
adjusted README; first commit on the new server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from django.contrib.auth.models import User
 
2
from django.http import HttpResponse
 
3
import json
 
4
 
 
5
 
 
6
def get_usernames(request):
 
7
    """AJAX Callback for JS autocomplete.
 
8
 
 
9
    This is used for autocompletion of usernames when writing PMs.
 
10
    The path.name of this function has to be used in each place:
 
11
    1. Argument of source of the JS widget
 
12
    2. urls.py
 
13
 
 
14
    """
 
15
    if request.is_ajax():
 
16
        q = request.GET.get('term', '')
 
17
 
 
18
        usernames = User.objects.exclude(is_active=False).filter(username__icontains=q)
 
19
        results = []
 
20
        for user in usernames:
 
21
            name_json = {'value': user.username}
 
22
            results.append(name_json)
 
23
        data = json.dumps(results)
 
24
    else:
 
25
        data = 'fail'
 
26
    mimetype = 'application/json'
 
27
    return HttpResponse(data, mimetype)