From f4e51fff4a9dc8d44390482af11551dfa10e6f4b Mon Sep 17 00:00:00 2001 From: Michael Wood Date: Tue, 4 Aug 2015 22:46:32 +0300 Subject: 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 Signed-off-by: Ed Bartosh Signed-off-by: Richard Purdie --- bitbake/lib/toaster/toastergui/widgets.py | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) 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): content_type = "application/json; charset=utf-8") return super(ToasterTemplateView, self).get(*args, **kwargs) + +class ToasterTypeAhead(View): + """ A typeahead mechanism to support the front end typeahead widgets """ + MAX_RESULTS = 6 + + class MissingFieldsException(Exception): + pass + + def __init__(self, *args, **kwargs): + super(ToasterTypeAhead, self).__init__() + + def get(self, request, *args, **kwargs): + def response(data): + return HttpResponse(json.dumps(data, + indent=2, + cls=DjangoJSONEncoder), + content_type="application/json") + + error = "ok" + + search_term = request.GET.get("search", None) + if search_term == None: + # We got no search value so return empty reponse + return response({'error' : error , 'results': []}) + + try: + prj = Project.objects.get(pk=kwargs['pid']) + except KeyError: + prj = None + + results = self.apply_search(search_term, prj, request)[:ToasterTypeAhead.MAX_RESULTS] + + if len(results) > 0: + try: + self.validate_fields(results[0]) + except MissingFieldsException as e: + error = e + + data = { 'results' : results, + 'error' : error, + } + + return response(data) + + def validate_fields(self, result): + if 'name' in result == False or 'detail' in result == False: + raise MissingFieldsException("name and detail are required fields") + + def apply_search(self, search_term, prj): + """ Override this function to implement search. Return an array of + dictionaries with a minium of a name and detail field""" + pass -- cgit v1.2.3-54-g00ecf