Beispiel #1
0
def collection(request, pk):
    collection = get_object_or_404(Collection, pk=pk)

    # Sort options
    sort_options = {"identifier": "dc.identifier.raw", "title": "dc.title.raw"}
    sort_option, sort_dir = get_sort_params(request.GET, sort_options,
                                            "identifier")
    sort_field = sort_options.get(sort_option)

    # Search
    search = DIP.es_doc.search().query("match", **{"collection.id": pk})
    # Exclude DIPs with 'PENDING' or 'FAILURE' import status
    # when the user is not an editor or an administrator.
    if not request.user.is_editor():
        search = search.exclude(
            "terms", import_status=[DIP.IMPORT_PENDING, DIP.IMPORT_FAILURE])
    search = add_query_to_search(search, request.GET.get("query", ""),
                                 ["dc.*"])
    search = search.sort({sort_field: {"order": sort_dir}})

    # Pagination
    page = get_page_from_search(search, request.GET)
    dips = page.object_list.execute()

    table_headers = [
        {
            "label": _("Identifier"),
            "sort_param": "identifier"
        },
        {
            "label": _("Title"),
            "sort_param": "title",
            "width": "25%"
        },
        {
            "label": _("Date"),
            "width": "10%"
        },
        {
            "label": _("Description")
        },
        {
            "label": _("Details")
        },
    ]

    return render(
        request,
        "collection.html",
        {
            "collection": collection,
            "dips": dips,
            "table_headers": table_headers,
            "sort_option": sort_option,
            "sort_dir": sort_dir,
            "page": page,
            "statuses": DIP.import_statuses(),
        },
    )
def collections(request, template):
    # Sort options
    sort_options = {
        'identifier': 'dc.identifier.raw',
        'title': 'dc.title.raw',
    }
    sort_option, sort_dir = get_sort_params(request.GET, sort_options,
                                            'identifier')
    sort_field = sort_options.get(sort_option)

    # Search:
    # This view is used in two URLs from scope.urls, where the template
    # is defined for each case. The query parameter should only filter in
    # the collections page.
    search = Collection.es_doc.search()
    if template == 'collections.html':
        search = add_query_to_search(search, request.GET.get('query', ''),
                                     ['dc.*'])
    search = search.sort({sort_field: {'order': sort_dir}})

    # Pagination
    page = get_page_from_search(search, request.GET)
    collections = page.object_list.execute()

    table_headers = [
        {
            'label': _('Identifier'),
            'sort_param': 'identifier'
        },
        {
            'label': _('Title'),
            'sort_param': 'title',
            'width': '25%'
        },
        {
            'label': _('Date'),
            'width': '10%'
        },
        {
            'label': _('Description')
        },
        {
            'label': _('Details')
        },
    ]

    # Date column should only appear in collections page
    if template != 'collections.html':
        table_headers.pop(2)

    return render(
        request, template, {
            'collections': collections,
            'table_headers': table_headers,
            'sort_option': sort_option,
            'sort_dir': sort_dir,
            'page': page,
        })
Beispiel #3
0
def collections(request, template):
    # Sort options
    sort_options = {"identifier": "dc.identifier.raw", "title": "dc.title.raw"}
    sort_option, sort_dir = get_sort_params(request.GET, sort_options,
                                            "identifier")
    sort_field = sort_options.get(sort_option)

    # Search:
    # This view is used in two URLs from scope.urls, where the template
    # is defined for each case. The query parameter should only filter in
    # the collections page.
    search = Collection.es_doc.search()
    if template == "collections.html":
        search = add_query_to_search(search, request.GET.get("query", ""),
                                     ["dc.*"])
    search = search.sort({sort_field: {"order": sort_dir}})

    # Pagination
    page = get_page_from_search(search, request.GET)
    collections = page.object_list.execute()

    table_headers = [
        {
            "label": _("Identifier"),
            "sort_param": "identifier"
        },
        {
            "label": _("Title"),
            "sort_param": "title",
            "width": "25%"
        },
        {
            "label": _("Date"),
            "width": "10%"
        },
        {
            "label": _("Description")
        },
        {
            "label": _("Details")
        },
    ]

    # Date column should only appear in collections page
    if template != "collections.html":
        table_headers.pop(2)

    return render(
        request,
        template,
        {
            "collections": collections,
            "table_headers": table_headers,
            "sort_option": sort_option,
            "sort_dir": sort_dir,
            "page": page,
        },
    )
