Beispiel #1
0
def view_dataset_by_version(request, dataset_version_id):
    """Dataset view test.  Given dataset version id, render HTML"""

    try:
        dsv = DatasetVersion.objects.select_related('dataset')\
            .get(pk=dataset_version_id,
                versionstate=VERSION_STATE_RELEASED)
    except DatasetVersion.DoesNotExist:
        raise Http404

    dataset_dict = DatasetSerializer(dsv).as_json()
    citation_block=dataset_dict.get('metadata_blocks', {}).get('citation')

    dataverse_id = dataset_dict['ownerInfo']['id']
    try:
        dataverse = Dataverse.objects.select_related('dvobject')\
                .get(dvobject__id=dataverse_id)
    except Dataverse.DoesNotExist:
        raise Http404('No Dataverse with id: %s' % dataverse_id)

    dataverse_dict = DataverseSerializer(dataverse).as_json()

    metadata_blocks = dataset_dict.get('metadata_blocks', None)
    metadata_blocks_as_json = json.dumps(metadata_blocks, indent=4)

    lu = dict(ds=dataset_dict,
            dv=dataverse_dict,
            citation_block=citation_block,
            metadata_blocks_as_json=metadata_blocks_as_json)

    return render(request, 'dvobject_api/dataset_view.html', lu)
def view_dataset_by_version(request, dataset_version_id):
    """Dataset view test.  Given dataset version id, render HTML"""

    try:
        dsv = DatasetVersion.objects.select_related('dataset')\
            .get(pk=dataset_version_id,
                versionstate=VERSION_STATE_RELEASED)
    except DatasetVersion.DoesNotExist:
        raise Http404

    dataset_dict = DatasetSerializer(dsv).as_json()
    citation_block = dataset_dict.get('metadata_blocks', {}).get('citation')

    dataverse_id = dataset_dict['ownerInfo']['id']
    try:
        dataverse = Dataverse.objects.select_related('dvobject')\
                .get(dvobject__id=dataverse_id)
    except Dataverse.DoesNotExist:
        raise Http404('No Dataverse with id: %s' % dataverse_id)

    dataverse_dict = DataverseSerializer(dataverse).as_json()

    metadata_blocks = dataset_dict.get('metadata_blocks', None)
    metadata_blocks_as_json = json.dumps(metadata_blocks, indent=4)

    lu = dict(ds=dataset_dict,
              dv=dataverse_dict,
              citation_block=citation_block,
              metadata_blocks_as_json=metadata_blocks_as_json)

    return render(request, 'dvobject_api/dataset_view.html', lu)
def view_get_slack_dataset_info(dataset_id, **kwargs):

    citation_only = kwargs.get('citation', False)

    dsv = get_latest_dataset_version(dataset_id)
    if dsv is None:
        return "Sorry, no Dataset found for id: %s" % dataset_id

    dataset_dict = DatasetSerializer(dsv).as_json()

    ref_url = '%s/dataset.xhtml?id=%s' % (\
                    settings.DATAVERSE_INSTALLATION_URL,
                    dataset_id)

    if citation_only:
        citation_block = dataset_dict.get('metadata_blocks',
                                          {}).get('citation')

        return """%s\n\nreference: %s""" %\
            (json.dumps(citation_block, indent=4), ref_url)



    return """%s\n\nreference: %s""" %\
        (json.dumps(dataset_dict, indent=4), ref_url)
