def filter_news(request, default_limit=None): # read hosts list or single host_slugs = parse_union(request) contains = request.GET.get("search") limit = parse_limit( request, default=default_limit) if default_limit else parse_limit(request) start, end = parse_filter_date(request) results = SearchQuerySet() if host_slugs: results = results.filter_or(host_slug__in=host_slugs) if contains: results = results.filter_and(content__contains=contains) if start: results = results.filter_and(published__gte=start) if end: results = results.filter_and(published__lt=end) results = results.models(NewsPost).order_by('-published') if limit: results = results[:limit] posts = [result.object for result in results] return posts
def search(request): q = request.GET.get('q', '') cat_filter = request.GET.get('category') tag_filter = request.GET.get('tag') facet_counts = {} if q or cat_filter or tag_filter: qs = SearchQuerySet() if q: qs = qs.filter(content=q) qs = qs.filter_or(speakers__startswith=q.lower()) if cat_filter: # TODO: This doesn't work quite right. It should filter # out anything that's not *exactly* cat_filter but it's # not. Could be a problem here or with the indexing. The # haystack docs are mysterious. qs = qs.filter_and(category__exact=cat_filter) if tag_filter: qs = qs.filter_and(tags__in=[tag_filter]) # TODO: Whoosh doesn't handle faceting, so we have to do it # manually. Fix this so it detects whether the haystack backend # supports facets and if so, uses the backend and not the db. cat_counts = {} tag_counts = {} for mem in qs: cat_counts[mem.category] = cat_counts.get(mem.category, 0) + 1 for tag in mem.tags: tag_counts[tag] = tag_counts.get(tag, 0) + 1 facet_counts['category'] = sorted(cat_counts.items(), key=lambda pair: pair[1], reverse=True) facet_counts['tag'] = sorted(tag_counts.items(), key=lambda pair: pair[1], reverse=True) page = Paginator(qs, 25) p = request.GET.get('p', '1') try: p = max(1, int(p)) except ValueError: p = 1 try: page = page.page(p) except EmptyPage: page = page.page(1) else: page = None return render( request, 'videos/search.html', { 'query': q, 'tag': tag_filter, 'category': cat_filter, 'facet_counts': facet_counts, 'page': page })
def __init__(self, haystack_result, obj, user, *args, **kwargs): kwargs.update({ 'is_superuser': check_user_superuser(user), 'is_member': check_ug_membership(user, obj), 'is_pending': check_ug_pending(user, obj), 'is_invited': check_ug_invited_pending(user, obj), 'creator_name': obj.creator and obj.creator.get_full_name(), 'creator_slug': obj.creator and obj.creator.username, 'title': obj.name, 'organization_type': obj.get_type(), 'website_url': obj.website, 'email': obj.email, 'phone_number': obj.phone_number.as_international if obj.phone_number else None, 'social_media': [{ 'url': sm.url, 'icon': sm.icon } for sm in obj.social_media.all()], 'edit_url': obj.get_edit_url(), 'accept_url': reverse('cosinnus:organization-user-accept', kwargs={'organization': obj.slug}), }) # collect administrator users. these are *not* filtered by visibility, as project admins are always visible! sqs = SearchQuerySet().models(SEARCH_MODEL_NAMES_REVERSE['people']) sqs = sqs.filter_and(admin_organizations=obj.id) #sqs = filter_searchqueryset_for_read_access(sqs, user) # private users are not visible to anonymous users, BUT they are visible to logged in users! # because if a user chose to make his group visible, he has to take authorship responsibilities if not user.is_authenticated: sqs = filter_searchqueryset_for_read_access(sqs, user) kwargs.update( {'admins': [HaystackUserMapCard(result) for result in sqs]}) # Groups sqs = SearchQuerySet().models(SEARCH_MODEL_NAMES_REVERSE['projects'], SEARCH_MODEL_NAMES_REVERSE['groups']) sqs = sqs.filter_and(id__in=haystack_result.groups) sqs = filter_searchqueryset_for_read_access(sqs, user) sqs = sqs.order_by('title') kwargs.update( {'groups': [HaystackGroupMapCard(result) for result in sqs]}) super(DetailedOrganizationMapResult, self).__init__(haystack_result, obj, user, *args, **kwargs)
def search(request): q = request.GET.get('q', '') cat_filter = request.GET.get('category') tag_filter = request.GET.get('tag') facet_counts = {} if q or cat_filter or tag_filter: qs = SearchQuerySet() if q: qs = qs.filter(content=q) qs = qs.filter_or(speakers__startswith=q.lower()) if cat_filter: # TODO: This doesn't work quite right. It should filter # out anything that's not *exactly* cat_filter but it's # not. Could be a problem here or with the indexing. The # haystack docs are mysterious. qs = qs.filter_and(category__exact=cat_filter) if tag_filter: qs = qs.filter_and(tags__in=[tag_filter]) # TODO: Whoosh doesn't handle faceting, so we have to do it # manually. Fix this so it detects whether the haystack backend # supports facets and if so, uses the backend and not the db. cat_counts = {} tag_counts = {} for mem in qs: cat_counts[mem.category] = cat_counts.get(mem.category, 0) + 1 for tag in mem.tags: tag_counts[tag] = tag_counts.get(tag, 0) + 1 facet_counts['category'] = sorted( cat_counts.items(), key=lambda pair: pair[1], reverse=True) facet_counts['tag'] = sorted( tag_counts.items(), key=lambda pair: pair[1], reverse=True) page = Paginator(qs, 25) p = request.GET.get('p', '1') try: p = max(1, int(p)) except ValueError: p = 1 try: page = page.page(p) except EmptyPage: page = page.page(1) else: page = None return render(request, 'videos/search.html', {'query': q, 'tag': tag_filter, 'category': cat_filter, 'facet_counts': facet_counts, 'page': page})
def get_results(self): sqs = SearchQuerySet() sqs = sqs.models(MovieInCollection) sqs = sqs.filter_and(collection__exact="watched") sqs = sqs.filter_and(collection_user=self.request.user.id) sqs = sqs.order_by("-date") if (self.form.is_valid()): sqs = sqs.filter_and(content=Raw(self.form.cleaned_data['q'])) return sqs.all() return sqs.none()
def __init__(self, haystack_result, obj, user, *args, **kwargs): group_admins = list(obj.actual_admins) message_url = None if not settings.COSINNUS_IS_INTEGRATED_PORTAL: message_url = message_group_admins_url(obj, group_admins) kwargs.update({ 'is_member': check_ug_membership(user, obj), 'is_pending': check_ug_pending(user, obj), 'is_invited': check_ug_invited_pending(user, obj), 'action_url_1': _prepend_url(user, obj.portal) + group_aware_reverse('cosinnus:group-microsite', kwargs={'group': obj}, skip_domain=True) + '?join=1', 'action_url_2': (_prepend_url(user, obj.portal) + message_url) if message_url else None, 'youtube_url': obj.video, 'twitter_username': obj.twitter_username, 'flickr_url': obj.flickr_url, 'website_url': obj.website, 'contact': linebreaksbr(escape(obj.contact_info)), }) """ TODO: check all read permissions on related objects! """ # collect upcoming and visible project/group events sqs = SearchQuerySet().models(SEARCH_MODEL_NAMES_REVERSE['events']) sqs = sqs.filter_and(group=obj.id) sqs = filter_searchqueryset_for_read_access(sqs, user) sqs = filter_event_searchqueryset_by_upcoming(sqs) sqs = sqs.order_by('from_date') kwargs.update( {'events': [HaystackEventMapCard(result) for result in sqs]}) # collect administrator users. these are *not* filtered by visibility, as project admins are always visible! sqs = SearchQuerySet().models(SEARCH_MODEL_NAMES_REVERSE['people']) sqs = sqs.filter_and(admin_groups=obj.id) #sqs = filter_searchqueryset_for_read_access(sqs, user) sqs = sqs.order_by('title') kwargs.update( {'admins': [HaystackUserMapCard(result) for result in sqs]}) return super(DetailedBaseGroupMapResult, self).__init__(haystack_result, obj, user, *args, **kwargs)
def subject_statisticInfo(request): """ 1. 前台返回扩展属性 2. 前台默认提交当前专题 3. 修复扩展字段组合查询bug :param request: :return: """ query = request.GET['q'] if (query != ''): posts = SearchQuerySet().filter(content=query) else: posts = SearchQuerySet().all() for key in [ 'title', 'keywords', 'creator', 'language', 'fileFormat', 'spatial', 'discipline', 'fileType' ]: if request.GET.has_key(key) and request.GET[key] != u'': posts = posts.filter_and(**{key: request.GET[key]}) subject_name = request.GET['subjectName'] posts_sub = Subject.objects.get(subjectName=subject_name) subject_id = posts_sub.id posts = posts.filter_and(subjecttype=subject_id) temp = SubjectTheme.objects.filter(subjectId=subject_id, fieldType='6') for extfeature in temp: if request.GET.has_key(str(extfeature.id) + '_ext'): posts = posts.filter_and( **{ str(extfeature.id) + '_ext': request.GET[str(extfeature.id) + '_ext'] }) sqs = posts for extfeature in temp: if request.GET.has_key(str(extfeature.id) + '_ext'): cer = (request.GET[str(extfeature.id) + '_ext']).split('/', 1) path = str(int(cer[0]) + 1) + '/' + cer[1] print(path) else: path = '1' sqs = sqs.facet(str(extfeature.id) + '_ext', prefix=path, mincount=1) sqs = sqs.facet('fileFormat').facet('spatial').facet('discipline').facet( 'language').facet('fileType').facet_counts() ret_num = posts.count() sub_theme = SubjectTheme.objects.filter(subjectId_id=subject_id).filter( fieldType=6).exclude(corrAttri='-1') return render( request, 'himalaya/subject_statisticInfo.html', { 'posts': posts, 'query': query, 'num': ret_num, 'sqs': sqs, 'sub_theme': sub_theme })
def refined_search(data): searched_skills = searched_locations = searched_industry = searched_edu = state = Skill.objects.none() sqs = SearchQuerySet().models(JobPost).filter_and(status='Live') if 'refine_skill' in data and data.getlist('refine_skill'): term = data.getlist('refine_skill') sqs = sqs.filter_and(SQ(title__in=term) | SQ(skills__in=term) | SQ( description__in=term) | SQ(designation__in=term) | SQ(edu_qualification__in=term)) searched_skills = Skill.objects.filter(name__in=term) location = data.getlist('refine_location') if 'refine_location' in data else [] searched_locations = City.objects.filter(name__in=location) if 'Across India' in location: india = Country.objects.filter(name='India') sqs = sqs.filter_and(SQ(location__in=india.values_list('state__state__name', flat=True))) elif location: other_cities = searched_locations.values_list('parent_city__name', flat=True) sqs = sqs.filter_and(SQ(location__in=location) | SQ(location__in=other_cities)) if 'refine_state' in data and data.getlist('refine_state'): state = State.objects.filter(name__in=data.getlist('refine_state')) sqs = sqs.filter_and(location__in=state.values_list('state__name', flat=True)) if data.get('job_type'): if data['job_type'] == 'Fresher': sqs = sqs.filter_and(min_year__lte=int(0)) else: sqs = sqs.filter_and(job_type__in=[data['job_type']]) if 'refine_industry' in data and data.getlist('refine_industry'): term = data.getlist('refine_industry') sqs = sqs.filter_and(industry__in=term) searched_industry = Industry.objects.filter(name__in=term) if 'refine_education' in data and data.getlist('refine_education'): term = data.getlist('refine_education') sqs = sqs.filter_and(edu_qualification__in=term) searched_edu = Qualification.objects.filter(name__in=term) if 'functional_area' in data and data.getlist('functional_area'): term = data.getlist('functional_area') sqs = sqs.filter_or(functional_area__in=term) if data.get('refine_experience_min') or data.get('refine_experience_min') == 0: sqs = sqs.filter_and(min_year__lte=int(data['refine_experience_min'])) if data.get('refine_experience_max') or data.get('refine_experience_max') == 0: sqs = sqs.filter_and(max_year__lte=int(data['refine_experience_max'])) # TODO: this line is taking 500ms, nikhila has to look into it. # job_list = JobPost.objects.filter(status='Live', pk__in=results).select_related( # 'company', 'user').prefetch_related('location', 'skills', 'industry') sqs = sqs.load_all().order_by('-published_on') return sqs, searched_skills, searched_locations, searched_industry, searched_edu, state
def autocomplete(request): """Return autocomple JSON results""" term = request.GET.get("term", "").strip() response_data = [] if len(term): # Does not work - probably because the FLAG_PARTIAL is not set on Xapian # (trying to set it in settings.py as documented appears to have no effect) # sqs = SearchQuerySet().autocomplete(name_auto=term) # Split the search term up into little bits terms = re.split(r"\s+", term) # Build up a query based on the bits sqs = SearchQuerySet() for bit in terms: # print "Adding '%s' to the '%s' query" % (bit,term) sqs = sqs.filter_and(name_auto__startswith=sqs.query.clean(bit)) # collate the results into json for the autocomplete js for result in sqs.all()[0:10]: response_data.append({"url": result.object.get_absolute_url(), "label": result.object.name}) # send back the results as JSON return HttpResponse(simplejson.dumps(response_data), mimetype="application/json")
def __init__(self, haystack_result, obj, user, *args, **kwargs): kwargs.update({ 'is_member': user.id == obj.user_id, }) if not settings.COSINNUS_IS_INTEGRATED_PORTAL: kwargs.update({ 'action_url_1': _prepend_url(user, None) + reverse('postman:write', kwargs={'recipients': obj.user.username}), }) # collect visible groups and projects that this user is in sqs = SearchQuerySet().models(SEARCH_MODEL_NAMES_REVERSE['projects'], SEARCH_MODEL_NAMES_REVERSE['groups']) sqs = sqs.filter_and(id__in=haystack_result.membership_groups) # the preview for projects and groups is always visible for everyone! #sqs = filter_searchqueryset_for_read_access(sqs, user) sqs = sqs.order_by('title') kwargs.update({ 'projects': [], 'groups': [], }) for result in sqs: if SEARCH_MODEL_NAMES[result.model] == 'projects': kwargs['projects'].append(HaystackProjectMapCard(result)) else: kwargs['groups'].append(HaystackGroupMapCard(result)) return super(DetailedUserMapResult, self).__init__(haystack_result, obj, user, *args, **kwargs)
def __init__(self, haystack_result, obj, user, *args, **kwargs): # collect group's created visible projects sqs = SearchQuerySet().models(SEARCH_MODEL_NAMES_REVERSE['projects']) sqs = sqs.filter_and( id__in=obj.created_groups.all().values_list('id', flat=True)) # the preview for projects and groups is always visible for everyone! #sqs = filter_searchqueryset_for_read_access(sqs, user) sqs = sqs.order_by('title') kwargs.update({ 'projects': [HaystackProjectMapCard(result) for result in sqs], 'action_url_1': _prepend_url(user, obj.portal) + reverse('cosinnus:group-add') + ('?idea=%s&name=%s' % (itemid_from_searchresult(haystack_result), escape(haystack_result.title))), 'creator_name': obj.creator.get_full_name(), 'creator_slug': obj.creator.username, 'followed': obj.likes.filter(followed=True, liked=True, user__id=user.id).count() > 0, }) ret = super(DetailedIdeaMapResult, self).__init__(haystack_result, obj, user, *args, **kwargs) return ret
def _type_match_check(self, bundle): ''' This check does not allow for mixed target values to be kept on same element. ''' sqs = SearchQuerySet().filter_and( django_ct='targeting.publishertargetvalue', inventory=bundle.data['inventory'], django_id__in=bundle.data['targetvalues_ids']) if bundle.data['is_network']: sqs = sqs.filter_and(pubkey=dimensions.network) else: sqs = sqs.filter_and(pubkey=dimensions.publisher_name) return sqs.count() == len(bundle.data['targetvalues_ids'])
def autocomplete(request): """Return autocomple JSON results""" term = request.GET.get('term', '').strip() response_data = [] if len(term): # Does not work - probably because the FLAG_PARTIAL is not set on Xapian # (trying to set it in settings.py as documented appears to have no effect) # sqs = SearchQuerySet().autocomplete(name_auto=term) # Split the search term up into little bits terms = re.split(r'\s+', term) # Build up a query based on the bits sqs = SearchQuerySet() for bit in terms: # print "Adding '%s' to the '%s' query" % (bit,term) sqs = sqs.filter_and(name_auto__startswith=sqs.query.clean(bit)) # collate the results into json for the autocomplete js for result in sqs.all()[0:10]: response_data.append({ 'url': result.object.get_absolute_url(), 'label': result.object.name, }) # send back the results as JSON return HttpResponse(simplejson.dumps(response_data), mimetype='application/json')
def search(self): if not hasattr(self, "cleaned_data"): return self.no_query_found() search_fields = [key for key, value in self.cleaned_data.iteritems() if value == True] if 'title' not in search_fields: sqs = SearchQuerySet() else: sqs = super(SearchOptionsForm, self).search() # title is a document field and has been used for filtering in super method search() search_fields = [key for key in search_fields if key != 'title'] query = sqs.query.clean(self.cleaned_data.pop('q')) galleries = [g.id for g in self.cleaned_data.get('galleries', [])] search_galleries = self.cleaned_data.get('search_galleries_choice', "ALL") query_words = query.split() for key in search_fields: if key == "tags": sqs = sqs.filter_or(tags__in=[query.lower() for query in query_words]) else: sqs = self._filter_or_query_words(sqs, key, query_words) if search_galleries == 'SELECTED': sqs = sqs.filter_and(galleries_ids__in=galleries) return sqs
def filter_people_search(request): main = request.POST['keyword'] constraint = request.POST['constraint'] skills = request.POST['skills'] if skills != "": skills = skills.split(',') if main == '': results = SearchQuerySet().all().models(UserProfile) else: results = SearchQuerySet().filter(content=main).models(UserProfile) if skills != "": for s in skills: results = results.filter_and(tags=s) if constraint != "": results = results.filter_and(votesup__gte=constraint) return render_to_response('search/results_people.html', {'presults': results, 'keyword': main, 'peopleCount': len(results)}, RequestContext(request))
def search_lecture_detail(request): qs = request.GET if len(qs) == 0: return Response(data=[], status=status.HTTP_200_OK) sqs = SearchQuerySet().models(Lecture) for q in qs: if q == 'name': sqs = sqs.filter_and(name__contains=qs[q]) if q == 'type': sqs = sqs.filter_and(type__contains=qs[q]) if q == 'department': sqs = sqs.filter_and(department__contains=qs[q]) if q == 'area': sqs = sqs.filter_and(area__contains=qs[q]) if q == 'lecturer': sqs = sqs.filter_and(lecturer__contains=qs[q]) if q == 'credit_Total': sqs = sqs.filter_and(credit_Total=int(qs[q])) if q == 'grade': sqs = sqs.filter_and(grade=qs[q]) serializer_list = [] for sqs_element in sqs: lecture = Lecture.objects.get(id=sqs_element.object.id) serializer_list.append(lecture) serializer = LectureSearchSerializer(serializer_list, many=True) return Response(data=serializer.data, status=status.HTTP_200_OK)
def filter_projects(request, default_limit=None, return_queryset=False): host_slugs = parse_union(request) contains = request.GET.get("search") visibility = request.GET.get("visibility") country_codes = parse_country(request) limit = parse_limit( request, default=default_limit) if default_limit else parse_limit(request) start, end = parse_filter_date(request) results = SearchQuerySet() if host_slugs: results = results.filter(hosts_slug__in=host_slugs) if visibility == 'completed': results = results.filter_and(completed=True) elif visibility == 'current': results = results.exclude(completed=True) if country_codes: results = results.filter_and(country_code__in=country_codes) if contains: results = results.filter_and(text__contains=contains) if start: results &= SearchQuerySet().filter( start_date__gte=start) | SearchQuerySet().filter( end_date__gte=start) if end: results &= SearchQuerySet().filter( start_date__lt=end) | SearchQuerySet().filter(end_date__lt=end) results = results.models(Project).all() if limit: results = results[:limit] if return_queryset: projects_pk = [r.pk for r in results] if len(host_slugs) == 1: projects = Project.objects.filter(pk__in=projects_pk).order_by( 'completed', '-priority', '-updated') else: projects = Project.objects.filter(pk__in=projects_pk).order_by( 'completed', '-updated') else: projects = [result.object for result in results] return projects
def search(request): q = request.GET.get("q", "") facet_counts = {} if q: cat_filter = request.GET.get("category") qs = SearchQuerySet() qs = qs.filter(content=q) qs = qs.filter_or(speakers__startswith=q.lower()) if cat_filter: # TODO: This doesn't work quite right. It should filter # out anything that's not *exactly* cat_filter but it's # not. Could be a problem here or with the indexing. The # haystack docs are mysterious. qs = qs.filter_and(category__exact=cat_filter) # TODO: Whoosh doesn't handle faceting, so we have to do it # manually. Fix this so it detects whether the haystack backend # supports facets and if so, uses the backend and not the db. cat_counts = {} for mem in qs: cat_counts[mem.category] = cat_counts.get(mem.category, 0) + 1 facet_counts["category"] = sorted(cat_counts.items(), key=lambda pair: pair[1], reverse=True) page = Paginator(qs, 25) p = request.GET.get("p", "1") try: p = max(1, int(p)) except ValueError: p = 1 try: page = page.page(p) except EmptyPage: page = page.page(1) else: page = None if q: title = u"Search: {query}".format(query=q) else: title = u"Search" get_params = request.GET.copy() if "category" in get_params: get_params.pop("category") base_url = request.path + "?" + get_params.urlencode() return render( request, "videos/search.html", {"query": q, "base_url": base_url, "title": title, "facet_counts": facet_counts, "page": page}, )
def __init__(self, haystack_result, obj, user, *args, **kwargs): # collect group's visible projects sqs = SearchQuerySet().models(SEARCH_MODEL_NAMES_REVERSE['projects']) sqs = sqs.filter_and(id__in=obj.groups.all().values_list('id', flat=True)) # the preview for projects and groups is always visible for everyone! #sqs = filter_searchqueryset_for_read_access(sqs, user) sqs = sqs.order_by('title') kwargs.update({ 'projects': [HaystackProjectMapCard(result) for result in sqs] }) return super(DetailedSocietyMapResult, self).__init__(haystack_result, obj, user, *args, **kwargs)
def subject_statisticInfo(request): """ 1. 前台返回扩展属性 2. 前台默认提交当前专题 3. 修复扩展字段组合查询bug :param request: :return: """ query = request.GET['q'] if (query != ''): posts = SearchQuerySet().filter(content=query) else: posts = SearchQuerySet().all() for key in ['title', 'keywords', 'creator', 'language', 'fileFormat', 'spatial', 'discipline', 'fileType']: if request.GET.has_key(key) and request.GET[key] != u'': posts = posts.filter_and(**{key: request.GET[key]}) subject_name = request.GET['subjectName'] posts_sub = Subject.objects.get(subjectName=subject_name) subject_id = posts_sub.id posts = posts.filter_and(subjecttype=subject_id) temp = SubjectTheme.objects.filter(subjectId=subject_id,fieldType='6') for extfeature in temp: if request.GET.has_key(str(extfeature.id)+'_ext'): posts = posts.filter_and(**{str(extfeature.id)+'_ext': request.GET[str(extfeature.id)+'_ext']}) sqs = posts for extfeature in temp: if request.GET.has_key(str(extfeature.id)+'_ext'): cer = (request.GET[str(extfeature.id)+'_ext']).split('/', 1) path = str(int(cer[0]) + 1)+'/' + cer[1] print(path) else: path = '1' sqs = sqs.facet(str(extfeature.id) + '_ext', prefix=path, mincount=1) sqs = sqs.facet('fileFormat').facet('spatial').facet('discipline').facet('language').facet( 'fileType').facet_counts() ret_num = posts.count() sub_theme = SubjectTheme.objects.filter(subjectId_id=subject_id).filter(fieldType=6).exclude( corrAttri='-1') return render(request, 'himalaya/subject_statisticInfo.html', {'posts': posts, 'query': query, 'num': ret_num, 'sqs': sqs, 'sub_theme': sub_theme})
def filter_people_search(request): main = request.POST['keyword'] constraint = request.POST['constraint'] skills = request.POST['skills'] if skills != "": skills = skills.split(',') if main == '': results = SearchQuerySet().all().models(UserProfile) else: results = SearchQuerySet().filter(content=main).models(UserProfile) if skills != "": for s in skills: results = results.filter_and(tags=s) if constraint != "": results = results.filter_and(votesup__gte=constraint) return render_to_response('search/results_people.html', { 'presults': results, 'keyword': main, 'peopleCount': len(results) }, RequestContext(request))
def targetvalues(self): ''' Returns either networks or publishers depend on is_network flag ''' from haystack.query import SearchQuerySet ids = [] for pub_id in self.target_values.values('id'): ids.append(pub_id['id']) django_ct = '{0}.{1}'.format( PublisherTargetValue._meta.app_label, PublisherTargetValue._meta.object_name.lower()) sqs = SearchQuerySet().filter_and(django_ct=django_ct, django_id__in=ids, inventory=self.inventory) if self.is_network: return sqs.filter_and(pubkey=dimensions.network) return sqs.filter_and(pubkey=dimensions.publisher_name)
def __init__(self, haystack_result, obj, user, *args, **kwargs): kwargs.update({ 'is_member': user.id == obj.user_id, }) if not settings.COSINNUS_IS_INTEGRATED_PORTAL and not 'cosinnus_message' in settings.COSINNUS_DISABLED_COSINNUS_APPS: if settings.COSINNUS_ROCKET_ENABLED: kwargs.update({ 'action_url_1': _prepend_url(user, None) + reverse('cosinnus:message-write', kwargs={'username': obj.user.username}), }) else: kwargs.update({ 'action_url_1': _prepend_url(user, None) + reverse('postman:write', kwargs={'recipients': obj.user.username}), }) # collect visible groups and projects that this user is in sqs = SearchQuerySet().models(SEARCH_MODEL_NAMES_REVERSE['projects'], SEARCH_MODEL_NAMES_REVERSE['groups']) sqs = sqs.filter_and(id__in=haystack_result.membership_groups) # the preview for projects and groups is always visible for everyone! #sqs = filter_searchqueryset_for_read_access(sqs, user) sqs = sqs.order_by('title') kwargs.update({ 'projects': [], 'groups': [], }) for result in sqs: if SEARCH_MODEL_NAMES[result.model] == 'projects': kwargs['projects'].append(HaystackProjectMapCard(result)) elif SEARCH_MODEL_NAMES[result.model] == 'conferences': kwargs['conferences'].append(HaystackConferenceMapCard(result)) else: kwargs['groups'].append(HaystackGroupMapCard(result)) if getattr(settings, 'COSINNUS_USER_SHOW_MAY_BE_CONTACTED_FIELD', False): kwargs.update({ 'may_be_contacted': obj.may_be_contacted, }) if obj.user_id == user.id: kwargs.update({ 'is_self': True, }) return super(DetailedUserMapResult, self).__init__(haystack_result, obj, user, *args, **kwargs)
def ms_advanced_search(self, request, **kwargs): get_params = request.GET.copy() limit = get_params.get('limit', self._meta.limit) sqs = SearchQuerySet().models(self.Meta.object_class).facet('tags') allWords = get_params.get('allWords', "").split(',') exactExpressions = get_params.get('exactExpressions', "").split(',') noneWord = get_params.get('noneWord', "").split(',') allWords = [] if allWords == [''] else allWords exactExpressions = [] if exactExpressions == [''] else exactExpressions noneWord = [] if noneWord == [''] else noneWord if get_params['searchIn'] in ['all', 'titles']: for term in allWords: sqs = sqs.filter_and(text=term.strip()) for term in exactExpressions: sqs = sqs.filter_and(text=term.strip()) for term in noneWord: sqs = sqs.exclude(text=term.strip()) elif get_params['searchIn'] == 'tags': for term in allWords: sqs = sqs.filter_and(tags=slugify(term.strip())) for term in exactExpressions: sqs = sqs.filter_and(tags=slugify(term.strip())) for term in noneWord: sqs = sqs.exclude(tags=slugify(term.strip())) self.log_throttled_access(request) return self.create_response(request, self.prepare_result(request, sqs, limit))
def ms_advanced_search(self, request, **kwargs): get_params = request.GET.copy() limit = get_params.get('limit', self._meta.limit) sqs = SearchQuerySet().models(self.Meta.object_class).facet('tags') allWords = get_params.get('allWords', "").split(',') exactExpressions = get_params.get('exactExpressions', "").split(',') noneWord = get_params.get('noneWord', "").split(',') allWords = [] if allWords == [''] else allWords exactExpressions = [] if exactExpressions == [''] else exactExpressions noneWord = [] if noneWord == [''] else noneWord if get_params['searchIn'] in ['all', 'titles']: for term in allWords: sqs = sqs.filter_and(text=term.strip()) for term in exactExpressions: sqs = sqs.filter_and(text=term.strip()) for term in noneWord: sqs = sqs.exclude(text=term.strip()) elif get_params['searchIn'] == 'tags': for term in allWords: sqs = sqs.filter_and(tags=slugify(term.strip())) for term in exactExpressions: sqs = sqs.filter_and(tags=slugify(term.strip())) for term in noneWord: sqs = sqs.exclude(tags=slugify(term.strip())) self.log_throttled_access(request) return self.create_response(request, self.prepare_result(request, sqs, limit))
def filter_partners(request, default_limit=None, return_queryset=False): host_slugs = parse_union(request) contains = request.GET.get("search") active = request.GET.get("active") categories = parse_categories(request) limit = parse_limit( request, default=default_limit) if default_limit else parse_limit(request) results = SearchQuerySet() if host_slugs: results = results.filter(hosts_slug__in=host_slugs) if contains: results = results.filter_and(text__contains=contains) if categories: results = results.filter_and(category__in=categories) if active == 'active': results = results.filter_and(active=True) elif active == 'former': results = results.exclude(active=True) results = results.models(Partner).all() if limit: results = results[:limit] if return_queryset: partners_pk = [r.pk for r in results] if len(host_slugs) == 1: partners = Partner.objects.filter(pk__in=partners_pk).order_by( '-active', '-priority', '-updated') else: partners = Partner.objects.filter(pk__in=partners_pk).order_by( '-active', '-updated') else: # for rest_views partners = [result.object for result in results] return partners
def loose_match_name(self, name): """Search for a loose match on a name. May not be too reliable""" # Try matching all the bits results = SearchQuerySet().filter_and( content=name ).models( self.model ) # if that fails try matching all the bits in any order if not len( results ): results = SearchQuerySet().models(Person) for bit in re.split(r'\s+', name): results = results.filter_and( content=bit ) # If we have exactly one result return that if len( results ) == 1: return results[0].object else: return None
def __init__(self, haystack_result, obj, user, *args, **kwargs): kwargs.update({ 'is_member': check_ug_membership(user, obj.group), 'time_html': haystack_result.humanized_event_time_html, }) # collect visible attending users sqs = SearchQuerySet().models(SEARCH_MODEL_NAMES_REVERSE['people']) sqs = sqs.filter_and(user_id__in=haystack_result.participants) sqs = filter_searchqueryset_for_read_access(sqs, user) sqs = sqs.order_by('title') kwargs.update({ 'participants': [HaystackUserMapCard(result) for result in sqs], 'participant_count': haystack_result.participant_count, 'followed': obj.is_user_following(user), }) return super(DetailedEventResult, self).__init__(haystack_result, obj, user, *args, **kwargs)
def similarTask(request, task_id): task = Task.objects.get(id=task_id) skills = task.tags results = SearchQuerySet().all() q = "" for s in skills: results = results.filter_and(tags=s) q += " " + s.name people = results.models(UserProfile) tasks = results.models(Task) return render_to_response( 'search/results.html', { 'tresults': tasks, 'presults': people, 'keyword': q, 'peopleCount': len(people), 'taskCount': len(tasks) }, RequestContext(request))
def loose_match_name(self, name): """Search for a loose match on a name. May not be too reliable""" # import here to avoid creating an import loop from haystack.query import SearchQuerySet # Try matching all the bits results = SearchQuerySet().filter_and(content=name).models(self.model) # if that fails try matching all the bits in any order if not len(results): results = SearchQuerySet().models(Person) for bit in re.split(r'\s+', name): results = results.filter_and(content=bit) # If we have exactly one result return that if len(results) == 1: return results[0].object else: return None
def filter_Search(request): """ Filtering the Search according to the keys sent and the main search keyword Main : is the main keyword searched by the user from navbar Key: array of filter keywords sent from the view """ main = '' if 'main' in request.POST: main = request.POST['main'] if main != '': results = SearchQuerySet().filter( content=AutoQuery(main)).models(Task) results = results.filter_or(tags=main).models(Task) results = results.filter_or(title=AutoQuery(main)).models(Task) results = results.filter_or(location=AutoQuery(main)).models(Task) else: results = SearchQuerySet().all().models(Task) else: results = SearchQuerySet().all() if 'loca' in request.POST: loca = request.POST['loca'] if loca != '': results = results.filter_and(city=loca) if 'word' in request.POST: word = request.POST['word'] if word != '': results1 = results.filter_and(content=AutoQuery(word)) results2 = results.filter_and(title=AutoQuery(word)) results3 = results.filter_and(location=AutoQuery(word)) results4 = results1.__or__(results2) results = results4.__or__(results3) if 'skills' in request.POST: skills = request.POST['skills'] if skills != '': skills = skills.split(',') for s in skills: results = results.filter_and(tags=s) if 'from' in request.POST: from_price = request.POST['from'] to_price = request.POST['to'] if from_price != '' and to_price != '' and to_price != 'all': results = results.filter_and( price__range=[int(from_price), int(to_price)]) return render_to_response('search/results_tasks.html', { 'tresults': results, 'keyword': main, 'taskCount': len(results) }, RequestContext(request))
def filter_events(request, default_limit=None): host_slugs = parse_union(request) contains = request.GET.get("search") start, end = parse_filter_date(request) limit = parse_limit( request, default=default_limit) if default_limit else parse_limit(request) results = SearchQuerySet() if host_slugs: results = results.filter_or(hosts_slug__in=host_slugs) if contains: results = results.filter_and(content__contains=contains) events = results.models(Event) # search results for start and end date # handle cases where no dates are given if start and end: start_date = start end_date = end elif start: then = start.replace(year=start.year + 10) start_date = start end_date = then elif end: last_year = datetime.now() - relativedelta(years=1) start_date = last_year end_date = end else: last_year = datetime.now() - relativedelta(years=1) then = datetime.now().replace(year=last_year.year + 10) start_date = last_year end_date = then return events, start_date, end_date, limit
def filter_Search(request): """ Filtering the Search according to the keys sent and the main search keyword Main : is the main keyword searched by the user from navbar Key: array of filter keywords sent from the view """ main = '' if 'main' in request.POST: main = request.POST['main'] if main != '': results = SearchQuerySet().filter(content=AutoQuery(main)).models(Task) results = results.filter_or(tags=main).models(Task) results = results.filter_or(title=AutoQuery(main)).models(Task) results = results.filter_or(location=AutoQuery(main)).models(Task) else: results = SearchQuerySet().all().models(Task) else: results = SearchQuerySet().all() if 'loca' in request.POST: loca = request.POST['loca'] if loca != '': results = results.filter_and(city=loca) if 'word' in request.POST: word = request.POST['word'] if word != '': results1 = results.filter_and(content=AutoQuery(word)) results2 = results.filter_and(title=AutoQuery(word)) results3 = results.filter_and(location=AutoQuery(word)) results4 = results1.__or__(results2) results = results4.__or__(results3) if 'skills' in request.POST: skills = request.POST['skills'] if skills != '': skills = skills.split(',') for s in skills: results = results.filter_and(tags=s) if 'from' in request.POST: from_price = request.POST['from'] to_price = request.POST['to'] if from_price != '' and to_price != '' and to_price != 'all': results = results.filter_and(price__range=[int(from_price), int(to_price)]) return render_to_response('search/results_tasks.html', {'tresults': results, 'keyword': main, 'taskCount': len(results)}, RequestContext(request))
def statisticInfo(request): """ 1 q=go不做全文过滤,检索标识 2 时间特殊处理 3 其他字段统一检索 4 使用 request.GET 注意预先检查该键是否存在,以及键对应的值是否为空(空值,不参与检索) :param request: :return: """ query = request.GET['q'] if (query != ''): posts = SearchQuerySet().filter(content=query) else: posts = SearchQuerySet().all() for key in [ 'title', 'keywords', 'creator', 'language', 'fileFormat', 'spatial', 'discipline', 'fileType' ]: if request.GET.has_key(key) and request.GET[key] != u'': posts = posts.filter_and(**{key: request.GET[key]}) if request.GET.__contains__('inp_start_date'): start_date = request.GET['inp_start_date'] if start_date: try: strcreatedate = '' if (int(start_date) >= 10000): strcreatedate = str(start_date)[0:4] elif (len(str(start_date)) == 1): if (int(start_date) == 0): strcreatedate = '0001' else: strcreatedate = '000' + str(start_date) elif (len(str(start_date)) == 2): strcreatedate = '00' + str(start_date) elif (len(str(start_date)) == 3): strcreatedate = '0' + str(start_date) else: strcreatedate = str(start_date) aa = (str(strcreatedate) + '/01/01').split('/') s_t = datetime.strptime(''.join(aa), "%Y%m%d").date() # print(s_t) posts = posts.filter_and(pubDate__gte=s_t) except ValueError: html = "<html><script>alert('请输入正确是年份 !')</script></html>" return HttpResponse(html) if request.GET.__contains__('inp_end_date'): end_date = request.GET['inp_end_date'] if end_date: strcreatedate = '' if (int(end_date) >= 10000): strcreatedate = str(end_date)[0:4] elif (len(str(end_date)) == 1): if (int(end_date) == 0): strcreatedate = '0001' else: strcreatedate = '000' + str(end_date) elif (len(str(end_date)) == 2): strcreatedate = '00' + str(end_date) elif (len(str(end_date)) == 3): strcreatedate = '0' + str(end_date) else: strcreatedate = str(end_date) aa = (str(strcreatedate) + '/01/01').split('/') e_t = datetime.strptime(''.join(aa), "%Y%m%d").date() posts = posts.filter_and(pubDate__lte=e_t) posts = posts.filter(check=True) posts = posts.order_by("-updateDate") sqs = posts.facet('fileFormat').facet('spatial').facet('discipline').facet( 'language').facet('fileType').facet_counts() ret_num = posts.count() return render_to_response('himalaya/statisticInfo.html', { 'posts': posts, 'num': ret_num, 'sqs': sqs, 'query': query }, context_instance=RequestContext(request))
def base_statisticInfo(request): base_type = u'基础文献' query = request.GET['q'] if (query == ''): posts = SearchQuerySet().all() else: posts = SearchQuerySet().filter(content=query) for key in [ 'title', 'keywords', 'creator', 'language', 'fileFormat', 'spatial', 'discipline', 'fileType' ]: if request.GET.has_key(key) and request.GET[key] != u'': posts = posts.filter(**{key: request.GET[key]}) posts = posts.filter_and(check=1).filter(subjecttype='-1') if request.GET.__contains__('inp_start_date'): start_date = request.GET['inp_start_date'] if start_date: strcreatedate = '' if (int(start_date) >= 10000): strcreatedate = str(start_date)[0:4] elif (len(str(start_date)) == 1): if (int(start_date) == 0): strcreatedate = '0001' else: strcreatedate = '000' + str(start_date) elif (len(str(start_date)) == 2): strcreatedate = '00' + str(start_date) elif (len(str(start_date)) == 3): strcreatedate = '0' + str(start_date) else: strcreatedate = str(start_date) aa = (str(strcreatedate) + '/01/01').split('/') s_t = datetime.strptime(''.join(aa), "%Y%m%d").date() posts = posts.filter_and(pubDate__gte=s_t) if request.GET.__contains__('inp_end_date'): end_date = request.GET['inp_end_date'] if end_date: strcreatedate = '' if (int(end_date) >= 10000): strcreatedate = str(end_date)[0:4] elif (len(str(end_date)) == 1): if (int(end_date) == 0): strcreatedate = '0001' else: strcreatedate = '000' + str(end_date) elif (len(str(end_date)) == 2): strcreatedate = '00' + str(end_date) elif (len(str(end_date)) == 3): strcreatedate = '0' + str(end_date) else: strcreatedate = str(end_date) aa = (str(strcreatedate) + '/01/01').split('/') e_t = datetime.strptime(''.join(aa), "%Y%m%d").date() posts = posts.filter_and(pubDate__lte=e_t) posts = posts.order_by("-updateDate") sqs = posts.facet('fileFormat').facet('spatial').facet('discipline').facet( 'language').facet('fileType').facet_counts() ret_num = posts.count() # 得到面搜索结果 # print query return render( request, 'himalaya/base_statisticInfo.html', { 'posts': posts, 'num': ret_num, 'sqs': sqs, 'query': query, 'base_type': base_type })
def autocomplete(request): """Return autocomplete JSON results""" term = request.GET.get('term', '').strip() response_data = [] if len(term): # Does not work - probably because the FLAG_PARTIAL is not set on Xapian # (trying to set it in settings.py as documented appears to have no effect) # sqs = SearchQuerySet().autocomplete(name_auto=term) # Split the search term up into little bits terms = re.split(r'\s+', term) # Build up a query based on the bits sqs = SearchQuerySet() for bit in terms: # print "Adding '%s' to the '%s' query" % (bit,term) sqs = sqs.filter_and(name_auto__startswith=sqs.query.clean(bit)) sqs = sqs.exclude(hidden=False) # If we have a kind then filter on that too model_kind = request.GET.get('model', None) if model_kind: model = known_kinds.get(model_kind, None) if model: sqs = sqs.models(model) else: sqs = sqs.models( models.Person, models.Organisation, models.Place, models.PositionTitle, ) # collate the results into json for the autocomplete js for result in sqs.all()[0:10]: o = result.object css_class = o.css_class() extra_autocomplete_data = None if hasattr(o, 'extra_autocomplete_data'): extra_autocomplete_data = o.extra_autocomplete_data image_url = None if hasattr(o, 'primary_image'): image = o.primary_image() if image: image_url = get_thumbnail(image, '16x16', crop="center").url if not image_url: image_url = "/static/images/" + css_class + "-16x16.jpg" response_data.append({ 'url': o.get_absolute_url(), 'name': o.name, 'image_url': image_url, 'extra_data': extra_autocomplete_data, 'type': css_class, 'value': o.name, 'object': o }) remove_duplicate_places(response_data) # Remove the 'object' elements before returning the response: for d in response_data: del d['object'] # send back the results as JSON return HttpResponse( simplejson.dumps(response_data), content_type='application/json', )
def search(self,repeat_search=False): # First, store the SearchQuerySet received from other processing. sqs = super(PermissionSearchForm, self).search() #Whoosh workaround #if self.get_models(): # for model in ['%s.%s'%(m._meta.app_label,m._meta.object_name) for m in self.get_models()]: # sqs = sqs.filter_or(django_ct=model) if not self.is_valid(): return self.no_query_found() query_text = self.cleaned_data['q'] states = self.cleaned_data['state'] ras = self.cleaned_data['ra'] has_filter = states or ras if has_filter and not query_text: # If there is a filter, but no `q`uery then we'll force some results. sqs = SearchQuerySet().order_by('-modified') if query_text and sqs.count() == 0: from urllib import quote_plus suggestions = [] has_suggestions = False suggested_query = [] for token in query_text.split(" "): if token: # remove blanks suggestion = SearchQuerySet().spelling_suggestion(token) if suggestion: suggested_query.append(suggestion) has_suggestions = True else: suggested_query.append(token) suggestions.append((token,suggestion)) self.spelling_suggestions = suggestions self.has_spelling_suggestions = has_suggestions self.suggested_query = quote_plus(' '.join(suggested_query),safe="") user = self.request.user if states and not ras: states = [MDR.STATES[int(s)] for s in self.cleaned_data['state']] sqs = sqs.filter(statuses__in=states) elif ras and not states: ras = [ra for ra in self.cleaned_data['ra']] sqs = sqs.filter(registrationAuthorities__in=ras) elif states and ras: # If we have both states and ras, merge them so we only search for # items with those statuses in those ras terms = ["%s___%s"%(str(r),str(s)) for r in ras for s in states] sqs = sqs.filter(ra_statuses__in=terms) if user.is_anonymous(): # Regular users can only see public items, so boot them off now. return sqs.filter_and(is_public=True) q = Q() if user.is_superuser: pass elif not user.profile.is_registrar: # Non-registrars can only see public things or things in their workgroup q |= Q(workgroup__in=user.profile.workgroups.all()) elif user.profile.is_registrar: q |= Q(workgroup__in=user.profile.workgroups.all()) q |= Q(registrationAuthorities__in=user.profile.registrarAuthorities) else: #I'm paranoid... q = Q(is_public=True) return sqs.filter(q) if self.cleaned_data['public_only'] == True: q &= Q(is_public=True) if self.cleaned_data['myWorkgroups_only'] == True: q &= Q(workgroup__in=user.profile.workgroups.all()) sqs = sqs.filter(q) if repeat_search == False and (states or ras) and sqs.count() == 0: # If there are 0 results, and filters, lets be nice and remove them. # There will be a big message on the search page that says what we did. self.cleaned_data['state'] = None self.cleaned_data['ra'] = None self.auto_broaden_search = True sqs = self.search(repeat_search=True) return sqs
def search(request): q = request.GET.get('q', '') facet_counts = {} if q: cat_filter = request.GET.get('category') qs = SearchQuerySet() qs = qs.filter(content=q) qs = qs.filter_or(speakers__startswith=q.lower()) if cat_filter: # TODO: This doesn't work quite right. It should filter # out anything that's not *exactly* cat_filter but it's # not. Could be a problem here or with the indexing. The # haystack docs are mysterious. qs = qs.filter_and(category__exact=cat_filter) # TODO: Whoosh doesn't handle faceting, so we have to do it # manually. Fix this so it detects whether the haystack backend # supports facets and if so, uses the backend and not the db. cat_counts = {} for mem in qs: cat_counts[mem.category] = cat_counts.get(mem.category, 0) + 1 facet_counts['category'] = sorted( cat_counts.items(), key=lambda pair: pair[1], reverse=True) page = Paginator(qs, 25) p = request.GET.get('p', '1') try: p = max(1, int(p)) except ValueError: p = 1 try: page = page.page(p) except EmptyPage: page = page.page(1) else: page = None if q: title = u'Search: {query}'.format(query=q) else: title = u'Search' get_params = request.GET.copy() if 'category' in get_params: get_params.pop('category') base_url = request.path + '?' + get_params.urlencode() return render( request, 'videos/search.html', { 'query': q, 'base_url': base_url, 'title': title, 'facet_counts': facet_counts, 'page': page })
def statisticInfo(request): """ 1 q=go不做全文过滤,检索标识 2 时间特殊处理 3 其他字段统一检索 4 使用 request.GET 注意预先检查该键是否存在,以及键对应的值是否为空(空值,不参与检索) :param request: :return: """ query = request.GET['q'] if(query!=''): posts = SearchQuerySet().filter(content=query) else: posts = SearchQuerySet().all() for key in ['title', 'keywords', 'creator', 'language','fileFormat','spatial','discipline','fileType']: if request.GET.has_key(key) and request.GET[key]!=u'': posts = posts.filter_and(**{key: request.GET[key]}) if request.GET.__contains__('inp_start_date'): start_date = request.GET['inp_start_date'] if start_date: try: strcreatedate = '' if (int(start_date) >= 10000): strcreatedate = str(start_date)[0:4] elif (len(str(start_date)) == 1): if (int(start_date) == 0): strcreatedate = '0001' else: strcreatedate = '000' + str(start_date) elif (len(str(start_date)) == 2): strcreatedate = '00' + str(start_date) elif (len(str(start_date)) == 3): strcreatedate = '0' + str(start_date) else: strcreatedate = str(start_date) aa = (str(strcreatedate) + '/01/01').split('/') s_t = datetime.strptime(''.join(aa), "%Y%m%d").date() # print(s_t) posts = posts.filter_and(pubDate__gte=s_t) except ValueError: html = "<html><script>alert('请输入正确是年份 !')</script></html>" return HttpResponse(html) if request.GET.__contains__('inp_end_date'): end_date = request.GET['inp_end_date'] if end_date: strcreatedate = '' if (int(end_date) >= 10000): strcreatedate = str(end_date)[0:4] elif (len(str(end_date)) == 1): if (int(end_date) == 0): strcreatedate = '0001' else: strcreatedate = '000' + str(end_date) elif (len(str(end_date)) == 2): strcreatedate = '00' + str(end_date) elif (len(str(end_date)) == 3): strcreatedate = '0' + str(end_date) else: strcreatedate = str(end_date) aa = (str(strcreatedate) + '/01/01').split('/') e_t = datetime.strptime(''.join(aa), "%Y%m%d").date() posts = posts.filter_and(pubDate__lte=e_t) posts = posts.filter(check=True) posts = posts.order_by("-updateDate") sqs = posts.facet('fileFormat').facet('spatial').facet('discipline').facet('language').facet( 'fileType').facet_counts() ret_num = posts.count() return render_to_response('himalaya/statisticInfo.html', {'posts': posts, 'num': ret_num, 'sqs': sqs, 'query': query}, context_instance=RequestContext(request))
def map_search_endpoint(request, filter_group_id=None): """ Maps API search endpoint using haystack search results. For parameters see ``MAP_SEARCH_PARAMETERS`` returns JSON with the contents of type ``HaystackMapResult`` @param filter_group_id: Will filter all items by group relation, where applicable (i.e. users are filtered by group memberships for that group, events as events in that group) """ implicit_ignore_location = not any([ loc_param in request.GET for loc_param in ['sw_lon', 'sw_lat', 'ne_lon', 'ne_lat'] ]) params = _collect_parameters(request.GET, MAP_SEARCH_PARAMETERS) query = force_text(params['q']) limit = params['limit'] page = params['page'] item_id = params['item'] prefer_own_portal = getattr(settings, 'MAP_API_HACKS_PREFER_OWN_PORTAL', False) if not is_number(limit) or limit < 0: return HttpResponseBadRequest( '``limit`` param must be a positive number or 0!') limit = min(limit, SERVER_SIDE_SEARCH_LIMIT) if not is_number(page) or page < 0: return HttpResponseBadRequest( '``page`` param must be a positive number or 0!') # filter for requested model types model_list = [ klass for klass, param_name in list(SEARCH_MODEL_NAMES.items()) if params.get(param_name, False) ] sqs = SearchQuerySet().models(*model_list) # filter for map bounds (Points are constructed ith (lon, lat)!!!) if not params['ignore_location'] and not implicit_ignore_location: sqs = sqs.within('location', Point(params['sw_lon'], params['sw_lat']), Point(params['ne_lon'], params['ne_lat'])) # filter for user's own content if params['mine'] and request.user.is_authenticated: user_id = request.user.id sqs = sqs.filter_and( Q(creator=user_id) | Q(user_id=user_id) | Q(group_members=user_id)) # filter for search terms if query: sqs = sqs.auto_query(query) # group-filtered-map view for on-group pages if filter_group_id: group = get_object_or_None(get_cosinnus_group_model(), id=filter_group_id) if group: filtered_groups = [filter_group_id] # get child projects of this group filtered_groups += [ subproject.id for subproject in group.get_children() if subproject.is_active ] sqs = sqs.filter_and( Q(membership_groups__in=filtered_groups) | Q(group__in=filtered_groups)) # filter topics topics = ensure_list_of_ints(params.get('topics', '')) if topics: sqs = sqs.filter_and(mt_topics__in=topics) # filter for portal visibility sqs = filter_searchqueryset_for_portal( sqs, restrict_multiportals_to_current=prefer_own_portal) # filter for read access by this user sqs = filter_searchqueryset_for_read_access(sqs, request.user) # filter events by upcoming status if params['events'] and Event is not None: sqs = filter_event_searchqueryset_by_upcoming(sqs) # filter all default user groups if the new dashboard is being used (they count as "on plattform" and aren't shown) if getattr(settings, 'COSINNUS_USE_V2_DASHBOARD', False): sqs = sqs.exclude(is_group_model=True, slug__in=get_default_user_group_slugs()) # if we hae no query-boosted results, use *only* our custom sorting (haystack's is very random) if not query: if prefer_own_portal: sqs = sqs.order_by('-portal', '-local_boost') else: sqs = sqs.order_by('-local_boost') # sort results into one list per model total_count = sqs.count() sqs = sqs[limit * page:limit * (page + 1)] results = [] for result in sqs: # if we hae no query-boosted results, use *only* our custom sorting (haystack's is very random) if not query: result.score = result.local_boost if prefer_own_portal and is_number(result.portal) and int( result.portal) == CosinnusPortal.get_current().id: result.score += 100.0 results.append(HaystackMapResult(result, user=request.user)) # if the requested item (direct select) is not in the queryset snippet # (might happen because of an old URL), then mix it in as first item and drop the last if item_id: item_id = str(item_id) if not any([res['id'] == item_id for res in results]): item_result = get_searchresult_by_itemid(item_id, request.user) if item_result: results = [HaystackMapResult(item_result, user=request.user) ] + results[:-1] page_obj = None if results: page_obj = { 'index': page, 'count': len(results), 'total_count': total_count, 'start': (limit * page) + 1, 'end': (limit * page) + len(results), 'has_next': total_count > (limit * (page + 1)), 'has_previous': page > 0, } data = { 'results': results, 'page': page_obj, } return JsonResponse(data)
def autocomplete(request): """Return autocomplete JSON results""" term = request.GET.get('term','').strip() response_data = [] if len(term): # Does not work - probably because the FLAG_PARTIAL is not set on Xapian # (trying to set it in settings.py as documented appears to have no effect) # sqs = SearchQuerySet().autocomplete(name_auto=term) # Split the search term up into little bits terms = re.split(r'\s+', term) # Build up a query based on the bits sqs = SearchQuerySet() for bit in terms: # print "Adding '%s' to the '%s' query" % (bit,term) sqs = sqs.filter_and( name_auto__startswith = sqs.query.clean( bit ) ) sqs = sqs.exclude(hidden=False) # If we have a kind then filter on that too model_kind = request.GET.get('model', None) if model_kind: model = known_kinds.get(model_kind, None) if model: sqs = sqs.models(model) else: sqs = sqs.models( models.Person, models.Organisation, models.Place, models.PositionTitle, ) # collate the results into json for the autocomplete js for result in sqs.all()[0:10]: object = result.object css_class = object.css_class() # use the specific field if it has one if hasattr(object, 'name_autocomplete_html'): label = object.name_autocomplete_html else: label = object.name image_url = None if hasattr(object, 'primary_image'): image = object.primary_image() if image: image_url = get_thumbnail(image, '16x16', crop="center").url if not image_url: image_url = "/static/images/" + css_class + "-16x16.jpg" response_data.append({ 'url': object.get_absolute_url(), 'label': '<img height="16" width="16" src="%s" /> %s' % (image_url, label), 'type': css_class, 'value': object.name, 'object': object }) remove_duplicate_places(response_data) # Remove the 'object' elements before returning the response: for d in response_data: del d['object'] # send back the results as JSON return HttpResponse( simplejson.dumps(response_data), content_type='application/json', )
def base_statisticInfo(request): base_type = u'基础文献' query = request.GET['q'] if (query == ''): posts = SearchQuerySet().all() else: posts = SearchQuerySet().filter(content=query) for key in ['title', 'keywords', 'creator', 'language', 'fileFormat', 'spatial', 'discipline', 'fileType']: if request.GET.has_key(key) and request.GET[key] != u'': posts = posts.filter(**{key: request.GET[key]}) posts = posts.filter_and(check=1).filter(subjecttype='-1') if request.GET.__contains__('inp_start_date'): start_date = request.GET['inp_start_date'] if start_date: strcreatedate = '' if (int(start_date) >= 10000): strcreatedate = str(start_date)[0:4] elif (len(str(start_date)) == 1): if (int(start_date) == 0): strcreatedate = '0001' else: strcreatedate = '000' + str(start_date) elif (len(str(start_date)) == 2): strcreatedate = '00' + str(start_date) elif (len(str(start_date)) == 3): strcreatedate = '0' + str(start_date) else: strcreatedate = str(start_date) aa = (str(strcreatedate) + '/01/01').split('/') s_t = datetime.strptime(''.join(aa), "%Y%m%d").date() posts = posts.filter_and(pubDate__gte=s_t) if request.GET.__contains__('inp_end_date'): end_date = request.GET['inp_end_date'] if end_date: strcreatedate = '' if (int(end_date) >= 10000): strcreatedate = str(end_date)[0:4] elif (len(str(end_date)) == 1): if (int(end_date) == 0): strcreatedate = '0001' else: strcreatedate = '000' + str(end_date) elif (len(str(end_date)) == 2): strcreatedate = '00' + str(end_date) elif (len(str(end_date)) == 3): strcreatedate = '0' + str(end_date) else: strcreatedate = str(end_date) aa = (str(strcreatedate) + '/01/01').split('/') e_t = datetime.strptime(''.join(aa), "%Y%m%d").date() posts = posts.filter_and(pubDate__lte=e_t) posts = posts.order_by("-updateDate") sqs = posts.facet('fileFormat').facet('spatial').facet('discipline').facet('language').facet( 'fileType').facet_counts() ret_num = posts.count() # 得到面搜索结果 # print query return render(request, 'himalaya/base_statisticInfo.html', {'posts': posts, 'num': ret_num, 'sqs': sqs, 'query': query, 'base_type': base_type})
def search(request): q = request.GET.get('q', '') facet_counts = {} if q: cat_filter = request.GET.get('category') qs = SearchQuerySet() qs = qs.filter(content=q) qs = qs.filter_or(speakers__startswith=q.lower()) if cat_filter: # TODO: This doesn't work quite right. It should filter # out anything that's not *exactly* cat_filter but it's # not. Could be a problem here or with the indexing. The # haystack docs are mysterious. qs = qs.filter_and(category__exact=cat_filter) # TODO: Whoosh doesn't handle faceting, so we have to do it # manually. Fix this so it detects whether the haystack backend # supports facets and if so, uses the backend and not the db. cat_counts = {} for mem in qs: cat_counts[mem.category] = cat_counts.get(mem.category, 0) + 1 facet_counts['category'] = sorted( cat_counts.items(), key=lambda pair: pair[1], reverse=True) page = Paginator(qs, 25) p = request.GET.get('p', '1') try: p = max(1, int(p)) except ValueError: p = 1 try: page = page.page(p) except EmptyPage: page = page.page(1) else: page = None if q: title = u'Search: {query}'.format( query=bleach.clean(q, tags=[], strip=True)) else: title = u'Search' get_params = request.GET.copy() if 'category' in get_params: get_params.pop('category') base_url = request.path + '?' + get_params.urlencode() return render( request, 'videos/search.html', { 'query': q, 'base_url': base_url, 'title': title, 'facet_counts': facet_counts, 'page': page })
def get_haystack_queryset(self, order_by='-obligation_date'): """ Returns a Haystack QuerySet object with the appropriate filters """ # order AND filters first, then OR filters and_filters = {} or_filters = {} for f in self.filters: filter_field = f[0] filter_value = f[1] filter_conjunction = f[2] # select appropriate target list target_filter_list = and_filters if f[2] == self.CONJUNCTION_OR: target_filter_list = or_filters # deal with fk if self.FIELD_MAPPINGS[filter_field]['type'] == 'fk': field_operator = filter_field + '__in' fk_transformation = self.FIELD_MAPPINGS[filter_field].get( 'solr_transformation', lambda x: x) if type(filter_value) not in (list, tuple): filter_value = (filter_value, ) fk_values = [] for value in filter_value: fk_value = fk_transformation(value) if fk_value is not None: fk_values.append(str(fk_value)) target_filter_list[field_operator] = fk_values # deal with range if self.FIELD_MAPPINGS[filter_field]['type'] == 'range': clause_parts = [] range_transformation = self.FIELD_MAPPINGS[filter_field].get( 'solr_transformation', lambda x: x) # putting this in place filter_operators = ('__gte', '__lte') for i, range_specifier in enumerate(filter_value): if range_specifier is not None: target_filter_list[ self.FIELD_MAPPINGS[filter_field]['solr_field'] + filter_operators[i]] = range_specifier # deal with text elif self.FIELD_MAPPINGS[filter_field]['type'] == 'text': target_filter_list[filter_field] = filter_value s = SearchQuerySet().models(self.DJANGO_MODEL) # add sector filtering -- only works for a single sector at the moment if len(self.sectors): s = s.filter_and( sectors__in=map(lambda x: int(x.id), self.sectors)) if len(or_filters): s = s.filter_or(**or_filters) if len(and_filters): s = s.filter_and(**and_filters) s = s.order_by(order_by) return s