Skip to content

Commit

Permalink
Merge pull request #2047 from WikiWatershed/tt/bigcz-refactor-backend…
Browse files Browse the repository at this point in the history
…-support-catalog-fields

BiG-CZ Refactor Back-end

Connects #2036
  • Loading branch information
rajadain committed Jul 17, 2017
2 parents ecc3cbe + c5ebd76 commit 981f3f1
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 31 deletions.
23 changes: 17 additions & 6 deletions src/mmw/apps/bigcz/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@
from __future__ import unicode_literals
from __future__ import division

from apps.bigcz.clients import hydroshare, cuahsi, cinergi
from apps.bigcz.clients import cinergi, hydroshare, cuahsi


SEARCH_FUNCTIONS = {
hydroshare.CATALOG_NAME: hydroshare.search,
cuahsi.CATALOG_NAME: cuahsi.search,
cinergi.CATALOG_NAME: cinergi.search,
CATALOGS = {
cinergi.CATALOG_NAME: {
'model': cinergi.model,
'serializer': cinergi.serializer,
'search': cinergi.search,
},
hydroshare.CATALOG_NAME: {
'model': hydroshare.model,
'serializer': hydroshare.serializer,
'search': hydroshare.search,
},
cuahsi.CATALOG_NAME: {
'model': cuahsi.model,
'serializer': cuahsi.serializer,
'search': cuahsi.search,
},
}
13 changes: 13 additions & 0 deletions src/mmw/apps/bigcz/clients/cinergi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from apps.bigcz.models import Resource
from apps.bigcz.serializers import ResourceSerializer

# Import catalog name and search function, so it can be exported from here
from apps.bigcz.clients.cinergi.search import CATALOG_NAME, search # NOQA

model = Resource
serializer = ResourceSerializer
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


CATALOG_NAME = 'cinergi'
GEOPORTAL_URL = 'http://132.249.238.169:8080/geoportal/opensearch'
CATALOG_URL = 'http://132.249.238.169:8080/geoportal/opensearch'


def parse_date(value):
Expand Down Expand Up @@ -132,7 +132,7 @@ def search(**kwargs):
})

