Skip to content

Commit 91935d8

Browse files
committed
Get rid of invalid filters in the URL.
1 parent 98ac8a9 commit 91935d8

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

pydis_site/apps/resources/views/resources.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import typing as t
23
from pathlib import Path
34

@@ -8,6 +9,7 @@
89
from django.views import View
910

1011
from pydis_site import settings
12+
from pydis_site.apps.resources.templatetags.as_css_class import as_css_class
1113

1214
RESOURCES_PATH = Path(settings.BASE_DIR, "pydis_site", "apps", "resources", "resources")
1315

@@ -83,6 +85,14 @@ def __init__(self, *args, **kwargs):
8385
self.filters["Topics"]["filters"].remove("Other")
8486
self.filters["Topics"]["filters"].append("Other")
8587

88+
# A complete list of valid filter names
89+
self.valid_filters = {
90+
"topics": [as_css_class(topic) for topic in self.filters["Topics"]["filters"]],
91+
"payment_tiers": [as_css_class(tier) for tier in self.filters["Payment tiers"]["filters"]],
92+
"type": [as_css_class(type_) for type_ in self.filters["Type"]["filters"]],
93+
"difficulty": [as_css_class(tier) for tier in self.filters["Difficulty"]["filters"]],
94+
}
95+
8696
def get(self, request: WSGIRequest, resource_type: t.Optional[str] = None) -> HttpResponse:
8797
"""List out all the resources, and any filtering options from the URL."""
8898
# Add type filtering if the request is made to somewhere like /resources/video.
@@ -101,6 +111,7 @@ def get(self, request: WSGIRequest, resource_type: t.Optional[str] = None) -> Ht
101111
context={
102112
"resources": self.resources,
103113
"filters": self.filters,
114+
"valid_filters": json.dumps(self.valid_filters),
104115
"resource_type": resource_type,
105116
}
106117
)

pydis_site/static/js/resources/resources.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,27 @@ function deserializeURLParams() {
6767
if (paramFilterContent !== null) {
6868
// We use split here because we always want an array, not a string.
6969
let paramFilterArray = paramFilterContent.split(",");
70-
activeFilters[filterType] = paramFilterArray;
7170

7271
// Update the corresponding filter UI, so it reflects the internal state.
7372
$(paramFilterArray).each(function(_, filter) {
74-
let checkbox = $(`.filter-checkbox[data-filter-name='${filterType}'][data-filter-item='${filter}']`);
75-
let filterTag = $(`.filter-box-tag[data-filter-name='${filterType}'][data-filter-item='${filter}']`);
76-
let resourceTags = $(`.resource-tag[data-filter-name='${filterType}'][data-filter-item='${filter}']`);
77-
checkbox.prop("checked", true);
78-
filterTag.show();
79-
resourceTags.addClass("active");
73+
// Make sure the filter is valid before we do anything.
74+
if (String(filter) === "rickroll" && filterType === "type") {
75+
window.location.href = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
76+
} else if (String(filter) === "sneakers" && filterType === "topics") {
77+
window.location.href = "https://www.youtube.com/watch?v=NNZscmNE9QI";
78+
} else if (validFilters[filterType].includes(String(filter))) {
79+
let checkbox = $(`.filter-checkbox[data-filter-name='${filterType}'][data-filter-item='${filter}']`);
80+
let filterTag = $(`.filter-box-tag[data-filter-name='${filterType}'][data-filter-item='${filter}']`);
81+
let resourceTags = $(`.resource-tag[data-filter-name='${filterType}'][data-filter-item='${filter}']`);
82+
checkbox.prop("checked", true);
83+
filterTag.show();
84+
resourceTags.addClass("active");
85+
activeFilters[filterType].push(filter);
86+
}
8087
});
88+
89+
// Ditch all the params from the URL, and recalculate the URL params
90+
updateURL();
8191
}
8292
});
8393
}

pydis_site/templates/resources/resources.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
{% block title %}Resources{% endblock %}
88
{% block head %}
9+
{# Inject a JSON object of all valid filter types from the view #}
10+
<script>
11+
const validFilters = {{ valid_filters | safe }}
12+
</script>
13+
914
<link rel="stylesheet" href="{% static "css/resources/resources.css" %}">
1015
<link rel="stylesheet" href="{% static "css/collapsibles.css" %}">
1116
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

0 commit comments

Comments
 (0)