Beispiel #4
0
def view_differences(request, dataset_id):
    """View differences between two datasets"""
    try:
        dataset = Dataset.objects.get(pk=dataset_id)
    except Dataset.DoesNotExist:
        raise Http404('dataset_id not found')


    dataset_versions = DatasetVersion.objects.select_related('dataset'\
                    ).filter(dataset=dataset)

    lookup = dict(dataset=dataset,\
                 dataset_versions=dataset_versions)

    if dataset_versions.count() < 2:
        return HttpResponse("Only one version of this dataset")

    # -----------------------------
    # Retrieve two dataset versions and create JSON docs
    # -----------------------------
    latest_version = dataset_versions[0]
    previous_version = dataset_versions[6]

    latest_json = DatasetSerializer(latest_version).as_json()
    latest_json_string = json.dumps(latest_json, indent=4)

    previous_json = DatasetSerializer(previous_version).as_json()
    previous_json_string = json.dumps(previous_json, indent=4)
    #return HttpResponse('ok')

    # -----------------------------
    # compare JSON docs
    # -----------------------------
    ds_diffs = DatasetDifferences(latest_json, previous_json)
    ds_diffs.run_comparison()
    ds_diffs.show_diffs()

    # -----------------------------
    # format attributes for template
    # -----------------------------
    lookup = dict(\
                dataset=dataset,
                latest_json=latest_json_string,
                previous_json=previous_json_string,
                diff_list=ds_diffs.get_diffferences())

    return render(request, 'view_differences.html', lookup)
Beispiel #5
0
def view_get_slack_dataset_info(dataset_id, **kwargs):

    citation_only = kwargs.get('citation', False)

    dsv = get_latest_dataset_version(dataset_id)
    if dsv is None:
        return "Sorry, no Dataset found for id: %s" % dataset_id

    dataset_dict = DatasetSerializer(dsv).as_json()

    ref_url = '%s/dataset.xhtml?id=%s' % (\
                    settings.DATAVERSE_INSTALLATION_URL,
                    dataset_id)

    if citation_only:
        citation_block = dataset_dict.get('metadata_blocks', {}).get('citation')

        return """%s\n\nreference: %s""" %\
            (json.dumps(citation_block, indent=4), ref_url)



    return """%s\n\nreference: %s""" %\
        (json.dumps(dataset_dict, indent=4), ref_url)
Beispiel #6
0
    def get_stats_result(self, request):
        """Return the StatsResult object for this statistic"""

        dv_id = self.kwargs.get('ds_id', None)
        if dv_id is None:
            return StatsResult.build_error_result("No Dataset id specified",
                                                  400)

        # Get the latest version
        dataset_version = get_latest_dataset_version(dv_id)

        if dataset_version is None:
            return StatsResult.build_error_result(
                'No published Dataset with id: %s' % dv_id, 404)

        dataset_as_json = DatasetSerializer(dataset_version).as_json()

        return StatsResult.build_success_result(dataset_as_json)
Beispiel #7
0
    def get_stats_result(self, request):
        """Return the StatsResult object for this statistic"""
        persistent_id = request.GET.get('persistentId', None)
        if persistent_id is None:
            return StatsResult.build_error_result(
                "No Dataset persistent id specified", 400)

        ds = Dataset.get_dataset_by_persistent_id(persistent_id)

        err_404 = 'No published dataset found for persistentId: %s' % persistent_id

        if ds is None or not ds.dvobject.publicationdate:
            return StatsResult.build_error_result(err_404, 404)

        # Get the latest version
        dataset_version = get_latest_dataset_version(ds.dvobject.id)

        if dataset_version is None:
            return StatsResult.build_error_result(err_404, 404)

        dataset_as_json = DatasetSerializer(dataset_version).as_json()

        return StatsResult.build_success_result(dataset_as_json)
    def make_json_files(self):

        # Set publication status
        #
        filters = {}
        if self.published_only:
            filters.update(query_helper.get_is_published_filter_param())

        # Query for dataset ids
        #
        ds_id_query = Dataset.objects.filter(**filters\
                            ).annotate(ds_id=F('dvobject__id')\
                            ).values_list('ds_id', flat=True\
                            ).order_by('ds_id')

        # Iterate through dataset ids
        #
        #start_time = datetime.now()
        start_time = int(time.time())  # epoch seconds

        cnt = 0
        no_versions_found_list = [45900]

        for ds_id in ds_id_query:
            cnt += 1
            msgt('(%d) Checking dataset id %s' % (cnt, ds_id))
            if ds_id < self.dataset_start_id:
                msg('skipping...(start at dataset id: %d)' %
                    self.dataset_start_id)
                continue

            # Create file name
            #
            fname = 'ds_%s.json' % (str(ds_id).zfill(8))
            full_fname = join(OUTPUT_DIR, fname)

            # Should we overwrite the existing file?
            #
            if isfile(full_fname) and not self.overwrite_existing_files:
                msg('skipping...file already exists')
                continue

            dataset_version = get_latest_dataset_version(ds_id)

            if dataset_version is None:
                msg("Could not find dataset_version!")
                no_versions_found_list.append(ds_id)
                continue

            dataset_as_json = DatasetSerializer(dataset_version).as_json()

            open(full_fname, 'w').write(json.dumps(dataset_as_json, indent=4))
            msg('File written: %s' % full_fname)

            if cnt % 500 == 0:
                self.show_elapsed_time(start_time)
            #if cnt > 10:
            #    self.show_elapsed_time(start_time)
            #    break

        self.show_elapsed_time(start_time)
        print 'no_versions_found_list: %s' % no_versions_found_list
