def wrap(ob, project_sm=False): try: ex = extent(ob) precision = ex['precision'] geometry = ex['extent'] if project_sm: g = geometry.get('geometry', geometry) geo = TGOOGLE(g) geometry = geojson.GeoJSON.to_instance(geo) except (KeyError, TypeError, ValueError, NotLocatedError): geometry = None precision = None return geojson.Feature( id=ob.getId(), properties=dict( title=ob.Title(), snippet=SnippetWrapper(ob).snippet, description=ob.Description(), link=ob.absolute_url(), location_precision=precision, ), geometry=geometry, )
def _data(self, published_only=False): if published_only: contentFilter = {'review_state': 'published'} else: contentFilter = {} sm = bool(self.request.form.get('sm', 0)) x = sorted( getContents( self.context, **dict( [('portal_type', 'Location')] + contentFilter.items())), reverse=True) if len(x) > 0: features = [wrap(ob, sm) for ob in x] else: features = [wrap(ob, sm) for ob in self.context.getFeatures()] try: ex = extent(self.context) bbox = shape(ex['extent']).bounds reprPoint = representative_point(self.context)['coords'] except: bbox = None reprPoint = None # Names objs = sorted( getContents( self.context, **dict( [('portal_type', 'Name')] + contentFilter.items())), reverse=True) names = [o.getNameAttested() or o.getNameTransliterated() for o in objs] # Modification time, actor, contributors try: context = aq_inner(self.context) rt = getToolByName(context, "portal_repository") records = [] history = rt.getHistoryMetadata(context) if history: metadata = history.retrieve(-1)['metadata']['sys_metadata'] records.append((metadata['timestamp'], metadata)) for ob in getContents(self.context, **contentFilter): history = rt.getHistoryMetadata(ob) if not history: continue metadata = history.retrieve(-1)['metadata']['sys_metadata'] records.append((metadata['timestamp'], metadata)) records = sorted(records, reverse=True) recent_changes = [] modified = DateTime(records[0][0]).HTML4() principal0 = records[0][1]['principal'] recent_changes.append(dict(modified=modified, principal=principal0)) for record in records[1:]: principal = record[1]['principal'] if principal != principal0: modified = DateTime(record[0]).HTML4() recent_changes.append( dict(modified=modified, principal=principal)) break except: log.error( "Failed to find last change metadata for %s", repr(self.context), ) recent_changes = None # connections connections = [o.getId() for o in self.context.getRefs( "connectsWith") + self.context.getBRefs("connectsWith")] return { '@context': make_ld_context(), 'type': 'FeatureCollection', 'id': self.context.getId(), 'title': self.context.Title(), 'description': self.context.Description(), 'features': sorted(features, key=W, reverse=True), 'recent_changes': recent_changes, 'names': [unicode(n, "utf-8") for n in names], 'reprPoint': reprPoint, 'bbox': bbox, 'connectsWith': connections }