Beispiel #4
0
def orphan_dips(request):
    if not request.user.is_editor():
        return redirect("home")

    # Sort options
    sort_options = {"identifier": "dc.identifier.raw", "title": "dc.title.raw"}
    sort_option, sort_dir = get_sort_params(request.GET, sort_options,
                                            "identifier")
    sort_field = sort_options.get(sort_option)

    # Search
    search = DIP.es_doc.search().exclude("exists", field="collection.id")
    search = add_query_to_search(search, request.GET.get("query", ""),
                                 ["dc.*"])
    search = search.sort({sort_field: {"order": sort_dir}})

    # Pagination
    page = get_page_from_search(search, request.GET)
    dips = page.object_list.execute()

    table_headers = [
        {
            "label": _("Identifier"),
            "sort_param": "identifier"
        },
        {
            "label": _("Title"),
            "sort_param": "title",
            "width": "25%"
        },
        {
            "label": _("Date"),
            "width": "10%"
        },
        {
            "label": _("Description")
        },
        {
            "label": _("Details")
        },
    ]

    return render(
        request,
        "orphan_dips.html",
        {
            "dips": dips,
            "table_headers": table_headers,
            "sort_option": sort_option,
            "sort_dir": sort_dir,
            "page": page,
            "statuses": DIP.import_statuses(),
        },
    )
Beispiel #5
0
 def test_add_query_to_search_populated_query(self):
     query = "test_query"
     modified_search = add_query_to_search(self.search, query,
                                           self.query_fields)
     expected_search = {
         "query": {
             "simple_query_string": {
                 "query": query,
                 "default_operator": "and",
                 "fields": self.query_fields,
             }
         }
     }
     self.assertEqual(modified_search.to_dict(), expected_search)
def dip(request, pk):
    dip = get_object_or_404(DIP, pk=pk)

    # Redirect to the collection page if the DIP is not visible
    if not dip.is_visible_by_user(request.user):
        return redirect('collection', pk=dip.collection.pk)

    # Show notification to user about import error
    if dip.import_status == DIP.IMPORT_FAILURE:
        messages.error(request, dip.get_import_error_message())

    # Sort options
    sort_options = {
        'path': 'filepath.raw',
        'format': 'fileformat.raw',
        'size': 'size_bytes',
        'date': 'datemodified',
    }
    sort_option, sort_dir = get_sort_params(request.GET, sort_options, 'path')
    sort_field = sort_options.get(sort_option)

    # Search
    search = DigitalFile.es_doc.search().query(
        'match',
        **{'dip.id': pk},
    )
    fields = ['filepath', 'fileformat']
    search = add_query_to_search(search, request.GET.get('query', ''), fields)
    search = search.sort({sort_field: {'order': sort_dir}})

    # Aggregations and filters
    search = add_digital_file_aggs(search, collections=False)
    filters, valid_filters = _get_and_validate_digital_file_filters(request)
    search = add_digital_file_filters(search, valid_filters)

    # Pagination
    page = get_page_from_search(search, request.GET)
    es_response = page.object_list.execute()

    table_headers = [
        {
            'label': _('Filepath'),
            'sort_param': 'path'
        },
        {
            'label': _('Format'),
            'sort_param': 'format'
        },
        {
            'label': _('Size'),
            'sort_param': 'size'
        },
        {
            'label': _('Last modified'),
            'sort_param': 'date'
        },
        {
            'label': _('File details')
        },
    ]

    return render(
        request, 'dip.html', {
            'dip': dip,
            'digital_files': es_response.hits,
            'aggs': es_response.aggregations,
            'filters': filters,
            'table_headers': table_headers,
            'sort_option': sort_option,
            'sort_dir': sort_dir,
            'page': page,
            'statuses': DIP.import_statuses(),
        })
