Ejemplo n.º 1
0
def _search(request, cleaned_data):
    search_strings = _filter_search_terms(request, cleaned_data["search"])
    if len(search_strings) == 0:
        messages.error(request, "Error: no search term left, can't search.")
        return

    preferences = get_preferences()
    min_pause = preferences["min_pause"]
    ban_limit = preferences["ban_limit"]

    try:
        LogEntry.objects.request_limit(request, min_pause, ban_limit, app_label="search")
    except LogEntry.RequestTooFast:
        # min_pause is not observed, page_msg has been created -> don't search
        return

    search_lang_codes = cleaned_data["language"]
    search_results = Search(request).search(search_lang_codes, search_strings)
    hits_count = len(search_results.hits)
    duration = human_duration(search_results.duration)
    msg = "done in %s, %s hits for: %r" % (duration, hits_count, search_strings)
    LogEntry.objects.log_action(
        app_label="search", action="search done", message=msg,
        data={
            "search_strings": search_strings,
            "duration": search_results.duration,
            "hits": hits_count,
        }
    )
    return search_results
Ejemplo n.º 2
0
def _search(request, cleaned_data):
    search_strings = _filter_search_terms(request, cleaned_data["search"])
    if len(search_strings) == 0:
        messages.error(request, "Error: no search term left, can't search.")
        return

    preferences = get_preferences()
    min_pause = preferences["min_pause"]
    ban_limit = preferences["ban_limit"]

    try:
        LogEntry.objects.request_limit(request, min_pause, ban_limit, app_label="search")
    except LogEntry.RequestTooFast:
        # min_pause is not observed, page_msg has been created -> don't search
        return

    search_lang_codes = cleaned_data["language"]
    search_results = Search(request, preferences).search(search_lang_codes, search_strings)
    hits_count = len(search_results.hits)
    duration = human_duration(search_results.duration)
    msg = "done in %s, %s hits for: %r" % (duration, hits_count, search_strings)
    LogEntry.objects.log_action(
        app_label="search", action="search done", message=msg,
        data={
            "search_strings": search_strings,
            "duration": search_results.duration,
            "hits": hits_count,
        }
    )
    return search_results
Ejemplo n.º 3
0
    def __init__(self, request, search_strings):
        self.request = request
        self.search_strings = search_strings
        self.search_strings_lower = tuple([i.lower() for i in search_strings])
        self.hits = []

        self.duration = None # set in self.done()
        self.plugin_count = None # set in self.done()
        self.start_time = time.time()

        self.preferences = get_preferences()
Ejemplo n.º 4
0
    def __init__(self, model_instance, search_strings, score, headline, language, url, content):
        self.model_instance = model_instance
        self.search_strings = search_strings
        self.score = score
        self.headline = headline
        self.language = language
        self.url = url
        self.content = strip_tags(content)

        preferences = get_preferences()
        self.text_cutout_len = preferences["text_cutout_len"]
        self.text_cutout_lines = preferences["text_cutout_lines"]
Ejemplo n.º 5
0
def _filter_search_terms(request, search_string):
    """
    Split and filter the search terms.
    """
    preferences = get_preferences()

    raw_search_strings = parse_tag_input(search_string) # split with django-tagging
    search_strings = []
    for term in raw_search_strings:
        if len(term) < preferences["min_term_len"]:
            messages.warning(request, "Ignore '%s' (too small)" % term)
        else:
            search_strings.append(term)

    return search_strings
Ejemplo n.º 6
0
def _filter_search_terms(request, search_string):
    """
    Split and filter the search terms.
    """
    preferences = get_preferences()

    raw_search_strings = parse_tag_input(search_string) # split with django-tagging
    search_strings = []
    for term in raw_search_strings:
        if len(term) < preferences["min_term_len"]:
            messages.warning(request, "Ignore '%s' (too small)" % term)
        else:
            search_strings.append(term)

    return search_strings
Ejemplo n.º 7
0
def http_get_view(request):

    # XXX: Should we support GET search ?

    if "search_page" in request.POST:
        # Form send by search page
        querydict = request.POST
    else:
        # Form was send by lucidTag -> set default search language

        querydict = request.POST.copy() # make POST QueryDict mutable

        preferences = get_preferences()
        use_all_languages = preferences["all_languages"]

        if use_all_languages:
            # use all accessible languages
            accessible_languages = Language.objects.all_accessible(request.user)
            codes = accessible_languages.values_list('code', flat=True)
            querydict.setlist("language", codes)
        else:
            # preselect only the client preferred language
            querydict["language"] = request.PYLUCID.current_language.code

    form = AdvancedSearchForm(querydict)

    if form.is_valid():
        search_results = _search(request, form.cleaned_data)
    else:
        search_results = None

    # For adding page update information into context by pylucid context processor
    request.PYLUCID.updateinfo_object = NowUpdateInfo(request)

    context = {
        "page_title": "Advanced search", # Change the global title with blog headline
        "form": form,
        "form_url": request.path + "?search=",
        "search_results": search_results,
    }
    return context
Ejemplo n.º 8
0
def http_get_view(request):

    # XXX: Should we support GET search ?

    if "search_page" in request.POST:
        # Form send by search page
        querydict = request.POST
    else:
        # Form was send by lucidTag -> set default search language

        querydict = request.POST.copy() # make POST QueryDict mutable

        preferences = get_preferences()
        use_all_languages = preferences["all_languages"]

        if use_all_languages:
            # use all accessible languages
            accessible_languages = Language.objects.all_accessible(request.user)
            codes = accessible_languages.values_list('code', flat=True)
            querydict.setlist("language", codes)
        else:
            # preselect only the client preferred language
            querydict["language"] = request.PYLUCID.current_language.code

    form = AdvancedSearchForm(querydict)

    if form.is_valid():
        search_results = _search(request, form.cleaned_data)
    else:
        search_results = None

    # For adding page update information into context by pylucid context processor
    request.PYLUCID.updateinfo_object = NowUpdateInfo(request)

    context = {
        "page_title": "Advanced search", # Change the global title with blog headline
        "form": form,
        "form_url": request.path + "?search=",
        "search_results": search_results,
    }
    return context
Ejemplo n.º 9
0
    def __init__(self, *args, **kwargs):
        super(SearchForm, self).__init__(*args, **kwargs)

        preferences = get_preferences()
        self.fields['search'].min_length = preferences["min_term_len"]
        self.fields['search'].max_length = preferences["max_term_len"]
Ejemplo n.º 10
0
    def __init__(self, *args, **kwargs):
        super(SearchForm, self).__init__(*args, **kwargs)

        preferences = get_preferences()
        self.fields['search'].min_length = preferences["min_term_len"]
        self.fields['search'].max_length = preferences["max_term_len"]