diff options
-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 | ||