def collection(request, pk):
    collection = get_object_or_404(Collection, pk=pk)

    # Sort options
    sort_options = {
        'identifier': 'dc.identifier.raw',
        'title': 'dc.title.raw',
    }
    sort_option, sort_dir = get_sort_params(request.GET, sort_options,
                                            'identifier')
    sort_field = sort_options.get(sort_option)

    # Search
    search = DIP.es_doc.search().query(
        'match',
        **{'collection.id': pk},
    )
    # Exclude DIPs with 'PENDING' or 'FAILURE' import status
    # when the user is not an editor or an administrator.
    if not request.user.is_editor():
        search = search.exclude(
            'terms',
            import_status=[DIP.IMPORT_PENDING, DIP.IMPORT_FAILURE],
        )
    search = add_query_to_search(search, request.GET.get('query', ''),
                                 ['dc.*'])
    search = search.sort({sort_field: {'order': sort_dir}})

    # Pagination
    page = get_page_from_search(search, request.GET)
    dips = page.object_list.execute()

    table_headers = [
        {
            'label': _('Identifier'),
            'sort_param': 'identifier'
        },
        {
            'label': _('Title'),
            'sort_param': 'title',
            'width': '25%'
        },
        {
            'label': _('Date'),
            'width': '10%'
        },
        {
            'label': _('Description')
        },
        {
            'label': _('Details')
        },
    ]

    return render(
        request, 'collection.html', {
            'collection': collection,
            'dips': dips,
            'table_headers': table_headers,
            'sort_option': sort_option,
            'sort_dir': sort_dir,
            'page': page,
            'statuses': DIP.import_statuses(),
        })
def search(request):
    # Sort options
    sort_options = {
        'path': 'filepath.raw',
        'format': 'fileformat.raw',
        'size': 'size_bytes',
        'date': 'datemodified',
    }
    sort_option, sort_dir = get_sort_params(request.GET, sort_options, 'path')
    sort_field = sort_options.get(sort_option)

    # Search
    search = DigitalFile.es_doc.search()
    # Exclude DigitalFiles in DIPs with 'PENDING' or 'FAILURE' import
    # status when the user is not an editor or an administrator.
    if not request.user.is_editor():
        search = search.exclude(
            'terms',
            **{'dip.import_status': [DIP.IMPORT_PENDING, DIP.IMPORT_FAILURE]},
        )
    fields = ['filepath', 'fileformat', 'collection.title']
    search = add_query_to_search(search, request.GET.get('query', ''), fields)
    search = search.sort({sort_field: {'order': sort_dir}})

    # Aggregations and filters
    search = add_digital_file_aggs(search)
    filters, valid_filters = _get_and_validate_digital_file_filters(request)
    search = add_digital_file_filters(search, valid_filters)

    # Pagination
    page = get_page_from_search(search, request.GET)
    es_response = page.object_list.execute()

    table_headers = [
        {
            'label': _('Filepath'),
            'sort_param': 'path'
        },
        {
            'label': _('Format'),
            'sort_param': 'format'
        },
        {
            'label': _('Size'),
            'sort_param': 'size'
        },
        {
            'label': _('Last modified'),
            'sort_param': 'date'
        },
        {
            'label': _('Collection name')
        },
        {
            'label': _('File details')
        },
    ]

    return render(
        request, 'search.html', {
            'digital_files': es_response.hits,
            'aggs': es_response.aggregations,
            'filters': filters,
            'table_headers': table_headers,
            'sort_option': sort_option,
            'sort_dir': sort_dir,
            'page': page,
            'statuses': DIP.import_statuses(),
        })
