コード例 #1
0
ファイル: obs.py プロジェクト: gut-space/svarog
def obs(obs_id: ObservationId = None, limit_and_offset=None):
    if obs_id is None:
        abort(300, description="ID is required")
        return

    repository = Repository()
    with repository.transaction():
        observation = repository.read_observation(obs_id)

        orbit = None

        if observation is None:
            abort(404, "Observation not found")

        files = repository.read_observation_files(observation["obs_id"],
                                                  **limit_and_offset)
        files_count = repository.count_observation_files(obs_id)
        satellite = repository.read_satellite(observation["sat_id"])

        orbit = observation
        if observation['tle'] is not None:
            # observation['tle'] is always an array of exactly 2 strings.
            orbit = parse_tle(*observation['tle'], satellite["sat_name"])

        station = repository.read_station(observation["station_id"])

    # Now tweak some observation parameters to make them more human readable
    observation = human_readable_obs(observation)

    # Now determine if there is a logged user and if there is, if this user is the owner of this
    # station. If he is, we should show the admin panel.
    user_id = 0
    owner = False
    if current_user.is_authenticated:
        user_id = current_user.get_id()

        # Check if the current user is the owner of the station.
        station_id = station['station_id']

        owner = repository.is_station_owner(user_id, station_id)

    return 'obs.html', dict(obs=observation,
                            files=files,
                            sat_name=satellite["sat_name"],
                            item_count=files_count,
                            orbit=orbit,
                            station=station,
                            is_owner=owner)
コード例 #2
0
ファイル: obs.py プロジェクト: gut-space/svarog
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
コード例 #3
0
    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)