def test_synchronize_associations(self): self.route3 = Route(activities=['hiking']) self.route4 = Route(activities=['hiking']) self.session.add_all([self.route3, self.route4]) self.session.flush() self.session.add_all([ Association.create(parent_document=self.route1, child_document=self.route2), Association.create(parent_document=self.route1, child_document=self.route3) ]) self.session.flush() new_associations = { 'routes': [{ 'document_id': self.route2.document_id, 'is_parent': True }, { 'document_id': self.route4.document_id, 'is_parent': True }], 'waypoints': [{ 'document_id': self.waypoint1.document_id, 'is_parent': True }] } synchronize_associations(self.route1, new_associations, self.global_userids['contributor']) self.assertIsNotNone(self._get_association(self.route1, self.route2)) self.assertIsNotNone(self._get_association(self.route4, self.route1)) self.assertIsNone(self._get_association(self.route2, self.route1)) self.assertIsNone(self._get_association(self.route1, self.route3)) self.assertIsNotNone(self._get_association(self.waypoint1, self.route1))
def test_synchronize_associations(self): self.route3 = Route(activities=['hiking']) self.route4 = Route(activities=['hiking']) self.session.add_all([self.route3, self.route4]) self.session.flush() self.session.add_all([ Association.create( parent_document=self.route1, child_document=self.route2), Association.create( parent_document=self.route1, child_document=self.route3) ]) self.session.flush() new_associations = { 'routes': [ {'document_id': self.route2.document_id, 'is_parent': True}, {'document_id': self.route4.document_id, 'is_parent': True} ], 'waypoints': [ {'document_id': self.waypoint1.document_id, 'is_parent': True} ] } synchronize_associations( self.route1, new_associations, self.global_userids['contributor']) self.assertIsNotNone(self._get_association(self.route1, self.route2)) self.assertIsNotNone(self._get_association(self.route4, self.route1)) self.assertIsNone(self._get_association(self.route2, self.route1)) self.assertIsNone(self._get_association(self.route1, self.route3)) self.assertIsNotNone( self._get_association(self.waypoint1, self.route1))
def _put( self, clazz, schema, clazz_locale=None, before_update=None, after_update=None): 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, clazz_locale=clazz_locale) if document.redirects_to: raise HTTPBadRequest('can not update merged document') if document.protected and not self.request.has_permission('moderator'): raise HTTPForbidden('No permission to change a protected document') self._check_versions(document, document_in) # remember the current version numbers of the document old_versions = document.get_versions() # update the document with the input document document.update(document_in) if before_update: before_update(document, document_in, user_id=user_id) 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_types, changed_langs) = document.get_update_type(old_versions) if update_types: # A new version needs to be created and persisted self._update_version( document, user_id, self.request.validated['message'], update_types, changed_langs) if document.type != AREA_TYPE and UpdateType.GEOM in update_types: update_areas_for_document(document, reset=True) if after_update: after_update(document, update_types, user_id=user_id) # And the search updated notify_es_syncer(self.request.registry.queue_config) associations = self.request.validated.get('associations', None) if associations: synchronize_associations(document, associations, user_id) return {}
def update_document(document, document_in, request, before_update=None, after_update=None, manage_versions=None): user_id = request.authenticated_userid # remember the current version numbers of the document old_versions = document.get_versions() if before_update: before_update(document, document_in) # update the document with the input document document.update(document_in) if manage_versions: manage_versions(document, old_versions) 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_types, changed_langs) = document.get_update_type(old_versions) if update_types: # A new version needs to be created and persisted DocumentRest.update_version(document, user_id, request.validated['message'], update_types, changed_langs) if document.type != AREA_TYPE and UpdateType.GEOM in update_types: update_areas_for_document(document, reset=True) if document.type != MAP_TYPE and UpdateType.GEOM in update_types: update_maps_for_document(document, reset=True) if after_update: after_update(document, update_types, user_id=user_id) update_cache_version(document) associations = request.validated.get('associations', None) if associations: check_association_add = \ association_permission_checker(request) check_association_remove = \ association_permission_removal_checker(request) added_associations, removed_associations = \ synchronize_associations( document, associations, user_id, check_association_add=check_association_add, check_association_remove=check_association_remove) if update_types or associations: # update search index notify_es_syncer(request.registry.queue_config) update_feed_document_update(document, user_id, update_types) if associations and (removed_associations or added_associations): update_cache_version_associations(added_associations, removed_associations) return update_types
def _put( self, clazz, schema, clazz_locale=None, before_update=None, after_update=None): 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, clazz_locale=clazz_locale) if document.redirects_to: raise HTTPBadRequest('can not update merged document') if document.protected and not self.request.has_permission('moderator'): raise HTTPForbidden('No permission to change a protected document') self._check_versions(document, document_in) # remember the current version numbers of the document old_versions = document.get_versions() # update the document with the input document document.update(document_in) if before_update: before_update(document, document_in, user_id=user_id) 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_types, changed_langs) = document.get_update_type(old_versions) if update_types: # A new version needs to be created and persisted DocumentRest.update_version( document, user_id, self.request.validated['message'], update_types, changed_langs) if document.type != AREA_TYPE and UpdateType.GEOM in update_types: update_areas_for_document(document, reset=True) if document.type != MAP_TYPE and UpdateType.GEOM in update_types: update_maps_for_document(document, reset=True) if after_update: after_update(document, update_types, user_id=user_id) update_cache_version(document) associations = self.request.validated.get('associations', None) if associations: check_association_add = \ association_permission_checker(self.request) check_association_remove = \ association_permission_removal_checker(self.request) added_associations, removed_associations = \ synchronize_associations( document, associations, user_id, check_association_add=check_association_add, check_association_remove=check_association_remove) if update_types or associations: # update search index notify_es_syncer(self.request.registry.queue_config) update_feed_document_update(document, user_id, update_types) if associations and (removed_associations or added_associations): update_cache_version_associations( added_associations, removed_associations) return {}