def forceDeleteAllHandler(self, params): updatedCollections = [] for collection in CollectionModel().list(user=self.getCurrentUser()): if BOUNDS_KEY in collection: del collection[BOUNDS_KEY] updatedCollections.append({ k: v for (k, v) in CollectionModel().save(collection).items() if k in COLLECTION_RETURN_FIELDS }) return updatedCollections
def itemRemovedFromCollection(self, event): # More efficient way instead of whole recomputation? item = ItemModel().load(event.info['_id'], user=self.getCurrentUser()) if item['baseParentType'] != 'collection': return collection = CollectionModel().load(item['baseParentId'], user=self.getCurrentUser()) # Default to just bounding box of item itself newConvexHull = computeCollectionConvexHull(collection, self.getCurrentUser()) collection[BOUNDS_KEY] = { 'type': 'Polygon', 'coordinates': [list(newConvexHull.exterior.coords)] } collection = CollectionModel().save(collection)
def singleCollectionHandler(self, params, id=None): collection = CollectionModel().load(id, user=self.getCurrentUser()) if (BOUNDS_KEY in collection): return { k: v for (k, v) in collection.items() if k in COLLECTION_RETURN_FIELDS } else: return {}
def forceRecomputeAllHandler(self, params): filteredCollections = [] for collection in CollectionModel().list(user=self.getCurrentUser()): convexHull = computeCollectionConvexHull(collection, self.getCurrentUser()) if (convexHull): collection[BOUNDS_KEY] = { 'type': 'Polygon', 'coordinates': [list(convexHull.exterior.coords)] } collection = CollectionModel().save(collection) filteredCollections.append({ k: v for (k, v) in collection.items() if k in COLLECTION_RETURN_FIELDS }) return filteredCollections
def listCollectionHandler(self, params): filteredCollections = [] for collection in CollectionModel().list(user=self.getCurrentUser()): if BOUNDS_KEY in collection: filteredCollections.append({ k: v for (k, v) in collection.items() if k in COLLECTION_RETURN_FIELDS }) return filteredCollections
def __init__(self): super(Collection, self).__init__() self.resourceName = 'collection' self._model = CollectionModel() self.route('DELETE', (':id', ), self.deleteCollection) self.route('GET', (), self.find) self.route('GET', (':id', ), self.getCollection) self.route('GET', (':id', 'details'), self.getCollectionDetails) self.route('GET', (':id', 'download'), self.downloadCollection) self.route('GET', (':id', 'access'), self.getCollectionAccess) self.route('POST', (), self.createCollection) self.route('PUT', (':id', ), self.updateCollection) self.route('PUT', (':id', 'access'), self.updateCollectionAccess)
def itemAddedToCollection(self, event): item = ItemModel().load(event.info['file']['itemId'], user=self.getCurrentUser()) if item['baseParentType'] != 'collection' or 'geometa' not in item: return collection = CollectionModel().load(item['baseParentId'], user=self.getCurrentUser()) # Default to just bounding box of item itself newConvexHull = Polygon(*item['geometa']['bounds']['coordinates']) if BOUNDS_KEY in collection: oldConvexHull = Polygon(*collection[BOUNDS_KEY]['coordinates']) newConvexHull = MultiPolygon( polygons=[oldConvexHull, newConvexHull]).convex_hull collection[BOUNDS_KEY] = { 'type': 'Polygon', 'coordinates': [list(newConvexHull.exterior.coords)] } collection = CollectionModel().save(collection)
def afterPostPutCollection(event): # This will only trigger if no exceptions (for access, invalid id, etc.) are thrown extraParams = event.info['params'] if 'terms' in extraParams: collectionResponse = event.info['returnVal'] collectionId = collectionResponse['_id'] terms = extraParams['terms'] CollectionModel().update({'_id': collectionId}, {'$set': { 'terms': terms }}) collectionResponse['terms'] = terms event.addResponse(collectionResponse)
def load(info): # Augment the collection creation and edit routes to accept a terms field events.bind('rest.post.collection.after', 'terms', afterPostPutCollection) events.bind('rest.put.collection/:id.after', 'terms', afterPostPutCollection) for handler in [Collection.createCollection, Collection.updateCollection]: handler.description.param('terms', 'The Terms of Use for the collection.', required=False) # Expose the terms field on all collections CollectionModel().exposeFields(level=AccessType.READ, fields={'terms'}) # Add endpoint for registered users to accept terms info['apiRoot'].collection.route('POST', (':id', 'acceptTerms'), acceptCollectionTerms) # Expose the terms field on all users User().exposeFields(level=AccessType.ADMIN, fields={'terms'})
def facetedSearchHandler(self, params): query = self.getParamJson('query', params) docs = CollectionModel().findWithPermissions(query=query, user=self.getCurrentUser()) return [doc for doc in docs]