Skip to content

Commit 2c93b72

Browse files
authored
Merge pull request #8 from hydroshare:include-published
include published resources in batch update
2 parents 5ceef52 + 3371cd5 commit 2c93b72

3 files changed

Lines changed: 35 additions & 70 deletions

File tree

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import requests
2-
import json
31
from django.core.management.base import BaseCommand
4-
from hs_data_services import settings
52
from hs_data_services_sync import tasks
3+
from hs_data_services_sync.utilities import get_list_of_public_geo_resources
64

75

86
class Command(BaseCommand):
@@ -19,7 +17,7 @@ def handle(self, *args, **options):
1917
result = self.copy_files(resource_id)
2018
print(result)
2119
else:
22-
resources = self.get_list_of_public_geo_resources()
20+
resources = get_list_of_public_geo_resources()
2321
num_resources = len(resources)
2422
print(f"Copying files from {num_resources} resources")
2523
counter = 1
@@ -30,36 +28,5 @@ def handle(self, *args, **options):
3028
counter += 1
3129
print("Done copying files")
3230

33-
def get_list_of_public_geo_resources(self):
34-
hydroshare_url = "/".join(settings.HYDROSHARE_URL.split("/")[:-1])
35-
types = ["Geographic Feature (ESRI Shapefiles)", "Geographic Raster"]
36-
# replace spaces with + for the query string
37-
types = [t.replace(" ", "+") for t in types]
38-
params = {
39-
"type": types,
40-
"availability": ["public"],
41-
"geofilter": "false"
42-
}
43-
rest_url = f"{hydroshare_url}/discoverapi/?filter={json.dumps(params)}"
44-
rest_url = rest_url.replace(" ", "")
45-
print(f"Getting list of public geospatial resources from: {rest_url}")
46-
response = requests.get(rest_url)
47-
response_json = response.json()
48-
page_count = response_json.get('pagecount', 0)
49-
rescount = response_json.get('rescount', 0)
50-
perpage = response_json.get('perpage', 0)
51-
res_ids = []
52-
print(f"Iterating over {page_count} pages of {perpage} resources each, total resources: {rescount}")
53-
for i in range(1, page_count + 1):
54-
print(f"Getting page {i}")
55-
response = requests.get(f"{rest_url}&pnum={i}")
56-
response_json = response.json()
57-
resources = json.loads(response_json.get('resources', []))
58-
new_ids = [resource["short_id"] for resource in resources if resource.get('short_id', None)]
59-
res_ids.extend(new_ids)
60-
print(f"Found {len(res_ids)} public geospatial resources")
61-
print(f"Should match {rescount}")
62-
return res_ids
63-
6431
def copy_files(self, resource_id):
6532
return tasks.update_data_services_task.delay(resource_id)
Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import requests
2-
import json
31
from django.core.management.base import BaseCommand
4-
from hs_data_services import settings
52
from hs_data_services_sync import utilities
63

74

@@ -19,7 +16,7 @@ def handle(self, *args, **options):
1916
result = utilities.unregister_geoserver_databases(resource_id)
2017
print(result)
2118
else:
22-
resources = self.get_list_of_public_geo_resources()
19+
resources = utilities.get_list_of_public_geo_resources()
2320
num_resources = len(resources)
2421
print(f"Unregistering {num_resources} public resources")
2522
counter = 1
@@ -29,34 +26,3 @@ def handle(self, *args, **options):
2926
print(result)
3027
counter += 1
3128
print("Done unregistering resources")
32-
33-
def get_list_of_public_geo_resources(self):
34-
hydroshare_url = "/".join(settings.HYDROSHARE_URL.split("/")[:-1])
35-
types = ["Geographic Feature (ESRI Shapefiles)", "Geographic Raster"]
36-
# replace spaces with + for the query string
37-
types = [t.replace(" ", "+") for t in types]
38-
params = {
39-
"type": types,
40-
"availability": ["public"],
41-
"geofilter": "false"
42-
}
43-
rest_url = f"{hydroshare_url}/discoverapi/?filter={json.dumps(params)}"
44-
rest_url = rest_url.replace(" ", "")
45-
print(f"Getting list of public geospatial resources from: {rest_url}")
46-
response = requests.get(rest_url)
47-
response_json = response.json()
48-
page_count = response_json.get('pagecount', 0)
49-
rescount = response_json.get('rescount', 0)
50-
perpage = response_json.get('perpage', 0)
51-
res_ids = []
52-
print(f"Iterating over {page_count} pages of {perpage} resources each, total resources: {rescount}")
53-
for i in range(1, page_count + 1):
54-
print(f"Getting page {i}")
55-
response = requests.get(f"{rest_url}&pnum={i}")
56-
response_json = response.json()
57-
resources = json.loads(response_json.get('resources', []))
58-
new_ids = [resource["short_id"] for resource in resources if resource.get('short_id', None)]
59-
res_ids.extend(new_ids)
60-
print(f"Found {len(res_ids)} public geospatial resources")
61-
print(f"Should match {rescount}")
62-
return res_ids

hs_data_services/hs_data_services_sync/utilities.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,35 @@ def get_layer_style(max_value, min_value, ndv_value, layer_id):
601601
</StyledLayerDescriptor>"""
602602

603603
return layer_style
604+
605+
606+
def get_list_of_public_geo_resources():
607+
hydroshare_url = "/".join(settings.HYDROSHARE_URL.split("/")[:-1])
608+
types = ["Geographic Feature (ESRI Shapefiles)", "Geographic Raster"]
609+
# replace spaces with + for the query string
610+
types = [t.replace(" ", "+") for t in types]
611+
params = {
612+
"type": types,
613+
"availability": ["public", "published"],
614+
"geofilter": "false"
615+
}
616+
rest_url = f"{hydroshare_url}/discoverapi/?filter={json.dumps(params)}"
617+
rest_url = rest_url.replace(" ", "")
618+
print(f"Getting list of public geospatial resources from: {rest_url}")
619+
response = requests.get(rest_url)
620+
response_json = response.json()
621+
page_count = response_json.get('pagecount', 0)
622+
rescount = response_json.get('rescount', 0)
623+
perpage = response_json.get('perpage', 0)
624+
res_ids = []
625+
print(f"Iterating over {page_count} pages of {perpage} resources each, total resources: {rescount}")
626+
for i in range(1, page_count + 1):
627+
print(f"Getting page {i}")
628+
response = requests.get(f"{rest_url}&pnum={i}")
629+
response_json = response.json()
630+
resources = json.loads(response_json.get('resources', []))
631+
new_ids = [resource["short_id"] for resource in resources if resource.get('short_id', None)]
632+
res_ids.extend(new_ids)
633+
print(f"Found {len(res_ids)} public geospatial resources")
634+
print(f"Should match {rescount}")
635+
return res_ids

0 commit comments

Comments
 (0)