Skip to content

Commit

Permalink
WIP first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Manoj-nathwani committed May 6, 2021
1 parent 6517299 commit 1e8761e
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 128 deletions.
26 changes: 26 additions & 0 deletions ckanext/scheming/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ def get_missing_resources(pkg, schema):
return ret


@helper
def get_core_resources(pkg):
"""
Using the dataset, return a list of it's resources which have
a resource_type set
"""
return [
resource
for resource in pkg.get('resources', [])
if resource['resource_type']
]


@helper
def get_extra_resources(pkg):
"""
Using the dataset, return a list of it's resources which do not
have a resource_type set
"""
return [
resource
for resource in pkg.get('resources', [])
if not resource['resource_type']
]


@helper
def scheming_language_text(text, prefer_lang=None):
"""
Expand Down
2 changes: 2 additions & 0 deletions ckanext/scheming/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def get_helpers(self):
'scheming_display_json_value': helpers.scheming_display_json_value,
'scheming_non_empty_fields': helpers.scheming_non_empty_fields,
'scheming_natural_sort': helpers.scheming_natural_sort,
'get_core_resources': helpers.get_core_resources,
'get_extra_resources': helpers.get_extra_resources,

'get_missing_resources': unaids_helpers.get_missing_resources,
'get_user': unaids_helpers.get_user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,36 @@
{% snippet "package/snippets/resources_list.html", pkg=pkg, resources=pkg.resources %}

#}
<section id="dataset-resources" class="resources">
<h3>{{ _('Data and Resources') }}</h3>
{% block resource_list %}
{% if resources %}
<ul class="{% block resource_list_class %}resource-list{% endblock %}">
{% block resource_list_inner %}
{% set can_edit = h.check_access('package_update', {'id':pkg.id }) and not is_activity_archive %}
{% for resource in resources %}
{% snippet 'package/snippets/resource_item.html', pkg=pkg, res=resource, can_edit=can_edit, is_activity_archive=is_activity_archive %}
{% endfor %}
{% endblock %}
</ul>

{% set core_resources = h.get_core_resources(pkg) %}
{% set can_edit = h.check_access('package_update', {'id':pkg.id }) and not is_activity_archive %}
<section id="dataset-resources" class="resources core-resources">
<h3>{{ _('Core Resources') }}</h3>
{% if core_resources %}
<ul class="resource-list">
{% for resource in core_resources %}
{% snippet 'package/snippets/resource_item.html', pkg=pkg, res=resource, can_edit=can_edit, is_activity_archive=is_activity_archive %}
{% endfor %}
</ul>
{% else %}
{% if h.check_access('resource_create', {'package_id': pkg['id']}) and not is_activity_archive %}
{% trans url=h.url_for(pkg.type ~ '_resource.new', id=pkg.name) %}
<p class="empty">This dataset has no core resources, <a href="{{ url }}">why not add some?</a></p>
{% endtrans %}
{% else %}
{% if h.check_access('resource_create', {'package_id': pkg['id']}) and not is_activity_archive %}
{% trans url=h.url_for(pkg.type ~ '_resource.new', id=pkg.name) %}
<p class="empty">This dataset has no data, <a href="{{ url }}">why not add some?</a></p>
{% endtrans %}
{% else %}
<p class="empty">{{ _('This dataset has no data') }}</p>
{% endif %}
<p class="empty">{{ _('This dataset has no core resources') }}</p>
{% endif %}
{% endblock %}
{% endif %}
</section>


{% block other_resources_list %}
{% set can_edit = h.check_access('package_update', {'id':pkg.id }) %}
{% if can_edit %}
{% set missing = h.get_missing_resources(pkg, schema) %}
{% if missing %}
<section id="dataset-resources" class="resources">
<section id="dataset-resources" class="resources missing-resources">
<h3>{{ _('Missing Resources') }}</h3>
<ul class="{% block missing_resource_list_class %}resource-list{% endblock %}">

{% for resource in missing %}
{% snippet 'scheming/package/snippets/missing_resource_item.html', pkg=pkg, res=resource, can_edit=can_edit %}
{% endfor %}
Expand All @@ -51,3 +47,15 @@ <h3>{{ _('Missing Resources') }}</h3>
{% endif %}
{% endif %}
{% endblock %}

{% set extra_resources = h.get_extra_resources(pkg) %}
{% if extra_resources %}
<section id="dataset-resources" class="resources extra-resources">
<h3>{{ _('Extra Resources') }}</h3>
<ul class="resource-list">
{% for resource in extra_resources %}
{% snippet 'package/snippets/resource_item.html', pkg=pkg, res=resource, can_edit=can_edit, is_activity_archive=is_activity_archive %}
{% endfor %}
</ul>
</section>
{% endif %}
232 changes: 127 additions & 105 deletions ckanext/scheming/tests/test_dataset_display.py
Original file line number Diff line number Diff line change
@@ -1,117 +1,139 @@
import pytest
import six
from ckantoolkit.tests.factories import Sysadmin, Dataset
from ckantoolkit.tests.factories import Sysadmin, Dataset, Resource
from bs4 import BeautifulSoup


@pytest.mark.usefixtures("clean_db")
class TestDatasetDisplay(object):
def test_dataset_displays_custom_fields(self, app):
user = Sysadmin()
Dataset(
user=user,
type="test-schema",
name="set-one",
humps=3,
resources=[
{"url": "http://example.com/camel.txt", "camels_in_photo": 2}
],
)

response = app.get("/dataset/set-one")
assert "Humps" in response.body

def test_resource_displays_custom_fields(self, app):
user = Sysadmin()
d = Dataset(
user=user,
type="test-schema",
name="set-two",
humps=3,
def test_dataset_view_core_resources(self, app):
dataset = Dataset(
type='test-schema',
name='test',
resources=[
{
"url": "http://example.com/camel.txt",
"camels_in_photo": 2,
"date": "2015-01-01",
'url': 'http://example.com/camel.txt',
'resource_type': 'inputs-unaids-geographic'
}
],
)

response = app.get(
"/dataset/set-two/resource/" + d["resources"][0]["id"]
)
assert "Camels in Photo" in response.body
assert "Date" in response.body

def test_choice_field_shows_labels(self, app):
user = Sysadmin()
Dataset(
user=user,
type="test-schema",
name="with-choice",
category="hybrid",
)
response = app.get("/dataset/with-choice")
assert "Hybrid Camel" in response.body

def test_notes_field_displayed(self, app):
user = Sysadmin()
Dataset(
user=user,
type="dataset",
name="plain-jane",
notes="# styled notes",
)

response = app.get("/dataset/plain-jane")
assert "<h1>styled notes" in response.body

def test_choice_field_shows_list_if_multiple_options(self, app):
user = Sysadmin()
Dataset(
user=user,
type="test-schema",
name="with-multiple-choice-n",
personality=["friendly", "spits"],
)

response = app.get("/dataset/with-multiple-choice-n")

assert (
"<ul><li>Often friendly</li><li>Tends to spit</li></ul>"
in response.body
)

def test_choice_field_does_not_show_list_if_one_options(self, app):
user = Sysadmin()
Dataset(
user=user,
type="test-schema",
name="with-multiple-choice-one",
personality=["friendly"],
)

response = app.get("/dataset/with-multiple-choice-one")

assert "Often friendly" in response.body
assert "<ul><li>Often friendly</li></ul>" not in response.body

def test_json_field_displayed(self, app):
user = Sysadmin()
Dataset(
user=user,
type="test-schema",
name="plain-json",
a_json_field={"a": "1", "b": "2"},
Resource(
package_id=dataset['id'],
resource_type='inputs-unaids-geographic'
)
response = app.get("/dataset/plain-json")

if six.PY3:
expected = """{\n "a": "1",\n "b": "2"\n}"""
else:
expected = """{\n "a": "1", \n "b": "2"\n}"""
expected = expected.replace(
'"', "&#34;"
) # Ask webhelpers

assert expected in response.body
assert "Example JSON" in response.body
response = app.get('/{}/{}'.format(dataset['type'], dataset['name']))
assert 'Geographic Data' in response.body
core_resources_container = BeautifulSoup(response.body)\
.select('.core-resources')[0]
assert dataset['name'] in core_resources_container

# def test_dataset_displays_custom_fields(self, app):
# user = Sysadmin()
# Dataset(
# user=user,
# type="test-schema",
# name="set-one",
# humps=3,
# resources=[
# {"url": "http://example.com/camel.txt", "camels_in_photo": 2}
# ],
# )

# response = app.get("/dataset/set-one")
# assert "Humps" in response.body

# def test_resource_displays_custom_fields(self, app):
# user = Sysadmin()
# d = Dataset(
# user=user,
# type="test-schema",
# name="set-two",
# humps=3,
# resources=[
# {
# "url": "http://example.com/camel.txt",
# "camels_in_photo": 2,
# "date": "2015-01-01",
# }
# ],
# )

# response = app.get(
# "/dataset/set-two/resource/" + d["resources"][0]["id"]
# )
# assert "Camels in Photo" in response.body
# assert "Date" in response.body

# def test_choice_field_shows_labels(self, app):
# user = Sysadmin()
# Dataset(
# user=user,
# type="test-schema",
# name="with-choice",
# category="hybrid",
# )
# response = app.get("/dataset/with-choice")
# assert "Hybrid Camel" in response.body

# def test_notes_field_displayed(self, app):
# user = Sysadmin()
# Dataset(
# user=user,
# type="dataset",
# name="plain-jane",
# notes="# styled notes",
# )

# response = app.get("/dataset/plain-jane")
# assert "<h1>styled notes" in response.body

# def test_choice_field_shows_list_if_multiple_options(self, app):
# user = Sysadmin()
# Dataset(
# user=user,
# type="test-schema",
# name="with-multiple-choice-n",
# personality=["friendly", "spits"],
# )

# response = app.get("/dataset/with-multiple-choice-n")

# assert (
# "<ul><li>Often friendly</li><li>Tends to spit</li></ul>"
# in response.body
# )

# def test_choice_field_does_not_show_list_if_one_options(self, app):
# user = Sysadmin()
# Dataset(
# user=user,
# type="test-schema",
# name="with-multiple-choice-one",
# personality=["friendly"],
# )

# response = app.get("/dataset/with-multiple-choice-one")

# assert "Often friendly" in response.body
# assert "<ul><li>Often friendly</li></ul>" not in response.body

# def test_json_field_displayed(self, app):
# user = Sysadmin()
# Dataset(
# user=user,
# type="test-schema",
# name="plain-json",
# a_json_field={"a": "1", "b": "2"},
# )
# response = app.get("/dataset/plain-json")

# if six.PY3:
# expected = """{\n "a": "1",\n "b": "2"\n}"""
# else:
# expected = """{\n "a": "1", \n "b": "2"\n}"""
# expected = expected.replace(
# '"', "&#34;"
# ) # Ask webhelpers

# assert expected in response.body
# assert "Example JSON" in response.body

0 comments on commit 1e8761e

Please sign in to comment.