Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | # BitBake Toaster Implementation |
| 3 | # |
| 4 | # Copyright (C) 2015 Intel Corporation |
| 5 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 6 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | |
| 9 | from django.views.generic import View, TemplateView |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 10 | from django.utils.decorators import method_decorator |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 11 | from django.views.decorators.cache import cache_control |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | from django.shortcuts import HttpResponse |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | from django.core.cache import cache |
| 14 | from django.core.paginator import Paginator, EmptyPage |
| 15 | from django.db.models import Q |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 16 | from orm.models import Project, Build |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | from django.template import Context, Template |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 18 | from django.template import VariableDoesNotExist |
| 19 | from django.template import TemplateSyntaxError |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | from django.core.serializers.json import DjangoJSONEncoder |
| 21 | from django.core.exceptions import FieldError |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 22 | from django.utils import timezone |
| 23 | from toastergui.templatetags.projecttags import sectohms, get_tasks |
| 24 | from toastergui.templatetags.projecttags import json as template_json |
| 25 | from django.http import JsonResponse |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 26 | from django.urls import reverse |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | |
| 28 | import types |
| 29 | import json |
| 30 | import collections |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | import re |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 32 | import os |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 33 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 34 | from toastergui.tablefilter import TableFilterMap |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame^] | 35 | from toastermain.logs import log_view_mixin |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 36 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 37 | try: |
| 38 | from urllib import unquote_plus |
| 39 | except ImportError: |
| 40 | from urllib.parse import unquote_plus |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 42 | import logging |
| 43 | logger = logging.getLogger("toaster") |
| 44 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 45 | |
| 46 | class NoFieldOrDataName(Exception): |
| 47 | pass |
| 48 | |
| 49 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | class ToasterTable(TemplateView): |
| 51 | def __init__(self, *args, **kwargs): |
| 52 | super(ToasterTable, self).__init__() |
| 53 | if 'template_name' in kwargs: |
| 54 | self.template_name = kwargs['template_name'] |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 55 | self.title = "Table" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | self.queryset = None |
| 57 | self.columns = [] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 58 | |
| 59 | # map from field names to Filter instances |
| 60 | self.filter_map = TableFilterMap() |
| 61 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | self.total_count = 0 |
| 63 | self.static_context_extra = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | self.empty_state = "Sorry - no data found" |
| 65 | self.default_orderby = "" |
| 66 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 67 | # prevent HTTP caching of table data |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 68 | @method_decorator(cache_control(must_revalidate=True, |
| 69 | max_age=0, no_store=True, no_cache=True)) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 70 | def dispatch(self, *args, **kwargs): |
| 71 | return super(ToasterTable, self).dispatch(*args, **kwargs) |
| 72 | |
| 73 | def get_context_data(self, **kwargs): |
| 74 | context = super(ToasterTable, self).get_context_data(**kwargs) |
| 75 | context['title'] = self.title |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 76 | context['table_name'] = type(self).__name__.lower() |
| 77 | context['empty_state'] = self.empty_state |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 78 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 79 | # global variables |
| 80 | context['project_enable'] = ('1' == os.environ.get('TOASTER_BUILDSERVER')) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 81 | try: |
| 82 | context['project_specific'] = ('1' == os.environ.get('TOASTER_PROJECTSPECIFIC')) |
| 83 | except: |
| 84 | context['project_specific'] = '' |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 85 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 86 | return context |
| 87 | |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame^] | 88 | @log_view_mixin |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 89 | def get(self, request, *args, **kwargs): |
| 90 | if request.GET.get('format', None) == 'json': |
| 91 | |
| 92 | self.setup_queryset(*args, **kwargs) |
| 93 | # Put the project id into the context for the static_data_template |
| 94 | if 'pid' in kwargs: |
| 95 | self.static_context_extra['pid'] = kwargs['pid'] |
| 96 | |
| 97 | cmd = request.GET.get('cmd', None) |
| 98 | if cmd and 'filterinfo' in cmd: |
| 99 | data = self.get_filter_info(request, **kwargs) |
| 100 | else: |
| 101 | # If no cmd is specified we give you the table data |
| 102 | data = self.get_data(request, **kwargs) |
| 103 | |
| 104 | return HttpResponse(data, content_type="application/json") |
| 105 | |
| 106 | return super(ToasterTable, self).get(request, *args, **kwargs) |
| 107 | |
| 108 | def get_filter_info(self, request, **kwargs): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | self.setup_filters(**kwargs) |
| 110 | |
| 111 | search = request.GET.get("search", None) |
| 112 | if search: |
| 113 | self.apply_search(search) |
| 114 | |
| 115 | name = request.GET.get("name", None) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 116 | table_filter = self.filter_map.get_filter(name) |
| 117 | return json.dumps(table_filter.to_json(self.queryset), |
| 118 | indent=2, |
| 119 | cls=DjangoJSONEncoder) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | |
| 121 | def setup_columns(self, *args, **kwargs): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 122 | """ function to implement in the subclass which sets up |
| 123 | the columns """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | pass |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 125 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 126 | def setup_filters(self, *args, **kwargs): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 127 | """ function to implement in the subclass which sets up the |
| 128 | filters """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 129 | pass |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 130 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 131 | def setup_queryset(self, *args, **kwargs): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 132 | """ function to implement in the subclass which sets up the |
| 133 | queryset""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 134 | pass |
| 135 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 136 | def add_filter(self, table_filter): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 137 | """Add a filter to the table. |
| 138 | |
| 139 | Args: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 140 | table_filter: Filter instance |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | """ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 142 | self.filter_map.add_filter(table_filter.name, table_filter) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 143 | |
| 144 | def add_column(self, title="", help_text="", |
| 145 | orderable=False, hideable=True, hidden=False, |
| 146 | field_name="", filter_name=None, static_data_name=None, |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 147 | static_data_template=None): |
| 148 | """Add a column to the table. |
| 149 | |
| 150 | Args: |
| 151 | title (str): Title for the table header |
| 152 | help_text (str): Optional help text to describe the column |
| 153 | orderable (bool): Whether the column can be ordered. |
| 154 | We order on the field_name. |
| 155 | hideable (bool): Whether the user can hide the column |
| 156 | hidden (bool): Whether the column is default hidden |
| 157 | field_name (str or list): field(s) required for this column's data |
| 158 | static_data_name (str, optional): The column's main identifier |
| 159 | which will replace the field_name. |
| 160 | static_data_template(str, optional): The template to be rendered |
| 161 | as data |
| 162 | """ |
| 163 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 164 | self.columns.append({'title': title, |
| 165 | 'help_text': help_text, |
| 166 | 'orderable': orderable, |
| 167 | 'hideable': hideable, |
| 168 | 'hidden': hidden, |
| 169 | 'field_name': field_name, |
| 170 | 'filter_name': filter_name, |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 171 | 'static_data_name': static_data_name, |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 172 | 'static_data_template': static_data_template}) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 173 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 174 | def set_column_hidden(self, title, hidden): |
| 175 | """ |
| 176 | Set the hidden state of the column to the value of hidden |
| 177 | """ |
| 178 | for col in self.columns: |
| 179 | if col['title'] == title: |
| 180 | col['hidden'] = hidden |
| 181 | break |
| 182 | |
| 183 | def set_column_hideable(self, title, hideable): |
| 184 | """ |
| 185 | Set the hideable state of the column to the value of hideable |
| 186 | """ |
| 187 | for col in self.columns: |
| 188 | if col['title'] == title: |
| 189 | col['hideable'] = hideable |
| 190 | break |
| 191 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 192 | def render_static_data(self, template, row): |
| 193 | """Utility function to render the static data template""" |
| 194 | |
| 195 | context = { |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 196 | 'extra': self.static_context_extra, |
| 197 | 'data': row, |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | context = Context(context) |
| 201 | template = Template(template) |
| 202 | |
| 203 | return template.render(context) |
| 204 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 205 | def apply_filter(self, filters, filter_value, **kwargs): |
| 206 | """ |
| 207 | Apply a filter submitted in the querystring to the ToasterTable |
| 208 | |
| 209 | filters: (str) in the format: |
| 210 | '<filter name>:<action name>' |
| 211 | filter_value: (str) parameters to pass to the named filter |
| 212 | |
| 213 | <filter name> and <action name> are used to look up the correct filter |
| 214 | in the ToasterTable's filter map; the <action params> are set on |
| 215 | TableFilterAction* before its filter is applied and may modify the |
| 216 | queryset returned by the filter |
| 217 | """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 218 | self.setup_filters(**kwargs) |
| 219 | |
| 220 | try: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 221 | filter_name, action_name = filters.split(':') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 222 | action_params = unquote_plus(filter_value) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 223 | except ValueError: |
| 224 | return |
| 225 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 226 | if "all" in action_name: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 227 | return |
| 228 | |
| 229 | try: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 230 | table_filter = self.filter_map.get_filter(filter_name) |
| 231 | action = table_filter.get_action(action_name) |
| 232 | action.set_filter_params(action_params) |
| 233 | self.queryset = action.filter(self.queryset) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 234 | except KeyError: |
| 235 | # pass it to the user - programming error here |
| 236 | raise |
| 237 | |
| 238 | def apply_orderby(self, orderby): |
| 239 | # Note that django will execute this when we try to retrieve the data |
| 240 | self.queryset = self.queryset.order_by(orderby) |
| 241 | |
| 242 | def apply_search(self, search_term): |
| 243 | """Creates a query based on the model's search_allowed_fields""" |
| 244 | |
| 245 | if not hasattr(self.queryset.model, 'search_allowed_fields'): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 246 | raise Exception("Search fields aren't defined in the model %s" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 247 | % self.queryset.model) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 248 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 249 | search_queries = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 250 | for st in search_term.split(" "): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 251 | queries = None |
| 252 | for field in self.queryset.model.search_allowed_fields: |
| 253 | query = Q(**{field + '__icontains': st}) |
| 254 | if queries: |
| 255 | queries |= query |
| 256 | else: |
| 257 | queries = query |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 258 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 259 | if search_queries: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 260 | search_queries &= queries |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 261 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 262 | search_queries = queries |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 263 | |
| 264 | self.queryset = self.queryset.filter(search_queries) |
| 265 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 266 | def get_data(self, request, **kwargs): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 267 | """ |
| 268 | Returns the data for the page requested with the specified |
| 269 | parameters applied |
| 270 | |
| 271 | filters: filter and action name, e.g. "outcome:build_succeeded" |
| 272 | filter_value: value to pass to the named filter+action, e.g. "on" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 273 | (for a toggle filter) or "2015-12-11,2015-12-12" |
| 274 | (for a date range filter) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 275 | """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 276 | |
| 277 | page_num = request.GET.get("page", 1) |
| 278 | limit = request.GET.get("limit", 10) |
| 279 | search = request.GET.get("search", None) |
| 280 | filters = request.GET.get("filter", None) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 281 | filter_value = request.GET.get("filter_value", "on") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 282 | orderby = request.GET.get("orderby", None) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 283 | nocache = request.GET.get("nocache", None) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 284 | |
| 285 | # Make a unique cache name |
| 286 | cache_name = self.__class__.__name__ |
| 287 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 288 | for key, val in request.GET.items(): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 289 | if key == 'nocache': |
| 290 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 291 | cache_name = cache_name + str(key) + str(val) |
| 292 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 293 | for key, val in kwargs.items(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 294 | cache_name = cache_name + str(key) + str(val) |
| 295 | |
| 296 | # No special chars allowed in the cache name apart from dash |
| 297 | cache_name = re.sub(r'[^A-Za-z0-9-]', "", cache_name) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 298 | |
| 299 | if nocache: |
| 300 | cache.delete(cache_name) |
| 301 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 302 | data = cache.get(cache_name) |
| 303 | |
| 304 | if data: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 305 | logger.debug("Got cache data for table '%s'" % self.title) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 306 | return data |
| 307 | |
| 308 | self.setup_columns(**kwargs) |
| 309 | |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame^] | 310 | self.apply_orderby('pk') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 311 | if search: |
| 312 | self.apply_search(search) |
| 313 | if filters: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 314 | self.apply_filter(filters, filter_value, **kwargs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 315 | if orderby: |
| 316 | self.apply_orderby(orderby) |
| 317 | |
| 318 | paginator = Paginator(self.queryset, limit) |
| 319 | |
| 320 | try: |
| 321 | page = paginator.page(page_num) |
| 322 | except EmptyPage: |
| 323 | page = paginator.page(1) |
| 324 | |
| 325 | data = { |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 326 | 'total': self.queryset.count(), |
| 327 | 'default_orderby': self.default_orderby, |
| 328 | 'columns': self.columns, |
| 329 | 'rows': [], |
| 330 | 'error': "ok", |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 334 | for model_obj in page.object_list: |
| 335 | # Use collection to maintain the order |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 336 | required_data = collections.OrderedDict() |
| 337 | |
| 338 | for col in self.columns: |
| 339 | field = col['field_name'] |
| 340 | if not field: |
| 341 | field = col['static_data_name'] |
| 342 | if not field: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 343 | raise NoFieldOrDataName("Must supply a field_name or" |
| 344 | "static_data_name for column" |
| 345 | "%s.%s" % |
| 346 | (self.__class__.__name__, col) |
| 347 | ) |
| 348 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 349 | # Check if we need to process some static data |
| 350 | if "static_data_name" in col and col['static_data_name']: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 351 | # Overwrite the field_name with static_data_name |
| 352 | # so that this can be used as the html class name |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 353 | col['field_name'] = col['static_data_name'] |
| 354 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 355 | try: |
| 356 | # Render the template given |
| 357 | required_data[col['static_data_name']] = \ |
| 358 | self.render_static_data( |
| 359 | col['static_data_template'], model_obj) |
| 360 | except (TemplateSyntaxError, |
| 361 | VariableDoesNotExist) as e: |
| 362 | logger.error("could not render template code" |
| 363 | "%s %s %s", |
| 364 | col['static_data_template'], |
| 365 | e, self.__class__.__name__) |
| 366 | required_data[col['static_data_name']] =\ |
| 367 | '<!--error-->' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 368 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 369 | else: |
| 370 | # Traverse to any foriegn key in the field |
| 371 | # e.g. recipe__layer_version__name |
| 372 | model_data = None |
| 373 | |
| 374 | if "__" in field: |
| 375 | for subfield in field.split("__"): |
| 376 | if not model_data: |
| 377 | # The first iteration is always going to |
| 378 | # be on the actual model object instance. |
| 379 | # Subsequent ones are on the result of |
| 380 | # that. e.g. forieng key objects |
| 381 | model_data = getattr(model_obj, |
| 382 | subfield) |
| 383 | else: |
| 384 | model_data = getattr(model_data, |
| 385 | subfield) |
| 386 | |
| 387 | else: |
| 388 | model_data = getattr(model_obj, |
| 389 | col['field_name']) |
| 390 | |
| 391 | # We might have a model function as the field so |
| 392 | # call it to return the data needed |
| 393 | if isinstance(model_data, types.MethodType): |
| 394 | model_data = model_data() |
| 395 | |
| 396 | required_data[col['field_name']] = model_data |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 397 | |
| 398 | data['rows'].append(required_data) |
| 399 | |
| 400 | except FieldError: |
| 401 | # pass it to the user - programming-error here |
| 402 | raise |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 403 | |
| 404 | data = json.dumps(data, indent=2, cls=DjangoJSONEncoder) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 405 | cache.set(cache_name, data, 60*30) |
| 406 | |
| 407 | return data |
| 408 | |
| 409 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 410 | class ToasterTypeAhead(View): |
| 411 | """ A typeahead mechanism to support the front end typeahead widgets """ |
| 412 | MAX_RESULTS = 6 |
| 413 | |
| 414 | class MissingFieldsException(Exception): |
| 415 | pass |
| 416 | |
| 417 | def __init__(self, *args, **kwargs): |
| 418 | super(ToasterTypeAhead, self).__init__() |
| 419 | |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame^] | 420 | @log_view_mixin |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 421 | def get(self, request, *args, **kwargs): |
| 422 | def response(data): |
| 423 | return HttpResponse(json.dumps(data, |
| 424 | indent=2, |
| 425 | cls=DjangoJSONEncoder), |
| 426 | content_type="application/json") |
| 427 | |
| 428 | error = "ok" |
| 429 | |
| 430 | search_term = request.GET.get("search", None) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 431 | if search_term is None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 432 | # We got no search value so return empty reponse |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 433 | return response({'error': error, 'results': []}) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 434 | |
| 435 | try: |
| 436 | prj = Project.objects.get(pk=kwargs['pid']) |
| 437 | except KeyError: |
| 438 | prj = None |
| 439 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 440 | results = self.apply_search(search_term, |
| 441 | prj, |
| 442 | request)[:ToasterTypeAhead.MAX_RESULTS] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 443 | |
| 444 | if len(results) > 0: |
| 445 | try: |
| 446 | self.validate_fields(results[0]) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 447 | except self.MissingFieldsException as e: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 448 | error = e |
| 449 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 450 | data = {'results': results, |
| 451 | 'error': error} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 452 | |
| 453 | return response(data) |
| 454 | |
| 455 | def validate_fields(self, result): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 456 | if 'name' in result is False or 'detail' in result is False: |
| 457 | raise self.MissingFieldsException( |
| 458 | "name and detail are required fields") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 459 | |
| 460 | def apply_search(self, search_term, prj): |
| 461 | """ Override this function to implement search. Return an array of |
| 462 | dictionaries with a minium of a name and detail field""" |
| 463 | pass |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 464 | |
| 465 | |
| 466 | class MostRecentBuildsView(View): |
| 467 | def _was_yesterday_or_earlier(self, completed_on): |
| 468 | now = timezone.now() |
| 469 | delta = now - completed_on |
| 470 | |
| 471 | if delta.days >= 1: |
| 472 | return True |
| 473 | |
| 474 | return False |
| 475 | |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame^] | 476 | @log_view_mixin |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 477 | def get(self, request, *args, **kwargs): |
| 478 | """ |
| 479 | Returns a list of builds in JSON format. |
| 480 | """ |
| 481 | project = None |
| 482 | |
| 483 | project_id = request.GET.get('project_id', None) |
| 484 | if project_id: |
| 485 | try: |
| 486 | project = Project.objects.get(pk=project_id) |
| 487 | except: |
| 488 | # if project lookup fails, assume no project |
| 489 | pass |
| 490 | |
| 491 | recent_build_objs = Build.get_recent(project) |
| 492 | recent_builds = [] |
| 493 | |
| 494 | for build_obj in recent_build_objs: |
| 495 | dashboard_url = reverse('builddashboard', args=(build_obj.pk,)) |
| 496 | buildtime_url = reverse('buildtime', args=(build_obj.pk,)) |
| 497 | rebuild_url = \ |
| 498 | reverse('xhr_buildrequest', args=(build_obj.project.pk,)) |
| 499 | cancel_url = \ |
| 500 | reverse('xhr_buildrequest', args=(build_obj.project.pk,)) |
| 501 | |
| 502 | build = {} |
| 503 | build['id'] = build_obj.pk |
| 504 | build['dashboard_url'] = dashboard_url |
| 505 | |
| 506 | buildrequest_id = None |
| 507 | if hasattr(build_obj, 'buildrequest'): |
| 508 | buildrequest_id = build_obj.buildrequest.pk |
| 509 | build['buildrequest_id'] = buildrequest_id |
| 510 | |
Brad Bishop | 5dd7cbb | 2018-09-05 22:26:40 -0700 | [diff] [blame] | 511 | if build_obj.recipes_to_parse > 0: |
| 512 | build['recipes_parsed_percentage'] = \ |
| 513 | int((build_obj.recipes_parsed / |
| 514 | build_obj.recipes_to_parse) * 100) |
| 515 | else: |
| 516 | build['recipes_parsed_percentage'] = 0 |
| 517 | if build_obj.repos_to_clone > 0: |
| 518 | build['repos_cloned_percentage'] = \ |
| 519 | int((build_obj.repos_cloned / |
| 520 | build_obj.repos_to_clone) * 100) |
| 521 | else: |
| 522 | build['repos_cloned_percentage'] = 0 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 523 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 524 | build['progress_item'] = build_obj.progress_item |
| 525 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 526 | tasks_complete_percentage = 0 |
| 527 | if build_obj.outcome in (Build.SUCCEEDED, Build.FAILED): |
| 528 | tasks_complete_percentage = 100 |
| 529 | elif build_obj.outcome == Build.IN_PROGRESS: |
| 530 | tasks_complete_percentage = build_obj.completeper() |
| 531 | build['tasks_complete_percentage'] = tasks_complete_percentage |
| 532 | |
| 533 | build['state'] = build_obj.get_state() |
| 534 | |
| 535 | build['errors'] = build_obj.errors.count() |
| 536 | build['dashboard_errors_url'] = dashboard_url + '#errors' |
| 537 | |
| 538 | build['warnings'] = build_obj.warnings.count() |
| 539 | build['dashboard_warnings_url'] = dashboard_url + '#warnings' |
| 540 | |
| 541 | build['buildtime'] = sectohms(build_obj.timespent_seconds) |
| 542 | build['buildtime_url'] = buildtime_url |
| 543 | |
| 544 | build['rebuild_url'] = rebuild_url |
| 545 | build['cancel_url'] = cancel_url |
| 546 | |
| 547 | build['is_default_project_build'] = build_obj.project.is_default |
| 548 | |
| 549 | build['build_targets_json'] = \ |
| 550 | template_json(get_tasks(build_obj.target_set.all())) |
| 551 | |
| 552 | # convert completed_on time to user's timezone |
| 553 | completed_on = timezone.localtime(build_obj.completed_on) |
| 554 | |
| 555 | completed_on_template = '%H:%M' |
| 556 | if self._was_yesterday_or_earlier(completed_on): |
| 557 | completed_on_template = '%d/%m/%Y ' + completed_on_template |
| 558 | build['completed_on'] = completed_on.strftime( |
| 559 | completed_on_template) |
| 560 | |
| 561 | targets = [] |
| 562 | target_objs = build_obj.get_sorted_target_list() |
| 563 | for target_obj in target_objs: |
| 564 | if target_obj.task: |
| 565 | targets.append(target_obj.target + ':' + target_obj.task) |
| 566 | else: |
| 567 | targets.append(target_obj.target) |
| 568 | build['targets'] = ' '.join(targets) |
| 569 | |
| 570 | # abbreviated form of the full target list |
| 571 | abbreviated_targets = '' |
| 572 | num_targets = len(targets) |
| 573 | if num_targets > 0: |
| 574 | abbreviated_targets = targets[0] |
| 575 | if num_targets > 1: |
| 576 | abbreviated_targets += (' +%s' % (num_targets - 1)) |
| 577 | build['targets_abbreviated'] = abbreviated_targets |
| 578 | |
| 579 | recent_builds.append(build) |
| 580 | |
| 581 | return JsonResponse(recent_builds, safe=False) |