# Path: cab/views/snippets.py
# def advanced_search(request):
# 
#     snippet_qs = Snippet.objects.annotate(
#         search=SearchVector(
#             "title",
#             "description",
#             "language__name",
#             "version",
#             "pub_date",
#             "bookmark_count",
#             "rating_score",
#             "author__username",
#         )
#     )
#     form = AdvancedSearchForm(request.GET)
#     if form.is_valid():
#         snippet_qs = form.search(snippet_qs)
# 
#     return snippet_list(
#         request,
#         queryset=snippet_qs,
#         template_name="search/advanced_search.html",
#         extra_context={"form": form},
#     )
# 
# def autocomplete(request):
# 
#     q = request.GET.get("q", "")
#     results = []
#     if len(q) > 2:
#         result_set = Snippet.objects.annotate(search=SearchVector("title")).filter(search=q)[:10]
#         for obj in result_set:
#             url = obj.get_absolute_url()
#             results.append({"title": obj.title, "author": obj.author.username, "url": url})
#     return HttpResponse(json.dumps(results), content_type="application/json")
# 
# def basic_search(request):
#     q = request.GET.get("q")
#     snippet_qs = Snippet.objects.annotate(search=SearchVector("title", "description", "author__username"))
#     form = AdvancedSearchForm(request.GET)
# 
#     if form.is_valid():
#         snippet_qs = form.search(snippet_qs)
# 
#     return snippet_list(
#         request,
#         queryset=snippet_qs,
#         template_name="search/search.html",
#         extra_context={"query": q, "form": form},
#     )


from django.urls import path
from ..views.snippets import advanced_search, autocomplete, basic_search


urlpatterns = [
    path("", basic_search, name="cab_search"),
    path("autocomplete/", autocomplete, name="snippet_autocomplete"),

# Path: cab/views/snippets.py
# def advanced_search(request):
# 
#     snippet_qs = Snippet.objects.annotate(
#         search=SearchVector(
#             "title",
#             "description",
#             "language__name",
#             "version",
#             "pub_date",
#             "bookmark_count",
#             "rating_score",
#             "author__username",
#         )
#     )
#     form = AdvancedSearchForm(request.GET)
#     if form.is_valid():
#         snippet_qs = form.search(snippet_qs)
# 
#     return snippet_list(
#         request,
#         queryset=snippet_qs,
#         template_name="search/advanced_search.html",
#         extra_context={"form": form},
#     )
# 
# def autocomplete(request):
# 
#     q = request.GET.get("q", "")
#     results = []
#     if len(q) > 2:
#         result_set = Snippet.objects.annotate(search=SearchVector("title")).filter(search=q)[:10]
#         for obj in result_set:
#             url = obj.get_absolute_url()
#             results.append({"title": obj.title, "author": obj.author.username, "url": url})
#     return HttpResponse(json.dumps(results), content_type="application/json")
# 
# def basic_search(request):
#     q = request.GET.get("q")
#     snippet_qs = Snippet.objects.annotate(search=SearchVector("title", "description", "author__username"))
#     form = AdvancedSearchForm(request.GET)
# 
#     if form.is_valid():
#         snippet_qs = form.search(snippet_qs)
# 
#     return snippet_list(
#         request,
#         queryset=snippet_qs,
#         template_name="search/search.html",
#         extra_context={"query": q, "form": form},
#     )


# Path: cab/urls/search.py
from django.urls import path
from ..views.snippets import advanced_search, autocomplete, basic_search



urlpatterns = [
    path("", basic_search, name="cab_search"),
    path("autocomplete/", autocomplete, name="snippet_autocomplete"),

    path("advanced/", advanced_search, name="cab_search_advanced"),