-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathviews.py
More file actions
178 lines (147 loc) · 5.74 KB
/
Copy pathviews.py
File metadata and controls
178 lines (147 loc) · 5.74 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from django.contrib.postgres.search import SearchVector
from django.core.paginator import Paginator
from django.db.models import Q
from django.shortcuts import get_object_or_404, render
from django.template.response import TemplateResponse
from prepaid_agreements.forms import FilterForm, SearchForm
from prepaid_agreements.models import PrepaidProduct
from v1.models.snippets import ReusableText
def get_available_filters(products):
available_filters = {"prepaid_type": [], "status": [], "issuer_name": []}
for product in products.all():
prepaid_type = product.prepaid_type
if (
prepaid_type
and prepaid_type != ""
and prepaid_type not in available_filters["prepaid_type"]
):
available_filters["prepaid_type"].append(prepaid_type)
status = product.status
if status and status not in available_filters["status"]:
available_filters["status"].append(status)
issuer_name = product.issuer_name
if issuer_name and issuer_name not in available_filters["issuer_name"]:
available_filters["issuer_name"].append(issuer_name)
for filter_type in available_filters:
available_filters[filter_type] = sorted(available_filters[filter_type])
return available_filters
def search_products(search_term, search_field, products):
search_fields = [
"issuer_name",
"other_relevant_parties",
"name",
"program_manager",
"prepaid_type",
]
if search_field and search_field in search_fields:
search_fields = [search_field]
products = products.annotate(
search=SearchVector(*search_fields),
).filter(search=search_term)
return products
def filter_products(filters, products):
if "issuer_name" in filters:
issuers = Q()
for issuer in filters["issuer_name"]:
issuers |= Q(issuer_name__iexact=issuer)
products = products.filter(issuers)
if "prepaid_type" in filters:
prepaid_types = Q()
for prepaid_type in filters["prepaid_type"]:
prepaid_types |= Q(prepaid_type__iexact=prepaid_type)
products = products.filter(prepaid_types)
if "status" in filters:
products = products.filter(status__iexact=filters["status"][0])
return products
def get_disclaimer_text():
return ReusableText.objects.filter(
title="Prepaid agreements database disclaimer"
).first()
def get_support_text():
return ReusableText.objects.filter(
title="Prepaid agreements support and inquiries"
).first()
def index(request):
products = PrepaidProduct.objects.valid()
total_count = products.count()
valid_filters = ["prepaid_type", "status", "issuer_name"]
available_filters = {}
# Provide valid initial values for the search form so that it is always
# valid. A blank search will return all products.
page_number = 1
search_term = ""
search_field = "all"
search_form = SearchForm(
request.GET,
initial={
"q": search_term,
"search_field": search_field,
"page": page_number,
},
)
# Get our search results. If the form is not valid, our default search_term
# and search_field will be used below.
if search_form.is_valid():
page_number = search_form.cleaned_data["page"]
search_field = search_form.cleaned_data["search_field"]
search_term = search_form.cleaned_data["q"].strip()
if search_term != "":
products = search_products(search_term, search_field, products)
# Get the available filters for products in the search results and then set
# those filter choices on the filter form
filters = {}
available_filters = get_available_filters(products)
filter_form = FilterForm(request.GET)
filter_form.set_issuer_name_choices(available_filters["issuer_name"])
filter_form.set_prepaid_type_choices(available_filters["prepaid_type"])
filter_form.set_status_choices(available_filters["status"])
# Filter our products based on the sanitized values from the filter form.
# If the form is not valid, the products already selected based on the
# search form will be used below.
if filter_form.is_valid():
filters = {
k: filter_form.cleaned_data[k]
for k in valid_filters
if filter_form.cleaned_data[k]
}
products = filter_products(filters, products)
current_count = products.count()
# Handle pagination
paginator = Paginator(products.all(), 25)
page = paginator.get_page(page_number)
pagination_params = dict(filters)
if search_term:
pagination_params["q"] = search_term
if search_field:
pagination_params["search_field"] = search_field
return TemplateResponse(
request,
"prepaid_agreements/index.html",
{
"current_page": page.number,
"results": page,
"total_count": total_count,
"paginator": paginator,
"current_count": current_count,
"filters": filters,
"query": search_term or "",
"active_filters": available_filters,
"valid_filters": valid_filters,
"search_field": search_field,
"disclaimer_text": get_disclaimer_text(),
"support_text": get_support_text(),
"pagination_params": pagination_params,
},
)
def detail(request, product_id):
return render(
request,
"prepaid_agreements/detail.html",
{
"product": get_object_or_404(
PrepaidProduct.objects.valid(), pk=product_id
),
"disclaimer_text": get_disclaimer_text(),
"support_text": get_support_text(),
},
)