Skip to content

Commit

Permalink
make pycsw local requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Sep 12, 2012
1 parent f07a1ea commit cedb12a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 37 deletions.
94 changes: 94 additions & 0 deletions geonode/catalogue/backends/pycsw_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
#
#########################################################################

import os
from lxml import etree
from django.conf import settings
from ConfigParser import SafeConfigParser
from owslib.iso import MD_Metadata
from pycsw import server
from geonode.catalogue.backends.generic import CatalogueBackend as GenericCatalogueBackend
from geonode.catalogue.backends.generic import METADATA_FORMATS

MD_CORE_MODEL = {
'typename': 'pycsw:CoreMetadata',
Expand Down Expand Up @@ -94,3 +101,90 @@ def remove_record(self, uuid):
def create_record(self, item):
pass

def get_record(self, uuid):
results = self._csw_local_dispatch(identifier=uuid)
if len(results) < 1:
return None

result = etree.fromstring(results).find('{http://www.isotc211.org/2005/gmd}MD_Metadata')

if result is None:
return None

record = MD_Metadata(result)
record.keywords = []
if hasattr(record, 'identification') and hasattr(record.identification, 'keywords'):
for kw in record.identification.keywords:
record.keywords.extend(kw['keywords'])

record.links = {}
record.links['metadata'] = self.catalogue.urls_for_uuid(uuid)
record.links['download'] = self.catalogue.extract_links(record)
return record

def search_records(self, keywords, start, limit, bbox):
with self.catalogue:
lresults = self._csw_local_dispatch(keywords, keywords, start+1, limit, bbox)
# serialize XML
e = etree.fromstring(lresults)
self.catalogue.records = [MD_Metadata(x) for x in e.findall('//{http://www.isotc211.org/2005/gmd}MD_Metadata')]

# build results into JSON for API
results = [self.catalogue.metadatarecord2dict(doc) for v, doc in self.catalogue.records.iteritems()]

result = {
'rows': results,
'total': e.find('{http://www.opengis.net/cat/csw/2.0.2}SearchResults').attrib.get('numberOfRecordsMatched'),
'next_page': e.find('{http://www.opengis.net/cat/csw/2.0.2}SearchResults').attrib.get('nextRecord')
}

return result


def _csw_local_dispatch(self, keywords=None, start=0, limit=10, bbox=None, identifier=None):
"""
HTTP-less CSW
"""
# set up configuration
config = SafeConfigParser()

for section, options in settings.PYCSW['CONFIGURATION'].iteritems():
config.add_section(section)
for option, value in options.iteritems():
config.set(section, option, value)

# fake HTTP environment variable
os.environ['QUERY_STRING'] = ''

# init pycsw
csw = server.Csw(config)

# fake HTTP method
csw.requesttype = 'POST'

# fake HTTP request parameters
if identifier is None: # it's a GetRecords request
formats = []
for f in self.catalogue.formats:
formats.append(METADATA_FORMATS[f][0])

csw.kvp = {
'elementsetname': 'full',
'typenames': formats,
'resulttype': 'results',
'constraintlanguage': 'CQL_TEXT',
'constraint': 'csw:AnyText like "%%%s%%"' % keywords,
'outputschema': 'http://www.isotc211.org/2005/gmd',
'constraint': None,
'startposition': start,
'maxrecords': limit
}
response = csw.getrecords()
else: # it's a GetRecordById request
csw.kvp = {
'id': [identifier],
'outputschema': 'http://www.isotc211.org/2005/gmd',
}
response = csw.getrecordbyid()

return etree.tostring(response)
37 changes: 0 additions & 37 deletions geonode/catalogue/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,40 +61,3 @@ def csw_global_dispatch(request):
content = csw.dispatch_wsgi()

return HttpResponse(content, content_type=csw.contenttype)

def csw_local_dispatch(request):
"""
HTTP-less CSW
"""
# set up configuration
config = SafeConfigParser()

for section, options in settings.PYCSW['CONFIGURATION'].iteritems():
config.add_section(section)
for option, value in options.iteritems():
config.set(section, option, value)

# fake HTTP environment variable
os.environ['QUERY_STRING'] = ''

# init pycsw
csw = server.Csw(config)

# fake HTTP method
csw.requesttype = request.method.upper()

# fake HTTP request parameters
csw.kvp = {
'elementsetname': 'brief',
'typenames': 'csw:Record',
'resulttype': 'results',
'constraintlanguage': 'CQL_TEXT',
#'constraint': 'csw:AnyText like "%iLor%"',
'constraint': None,
'maxrecords': '2'
}

response = csw.getrecords()
response_string = etree.tostring(response)

return HttpResponse(response, content_type=csw.contenttype)

0 comments on commit cedb12a

Please sign in to comment.