Skip to content

Commit cedb12a

Browse files
committed
make pycsw local requests
1 parent f07a1ea commit cedb12a

File tree

2 files changed

+94
-37
lines changed

2 files changed

+94
-37
lines changed

geonode/catalogue/backends/pycsw_local.py

+94
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717
#
1818
#########################################################################
1919

20+
import os
21+
from lxml import etree
22+
from django.conf import settings
23+
from ConfigParser import SafeConfigParser
24+
from owslib.iso import MD_Metadata
25+
from pycsw import server
2026
from geonode.catalogue.backends.generic import CatalogueBackend as GenericCatalogueBackend
27+
from geonode.catalogue.backends.generic import METADATA_FORMATS
2128

2229
MD_CORE_MODEL = {
2330
'typename': 'pycsw:CoreMetadata',
@@ -94,3 +101,90 @@ def remove_record(self, uuid):
94101
def create_record(self, item):
95102
pass
96103

104+
def get_record(self, uuid):
105+
results = self._csw_local_dispatch(identifier=uuid)
106+
if len(results) < 1:
107+
return None
108+
109+
result = etree.fromstring(results).find('{http://www.isotc211.org/2005/gmd}MD_Metadata')
110+
111+
if result is None:
112+
return None
113+
114+
record = MD_Metadata(result)
115+
record.keywords = []
116+
if hasattr(record, 'identification') and hasattr(record.identification, 'keywords'):
117+
for kw in record.identification.keywords:
118+
record.keywords.extend(kw['keywords'])
119+
120+
record.links = {}
121+
record.links['metadata'] = self.catalogue.urls_for_uuid(uuid)
122+
record.links['download'] = self.catalogue.extract_links(record)
123+
return record
124+
125+
def search_records(self, keywords, start, limit, bbox):
126+
with self.catalogue:
127+
lresults = self._csw_local_dispatch(keywords, keywords, start+1, limit, bbox)
128+
# serialize XML
129+
e = etree.fromstring(lresults)
130+
self.catalogue.records = [MD_Metadata(x) for x in e.findall('//{http://www.isotc211.org/2005/gmd}MD_Metadata')]
131+
132+
# build results into JSON for API
133+
results = [self.catalogue.metadatarecord2dict(doc) for v, doc in self.catalogue.records.iteritems()]
134+
135+
result = {
136+
'rows': results,
137+
'total': e.find('{http://www.opengis.net/cat/csw/2.0.2}SearchResults').attrib.get('numberOfRecordsMatched'),
138+
'next_page': e.find('{http://www.opengis.net/cat/csw/2.0.2}SearchResults').attrib.get('nextRecord')
139+
}
140+
141+
return result
142+
143+
144+
def _csw_local_dispatch(self, keywords=None, start=0, limit=10, bbox=None, identifier=None):
145+
"""
146+
HTTP-less CSW
147+
"""
148+
# set up configuration
149+
config = SafeConfigParser()
150+
151+
for section, options in settings.PYCSW['CONFIGURATION'].iteritems():
152+
config.add_section(section)
153+
for option, value in options.iteritems():
154+
config.set(section, option, value)
155+
156+
# fake HTTP environment variable
157+
os.environ['QUERY_STRING'] = ''
158+
159+
# init pycsw
160+
csw = server.Csw(config)
161+
162+
# fake HTTP method
163+
csw.requesttype = 'POST'
164+
165+
# fake HTTP request parameters
166+
if identifier is None: # it's a GetRecords request
167+
formats = []
168+
for f in self.catalogue.formats:
169+
formats.append(METADATA_FORMATS[f][0])
170+
171+
csw.kvp = {
172+
'elementsetname': 'full',
173+
'typenames': formats,
174+
'resulttype': 'results',
175+
'constraintlanguage': 'CQL_TEXT',
176+
'constraint': 'csw:AnyText like "%%%s%%"' % keywords,
177+
'outputschema': 'http://www.isotc211.org/2005/gmd',
178+
'constraint': None,
179+
'startposition': start,
180+
'maxrecords': limit
181+
}
182+
response = csw.getrecords()
183+
else: # it's a GetRecordById request
184+
csw.kvp = {
185+
'id': [identifier],
186+
'outputschema': 'http://www.isotc211.org/2005/gmd',
187+
}
188+
response = csw.getrecordbyid()
189+
190+
return etree.tostring(response)

geonode/catalogue/views.py

-37
Original file line numberDiff line numberDiff line change
@@ -61,40 +61,3 @@ def csw_global_dispatch(request):
6161
content = csw.dispatch_wsgi()
6262

6363
return HttpResponse(content, content_type=csw.contenttype)
64-
65-
def csw_local_dispatch(request):
66-
"""
67-
HTTP-less CSW
68-
"""
69-
# set up configuration
70-
config = SafeConfigParser()
71-
72-
for section, options in settings.PYCSW['CONFIGURATION'].iteritems():
73-
config.add_section(section)
74-
for option, value in options.iteritems():
75-
config.set(section, option, value)
76-
77-
# fake HTTP environment variable
78-
os.environ['QUERY_STRING'] = ''
79-
80-
# init pycsw
81-
csw = server.Csw(config)
82-
83-
# fake HTTP method
84-
csw.requesttype = request.method.upper()
85-
86-
# fake HTTP request parameters
87-
csw.kvp = {
88-
'elementsetname': 'brief',
89-
'typenames': 'csw:Record',
90-
'resulttype': 'results',
91-
'constraintlanguage': 'CQL_TEXT',
92-
#'constraint': 'csw:AnyText like "%iLor%"',
93-
'constraint': None,
94-
'maxrecords': '2'
95-
}
96-
97-
response = csw.getrecords()
98-
response_string = etree.tostring(response)
99-
100-
return HttpResponse(response, content_type=csw.contenttype)

0 commit comments

Comments
 (0)