Beispiel #9
0
def view_side_by_side1(request, dataset_id):

    dsv = get_latest_dataset_version(dataset_id)
    if dsv is None:
        raise Http404("No published datasets for id: %s" % dataset_id)

    template_basename = request.GET.get(PARAM_TEMPLATE_NAME,
                                        'title_citation.html')
    template_fname = path.join('dvobject_api', template_basename)

    try:
        template = get_template(template_fname)
    except TemplateDoesNotExist:
        raise Http404('Template does not exist: %s' % template_fname)

    #formatter = HtmlFormatter(linenos=True, cssclass="friendly")
    formatter = HtmlFormatter(linenos=False, cssclass="friendly")

    # -------------------------------
    # Format raw template
    # -------------------------------
    template_html = open(template.origin.name, 'r').read()
    django_lexer = get_lexer_by_name("django", stripall=True)
    template_snippet = highlight(template_html, django_lexer, formatter)
    template_name = path.basename(template.origin.name)

    # -------------------------------
    # Format Dataset JSON
    # -------------------------------
    dataset_dict = DatasetSerializer(dsv).as_json()

    citation_block = dataset_dict.get('metadata_blocks', {}).get('citation')
    #citation_dict = OrderedDict({'citation_block': citation_block})
    json_string = json.dumps(dataset_dict, indent=4)

    json_lexer = get_lexer_by_name("json", stripall=True)
    dataset_json_snippet = highlight(json_string, json_lexer, formatter)

    # -------------------------------
    # Format Dataverse JSON
    # -------------------------------
    dataverse_id = dataset_dict['ownerInfo']['id']
    try:
        dataverse = Dataverse.objects.select_related('dvobject')\
                    .get(dvobject__id=dataverse_id)
    except Dataverse.DoesNotExist:
        raise Http404('No Dataverse with id: %s' % dataverse_id)

    dataverse_dict = DataverseSerializer(dataverse).as_json()
    #dataset_json_snippet = highlight(json_string, json_lexer, formatter)

    # -------------------------------
    # Overall pygment css
    # -------------------------------
    pygment_css = formatter.get_style_defs('.friendly')

    # -------------------------------
    # API endpoint
    # -------------------------------
    swagger_base_url = '%s://%s' % (settings.SWAGGER_SCHEME,
                                    settings.SWAGGER_HOST)

    lu = dict(
        template_snippet=template_snippet,
        template_name=template_name,
        dataset_json_snippet=dataset_json_snippet,
        pygment_css=pygment_css,
        swagger_base_url=swagger_base_url,
        dataset_id=dataset_id,
        ds=dataset_dict,  # for rendering chosen template
        citation_block=citation_block,  # for rendering chosen template
    )

    lu['rendered_template'] = render_to_string(template_fname, lu)

    return render(request, 'composable/side_by_side.html', lu)