takahe/activities/views/search.py

23 lines
760 B
Python
Raw Normal View History

2022-11-17 17:52:00 -08:00
from django import forms
from django.views.generic import FormView
2022-12-11 10:22:06 -08:00
from activities.search import Searcher
2022-11-17 17:52:00 -08:00
class Search(FormView):
template_name = "activities/search.html"
class form_class(forms.Form):
2022-11-28 20:41:36 -08:00
query = forms.CharField(
2022-12-04 20:13:33 -08:00
help_text="Search for:\nA user by @username@domain or their profile URL\nA hashtag by #tagname\nA post by its URL",
2022-12-03 18:47:09 -08:00
widget=forms.TextInput(attrs={"type": "search", "autofocus": "autofocus"}),
2022-11-28 20:41:36 -08:00
)
2022-11-28 20:41:36 -08:00
def form_valid(self, form):
2022-12-11 10:22:06 -08:00
searcher = Searcher(form.cleaned_data["query"], self.request.identity)
2022-11-17 17:52:00 -08:00
# Render results
context = self.get_context_data(form=form)
2022-12-11 10:22:06 -08:00
context["results"] = searcher.search_all()
2022-11-17 17:52:00 -08:00
return self.render_to_response(context)