def obs_delete_db_and_disk(repository: Repository, obs_id: ObservationId): app.logger.info("About to delete observation %s and all its files" % obs_id) # Step 1: Create a list of files to be deleted. There may be several products. products = repository.read_observation_files(obs_id) obs = repository.read_observation(obs_id) files = [[f['filename'], "product"] for f in products] # Step 2: thumbnail is stored with the observation. There's at most one thumbnail. files.append([os.path.join("thumbs", obs['thumbnail']), "thumbnail"]) # Step 3: And there are two charts: alt-az pass chart and polar pass chart. files.append( [os.path.join("charts", "by_time-%s.png" % obs_id), "pass chart"]) files.append( [os.path.join("charts", "polar-%s.png" % obs_id), "polar pass chart"]) # All those files are stored in this dir root = app.config["storage"]['image_root'] status = [] for f in files: path = os.path.join(root, f[0]) app.logger.info("Trying to delete [%s]" % path) try: os.remove(path) status.append("Deleted %s file %s." % (f[1], f[0])) except Exception as ex: status.append("Failed to delete %s file: %s, reason: %s" % (f[1], path, repr(ex))) # Step 4: delete entries in the db repository.delete_observation(obs_id) status.append("DB removal complete.") return status
def test_observation_cr_d(self, repository: Repository): """Test checks if Create, Retrieve and Delete operations work for observations.""" observation: Observation = { 'obs_id': ObservationId(0), 'aos': datetime.datetime(2020, 3, 21, 12, 00, 0), 'tca': datetime.datetime(2020, 3, 21, 12, 15, 0), 'los': datetime.datetime(2020, 3, 21, 12, 30, 0), 'sat_id': SatelliteId(28654), 'thumbnail': 'thumb-123.png', 'config': None, 'notes': None, 'station_id': StationId(1), 'tle': [ "1 25544U 98067A 08264.51782528 -.00002182 00000-0 -11606-4 0 2927", "2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.72125391563537" ], 'rating': None } obs_id = repository.insert_observation(observation) self.assertEqual(type(obs_id), int) observation["obs_id"] = obs_id db_observation = repository.read_observation(obs_id) self.assertIsNotNone(db_observation) self.assertDictEqual(observation, db_observation) # type: ignore observation_file: ObservationFile = { 'obs_file_id': ObservationFileId(0), 'filename': '123.png', 'media_type': 'image/png', 'obs_id': obs_id, 'rating': 0.66 } file_id = repository.insert_observation_file(observation_file) self.assertEqual(type(file_id), int) observation_files = repository.read_observation_files(obs_id) self.assertEqual(len(observation_files), 1) observation_file["obs_file_id"] = file_id db_observation_file = observation_files[0] self.assertDictEqual(observation_file, db_observation_file) # type: ignore db_observation = repository.read_observation(obs_id) self.assertAlmostEqual(db_observation["rating"], 0.66, places=2) repository.delete_observation(obs_id) observation_files = repository.read_observation_files(obs_id) self.assertEqual(len(observation_files), 0)