Skip to content

Instantly share code, notes, and snippets.

@earthling-shruti
Created August 20, 2013 19:18
Show Gist options
  • Save earthling-shruti/6285935 to your computer and use it in GitHub Desktop.
Save earthling-shruti/6285935 to your computer and use it in GitHub Desktop.
class SearchView(FormView):
"""
Class that fetches and displays search results
"""
template_name = 'search.html'
form_class = SearchForm
def form_valid(self, form):
"""
Increments the count of the search phrase if it has been made before,
retrieves the latest results using the Twitter API and displays them
"""
# form = self.form_class(request.POST)
if form.is_valid():
search_phrase = form.cleaned_data['search_phrase']
try:
existing_search_term = SearchTerm.objects.get(phrase=search_phrase)
except SearchTerm.DoesNotExist:
# create a new search term
new_search_term = SearchTerm(phrase=search_phrase, repeat_count=1,
last_searched_date=datetime.utcnow().replace(tzinfo=utc))
new_search_term.save()
else:
# update the existing search term
existing_search_term.repeat_count += 1
existing_search_term.last_searched_date = datetime.utcnow().replace(tzinfo=utc)
existing_search_term.save()
search_results = self.twitter_search(search_phrase)
context = RequestContext(self.request, {'form': form,
'search_phrase': search_phrase,
'search_results': search_results})
return render_to_response('search.html', context)
else:
error = _("Please enter a search term.")
context = RequestContext(self.request, {'form': form,
'error_message': error})
return render_to_response('index.html', context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment