|
17 | 17 | #
|
18 | 18 | #########################################################################
|
19 | 19 |
|
| 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 |
20 | 26 | from geonode.catalogue.backends.generic import CatalogueBackend as GenericCatalogueBackend
|
| 27 | +from geonode.catalogue.backends.generic import METADATA_FORMATS |
21 | 28 |
|
22 | 29 | MD_CORE_MODEL = {
|
23 | 30 | 'typename': 'pycsw:CoreMetadata',
|
@@ -94,3 +101,90 @@ def remove_record(self, uuid):
|
94 | 101 | def create_record(self, item):
|
95 | 102 | pass
|
96 | 103 |
|
| 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) |
0 commit comments