-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathviews.py
More file actions
136 lines (115 loc) · 4.27 KB
/
Copy pathviews.py
File metadata and controls
136 lines (115 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# purldb is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/purldb for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
from packagedb.api import PackageFilterSet
from packagedb.models import Package
from django.views.generic.detail import DetailView
from django_filters.views import FilterView
PAGE_SIZE = 20
class TableColumnsMixin:
"""
table_columns = [
"<field_name>",
"<field_name>",
{
"field_name": "<field_name>",
"label": None,
"condition": None,
"sort_name": None,
"css_class": None,
},
]
"""
table_columns = []
def get_columns_data(self):
"""Return the columns data structure used in template rendering."""
columns_data = []
sortable_fields = []
active_sort = ""
filterset = getattr(self, "filterset", None)
if filterset and "sort" in filterset.filters:
sortable_fields = list(filterset.filters["sort"].param_map.keys())
active_sort = filterset.data.get("sort", "")
for column_definition in self.table_columns:
# Support for single "field_name" entry in columns list.
if not isinstance(column_definition, dict):
field_name = column_definition
column_data = {"field_name": field_name}
else:
field_name = column_definition.get("field_name")
column_data = column_definition.copy()
condition = column_data.get("condition", None)
if condition is not None and not bool(condition):
continue
if "label" not in column_data:
column_data["label"] = self.get_field_label(field_name)
sort_name = column_data.get("sort_name") or field_name
if sort_name in sortable_fields:
is_sorted = sort_name == active_sort.lstrip("-")
sort_direction = ""
if is_sorted and not active_sort.startswith("-"):
sort_direction = "-"
column_data["is_sorted"] = is_sorted
column_data["sort_direction"] = sort_direction
query_dict = self.request.GET.copy()
query_dict["sort"] = f"{sort_direction}{sort_name}"
column_data["sort_query"] = query_dict.urlencode()
if filter_fieldname := column_data.get("filter_fieldname"):
column_data["filter"] = filterset.form[filter_fieldname]
columns_data.append(column_data)
return columns_data
@staticmethod
def get_field_label(field_name):
"""Return a formatted label for display based on the `field_name`."""
return field_name.replace("_", " ").capitalize().replace("url", "URL")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["columns_data"] = self.get_columns_data()
context["request_query_string"] = self.request.GET.urlencode()
return context
class PackageListView(
TableColumnsMixin,
FilterView
):
model = Package
filterset_class = PackageFilterSet
paginate_by = PAGE_SIZE
template_name = "package/package_list.html"
table_columns = [
{
"field_name": "package_url",
"label": "Purl",
"sort_name": "projectmessages_count",
},
"name",
"type",
{
"field_name": "other_license_expression_spdx",
"label": "License",
},
{
"field_name": "download_url",
"label": "Download Url",
},
{
"field_name": "created_date",
"label": "Created Date",
"sort_name": "created_date",
}
]
class PackageDetailView(DetailView):
model = Package
slug_field = "uuid"
slug_url_kwarg = "uuid"
template_name = "package/package_detail.html"
def get_queryset(self):
return (
super()
.get_queryset()
.prefetch_related("parties", "dependencies")
)