Пример #1
0
 def test_from_queryset(self):
     bare_stats = BareAccessStatistics.from_queryset(
             Paper.objects.filter(authors_list__contains=[{'researcher_id': self.r2.id}]).distinct())
     stats = self.r2.stats
     self.assertEqual(bare_stats.num_oa, stats.num_oa)
     self.assertEqual(bare_stats.num_ok, stats.num_ok)
     self.assertEqual(bare_stats.num_couldbe, stats.num_couldbe)
     self.assertEqual(bare_stats.num_unk, stats.num_unk)
     self.assertEqual(bare_stats.num_closed, stats.num_closed)
     self.assertEqual(bare_stats.num_tot, stats.num_tot)
Пример #2
0
def searchView(request, **kwargs):
    """
    Any view that returns a list of papers can be expressed using this "search" view.
    It allows to select a list of papers based on a list of criteria (but no keyword search for now).
    """
    context = dict()
    # Build the queryset
    queryset = Paper.objects.all()
    args = request.GET.copy()
    args.update(kwargs)

    if 'journal' in args and not is_admin(request.user):
        del args['journal']

    search_description = _('Papers')
    head_search_description = _('Papers')

    context['researcher_id'] = None
    if 'researcher' in args or 'orcid' in args:
        researcher = None
        if 'researcher' in args:
            researcher = get_object_or_404(Researcher,
                                           pk=args.get('researcher'))
        elif 'orcid' in args:
            try:
                researcher = Researcher.objects.get(orcid=args.get('orcid'))
            except Researcher.DoesNotExist:
                try:
                    orcid = validate_orcid(args.get('orcid'))
                    researcher = Researcher.get_or_create_by_orcid(orcid)
                    researcher.init_from_orcid()
                except MetadataSourceException:
                    raise Http404(_('Invalid ORCID profile.'))

        # Redirect if slug doesn't match or researcher is looked up by ORCID.
        if kwargs.get('slug', '') != researcher.slug:
            view_args = {'researcher': researcher.id, 'slug': researcher.slug}
            url = reverse('researcher', kwargs=view_args)
            query_string = request.META.get('QUERY_STRING', '')
            if query_string:
                url += '?' + query_string
            return HttpResponsePermanentRedirect(url)

        queryset = queryset.filter(author__researcher=researcher)
        search_description += _(' authored by ') + unicode(researcher)
        head_search_description = unicode(researcher)
        context['researcher'] = researcher
        context['researcher_id'] = researcher.id
        context['breadcrumbs'] = researcher.breadcrumbs()
    elif 'department' in args:
        dept = get_object_or_404(Department, pk=args.get('department'))
        queryset = queryset.filter(author__researcher__department=dept)
        search_description = unicode(dept)
        head_search_description = unicode(dept)
        context['department'] = dept
        context['breadcrumbs'] = dept.breadcrumbs() + [(_('Papers'), '')]
    elif 'name' in args:
        name = get_object_or_404(Name, pk=args.get('name'))
        queryset = queryset.filter(author__name=name)
        search_description += _(' authored by ') + unicode(name)
        head_search_description = unicode(name)
        context['name'] = name
    if 'journal' in args:
        journal = get_object_or_404(Journal, pk=args.get('journal'))
        queryset = queryset.filter(oairecord__journal=journal)
        search_description += _(' in ') + unicode(journal)
        context['journal'] = journal
        context['breadcrumbs'] = journal.breadcrumbs()
    elif 'publisher' in args:
        publisher = get_object_or_404(Publisher, pk=args.get('publisher'))
        queryset = queryset.filter(oairecord__publisher=publisher)
        search_description += _(' published by ') + unicode(publisher)
        head_search_description = unicode(publisher)
        context['publisher'] = publisher
        context['breadcrumbs'] = publisher.breadcrumbs() + [(_('Papers'), '')]
    if 'state' in args:
        state = args.get('state')
        context['state'] = state
        state_filter = STATUS_QUERYSET_FILTER.get(state)
        if state_filter is not None:
            queryset = state_filter(queryset)
    if 'status' in args:
        queryset = queryset.filter(oa_status=args.get('status'))
        # We don't update the search description here, it will be displayed on the side
        context['status'] = args.get('status')
    if 'pdf' in args:
        val = args.get('pdf')
        if val == 'OK':
            queryset = queryset.filter(pdf_url__isnull=False)
        elif val == 'NOK':
            queryset = queryset.filter(pdf_url__isnull=True)
        context['pdf'] = val
    if 'pubtype' in args:
        val = args.get('pubtype')
        if val in PAPER_TYPE_PREFERENCE:
            queryset = queryset.filter(doctype=val)
        context['pubtype'] = val
    if 'visibility' in args and is_admin(request.user):
        val = args.get('visibility')
        if val in [x[0] for x in VISIBILITY_CHOICES]:
            queryset = queryset.filter(visibility=val)
        context['visibility'] = val
    else:
        queryset = queryset.filter(visibility='VISIBLE')
        context['visibility'] = 'VISIBLE'

    if search_description == _('Papers'):
        context['breadcrumbs'] = [(search_description, '')]
        search_description = _('All papers')

    # Sort
    queryset = queryset.order_by('-pubdate')
    # Make distinct
    queryset = queryset.distinct()
    # Create stats
    stats = BareAccessStatistics.from_queryset(queryset)

    # Build the paginator
    paginator = Paginator(queryset, NB_RESULTS_PER_PAGE)
    page = args.get('page')
    try:
        current_papers = paginator.page(page)
    except PageNotAnInteger:
        current_papers = paginator.page(1)
    except EmptyPage:
        current_papers = paginator.page(paginator.num_pages)

    context['search_stats'] = stats
    context['search_results'] = current_papers
    context['search_description'] = search_description
    context['head_search_description'] = head_search_description
    context['nb_results'] = paginator.count
    context['ajax_url'] = reverse('ajax-search') + '?' + urlencode(args)

    # Notifications
    # TODO: unefficient query.
    notifications = get_notifications(request)
    selected_messages = map(
        lambda n: n.serialize_to_json(),
        sorted(notifications, key=lambda msg: msg.level)[:3])
    context['messages'] = selected_messages

    # Build the GET requests for variants of the parameters
    args_without_page = args.copy()
    if 'page' in args_without_page:
        del args_without_page['page']
    oa_variants = varyQueryArguments('status', args_without_page,
                                     OA_STATUS_CHOICES)
    pdf_variants = varyQueryArguments('pdf', args_without_page,
                                      PDF_STATUS_CHOICES)
    pubtype_variants = varyQueryArguments('pubtype', args_without_page,
                                          PAPER_TYPE_CHOICES)
    visibility_variants = varyQueryArguments('visibility', args_without_page,
                                             VISIBILITY_CHOICES)
    state_variants = varyQueryArguments('state', args_without_page,
                                        COMBINED_STATUS_CHOICES)

    context['oa_status_choices'] = oa_variants
    context['pdf_status_choices'] = pdf_variants
    context['state_choices'] = state_variants
    context['pubtype_status_choices'] = pubtype_variants
    context['visibility_choices'] = visibility_variants

    if request.META.get(
            'CONTENT_TYPE') == 'application/json' and 'researcher' in context:
        researcher = context['researcher']
        context['request'] = request
        response = {}
        response['listPapers'] = loader.render_to_string(
            'papers/ajaxListPapers.html', context)
        response['stats'] = json.loads(stats.pie_data(researcher.object_id))
        response['stats']['numtot'] = stats.num_tot
        response['messages'] = selected_messages
        if researcher.current_task:
            response['status'] = researcher.current_task
            response['display'] = researcher.get_current_task_display()
        return HttpResponse(json.dumps(response),
                            content_type="application/json")
    return render(request, 'papers/search.html', context)
