1# Copyright 2013-2019 The Distro Tracker Developers
2# See the COPYRIGHT file at the top-level directory of this distribution and
3# at https://deb.li/DTAuthors
4#
5# This file is part of Distro Tracker. It is subject to the license terms
6# in the LICENSE file found in the top-level directory of this
7# distribution and at https://deb.li/DTLicense. No part of Distro Tracker,
8# including this file, may be copied, modified, propagated, or distributed
9# except according to the terms contained in the LICENSE file.
10"""Add debci-specific fields to the package tables shown on team pages."""
12from django.db.models import Prefetch
14from distro_tracker.core.models import (
15 PackageData,
16)
17from distro_tracker.core.package_tables import (
18 BaseTableField,
19)
22class DebciTableField(BaseTableField):
23 """
24 This table field displays information regarding the Debci status for
25 this package.
27 It displays the package's Debci status
28 """
29 column_name = 'Tests'
30 slug = 'debci'
31 template_name = 'debci_status/debci.html'
32 prefetch_related_lookups = [
33 Prefetch(
34 'data',
35 queryset=PackageData.objects.filter(key='debci'),
36 to_attr='debci'
37 )
38 ]
40 def context(self, package):
41 ctx = {}
43 try:
44 # build list of status hashes, for popover
45 ctx['statuses'] = []
46 for debci in sorted(getattr(package, 'debci', [])[0].value,
47 key=lambda e: e['repository']):
48 status = debci['result']['status']
49 repository = debci['repository']
50 ctx['statuses'].append({'repository': repository,
51 'status': status})
53 # url is per-package, not per repository, so re-using last value of
54 # loop variable is fine
55 ctx['url'] = debci['url']
57 # aggregated status to display in column
58 statuses = [e['status'] for e in ctx['statuses']]
59 ctx['aggregated_status'] = ' | '.join(statuses)
61 # success = all pass, danger = all fail, warning = mixed
62 if 'fail' in statuses and 'pass' in statuses:
63 ctx['label_type'] = 'warning'
64 elif 'fail' in statuses:
65 ctx['label_type'] = 'danger'
66 else:
67 ctx['label_type'] = 'success'
68 except IndexError:
69 # There is no debci info for the package
70 ctx['url'] = None
72 return ctx