diff options
Diffstat (limited to 'bitbake/lib/toaster/toastergui/views.py')
-rw-r--r-- | bitbake/lib/toaster/toastergui/views.py | 262 |
1 files changed, 258 insertions, 4 deletions
diff --git a/bitbake/lib/toaster/toastergui/views.py b/bitbake/lib/toaster/toastergui/views.py index ecb2f7e82f..7cb9b42379 100644 --- a/bitbake/lib/toaster/toastergui/views.py +++ b/bitbake/lib/toaster/toastergui/views.py | |||
@@ -16,11 +16,265 @@ | |||
16 | # with this program; if not, write to the Free Software Foundation, Inc., | 16 | # with this program; if not, write to the Free Software Foundation, Inc., |
17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 | 18 | ||
19 | import operator | ||
20 | |||
21 | from django.db.models import Q | ||
19 | from django.shortcuts import render | 22 | from django.shortcuts import render |
20 | from orm.models import Build, Task | 23 | from orm.models import Build, Target, Task, Layer, Layer_Version, Recipe, LogMessage, Variable |
24 | from orm.models import Task_Dependency, Recipe_Dependency, Package, Package_File, Package_Dependency | ||
25 | from orm.models import Target_Installed_Package | ||
26 | from django.views.decorators.cache import cache_control | ||
27 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger | ||
28 | |||
29 | |||
30 | def _build_page_range(paginator, index = 1): | ||
31 | try: | ||
32 | page = paginator.page(index) | ||
33 | except PageNotAnInteger: | ||
34 | page = paginator.page(1) | ||
35 | except EmptyPage: | ||
36 | page = paginator.page(paginator.num_pages) | ||
37 | |||
38 | page.page_range = [page.number] | ||
39 | crt_range = 0 | ||
40 | for i in range(1,5): | ||
41 | if (page.number + i) <= paginator.num_pages: | ||
42 | page.page_range = page.page_range + [ page.number + i] | ||
43 | crt_range +=1 | ||
44 | if (page.number - i) > 0: | ||
45 | page.page_range = [page.number -i] + page.page_range | ||
46 | crt_range +=1 | ||
47 | if crt_range == 4: | ||
48 | break | ||
49 | return page | ||
50 | |||
51 | @cache_control(no_store=True) | ||
52 | def build(request): | ||
53 | template = 'build.html' | ||
54 | logs = LogMessage.objects.all() | ||
55 | |||
56 | build_info = _build_page_range(Paginator(Build.objects.order_by("-id"), 10),request.GET.get('page', 1)) | ||
57 | |||
58 | context = {'objects': build_info, 'logs': logs , | ||
59 | 'hideshowcols' : [ | ||
60 | {'name': 'Output', 'order':10}, | ||
61 | {'name': 'Log', 'order':11}, | ||
62 | ]} | ||
63 | |||
64 | return render(request, template, context) | ||
65 | |||
66 | |||
67 | def _find_task_revdep(task): | ||
68 | tp = [] | ||
69 | for p in Task_Dependency.objects.filter(depends_on=task): | ||
70 | tp.append(p.task); | ||
71 | return tp | ||
72 | |||
73 | def _find_task_provider(task): | ||
74 | task_revdeps = _find_task_revdep(task) | ||
75 | for tr in task_revdeps: | ||
76 | if tr.outcome != Task.OUTCOME_COVERED: | ||
77 | return tr | ||
78 | for tr in task_revdeps: | ||
79 | trc = _find_task_provider(tr) | ||
80 | if trc is not None: | ||
81 | return trc | ||
82 | return None | ||
83 | |||
84 | def task(request, build_id): | ||
85 | template = 'task.html' | ||
86 | |||
87 | tasks = _build_page_range(Paginator(Task.objects.filter(build=build_id), 100),request.GET.get('page', 1)) | ||
88 | |||
89 | for t in tasks: | ||
90 | if t.outcome == Task.OUTCOME_COVERED: | ||
91 | t.provider = _find_task_provider(t) | ||
92 | |||
93 | context = {'build': Build.objects.filter(pk=build_id)[0], 'objects': tasks} | ||
94 | |||
95 | return render(request, template, context) | ||
96 | |||
97 | def configuration(request, build_id): | ||
98 | template = 'configuration.html' | ||
99 | variables = _build_page_range(Paginator(Variable.objects.filter(build=build_id), 50), request.GET.get('page', 1)) | ||
100 | context = {'build': Build.objects.filter(pk=build_id)[0], 'objects' : variables} | ||
101 | return render(request, template, context) | ||
102 | |||
103 | def bpackage(request, build_id): | ||
104 | template = 'bpackage.html' | ||
105 | packages = Package.objects.filter(build = build_id) | ||
106 | context = {'build': Build.objects.filter(pk=build_id)[0], 'objects' : packages} | ||
107 | return render(request, template, context) | ||
108 | |||
109 | def bfile(request, build_id, package_id): | ||
110 | template = 'bfile.html' | ||
111 | files = Package_File.objects.filter(package = package_id) | ||
112 | context = {'build': Build.objects.filter(pk=build_id)[0], 'objects' : files} | ||
113 | return render(request, template, context) | ||
114 | |||
115 | def tpackage(request, build_id, target_id): | ||
116 | template = 'package.html' | ||
117 | packages = map(lambda x: x.package, list(Target_Installed_Package.objects.filter(target=target_id))) | ||
118 | context = {'build': Build.objects.filter(pk=build_id)[0], 'objects' : packages} | ||
119 | return render(request, template, context) | ||
120 | |||
121 | def layer(request): | ||
122 | template = 'layer.html' | ||
123 | layer_info = Layer.objects.all() | ||
124 | |||
125 | for li in layer_info: | ||
126 | li.versions = Layer_Version.objects.filter(layer = li) | ||
127 | for liv in li.versions: | ||
128 | liv.count = Recipe.objects.filter(layer_version__id = liv.id).count() | ||
129 | |||
130 | context = {'objects': layer_info} | ||
131 | |||
132 | return render(request, template, context) | ||
133 | |||
134 | |||
135 | def layer_versions_recipes(request, layerversion_id): | ||
136 | template = 'recipe.html' | ||
137 | recipes = Recipe.objects.filter(layer_version__id = layerversion_id) | ||
138 | |||
139 | context = {'objects': recipes, | ||
140 | 'layer_version' : Layer_Version.objects.filter( id = layerversion_id )[0] | ||
141 | } | ||
142 | |||
143 | return render(request, template, context) | ||
144 | |||
145 | #### API | ||
146 | |||
147 | import json | ||
148 | from django.core import serializers | ||
149 | from django.http import HttpResponse, HttpResponseBadRequest | ||
150 | |||
151 | |||
152 | def model_explorer(request, model_name): | ||
153 | |||
154 | DESCENDING = 'desc' | ||
155 | response_data = {} | ||
156 | model_mapping = { | ||
157 | 'build': Build, | ||
158 | 'target': Target, | ||
159 | 'task': Task, | ||
160 | 'task_dependency': Task_Dependency, | ||
161 | 'package': Package, | ||
162 | 'layer': Layer, | ||
163 | 'layerversion': Layer_Version, | ||
164 | 'recipe': Recipe, | ||
165 | 'recipe_dependency': Recipe_Dependency, | ||
166 | 'package': Package, | ||
167 | 'package_dependency': Package_Dependency, | ||
168 | 'build_file': Package_File, | ||
169 | 'variable': Variable, | ||
170 | 'logmessage': LogMessage, | ||
171 | } | ||
172 | |||
173 | if model_name not in model_mapping.keys(): | ||
174 | return HttpResponseBadRequest() | ||
175 | |||
176 | model = model_mapping[model_name] | ||
177 | |||
178 | try: | ||
179 | limit = int(request.GET.get('limit', 0)) | ||
180 | except ValueError: | ||
181 | limit = 0 | ||
182 | |||
183 | try: | ||
184 | offset = int(request.GET.get('offset', 0)) | ||
185 | except ValueError: | ||
186 | offset = 0 | ||
187 | |||
188 | ordering_string, invalid = _validate_input(request.GET.get('orderby', ''), | ||
189 | model) | ||
190 | if invalid: | ||
191 | return HttpResponseBadRequest() | ||
192 | |||
193 | filter_string, invalid = _validate_input(request.GET.get('filter', ''), | ||
194 | model) | ||
195 | if invalid: | ||
196 | return HttpResponseBadRequest() | ||
197 | |||
198 | search_term = request.GET.get('search', '') | ||
199 | |||
200 | if filter_string: | ||
201 | filter_terms = _get_filtering_terms(filter_string) | ||
202 | try: | ||
203 | queryset = model.objects.filter(**filter_terms) | ||
204 | except ValueError: | ||
205 | queryset = [] | ||
206 | else: | ||
207 | queryset = model.objects.all() | ||
208 | |||
209 | if search_term: | ||
210 | queryset = _get_search_results(search_term, queryset, model) | ||
211 | |||
212 | if ordering_string and queryset: | ||
213 | column, order = ordering_string.split(':') | ||
214 | if order.lower() == DESCENDING: | ||
215 | queryset = queryset.order_by('-' + column) | ||
216 | else: | ||
217 | queryset = queryset.order_by(column) | ||
218 | |||
219 | if offset and limit: | ||
220 | queryset = queryset[offset:(offset+limit)] | ||
221 | elif offset: | ||
222 | queryset = queryset[offset:] | ||
223 | elif limit: | ||
224 | queryset = queryset[:limit] | ||
225 | |||
226 | if queryset: | ||
227 | response_data['count'] = queryset.count() | ||
228 | else: | ||
229 | response_data['count'] = 0 | ||
230 | |||
231 | response_data['list'] = serializers.serialize('json', queryset) | ||
232 | |||
233 | return HttpResponse(json.dumps(response_data), | ||
234 | content_type='application/json') | ||
235 | |||
236 | def _get_filtering_terms(filter_string): | ||
237 | |||
238 | search_terms = filter_string.split(":") | ||
239 | keys = search_terms[0].split(',') | ||
240 | values = search_terms[1].split(',') | ||
241 | |||
242 | return dict(zip(keys, values)) | ||
243 | |||
244 | def _validate_input(input, model): | ||
245 | |||
246 | invalid = 0 | ||
247 | |||
248 | if input: | ||
249 | input_list = input.split(":") | ||
250 | |||
251 | # Check we have only one colon | ||
252 | if len(input_list) != 2: | ||
253 | invalid = 1 | ||
254 | return None, invalid | ||
255 | |||
256 | # Check we have an equal number of terms both sides of the colon | ||
257 | if len(input_list[0].split(',')) != len(input_list[1].split(',')): | ||
258 | invalid = 1 | ||
259 | return None, invalid | ||
260 | |||
261 | # Check we are looking for a valid field | ||
262 | valid_fields = model._meta.get_all_field_names() | ||
263 | for field in input_list[0].split(','): | ||
264 | if field not in valid_fields: | ||
265 | invalid = 1 | ||
266 | return None, invalid | ||
267 | |||
268 | return input, invalid | ||
21 | 269 | ||
270 | def _get_search_results(search_term, queryset, model): | ||
271 | search_objects = [] | ||
272 | for st in search_term.split(" "): | ||
273 | q_map = map(lambda x: Q(**{x+'__icontains': st}), | ||
274 | model.search_allowed_fields) | ||
22 | 275 | ||
23 | def guihome(request): | 276 | search_objects.append(reduce(operator.or_, q_map)) |
24 | template = 'index.html' | 277 | search_object = reduce(operator.and_, search_objects) |
278 | queryset = queryset.filter(search_object) | ||
25 | 279 | ||
26 | return render(request, template) | 280 | return queryset |