Пример #3
0
def searchView(request, **kwargs):
    """
    Any view that returns a list of papers can be expressed using this "search" view.
    It allows to select a list of papers based on a list of criteria (but no keyword search for now).
    """
    context = dict()
    # Build the queryset
    queryset = Paper.objects.all()
    args = request.GET.copy()
    args.update(kwargs)

    if 'journal' in args and not is_admin(request.user):
        del args['journal']
    
    search_description = _('Papers')
    head_search_description = _('Papers')

    context['researcher_id'] = None
    if 'researcher' in args or 'orcid' in args:
        researcher = None
        if 'researcher' in args:
            researcher = get_object_or_404(Researcher, pk=args.get('researcher'))
        elif 'orcid' in args:
            try:
                researcher = Researcher.objects.get(orcid=args.get('orcid'))
            except Researcher.DoesNotExist:
                try:
                    orcid = validate_orcid(args.get('orcid'))
                    researcher = Researcher.get_or_create_by_orcid(orcid)
                    researcher.init_from_orcid()
                except MetadataSourceException:
                    raise Http404(_('Invalid ORCID profile.'))

        # Redirect if slug doesn't match or researcher is looked up by ORCID.
        if kwargs.get('slug', '') != researcher.slug:
            view_args = {'researcher': researcher.id, 'slug': researcher.slug}
            url = reverse('researcher', kwargs=view_args)
            query_string = request.META.get('QUERY_STRING', '')
            if query_string:
                url += '?' + query_string
            return HttpResponsePermanentRedirect(url)

        queryset = queryset.filter(author__researcher=researcher)
        search_description += _(' authored by ')+unicode(researcher)
        head_search_description = unicode(researcher)
        context['researcher'] = researcher
        context['researcher_id'] = researcher.id
        context['breadcrumbs'] = researcher.breadcrumbs()
    elif 'department' in args:
        dept = get_object_or_404(Department, pk=args.get('department'))
        queryset = queryset.filter(author__researcher__department=dept)
        search_description = unicode(dept)
        head_search_description = unicode(dept)
        context['department'] = dept
        context['breadcrumbs'] = dept.breadcrumbs()+[(_('Papers'), '')]
    elif 'name' in args:
        name = get_object_or_404(Name, pk=args.get('name'))
        queryset = queryset.filter(author__name=name)
        search_description += _(' authored by ')+unicode(name)
        head_search_description = unicode(name)
        context['name'] = name
    if 'journal' in args:
        journal = get_object_or_404(Journal, pk=args.get('journal'))
        queryset = queryset.filter(oairecord__journal=journal)
        search_description += _(' in ')+unicode(journal)
        context['journal'] = journal
        context['breadcrumbs'] = journal.breadcrumbs()
    elif 'publisher' in args:
        publisher = get_object_or_404(Publisher, pk=args.get('publisher'))
        queryset = queryset.filter(oairecord__publisher=publisher)
        search_description += _(' published by ')+unicode(publisher)
        head_search_description = unicode(publisher)
        context['publisher'] = publisher
        context['breadcrumbs'] = publisher.breadcrumbs()+[(_('Papers'),'')]
    if 'state' in args:
        state = args.get('state')
        context['state'] = state
        state_filter = STATUS_QUERYSET_FILTER.get(state)
        if state_filter is not None:
            queryset = state_filter(queryset)
    if 'status' in args:
        queryset = queryset.filter(oa_status=args.get('status'))
        # We don't update the search description here, it will be displayed on the side
        context['status'] = args.get('status')
    if 'pdf' in args:
        val = args.get('pdf')
        if val == 'OK':
            queryset = queryset.filter(pdf_url__isnull=False)
        elif val == 'NOK':
            queryset = queryset.filter(pdf_url__isnull=True)
        context['pdf'] = val
    if 'pubtype' in args:
        val = args.get('pubtype')
        if val in PAPER_TYPE_PREFERENCE:
            queryset = queryset.filter(doctype=val)
        context['pubtype'] = val
    if 'visibility' in args and is_admin(request.user):
        val = args.get('visibility')
        if val in [x[0] for x in VISIBILITY_CHOICES]:
            queryset = queryset.filter(visibility=val)
        context['visibility'] = val
    else:
        queryset = queryset.filter(visibility='VISIBLE')
        context['visibility'] = 'VISIBLE'

    if search_description == _('Papers'):
        context['breadcrumbs'] = [(search_description,'')]
        search_description = _('All papers')

    # Sort
    queryset = queryset.order_by('-pubdate')
    # Make distinct
    queryset = queryset.distinct()
    # Create stats
    stats = BareAccessStatistics.from_queryset(queryset)

    # Build the paginator
    paginator = Paginator(queryset, NB_RESULTS_PER_PAGE)
    page = args.get('page')
    try:
        current_papers = paginator.page(page)
    except PageNotAnInteger:
        current_papers = paginator.page(1)
    except EmptyPage:
        current_papers = paginator.page(paginator.num_pages)

    context['search_stats'] = stats
    context['search_results'] = current_papers
    context['search_description'] = search_description
    context['head_search_description'] = head_search_description
    context['nb_results'] = paginator.count
    context['ajax_url'] = reverse('ajax-search')+'?'+urlencode(args) 

    # Notifications
    # TODO: unefficient query.
    notifications = get_notifications(request)
    selected_messages = map(lambda n: n.serialize_to_json(),
            sorted(notifications, key=lambda msg: msg.level)[:3])
    context['messages'] = selected_messages

    # Build the GET requests for variants of the parameters
    args_without_page = args.copy()
    if 'page' in args_without_page:
        del args_without_page['page']
    oa_variants = varyQueryArguments('status', args_without_page, OA_STATUS_CHOICES)
    pdf_variants = varyQueryArguments('pdf', args_without_page, PDF_STATUS_CHOICES)
    pubtype_variants = varyQueryArguments('pubtype', args_without_page, PAPER_TYPE_CHOICES)
    visibility_variants = varyQueryArguments('visibility', args_without_page, VISIBILITY_CHOICES)
    state_variants = varyQueryArguments('state', args_without_page, COMBINED_STATUS_CHOICES)

    context['oa_status_choices'] = oa_variants
    context['pdf_status_choices'] = pdf_variants
    context['state_choices'] = state_variants
    context['pubtype_status_choices'] = pubtype_variants
    context['visibility_choices'] = visibility_variants

    if request.META.get('CONTENT_TYPE') == 'application/json' and 'researcher' in context:
        researcher = context['researcher']
        context['request'] = request
        response = {}
        response['listPapers'] = loader.render_to_string('papers/ajaxListPapers.html', context)
        response['stats'] = json.loads(stats.pie_data(researcher.object_id))
        response['stats']['numtot'] = stats.num_tot
        response['messages'] = selected_messages
        if researcher.current_task:
            response['status'] = researcher.current_task
            response['display'] = researcher.get_current_task_display()
        return HttpResponse(json.dumps(response), content_type="application/json")
    return render(request, 'papers/search.html', context)