Exemplo n.º 1
0
 def dehydrate_template(self, bundle):
     """
     Dehydrate the template using the bundle.
     """
     search_type = "prestataire" if self._meta.resource_name == "prestataires" else "famille"
     template = get_result_template_from_user(bundle.request, search_type)
     context = {"result": bundle.obj, "user": bundle.request.user}
     return render_to_string(template, context)
Exemplo n.º 2
0
def search(request):
    """
    Search view.
    """
    data = request.POST if request.method == "POST" else request.GET
    search_type = data.get("type")
    if request.user.is_authenticated():
        related = get_user_related(request.user)
        favorites = related.favorites.all()
        if not search_type and isinstance(related, Prestataire):
            search_type = "famille"
    else:
        favorites = []

    search_type = "prestataire" if search_type not in ["famille", "prestataire"] else search_type
    if search_type == "famille":
        FormClass = forms.FamilleSearchForm
        objects = Famille.objects.filter(compute_user_visibility_filters(request.user))
        template = "search/famille.html"
    else:
        FormClass = forms.PrestataireSearchForm
        objects = Prestataire.objects.filter(compute_user_visibility_filters(request.user))
        template = "search/prestataire.html"

    form = FormClass({"pc": data.get("postal_code")})

    if not settings.ALLOW_BASIC_PLAN_IN_SEARCH:
        objects = objects.filter(plan=UserInfo.PLANS["premium"])

    if data.get("postal_code"):
        objects = objects.filter(Q(postal_code=data["postal_code"]) | Q(city=data["postal_code"]))
    objects = objects.order_by("-updated_at")
    objects = [obj for obj in objects if obj.visibility_score_is_enough]

    total_search_results = len(objects)
    nb_search_results = min(settings.NB_SEARCH_RESULTS, total_search_results)
    objects = objects[:nb_search_results]
    result_template = get_result_template_from_user(request, search_type)
    return render(
        request, template,
        get_context(
            search_form=form, results=objects, result_template=result_template,
            nb_search_results=nb_search_results, ordering=form.ordering_dict,
            favorites=favorites, user=request.user, search_type=search_type,
            max_nb_search_results=settings.NB_SEARCH_RESULTS,
            total_search_results=total_search_results
        )
    )