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