Exemplo n.º 1
0
def hyperestraier_search(request, enc, q):
    searchphrase = ' AND '.join(q.split())
            
    queryset = []
    extra_context = {'mostviewed': cached_sets.most_viewed()[:6],
                     'q': q,
                     'get_args': "q=%s" % q,
                      'send_to_details': localsettings.FEATURE['send_to_details']}
    he_documents = searcher.search(str(searchphrase))
        
    for he_doc in he_documents:
        if searcher.get_attr(he_doc, '@flowgram_type') == 'flowgram':
            flowgram_id = searcher.get_attr(he_doc, '@flowgram_id')
            matching_flowgram = models.Flowgram.objects.filter(id=flowgram_id)
            matching_flowgram = matching_flowgram.filter(public=True) | \
                                matching_flowgram.filter(owner=request.user)
            if len(matching_flowgram):
                queryset.append(matching_flowgram[0])

    return webhelpers.flowgram_lister(request,
                                      enc='html',
                                      queryset=queryset,
                                      template_name='search/search_results.html',
                                      extra_context=extra_context,
                                      display_mode='list')
Exemplo n.º 2
0
def get_related_fg_changed(flowgram, request):
    if FEATURE['use_HEsearch']:
        non_alpha_charaters = re.compile(r'[a-zA-Z]+')
        tags = Tag.objects.filter(flowgram=flowgram)
        searchphrase_list = [tag.name for tag in tags] + non_alpha_charaters.findall(flowgram.title)
        searchphrase_list = [word for word in searchphrase_list if word not in non_search_words and word not in punctuation]
        
        searchphrase = ' OR '.join(searchphrase_list)
        
        related_flowgrams = []
        log.debug('use hyperestraier, the searchphrase is: ' + searchphrase)
        he_documents = searcher.search(str(searchphrase))
        for he_doc in he_documents:
            if searcher.get_attr(he_doc, '@flowgram_type') == 'flowgram':
                fg_id = searcher.get_attr(he_doc, '@flowgram_id')
                one_flowgram = get_object_or_404(Flowgram, pk=fg_id)
                if one_flowgram.public or \
                        request.user.is_authenticated() and request.user == one_flowgram.owner:
                    if one_flowgram != flowgram:
                        related_flowgrams.append(one_flowgram)

        related_flowgrams = related_flowgrams[:10]
        related_flowgrams_string = ";".join([fg.id for fg in related_flowgrams])

        # flowgram changed, update the related flowgrams in db
        flowgram_need_update = FlowgramRelated.objects.filter(flowgram=flowgram)
        flowgram_need_update = flowgram_need_update[0] if len(flowgram_need_update) else None
        if flowgram_need_update:
            flowgram_need_update.related = related_flowgrams_string
            flowgram_need_update.timestamp = datetime.datetime.now()
            flowgram_need_update.save()
        else:
            FlowgramRelated.objects.create(flowgram=flowgram, related=related_flowgrams_string)
            
        return related_flowgrams
    else:
        return get_featured(feat.FEATURED_PAGE)