def test_update_cache_version_for_area(self): waypoint = Waypoint(waypoint_type='summit') waypoint_unrelated = Waypoint(waypoint_type='summit') area = Area() self.session.add_all([waypoint, waypoint_unrelated, area]) self.session.flush() self.session.add( AreaAssociation(document_id=waypoint.document_id, area_id=area.document_id)) self.session.flush() update_cache_version_for_area(area) cache_version_waypoint = self.session.query(CacheVersion).get( waypoint.document_id) cache_version_untouched = self.session.query(CacheVersion).get( waypoint_unrelated.document_id) cache_version_area = self.session.query(CacheVersion).get( area.document_id) self.assertEqual(cache_version_waypoint.version, 2) self.assertEqual(cache_version_untouched.version, 1) # the cache key of the area is also not updated! self.assertEqual(cache_version_area.version, 1)
def update_associations(area, update_types, user_id): """Update the links between this area and documents when the geometry has changed. """ if update_types: # update cache key for currently associated docs update_cache_version_for_area(area) if UpdateType.GEOM in update_types: update_area(area, reset=True)
def update_area(area, reset=False): """Create associations for the given area with all intersecting documents. If `reset` is True, all possible existing associations to this area are dropped before creating new associations. """ if reset: DBSession.execute( AreaAssociation.__table__.delete().where( AreaAssociation.area_id == area.document_id) ) if area.redirects_to: # ignore forwarded areas return area_geom = select([DocumentGeometry.geom_detail]). \ where(DocumentGeometry.document_id == area.document_id) intersecting_documents = DBSession. \ query( DocumentGeometry.document_id, # id of a document literal_column(str(area.document_id))). \ join( Document, and_( Document.document_id == DocumentGeometry.document_id, Document.type != AREA_TYPE)). \ filter(Document.redirects_to.is_(None)). \ filter( or_( DocumentGeometry.geom.ST_Intersects( area_geom.label('t1')), DocumentGeometry.geom_detail.ST_Intersects( area_geom.label('t2')) )) DBSession.execute( AreaAssociation.__table__.insert().from_select( [AreaAssociation.document_id, AreaAssociation.area_id], intersecting_documents)) # update cache key for now associated docs update_cache_version_for_area(area)
def update_area(area, reset=False): """Create associations for the given area with all intersecting documents. If `reset` is True, all possible existing associations to this area are dropped before creating new associations. """ if reset: DBSession.execute(AreaAssociation.__table__.delete().where( AreaAssociation.area_id == area.document_id)) if area.redirects_to: # ignore forwarded areas return area_geom = select([DocumentGeometry.geom_detail]). \ where(DocumentGeometry.document_id == area.document_id) intersecting_documents = DBSession. \ query( DocumentGeometry.document_id, # id of a document literal_column(str(area.document_id))). \ join( Document, and_( Document.document_id == DocumentGeometry.document_id, Document.type != AREA_TYPE)). \ filter(Document.redirects_to.is_(None)). \ filter( or_( DocumentGeometry.geom.ST_Intersects( area_geom.label('t1')), DocumentGeometry.geom_detail.ST_Intersects( area_geom.label('t2')) )) DBSession.execute(AreaAssociation.__table__.insert().from_select( [AreaAssociation.document_id, AreaAssociation.area_id], intersecting_documents)) # update cache key for now associated docs update_cache_version_for_area(area)
def test_update_cache_version_for_area(self): waypoint = Waypoint(waypoint_type='summit') waypoint_unrelated = Waypoint(waypoint_type='summit') area = Area() self.session.add_all([waypoint, waypoint_unrelated, area]) self.session.flush() self.session.add(AreaAssociation( document_id=waypoint.document_id, area_id=area.document_id)) self.session.flush() update_cache_version_for_area(area) cache_version_waypoint = self.session.query(CacheVersion).get( waypoint.document_id) cache_version_untouched = self.session.query(CacheVersion).get( waypoint_unrelated.document_id) cache_version_area = self.session.query(CacheVersion).get( area.document_id) self.assertEqual(cache_version_waypoint.version, 2) self.assertEqual(cache_version_untouched.version, 1) # the cache key of the area is also not updated! self.assertEqual(cache_version_area.version, 1)