Move default template for tables out of template into table base class
[ganeti_webmgr.git] / ganeti_web / views / user_search.py
blob33465d96b98b2c96dc8e0d438aade668e74368ac
1 from django.http import HttpResponse
2 from django.contrib.auth.models import User, Group
3 from django.utils import simplejson
4 from ganeti_web.models import ClusterUser
7 def search_users(request):
8 """ search users and groups and return results as json """
9 if 'term' in request.GET:
10 term = request.GET['term']
11 else:
12 term = None
14 if 'pk' in request.GET:
15 pk = request.GET['pk']
16 else:
17 pk = None
19 limit = 10
20 if request.GET.get("groups", 'True') == 'True':
21 data = simplejson.dumps(search_users_and_groups(term, pk, limit))
22 else:
23 data = simplejson.dumps(search_users_only(term, pk, limit))
24 return HttpResponse(data, mimetype="application/json")
27 def search_owners(request):
28 if 'term' in request.GET:
29 term = request.GET['term']
30 else:
31 term = None
33 if 'pk' in request.GET:
34 pk = request.GET['pk']
35 else:
36 pk = None
38 limit = 10
39 data = simplejson.dumps(search_cluster_users(term, pk, limit))
40 return HttpResponse(data, mimetype="application/json")
43 def search_cluster_users(term=None, pk=None, limit=10):
45 if pk:
46 clusterUsers = ClusterUser.objects.filter(id=int(pk))
47 elif term:
48 clusterUsers = ClusterUser.objects.filter(name__istartswith=term)
49 else:
50 clusterUsers = ClusterUser.objects.all()
52 clusterUsers = clusterUsers.values('pk', 'name')
54 if pk:
55 query = clusterUsers[0]['name']
56 elif term:
57 query = term
58 else:
59 query = ""
61 if limit:
62 clusterUsers = clusterUsers[:limit]
64 # lable each item based on its real_type
65 labeledUsers = []
66 for i in clusterUsers:
67 f = 'other'
68 userType = str(ClusterUser.objects.get(id=i['pk'])
69 .cast()._get_real_type())
70 if userType == "profile":
71 f = 'user'
72 elif userType == "organization":
73 f = 'group'
74 labeledUsers.append((i['name'], f, i['pk']))
76 clusterUsers = labeledUsers
78 # sort list and crop out all but the top [limit] results
79 clusterUsers = sorted(clusterUsers, key=lambda x: x[0])
80 clusterUsers = clusterUsers if len(clusterUsers) \
81 < limit else clusterUsers[:limit]
83 return {
84 'query': query,
85 'results': clusterUsers
89 def search_users_only(term=None, pk=None, limit=10):
90 """
91 Returns a list of the top N matches from Users with a name
92 starting with term
94 @param term: the term to search for
95 @param limit: the number of results to return
96 """
97 if pk:
98 users = User.objects.filter(id=int(pk))
99 elif term:
100 users = User.objects.filter(username__istartswith=term)
101 else:
102 users = User.objects.all()
104 users = users.values('pk', 'username')
106 if pk:
107 query = users[0]['username']
108 elif term:
109 query = term
110 else:
111 query = ""
113 if limit:
114 users = users[:limit]
116 # lable each item as a user
117 f = 'user'
118 users = [(i['username'], f, i['pk']) for i in users]
120 # sort list and crop out all but the top [limit] results
121 users = sorted(users, key=lambda x: x[0])
122 users = users if len(users) \
123 < limit else users[:limit]
125 return {
126 'query': query,
127 'results': users
131 def search_users_and_groups(term=None, pk=None, limit=10):
133 Returns a list of the top N matches from Groups and Users with a name
134 starting with term.
135 Warning: Searching for users and groups using a primary key will return
136 a match from both users AND groups
138 @param term: the term to search for
139 @param pk: the primary key of the user/group to search for
140 @param limit: the number of results to return
142 if pk:
143 users = User.objects.filter(id=int(pk))
144 groups = Group.objects.filter(id=int(pk))
145 elif term:
146 users = User.objects.filter(username__istartswith=term)
147 groups = Group.objects.filter(name__istartswith=term)
148 else:
149 users = User.objects.all()
150 groups = Group.objects.all()
152 users = users.values('pk', 'username')
153 groups = groups.values('pk', 'name')
155 if pk:
156 query = ""
157 elif term:
158 query = term
159 else:
160 query = ""
162 if limit:
163 users = users[:limit]
164 groups = groups[:limit]
166 # label each item as either user or a group
167 f = 'user'
168 users = [(i['username'], f, i['pk']) for i in users]
169 f = 'group'
170 groups = [(i['name'], f, i['pk']) for i in groups]
172 # merge lists together
173 # then sort lists and crop out all but the top [limit] results
174 merged = users + groups
175 merged = sorted(merged, key=lambda x: x[0])
176 merged = merged if len(merged) < limit else merged[:limit]
178 return {
179 'query': query,
180 'results': merged