Skip to content

Commit 4702108

Browse files
committed
remove cookies all together
1 parent a1cc914 commit 4702108

File tree

5 files changed

+59
-72
lines changed

5 files changed

+59
-72
lines changed

tcf_core/context_processors.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Inject extra context to TCF templates."""
22

3-
import ast
4-
53
from django.conf import settings
64

75
from tcf_website.models import Discipline, Semester, Subdepartment
@@ -16,33 +14,6 @@ def base(request):
1614
}
1715

1816

19-
def history_cookies(request):
20-
"""Puts history from cookies into context variables."""
21-
if "previous_paths" in request.COOKIES:
22-
previous_paths = request.COOKIES["previous_paths"]
23-
previous_paths = ast.literal_eval(previous_paths)
24-
else:
25-
previous_paths = ""
26-
27-
if "previous_paths_titles" in request.COOKIES:
28-
previous_paths_titles = request.COOKIES["previous_paths_titles"]
29-
previous_paths_titles = ast.literal_eval(previous_paths_titles)
30-
else:
31-
previous_paths_titles = ""
32-
33-
previous_paths_titles = [title[:80] for title in previous_paths_titles]
34-
35-
previous_paths_and_titles = None
36-
if len(previous_paths) > 0 and len(previous_paths_titles) > 0:
37-
previous_paths_and_titles = zip(previous_paths, previous_paths_titles)
38-
39-
return {
40-
"previous_paths": previous_paths,
41-
"previous_path_titles": previous_paths_titles,
42-
"previous_paths_and_titles": previous_paths_and_titles,
43-
}
44-
45-
4617
def searchbar_context(request):
4718
"""Provide context for the search bar."""
4819
latest_semester = Semester.latest()

tcf_core/settings/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@
131131
"django.contrib.auth.context_processors.auth",
132132
"django.contrib.messages.context_processors.messages",
133133
"tcf_core.context_processors.base",
134-
"tcf_core.context_processors.history_cookies",
135134
"tcf_core.context_processors.searchbar_context",
136135
],
137136
},

tcf_core/settings/record_middleware.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
11
"""Middleware for recording cookie information."""
22

3-
import ast
4-
53

64
class RecordMiddleware: # pylint: disable=too-few-public-methods
7-
"""Records information about course section info into cookies."""
5+
"""
6+
Previously recorded course section info into cookies.
7+
Now does nothing as this functionality has been moved to client-side
8+
localStorage to avoid polluting Django sessions.
9+
"""
810

911
def __init__(self, get_response):
1012
self.get_response = get_response
1113

1214
def __call__(self, request):
13-
if "previous_paths_titles" in request.COOKIES:
14-
previous_paths = request.COOKIES["previous_paths"]
15-
# Converts string representation of list into list object
16-
previous_paths = ast.literal_eval(previous_paths)
17-
18-
previous_paths_titles = request.COOKIES["previous_paths_titles"]
19-
# Converts string representation of list into list object
20-
previous_paths_titles = ast.literal_eval(previous_paths_titles)
21-
else:
22-
previous_paths = []
23-
previous_paths_titles = []
24-
2515
# Process the request and get response
2616
response = self.get_response(request)
2717

28-
# We no longer manage history through the middleware
29-
# This is now handled by client-side JavaScript using localStorage
30-
# The cookie is still retained for backwards compatibility
31-
# but it's populated by JavaScript, not by the middleware
18+
# We no longer set cookies for history tracking
19+
# All history is now managed via localStorage in the browser
3220

3321
return response
3422

tcf_website/static/common/recently_viewed.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,4 @@ function saveCourseInfoIfPresent() {
7979
"previous_paths_titles",
8080
JSON.stringify(previousPathsTitles),
8181
);
82-
83-
// Also set cookies for compatibility with existing server-side code
84-
setCookie("previous_paths", JSON.stringify(previousPaths), 30);
85-
setCookie("previous_paths_titles", JSON.stringify(previousPathsTitles), 30);
86-
}
87-
88-
/**
89-
* Helper function to set cookies
90-
*/
91-
function setCookie(name, value, days) {
92-
let expires = "";
93-
if (days) {
94-
const date = new Date();
95-
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
96-
expires = "; expires=" + date.toUTCString();
97-
}
98-
document.cookie = name + "=" + (value || "") + expires + "; path=/";
9982
}

tcf_website/templates/common/history_page_modal.html

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,62 @@ <h5 class="modal-title font-weight-bold">Recently Viewed</h5>
1212
</div>
1313
<div class="modal-body py-0 px-4">
1414
<div class="text-center">
15-
{% if previous_paths_and_titles is None %}
15+
<div id="history-items-container">
1616
<p class="text-muted my-4">No course sections viewed yet.</p>
17-
{% endif %}
18-
<div class="list-group list-group-flush mb-3">
19-
{% for path, title in previous_paths_and_titles %}
20-
<a class="list-group-item list-group-item-action border-left-0 border-right-0 rounded-0" href={{path}}>{{title}}</a>
21-
{% endfor %}
2217
</div>
2318
</div>
2419
</div>
2520
</div>
2621
</div>
2722
</div>
23+
24+
<script>
25+
// Load and display history from localStorage when the modal is opened
26+
document.addEventListener('DOMContentLoaded', function() {
27+
// Get the modal element
28+
const historyModal = document.getElementById('{{ path_id }}');
29+
30+
// Add event listener for when the modal is shown
31+
$(historyModal).on('show.bs.modal', function() {
32+
loadHistoryFromLocalStorage();
33+
});
34+
35+
// Function to load history from localStorage
36+
function loadHistoryFromLocalStorage() {
37+
const container = document.getElementById('history-items-container');
38+
39+
try {
40+
// Get history from localStorage
41+
const paths = JSON.parse(localStorage.getItem('previous_paths') || '[]');
42+
const titles = JSON.parse(localStorage.getItem('previous_paths_titles') || '[]');
43+
44+
// Clear the container
45+
container.innerHTML = '';
46+
47+
// If no history, show message
48+
if (paths.length === 0 || titles.length === 0) {
49+
container.innerHTML = '<p class="text-muted my-4">No course sections viewed yet.</p>';
50+
return;
51+
}
52+
53+
// Create list group
54+
const listGroup = document.createElement('div');
55+
listGroup.className = 'list-group list-group-flush mb-3';
56+
57+
// Add each history item
58+
for (let i = 0; i < Math.min(paths.length, titles.length); i++) {
59+
const link = document.createElement('a');
60+
link.className = 'list-group-item list-group-item-action border-left-0 border-right-0 rounded-0';
61+
link.href = paths[i];
62+
link.textContent = titles[i];
63+
listGroup.appendChild(link);
64+
}
65+
66+
container.appendChild(listGroup);
67+
} catch (e) {
68+
console.error('Error loading history from localStorage:', e);
69+
container.innerHTML = '<p class="text-muted my-4">Error loading history.</p>';
70+
}
71+
}
72+
});
73+
</script>

0 commit comments

Comments
 (0)