Example #1
0
def alignment_view(request, mission, file_type):
    """ Django view function to perform the alignment check.
    """
    collection = models.Collection.objects.get(
        mission=mission, file_type=file_type
    )

    config = collection.configuration

    records = None
    locations = None
    locations_with_no_checksum = None
    frmt = "html"
    if request.method == "POST":
        form = forms.AlignmentForm(
            collection.locations.order_by("pk"),
            config.available_alignment_fields or [],
            request.POST
        )
        pagination_form = forms.PaginationForm(request.POST)
        if form.is_valid() and pagination_form.is_valid():
            frmt = form.cleaned_data.pop("format")
            observer = monitor(
                "alignment_check", mission=mission, file_type=file_type,
                **form.cleaned_data
            )
            with observer:
                locations, qs = queries.alignment(collection, form.cleaned_data)
            page = pagination_form.cleaned_data.pop("page")
            per_page = pagination_form.cleaned_data.pop("records_per_page")

            if frmt == "html":
                records = Paginator(qs, per_page).page(page)
            else:
                records = qs

            locations_with_no_checksum = [
                location for location in locations or []
                if collection.get_metadata_field_mapping(location).get("checksum") is None
            ]
    else:
        form = forms.AlignmentForm(
            collection.locations.order_by("pk"),
            config.available_alignment_fields or []
        )
        pagination_form = forms.PaginationForm(
            initial={'page': '1', 'records_per_page': '15'}
        )

    if frmt == "html":
        return render(
            request, "inventory/collection/alignment.html", {
                "collections": models.Collection.objects.all(),
                "collection": collection, "alignment_form": form,
                "pagination_form": pagination_form, "records": records,
                "locations": locations,
                "locations_with_no_checksum": locations_with_no_checksum
            }
        )
    elif frmt in ("csv", "tsv"):
        f = tempfile.SpooledTemporaryFile()
        writer = csv.writer(f, delimiter="," if frmt == "csv" else "\t")

        header = ["filename"]
        for location in locations:
            header.extend([
                "checksum %s" % location.url,
                "prevalence %s" % location.url
            ])
        header.append("annotations")
        writer.writerow(header)

        for row in qs:
            writer.writerow(
                [row["filename"]] +
                [item for incidence in row["incidences"] for item in incidence] +
                list(row["annotations"])
            )

        size = f.tell()
        f.seek(0)

        response = StreamingHttpResponse(f, content_type="text/csv")
        response["Content-Length"] = str(size)
        response["Content-Disposition"] = (
            'inline; filename="alignment-%s.csv"' % (
                now().replace(microsecond=0, tzinfo=None).isoformat("T")
            )
        )
        return response
Example #2
0
def alignment_view(request, mission, file_type):
    """ Django view function to perform the alignment check.
    """
    collection = models.Collection.objects.get(mission=mission,
                                               file_type=file_type)

    config = collection.configuration

    records = None
    locations = None
    locations_with_no_checksum = None
    frmt = "html"
    if request.method == "POST":
        form = forms.AlignmentForm(collection.locations.order_by("pk"),
                                   config.available_alignment_fields or [],
                                   request.POST)
        pagination_form = forms.PaginationForm(request.POST)
        if form.is_valid() and pagination_form.is_valid():
            frmt = form.cleaned_data.pop("format")
            observer = monitor("alignment_check",
                               mission=mission,
                               file_type=file_type,
                               **form.cleaned_data)
            with observer:
                locations, qs = queries.alignment(collection,
                                                  form.cleaned_data)
            page = pagination_form.cleaned_data.pop("page")
            per_page = pagination_form.cleaned_data.pop("records_per_page")

            if frmt == "html":
                records = Paginator(qs, per_page).page(page)
            else:
                records = qs

            locations_with_no_checksum = [
                location for location in locations or []
                if collection.get_metadata_field_mapping(location).get(
                    "checksum") is None
            ]
    else:
        form = forms.AlignmentForm(collection.locations.order_by("pk"),
                                   config.available_alignment_fields or [])
        pagination_form = forms.PaginationForm(initial={
            'page': '1',
            'records_per_page': '15'
        })

    if frmt == "html":
        return render(
            request, "inventory/collection/alignment.html", {
                "collections": models.Collection.objects.all(),
                "collection": collection,
                "alignment_form": form,
                "pagination_form": pagination_form,
                "records": records,
                "locations": locations,
                "locations_with_no_checksum": locations_with_no_checksum
            })
    elif frmt in ("csv", "tsv"):
        f = tempfile.SpooledTemporaryFile()
        writer = csv.writer(f, delimiter="," if frmt == "csv" else "\t")

        header = ["filename"]
        for location in locations:
            header.extend(
                ["checksum %s" % location.url,
                 "prevalence %s" % location.url])
        header.append("annotations")
        writer.writerow(header)

        for row in qs:
            writer.writerow([row["filename"]] + [
                item for incidence in row["incidences"] for item in incidence
            ] + list(row["annotations"]))

        size = f.tell()
        f.seek(0)

        response = StreamingHttpResponse(f, content_type="text/csv")
        response["Content-Length"] = str(size)
        response["Content-Disposition"] = (
            'inline; filename="alignment-%s.csv"' %
            (now().replace(microsecond=0, tzinfo=None).isoformat("T")))
        return response
Example #3
0
 def test_simple(self):
     results = queries.alignment(
         models.Collection.objects.get(mission="Landsat5",
                                       file_type="SIP-SCENE"))
     print results