diff options
| author | Michael Wood <michael.g.wood@intel.com> | 2015-08-04 22:46:32 +0300 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2015-08-06 16:04:45 -0500 |
| commit | f4e51fff4a9dc8d44390482af11551dfa10e6f4b (patch) | |
| tree | 8b006f8c3650cc8bca9a158a53e1f9c065571234 | |
| parent | 68128ad2f1ab2004e64511c2ce9162cdfa6a9cb5 (diff) | |
| download | poky-f4e51fff4a9dc8d44390482af11551dfa10e6f4b.tar.gz | |
bitbake: toastergui: widgets Add a typeahead widget
The typeahead behaviour is significantly different from searching in a
table so we need a separate widget to do this.
[YOCTO #7152]
(Bitbake rev: 195c5407a9de29d97f2525b9ae6c827afb934e37)
Signed-off-by: Michael Wood <michael.g.wood@intel.com>
Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
| -rw-r--r-- | bitbake/lib/toaster/toastergui/widgets.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/bitbake/lib/toaster/toastergui/widgets.py b/bitbake/lib/toaster/toastergui/widgets.py index 08854024f5..2adf574783 100644 --- a/bitbake/lib/toaster/toastergui/widgets.py +++ b/bitbake/lib/toaster/toastergui/widgets.py | |||
| @@ -354,3 +354,55 @@ class ToasterTemplateView(TemplateView): | |||
| 354 | content_type = "application/json; charset=utf-8") | 354 | content_type = "application/json; charset=utf-8") |
| 355 | 355 | ||
| 356 | return super(ToasterTemplateView, self).get(*args, **kwargs) | 356 | return super(ToasterTemplateView, self).get(*args, **kwargs) |
| 357 | |||
| 358 | class ToasterTypeAhead(View): | ||
| 359 | """ A typeahead mechanism to support the front end typeahead widgets """ | ||
| 360 | MAX_RESULTS = 6 | ||
| 361 | |||
| 362 | class MissingFieldsException(Exception): | ||
| 363 | pass | ||
| 364 | |||
| 365 | def __init__(self, *args, **kwargs): | ||
| 366 | super(ToasterTypeAhead, self).__init__() | ||
| 367 | |||
| 368 | def get(self, request, *args, **kwargs): | ||
| 369 | def response(data): | ||
| 370 | return HttpResponse(json.dumps(data, | ||
| 371 | indent=2, | ||
| 372 | cls=DjangoJSONEncoder), | ||
| 373 | content_type="application/json") | ||
| 374 | |||
| 375 | error = "ok" | ||
| 376 | |||
| 377 | search_term = request.GET.get("search", None) | ||
| 378 | if search_term == None: | ||
| 379 | # We got no search value so return empty reponse | ||
| 380 | return response({'error' : error , 'results': []}) | ||
| 381 | |||
| 382 | try: | ||
| 383 | prj = Project.objects.get(pk=kwargs['pid']) | ||
| 384 | except KeyError: | ||
| 385 | prj = None | ||
| 386 | |||
| 387 | results = self.apply_search(search_term, prj, request)[:ToasterTypeAhead.MAX_RESULTS] | ||
| 388 | |||
| 389 | if len(results) > 0: | ||
| 390 | try: | ||
| 391 | self.validate_fields(results[0]) | ||
| 392 | except MissingFieldsException as e: | ||
| 393 | error = e | ||
| 394 | |||
| 395 | data = { 'results' : results, | ||
| 396 | 'error' : error, | ||
| 397 | } | ||
| 398 | |||
| 399 | return response(data) | ||
| 400 | |||
| 401 | def validate_fields(self, result): | ||
| 402 | if 'name' in result == False or 'detail' in result == False: | ||
| 403 | raise MissingFieldsException("name and detail are required fields") | ||
| 404 | |||
| 405 | def apply_search(self, search_term, prj): | ||
| 406 | """ Override this function to implement search. Return an array of | ||
| 407 | dictionaries with a minium of a name and detail field""" | ||
| 408 | pass | ||