Beispiel #9
0
def dip(request, pk):
    dip = get_object_or_404(DIP, pk=pk)

    # Redirect to the collection or home page if the DIP is not visible
    if not dip.is_visible_by_user(request.user):
        if dip.collection:
            return redirect("collection", pk=dip.collection.pk)
        else:
            return redirect("home")

    # Show notification to user about import error
    if dip.import_status == DIP.IMPORT_FAILURE:
        messages.error(request, dip.get_import_error_message())

    # Sort options
    sort_options = {
        "path": "filepath.raw",
        "format": "fileformat.raw",
        "size": "size_bytes",
        "date": "datemodified",
    }
    sort_option, sort_dir = get_sort_params(request.GET, sort_options, "path")
    sort_field = sort_options.get(sort_option)

    # Search
    search = DigitalFile.es_doc.search().query("match", **{"dip.id": pk})
    fields = ["filepath", "fileformat"]
    search = add_query_to_search(search, request.GET.get("query", ""), fields)
    search = search.sort({sort_field: {"order": sort_dir}})

    # Aggregations and filters
    search = add_digital_file_aggs(search, collections=False)
    filters, valid_filters = _get_and_validate_digital_file_filters(request)
    search = add_digital_file_filters(search, valid_filters)

    # Pagination
    page = get_page_from_search(search, request.GET)
    es_response = page.object_list.execute()

    table_headers = [
        {
            "label": _("Filepath"),
            "sort_param": "path"
        },
        {
            "label": _("Format"),
            "sort_param": "format"
        },
        {
            "label": _("Size"),
            "sort_param": "size"
        },
        {
            "label": _("Last modified"),
            "sort_param": "date"
        },
        {
            "label": _("File details")
        },
    ]

    return render(
        request,
        "dip.html",
        {
            "dip": dip,
            "digital_files": es_response.hits,
            "aggs": es_response.aggregations,
            "filters": filters,
            "table_headers": table_headers,
            "sort_option": sort_option,
            "sort_dir": sort_dir,
            "page": page,
            "statuses": DIP.import_statuses(),
        },
    )
Beispiel #10
0
def search(request):
    # Sort options
    sort_options = {
        "path": "filepath.raw",
        "format": "fileformat.raw",
        "size": "size_bytes",
        "date": "datemodified",
    }
    sort_option, sort_dir = get_sort_params(request.GET, sort_options, "path")
    sort_field = sort_options.get(sort_option)

    # Search
    search = DigitalFile.es_doc.search()
    # Exclude DigitalFiles from orphan DIPS and from DIPs with 'PENDING' or 'FAILURE'
    # import status when the user is not an editor or an administrator.
    if not request.user.is_editor():
        search = search.query("exists", field="collection.id").exclude(
            "terms",
            **{"dip.import_status": [DIP.IMPORT_PENDING, DIP.IMPORT_FAILURE]})
    fields = ["filepath", "fileformat", "collection.title"]
    search = add_query_to_search(search, request.GET.get("query", ""), fields)
    search = search.sort({sort_field: {"order": sort_dir}})

    # Aggregations and filters
    search = add_digital_file_aggs(search)
    filters, valid_filters = _get_and_validate_digital_file_filters(request)
    search = add_digital_file_filters(search, valid_filters)

    # Pagination
    page = get_page_from_search(search, request.GET)
    es_response = page.object_list.execute()

    table_headers = [
        {
            "label": _("Filepath"),
            "sort_param": "path"
        },
        {
            "label": _("Format"),
            "sort_param": "format"
        },
        {
            "label": _("Size"),
            "sort_param": "size"
        },
        {
            "label": _("Last modified"),
            "sort_param": "date"
        },
        {
            "label": _("Collection name")
        },
        {
            "label": _("File details")
        },
    ]

    return render(
        request,
        "search.html",
        {
            "digital_files": es_response.hits,
            "aggs": es_response.aggregations,
            "filters": filters,
            "table_headers": table_headers,
            "sort_option": sort_option,
            "sort_dir": sort_dir,
            "page": page,
            "statuses": DIP.import_statuses(),
        },
    )
Beispiel #11
0
 def test_add_query_to_search_whitespace_query(self):
     modified_search = add_query_to_search(self.search, "      ",
                                           self.query_fields)
     self.assertTrue("query" not in modified_search.to_dict().keys())