Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # BitBake Toaster Implementation |
| 3 | # |
| 4 | # Copyright (C) 2015 Intel Corporation |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License version 2 as |
| 8 | # published by the Free Software Foundation. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License along |
| 16 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | |
| 19 | from toastergui.widgets import ToasterTypeAhead |
| 20 | from orm.models import Project |
| 21 | from django.core.urlresolvers import reverse |
| 22 | |
| 23 | class LayersTypeAhead(ToasterTypeAhead): |
| 24 | """ Typeahead for layers available and not added in the current project's |
| 25 | configuration """ |
| 26 | def __init__(self): |
| 27 | super(LayersTypeAhead, self).__init__() |
| 28 | |
| 29 | def apply_search(self, search_term, prj, request): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 30 | layers = prj.get_all_compatible_layer_versions() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | layers = layers.order_by('layer__name') |
| 32 | |
| 33 | # Unlike the other typeaheads we also don't want to show suggestions |
| 34 | # for layers already in the project unless required such as when adding |
| 35 | # layerdeps to a new layer. |
| 36 | if ("include_added" in request.GET and |
| 37 | request.GET['include_added'] != "true"): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 38 | layers = layers.exclude( |
| 39 | pk__in=prj.get_project_layer_versions(pk=True)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | |
| 41 | primary_results = layers.filter(layer__name__istartswith=search_term) |
| 42 | secondary_results = layers.filter(layer__name__icontains=search_term).exclude(pk__in=primary_results) |
| 43 | |
| 44 | results = [] |
| 45 | |
| 46 | for layer_version in list(primary_results) + list(secondary_results): |
| 47 | vcs_reference = layer_version.get_vcs_reference() |
| 48 | |
| 49 | detail = "[ %s | %s ]" % (layer_version.layer.vcs_url, |
| 50 | vcs_reference) |
| 51 | needed_fields = { |
| 52 | 'id' : layer_version.pk, |
| 53 | 'name' : layer_version.layer.name, |
| 54 | 'layerdetailurl' : layer_version.get_detailspage_url(prj.pk), |
| 55 | 'vcs_url' : layer_version.layer.vcs_url, |
| 56 | 'vcs_reference' : vcs_reference, |
| 57 | 'detail' : detail, |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 58 | 'local_source_dir' : layer_version.layer.local_source_dir, |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | results.append(needed_fields) |
| 62 | |
| 63 | return results |
| 64 | |
| 65 | class MachinesTypeAhead(ToasterTypeAhead): |
| 66 | """ Typeahead for all the machines available in the current project's |
| 67 | configuration """ |
| 68 | def __init__(self): |
| 69 | super(MachinesTypeAhead, self).__init__() |
| 70 | |
| 71 | def apply_search(self, search_term, prj, request): |
| 72 | machines = prj.get_available_machines() |
| 73 | machines = machines.order_by("name") |
| 74 | |
| 75 | primary_results = machines.filter(name__istartswith=search_term) |
| 76 | secondary_results = machines.filter(name__icontains=search_term).exclude(pk__in=primary_results) |
| 77 | tertiary_results = machines.filter(layer_version__layer__name__icontains=search_term).exclude(pk__in=primary_results).exclude(pk__in=secondary_results) |
| 78 | |
| 79 | results = [] |
| 80 | |
| 81 | for machine in list(primary_results) + list(secondary_results) + list(tertiary_results): |
| 82 | |
| 83 | detail = "[ %s ]" % (machine.layer_version.layer.name) |
| 84 | needed_fields = { |
| 85 | 'id' : machine.pk, |
| 86 | 'name' : machine.name, |
| 87 | 'detail' : detail, |
| 88 | } |
| 89 | |
| 90 | results.append(needed_fields) |
| 91 | |
| 92 | return results |
| 93 | |
| 94 | class RecipesTypeAhead(ToasterTypeAhead): |
| 95 | """ Typeahead for all the recipes available in the current project's |
| 96 | configuration """ |
| 97 | def __init__(self): |
| 98 | super(RecipesTypeAhead, self).__init__() |
| 99 | |
| 100 | def apply_search(self, search_term, prj, request): |
| 101 | recipes = prj.get_available_recipes() |
| 102 | recipes = recipes.order_by("name") |
| 103 | |
| 104 | |
| 105 | primary_results = recipes.filter(name__istartswith=search_term) |
| 106 | secondary_results = recipes.filter(name__icontains=search_term).exclude(pk__in=primary_results) |
| 107 | tertiary_results = recipes.filter(layer_version__layer__name__icontains=search_term).exclude(pk__in=primary_results).exclude(pk__in=secondary_results) |
| 108 | |
| 109 | results = [] |
| 110 | |
| 111 | for recipe in list(primary_results) + list(secondary_results) + list(tertiary_results): |
| 112 | |
| 113 | detail = "[ %s ]" % (recipe.layer_version.layer.name) |
| 114 | needed_fields = { |
| 115 | 'id' : recipe.pk, |
| 116 | 'name' : recipe.name, |
| 117 | 'detail' : detail, |
| 118 | } |
| 119 | |
| 120 | results.append(needed_fields) |
| 121 | |
| 122 | return results |
| 123 | |
| 124 | class ProjectsTypeAhead(ToasterTypeAhead): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 125 | """ Typeahead for all the projects, except for command line builds """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 126 | def __init__(self): |
| 127 | super(ProjectsTypeAhead, self).__init__() |
| 128 | |
| 129 | def apply_search(self, search_term, prj, request): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 130 | projects = Project.objects.exclude(is_default=True).order_by("name") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 131 | |
| 132 | primary_results = projects.filter(name__istartswith=search_term) |
| 133 | secondary_results = projects.filter(name__icontains=search_term).exclude(pk__in=primary_results) |
| 134 | |
| 135 | results = [] |
| 136 | |
| 137 | for project in list(primary_results) + list(secondary_results): |
| 138 | needed_fields = { |
| 139 | 'id' : project.pk, |
| 140 | 'name' : project.name, |
| 141 | 'detail' : "", |
| 142 | 'projectPageUrl' : reverse('project', args=(project.pk,)) |
| 143 | } |
| 144 | |
| 145 | results.append(needed_fields) |
| 146 | |
| 147 | return results |