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']
14 if 'pk' in request
.GET
:
15 pk
= request
.GET
['pk']
20 if request
.GET
.get("groups", 'True') == 'True':
21 data
= simplejson
.dumps(search_users_and_groups(term
, pk
, limit
))
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']
33 if 'pk' in request
.GET
:
34 pk
= request
.GET
['pk']
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):
46 clusterUsers
= ClusterUser
.objects
.filter(id=int(pk
))
48 clusterUsers
= ClusterUser
.objects
.filter(name__istartswith
=term
)
50 clusterUsers
= ClusterUser
.objects
.all()
52 clusterUsers
= clusterUsers
.values('pk', 'name')
55 query
= clusterUsers
[0]['name']
62 clusterUsers
= clusterUsers
[:limit
]
64 # lable each item based on its real_type
66 for i
in clusterUsers
:
68 userType
= str(ClusterUser
.objects
.get(id=i
['pk'])
69 .cast()._get
_real
_type
())
70 if userType
== "profile":
72 elif userType
== "organization":
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
]
85 'results': clusterUsers
89 def search_users_only(term
=None, pk
=None, limit
=10):
91 Returns a list of the top N matches from Users with a name
94 @param term: the term to search for
95 @param limit: the number of results to return
98 users
= User
.objects
.filter(id=int(pk
))
100 users
= User
.objects
.filter(username__istartswith
=term
)
102 users
= User
.objects
.all()
104 users
= users
.values('pk', 'username')
107 query
= users
[0]['username']
114 users
= users
[:limit
]
116 # lable each item as a 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
]
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
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
143 users
= User
.objects
.filter(id=int(pk
))
144 groups
= Group
.objects
.filter(id=int(pk
))
146 users
= User
.objects
.filter(username__istartswith
=term
)
147 groups
= Group
.objects
.filter(name__istartswith
=term
)
149 users
= User
.objects
.all()
150 groups
= Group
.objects
.all()
152 users
= users
.values('pk', 'username')
153 groups
= groups
.values('pk', 'name')
163 users
= users
[:limit
]
164 groups
= groups
[:limit
]
166 # label each item as either user or a group
168 users
= [(i
['username'], f
, i
['pk']) for i
in users
]
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
]