# HG changeset patch # User Brian Neal # Date 1390152345 21600 # Node ID 20a3bf7a637060ae9130e532ee66c2d19adf4646 # Parent 840f2579ef1c4975813c41c9293d31ed81f18ee3 Add username to search query logging. diff -r 840f2579ef1c -r 20a3bf7a6370 custom_search/forms.py --- a/custom_search/forms.py Sun Jan 19 00:19:44 2014 -0600 +++ b/custom_search/forms.py Sun Jan 19 11:25:45 2014 -0600 @@ -33,6 +33,9 @@ Haystack 2.1.0's auto_query() function did not seem to work right so we just rolled our own. + This form can optionally receive the user making the search as a keyword + argument ('user') to __init__. This will be used for logging search queries. + """ q = forms.CharField(required=False, label='All these words', widget=forms.TextInput(attrs={'type': 'search', 'class': 'search', @@ -45,6 +48,7 @@ 'size': 48})) def __init__(self, *args, **kwargs): + self.user = kwargs.pop('user', None) super(CustomModelSearchForm, self).__init__(*args, **kwargs) self.fields['models'] = forms.MultipleChoiceField(choices=MODEL_CHOICES, label='Search in', widget=forms.CheckboxSelectMultiple) @@ -65,11 +69,19 @@ if not self.is_valid(): return self.no_query_found() - logger.info('Search executed: /%s/%s/%s/ in %s', + if self.user is None: + username = 'UNKNOWN' + elif self.user.is_authenticated(): + username = self.user.username + else: + username = 'ANONYMOUS' + + logger.info('Search executed: /%s/%s/%s/ in %s by %s', self.cleaned_data['q'], self.cleaned_data['exact'], self.cleaned_data['exclude'], - self.cleaned_data['models']) + self.cleaned_data['models'], + username) sqs = self.searchqueryset diff -r 840f2579ef1c -r 20a3bf7a6370 custom_search/views.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/custom_search/views.py Sun Jan 19 11:25:45 2014 -0600 @@ -0,0 +1,17 @@ +"""Custom views for searching.""" + +from haystack.views import SearchView + + +class UserSearchView(SearchView): + """This class passes the user making the search as an __init__ argument to + the search form as the keyword argument 'user'. + + """ + def build_form(self, form_kwargs=None): + """Pass the request.user object to the form's constructor.""" + if not form_kwargs: + form_kwargs = {} + if 'user' not in form_kwargs: + form_kwargs['user'] = self.request.user + return super(UserSearchView, self).build_form(form_kwargs) diff -r 840f2579ef1c -r 20a3bf7a6370 sg101/urls.py --- a/sg101/urls.py Sun Jan 19 00:19:44 2014 -0600 +++ b/sg101/urls.py Sun Jan 19 11:25:45 2014 -0600 @@ -11,6 +11,7 @@ from news.feeds import LatestNewsFeed from forums.feeds import ForumsFeed from custom_search.forms import CustomModelSearchForm +from custom_search.views import UserSearchView from core.views import FixedView @@ -93,12 +94,14 @@ urlpatterns += patterns('haystack.views', url(r'^search/$', - search_view_factory(form_class=CustomModelSearchForm, + search_view_factory(view_class=UserSearchView, + form_class=CustomModelSearchForm, searchqueryset=sqs, load_all=True), name='haystack_search'), url(r'^search/ajax/$', - search_view_factory(template='search/search_ajax.html', + search_view_factory(view_class=UserSearchView, + template='search/search_ajax.html', form_class=CustomModelSearchForm, searchqueryset=sqs, load_all=True),