diff options
Diffstat (limited to 'djangopypi/views/search.py')
-rw-r--r-- | djangopypi/views/search.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/djangopypi/views/search.py b/djangopypi/views/search.py new file mode 100644 index 0000000..5d6a76b --- /dev/null +++ b/djangopypi/views/search.py | |||
@@ -0,0 +1,24 @@ | |||
1 | from django.template import RequestContext | ||
2 | from django.shortcuts import render_to_response | ||
3 | from django.db.models.query import Q | ||
4 | |||
5 | from djangopypi.models import Project | ||
6 | |||
7 | |||
8 | def _search_query(q): | ||
9 | return Q(name__contains=q) | Q(summary__contains=q) | ||
10 | |||
11 | |||
12 | def search(request, template="djangopypi/search_results.html"): | ||
13 | context = RequestContext(request, {"dists": None, "search_term": ""}) | ||
14 | |||
15 | if request.method == "POST": | ||
16 | search_term = context["search_term"] = request.POST.get("search_term") | ||
17 | if search_term: | ||
18 | query = _search_query(search_term) | ||
19 | context["dists"] = Project.objects.filter(query) | ||
20 | |||
21 | if context["dists"] is None: | ||
22 | context["dists"] = Project.objects.all() | ||
23 | |||
24 | return render_to_response(template, context_instance=context) | ||