comparison custom_search/forms.py @ 763:20a3bf7a6370

Add username to search query logging.
author Brian Neal <bgneal@gmail.com>
date Sun, 19 Jan 2014 11:25:45 -0600
parents ad53d929281a
children cf9918328c64
comparison
equal deleted inserted replaced
762:840f2579ef1c 763:20a3bf7a6370
31 31
32 We also provide "all words", "exact phrase", and "exclude" text input boxes. 32 We also provide "all words", "exact phrase", and "exclude" text input boxes.
33 Haystack 2.1.0's auto_query() function did not seem to work right so we just 33 Haystack 2.1.0's auto_query() function did not seem to work right so we just
34 rolled our own. 34 rolled our own.
35 35
36 This form can optionally receive the user making the search as a keyword
37 argument ('user') to __init__. This will be used for logging search queries.
38
36 """ 39 """
37 q = forms.CharField(required=False, label='All these words', 40 q = forms.CharField(required=False, label='All these words',
38 widget=forms.TextInput(attrs={'type': 'search', 'class': 'search', 41 widget=forms.TextInput(attrs={'type': 'search', 'class': 'search',
39 'size': 48})) 42 'size': 48}))
40 exact = forms.CharField(required=False, label='This exact word or phrase', 43 exact = forms.CharField(required=False, label='This exact word or phrase',
43 exclude = forms.CharField(required=False, label='None of these words', 46 exclude = forms.CharField(required=False, label='None of these words',
44 widget=forms.TextInput(attrs={'type': 'search', 'class': 'search', 47 widget=forms.TextInput(attrs={'type': 'search', 'class': 'search',
45 'size': 48})) 48 'size': 48}))
46 49
47 def __init__(self, *args, **kwargs): 50 def __init__(self, *args, **kwargs):
51 self.user = kwargs.pop('user', None)
48 super(CustomModelSearchForm, self).__init__(*args, **kwargs) 52 super(CustomModelSearchForm, self).__init__(*args, **kwargs)
49 self.fields['models'] = forms.MultipleChoiceField(choices=MODEL_CHOICES, 53 self.fields['models'] = forms.MultipleChoiceField(choices=MODEL_CHOICES,
50 label='Search in', widget=forms.CheckboxSelectMultiple) 54 label='Search in', widget=forms.CheckboxSelectMultiple)
51 55
52 def clean(self): 56 def clean(self):
63 67
64 def search(self): 68 def search(self):
65 if not self.is_valid(): 69 if not self.is_valid():
66 return self.no_query_found() 70 return self.no_query_found()
67 71
68 logger.info('Search executed: /%s/%s/%s/ in %s', 72 if self.user is None:
73 username = 'UNKNOWN'
74 elif self.user.is_authenticated():
75 username = self.user.username
76 else:
77 username = 'ANONYMOUS'
78
79 logger.info('Search executed: /%s/%s/%s/ in %s by %s',
69 self.cleaned_data['q'], 80 self.cleaned_data['q'],
70 self.cleaned_data['exact'], 81 self.cleaned_data['exact'],
71 self.cleaned_data['exclude'], 82 self.cleaned_data['exclude'],
72 self.cleaned_data['models']) 83 self.cleaned_data['models'],
84 username)
73 85
74 sqs = self.searchqueryset 86 sqs = self.searchqueryset
75 87
76 # Note that in Haystack 2.x content is untrusted and is automatically 88 # Note that in Haystack 2.x content is untrusted and is automatically
77 # auto-escaped for us. 89 # auto-escaped for us.