@@ -18,61 +18,65 @@ class SavedSearchView(SearchView):
18
18
Automatically handles saving the queries when they are run.
19
19
"""
20
20
search_key = 'general'
21
-
21
+
22
22
def __init__ (self , * args , ** kwargs ):
23
23
if 'search_key' in kwargs :
24
24
self .search_key = kwargs ['search_key' ]
25
25
del (kwargs ['search_key' ])
26
-
26
+
27
27
super (SavedSearchView , self ).__init__ (* args , ** kwargs )
28
-
29
- def create_response (self ):
28
+
29
+ def save_search (self , page ):
30
30
"""
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.
33
34
"""
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.
39
35
if self .query and page .number == 1 :
40
36
# Save the search.
41
37
saved_search = SavedSearch (
42
38
search_key = self .search_key ,
43
39
user_query = self .query ,
44
40
result_count = len (self .results )
45
41
)
46
-
42
+
47
43
if hasattr (self .results , 'query' ):
48
44
query_seen = self .results .query .build_query ()
49
-
45
+
50
46
if isinstance (query_seen , basestring ):
51
47
saved_search .full_query = query_seen
52
-
48
+
53
49
if self .request .user .is_authenticated ():
54
50
saved_search .user = self .request .user
55
-
51
+
56
52
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
+
58
62
context = {
59
63
'query' : self .query ,
60
64
'form' : self .form ,
61
65
'page' : page ,
62
66
'paginator' : paginator ,
63
67
}
64
68
context .update (self .extra_context ())
65
-
69
+
66
70
return render_to_response (self .template , context , context_instance = self .context_class (self .request ))
67
71
68
72
69
73
def most_recent (request , username = None , search_key = None ):
70
74
"""
71
75
Shows the most recent search results.
72
-
76
+
73
77
The ``username`` kwarg should be the ``username`` field of
74
78
``django.contrib.auth.models.User``. The ``search_key`` can be any string.
75
-
79
+
76
80
Template::
77
81
``saved_searches/most_recent.html``
78
82
Context::
@@ -89,15 +93,15 @@ def most_recent(request, username=None, search_key=None):
89
93
user = get_object_or_404 (User , username = username )
90
94
else :
91
95
user = None
92
-
96
+
93
97
most_recent = SavedSearch .objects .most_recent (user = user , search_key = search_key , threshold = SAVED_SEARCHES_THRESHOLD )
94
98
paginator = Paginator (most_recent , SAVED_SEARCHES_PER_PAGE )
95
-
99
+
96
100
try :
97
101
page = paginator .page (int (request .GET .get ('page' , 1 )))
98
102
except InvalidPage :
99
103
raise Http404 ("Invalid page." )
100
-
104
+
101
105
return render_to_response ('saved_searches/most_recent.html' , {
102
106
'by_user' : user ,
103
107
'by_search_key' : search_key ,
@@ -109,10 +113,10 @@ def most_recent(request, username=None, search_key=None):
109
113
def most_popular (request , username = None , search_key = None ):
110
114
"""
111
115
Shows the most popular search results.
112
-
116
+
113
117
The ``username`` kwarg should be the ``username`` field of
114
118
``django.contrib.auth.models.User``. The ``search_key`` can be any string.
115
-
119
+
116
120
Template::
117
121
``saved_searches/most_popular.html``
118
122
Context::
@@ -129,19 +133,19 @@ def most_popular(request, username=None, search_key=None):
129
133
user = get_object_or_404 (User , username = username )
130
134
else :
131
135
user = None
132
-
136
+
133
137
most_recent = SavedSearch .objects .most_popular (user = user , search_key = search_key , threshold = SAVED_SEARCHES_THRESHOLD )
134
138
paginator = Paginator (most_recent , SAVED_SEARCHES_PER_PAGE )
135
-
139
+
136
140
try :
137
141
page = paginator .page (int (request .GET .get ('page' , 1 )))
138
142
except InvalidPage :
139
143
raise Http404 ("Invalid page." )
140
-
144
+
141
145
return render_to_response ('saved_searches/most_popular.html' , {
142
146
'by_user' : user ,
143
147
'by_search_key' : search_key ,
144
148
'page' : page ,
145
149
'paginator' : paginator ,
146
150
}, context_instance = RequestContext (request ))
147
-
151
+
0 commit comments