def _collection_post(self, clazz, schema): document = schema.objectify(self.request.validated) document.document_id = None DBSession.add(document) DBSession.flush() user_id = self.request.authenticated_userid self._create_new_version(document, user_id) sync_search_index(document) return to_json_dict(document, schema)
def _put(self, clazz, schema): user_id = self.request.authenticated_userid id = self.request.validated['id'] document_in = \ schema.objectify(self.request.validated['document']) self._check_document_id(id, document_in.document_id) # get the current version of the document document = self._get_document(clazz, id) self._check_versions(document, document_in) # remember the current version numbers of the document old_versions = document.get_versions() # find out whether the update of the geometry should be skipped. skip_geometry_update = document_in.geometry is None # update the document with the input document document.update(document_in) try: DBSession.flush() except StaleDataError: raise HTTPConflict('concurrent modification') # when flushing the session, SQLAlchemy automatically updates the # version numbers in case attributes have changed. by comparing with # the old version numbers, we can check if only figures or only locales # have changed. (update_type, changed_langs) = document.get_update_type(old_versions) if update_type: # A new version needs to be created and persisted self._update_version( document, user_id, self.request.validated['message'], update_type, changed_langs) # And the search updated sync_search_index(document) json_dict = to_json_dict(document, schema) if skip_geometry_update: # Optimization: the geometry is not sent back if the client # requested to skip the geometry update. Geometries may be very # huge; this optimization should speed the data transfer. json_dict['geometry'] = None return json_dict
def test_sync_search_index_update(self): """Tests that already existing documents are updated. """ index = elasticsearch_config['index'] waypoint = Waypoint( document_id=51252, waypoint_type='summit', elevation=2000, geometry=DocumentGeometry( geom='SRID=3857;POINT(635956 5723604)'), locales=[ WaypointLocale( culture='fr', title='Mont Granier', description='...', summary='Le Mont Granier') ]) # insert the document t = transaction.begin() sync_search_index(waypoint) t.commit() # then update the document (add a new language) waypoint = Waypoint( document_id=51252, waypoint_type='summit', elevation=2000, geometry=DocumentGeometry( geom='SRID=3857;POINT(635956 5723604)'), locales=[ WaypointLocale( culture='fr', title='Mont Granier', description='...', summary='Le Mont Granier'), WaypointLocale( culture='en', title='Mont Granier', description='...', summary='The Mont Granier') ]) t = transaction.begin() sync_search_index(waypoint) t.commit() doc = SearchDocument.get(id=51252, index=index) self.assertEqual(doc['title_fr'], 'Mont Granier') self.assertEqual(doc['summary_fr'], 'Le Mont Granier') self.assertEqual(doc['title_en'], 'Mont Granier') self.assertEqual(doc['summary_en'], 'The Mont Granier')
def test_sync_search_index_insert(self): """Tests that new documents are inserted in the index. """ index = elasticsearch_config['index'] waypoint = Waypoint( document_id=51251, waypoint_type='summit', elevation=2000, geometry=DocumentGeometry( geom='SRID=3857;POINT(635956 5723604)'), locales=[ WaypointLocale( culture='fr', title='Mont Granier', description='...', summary='Le Mont [b]Granier[/b]') ]) t = transaction.begin() sync_search_index(waypoint) t.commit() doc = SearchDocument.get(id=51251, index=index) self.assertEqual(doc['title_fr'], 'Mont Granier') self.assertEqual(doc['summary_fr'], 'Le Mont Granier ')