Skip to content

Commit c6a48ab

Browse files
twiditoastdriven
authored andcommitted
Refactored the save code into its own method, which can now be overriden.
1 parent fe4db92 commit c6a48ab

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

AUTHORS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Primary author:
44

55
Contributors:
66

7-
*
7+
* Stéphane Angel (twidi) for factoring out the save method in ``SavedSearchView``.

saved_searches/views.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,65 @@ class SavedSearchView(SearchView):
1818
Automatically handles saving the queries when they are run.
1919
"""
2020
search_key = 'general'
21-
21+
2222
def __init__(self, *args, **kwargs):
2323
if 'search_key' in kwargs:
2424
self.search_key = kwargs['search_key']
2525
del(kwargs['search_key'])
26-
26+
2727
super(SavedSearchView, self).__init__(*args, **kwargs)
28-
29-
def create_response(self):
28+
29+
def save_search(self, page):
3030
"""
31-
Saves the details of a user's search and then generates the actual
32-
HttpResponse to send back to the user.
31+
Only save the search if we're on the first page.
32+
This will prevent an excessive number of duplicates for what is
33+
essentially the same search.
3334
"""
34-
(paginator, page) = self.build_page()
35-
36-
# Only save the search if we're on the first page.
37-
# This will prevent an excessive number of duplicates for what is
38-
# essentially the same search.
3935
if self.query and page.number == 1:
4036
# Save the search.
4137
saved_search = SavedSearch(
4238
search_key=self.search_key,
4339
user_query=self.query,
4440
result_count=len(self.results)
4541
)
46-
42+
4743
if hasattr(self.results, 'query'):
4844
query_seen = self.results.query.build_query()
49-
45+
5046
if isinstance(query_seen, basestring):
5147
saved_search.full_query = query_seen
52-
48+
5349
if self.request.user.is_authenticated():
5450
saved_search.user = self.request.user
55-
51+
5652
saved_search.save()
57-
53+
54+
def create_response(self):
55+
"""
56+
Saves the details of a user's search and then generates the actual
57+
``HttpResponse`` to send back to the user.
58+
"""
59+
(paginator, page) = self.build_page()
60+
self.save_search(page)
61+
5862
context = {
5963
'query': self.query,
6064
'form': self.form,
6165
'page': page,
6266
'paginator': paginator,
6367
}
6468
context.update(self.extra_context())
65-
69+
6670
return render_to_response(self.template, context, context_instance=self.context_class(self.request))
6771

6872

6973
def most_recent(request, username=None, search_key=None):
7074
"""
7175
Shows the most recent search results.
72-
76+
7377
The ``username`` kwarg should be the ``username`` field of
7478
``django.contrib.auth.models.User``. The ``search_key`` can be any string.
75-
79+
7680
Template::
7781
``saved_searches/most_recent.html``
7882
Context::
@@ -89,15 +93,15 @@ def most_recent(request, username=None, search_key=None):
8993
user = get_object_or_404(User, username=username)
9094
else:
9195
user = None
92-
96+
9397
most_recent = SavedSearch.objects.most_recent(user=user, search_key=search_key, threshold=SAVED_SEARCHES_THRESHOLD)
9498
paginator = Paginator(most_recent, SAVED_SEARCHES_PER_PAGE)
95-
99+
96100
try:
97101
page = paginator.page(int(request.GET.get('page', 1)))
98102
except InvalidPage:
99103
raise Http404("Invalid page.")
100-
104+
101105
return render_to_response('saved_searches/most_recent.html', {
102106
'by_user': user,
103107
'by_search_key': search_key,
@@ -109,10 +113,10 @@ def most_recent(request, username=None, search_key=None):
109113
def most_popular(request, username=None, search_key=None):
110114
"""
111115
Shows the most popular search results.
112-
116+
113117
The ``username`` kwarg should be the ``username`` field of
114118
``django.contrib.auth.models.User``. The ``search_key`` can be any string.
115-
119+
116120
Template::
117121
``saved_searches/most_popular.html``
118122
Context::
@@ -129,19 +133,19 @@ def most_popular(request, username=None, search_key=None):
129133
user = get_object_or_404(User, username=username)
130134
else:
131135
user = None
132-
136+
133137
most_recent = SavedSearch.objects.most_popular(user=user, search_key=search_key, threshold=SAVED_SEARCHES_THRESHOLD)
134138
paginator = Paginator(most_recent, SAVED_SEARCHES_PER_PAGE)
135-
139+
136140
try:
137141
page = paginator.page(int(request.GET.get('page', 1)))
138142
except InvalidPage:
139143
raise Http404("Invalid page.")
140-
144+
141145
return render_to_response('saved_searches/most_popular.html', {
142146
'by_user': user,
143147
'by_search_key': search_key,
144148
'page': page,
145149
'paginator': paginator,
146150
}, context_instance=RequestContext(request))
147-
151+

0 commit comments

Comments
 (0)