try:
response = requests.get(GEOPORTAL_URL,
response = requests.get(CATALOG_URL,
timeout=settings.BIGCZ_CLIENT_TIMEOUT,
params=params)
except requests.Timeout:
Expand Down
13 changes: 13 additions & 0 deletions src/mmw/apps/bigcz/clients/cuahsi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from apps.bigcz.clients.cuahsi.models import CuahsiResource
from apps.bigcz.clients.cuahsi.serializers import CuahsiResourceSerializer

# Import catalog name and search function, so it can be exported from here
from apps.bigcz.clients.cuahsi.search import CATALOG_NAME, search # NOQA

model = CuahsiResource
serializer = CuahsiResourceSerializer
16 changes: 16 additions & 0 deletions src/mmw/apps/bigcz/clients/cuahsi/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from apps.bigcz.models import Resource


class CuahsiResource(Resource):
def __init__(self, id, description, author, links, title,
created_at, updated_at, geom, test_field):
super(CuahsiResource, self).__init__(id, description, author, links,
title, created_at, updated_at,
geom)

self.test_field = test_field
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@

from django.conf import settings

from apps.bigcz.models import Resource, ResourceLink, ResourceList, BBox
from apps.bigcz.models import ResourceLink, ResourceList, BBox
from apps.bigcz.utils import parse_date, RequestTimedOutError

from apps.bigcz.clients.cuahsi.models import CuahsiResource


SQKM_PER_SQM = 0.000001
MAX_AREA_SQKM = 1500
CATALOG_NAME = 'cuahsi'
SOAP_URL = 'http://hiscentral.cuahsi.org/webservices/hiscentral.asmx?WSDL'
CATALOG_URL = 'http://hiscentral.cuahsi.org/webservices/hiscentral.asmx?WSDL'


DATE_MIN = date(1900, 1, 1)
DATE_MAX = date(2100, 1, 1)
DATE_FORMAT = '%m/%d/%Y'


client = Client(SOAP_URL, timeout=settings.BIGCZ_CLIENT_TIMEOUT)
client = Client(CATALOG_URL, timeout=settings.BIGCZ_CLIENT_TIMEOUT)


def parse_geom(record):
Expand Down Expand Up @@ -65,15 +67,16 @@ def parse_record(record, service):
if details_url:
links.append(ResourceLink('details', details_url))

return Resource(
return CuahsiResource(
id=record['location'],
title=record['Sitename'],
description=service['aabstract'],
author=None,
links=links,
created_at=record['beginDate'],
updated_at=None,
geom=geom)
geom=geom,
test_field="hello world")


def find_service(services, service_code):
Expand Down
12 changes: 12 additions & 0 deletions src/mmw/apps/bigcz/clients/cuahsi/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from rest_framework.serializers import CharField

from apps.bigcz.serializers import ResourceSerializer


class CuahsiResourceSerializer(ResourceSerializer):
test_field = CharField()
13 changes: 13 additions & 0 deletions src/mmw/apps/bigcz/clients/hydroshare/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from apps.bigcz.models import Resource
from apps.bigcz.serializers import ResourceSerializer

# Import catalog name and search function, so it can be exported from here
from apps.bigcz.clients.hydroshare.search import CATALOG_NAME, search # NOQA

model = Resource
serializer = ResourceSerializer
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


CATALOG_NAME = 'hydroshare'
HYDROSHARE_URL = 'https://www.hydroshare.org/hsapi/resource/'
CATALOG_URL = 'https://www.hydroshare.org/hsapi/resource/'


def parse_date(value):
Expand Down Expand Up @@ -75,7 +75,7 @@ def search(**kwargs):
params.update(prepare_bbox(bbox))

try:
response = requests.get(HYDROSHARE_URL,
response = requests.get(CATALOG_URL,
timeout=settings.BIGCZ_CLIENT_TIMEOUT,
params=params)
except requests.Timeout:
Expand Down
8 changes: 6 additions & 2 deletions src/mmw/apps/bigcz/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from __future__ import division

from rest_framework.serializers import \
Serializer, CharField, DateTimeField, IntegerField
Serializer, CharField, DateTimeField, IntegerField, SerializerMethodField
from rest_framework_gis.serializers import GeometryField


Expand All @@ -27,5 +27,9 @@ class ResourceSerializer(Serializer):
class ResourceListSerializer(Serializer):
catalog = CharField()
api_url = CharField()
results = ResourceSerializer(many=True)
results = SerializerMethodField()
count = IntegerField()

def get_results(self, obj):
serializer = self.context.get('serializer', ResourceSerializer)
return [serializer(r).data for r in obj.results]
29 changes: 15 additions & 14 deletions src/mmw/apps/bigcz/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from rest_framework.permissions import AllowAny
from rest_framework.response import Response

from apps.bigcz.clients import SEARCH_FUNCTIONS
from apps.bigcz.clients import CATALOGS
from apps.bigcz.serializers import ResourceListSerializer
from apps.bigcz.utils import parse_date

Expand All @@ -20,29 +20,30 @@ def _do_search(request):
raise ValidationError({
'error': 'Required argument: catalog'})

if catalog not in CATALOGS:
raise ValidationError({
'error': 'Catalog must be one of: {}'
.format(', '.join(CATALOGS.keys()))})

search_kwargs = {
'query': request.query_params.get('query'),
'to_date': parse_date(request.query_params.get('to_date')),
'from_date': parse_date(request.query_params.get('from_date')),
'bbox': request.query_params.get('bbox'),
}

search = SEARCH_FUNCTIONS.get(catalog)

if search:
try:
return search(**search_kwargs)
except ValueError as ex:
raise ParseError(ex.message)
search = CATALOGS[catalog]['search']
serializer = CATALOGS[catalog]['serializer']

raise ValidationError({
'error': 'Catalog must be one of: {}'
.format(', '.join(SEARCH_FUNCTIONS.keys()))
})
try:
result = ResourceListSerializer(search(**search_kwargs),
context={'serializer': serializer})
return result.data
except ValueError as ex:
raise ParseError(ex.message)


@decorators.api_view(['GET'])
@decorators.permission_classes((AllowAny,))
def search(request):
result = ResourceListSerializer(_do_search(request))
return Response(result.data)
return Response(_do_search(request))

0 comments on commit 981f3f1

Please sign in to comment.