diff --git a/production_tests.sh b/production_tests.sh index 8c4c8711d90ee90dc1f1a3b6cdb04d9dd1d59d9d..7cf9db8dbb88e22837a8e0b7fee736c0c7228a25 100755 --- a/production_tests.sh +++ b/production_tests.sh @@ -27,6 +27,8 @@ curl http://127.0.0.1:8000/stationmeta/China11 curl -X POST -H "Content-Type:application/json" -d '{"stationmeta": {"codes":["ttt3","ttt4"],"name":"Test_China","coordinates":{"lat":36.256,"lng":17.106,"alt":1534.0},"country":"China","state":"Shandong Sheng","coordinate_validation_status":0,"coordinate_validation_date":"2020-03-11T12:22:18.047974+01:00","type_of_environment":0,"type_of_area":0,"category":"","timezone":"", "coordinate_validator_id": 1, "additional_metadata":"{}"}}' "http://127.0.0.1:8000/stationmeta/" # nested upload with enum fields curl -X POST -H "Content-Type:application/json" -d '{"stationmeta": {"codes":["ttt3","ttt4"],"name":"Test_China","coordinates":{"lat":36.256,"lng":17.106,"alt":1534.0},"country":"China","state":"Shandong Sheng","coordinate_validation_status":0,"coordinate_validation_date":"2020-03-11T12:22:18.047974+01:00","type_of_environment":0,"type_of_area":0,"category":"","timezone":"", "coordinate_validator_id": 1, "additional_metadata":"{}", roles": [{"role": 0, "person_id": 3, "status": 0},{"role": 1, "person_id": 3, "status": 0}]}}' "http://127.0.0.1:8000/stationmeta/" +# nested upload including 'global' (not defining all metadata -- getting the rest from default values) +curl -X POST -H "Content-Type:application/json" -d '{"stationmeta": {"codes":["ttt58"],"name":"Ttt_China58","coordiShandong Sheng","coordinate_validation_status":0,"coordinate_validation_date":"2020-03-11T12:22:18.047974+01:00","type_of_environment":0,"type_of_area":0,"category":"","timezone":"", "coordinate_validator_id": 1, "additional_metadata":"{}", "roles": [{"role": 0, "person_id": 3, "status": 0},{"role": 1, "person_id": 3, "status": 0}], "globalmeta": {"climatic_zone":1}}}' http://127.0.0.1:8000/stationmeta/ # TBD: (nested) upload with human readable fields curl -X POST -H "Content-Type:application/json" -d '{"stationmeta": {"codes":["ttt3","ttt4"],"name":"Test_China","coordinates":{"lat":36.256,"lng":17.106,"alt":1534.0},"country":"China","state":"Shandong Sheng","coordinate_validation_status":0,"coordinate_validation_date":"2020-03-11T12:22:18.047974+01:00","type_of_environment":0,"type_of_area":0,"category":"","timezone":"", "coordinate_validator_id": 1, "additional_metadata":"{}", "roles": [{"role": "PointOfContact", "person": "s.schroeder@fz-juelich.de", "status": "active"},{"role": "Originator", "person": "Stefan.Feigenspan@uba.de", "status": "active"}]}}' "http://127.0.0.1:8000/stationmeta/" diff --git a/toardb/stationmeta/crud.py b/toardb/stationmeta/crud.py index 7991ca20b8497cf83085960f85bc8de7f9848ef3..6afe64c32ba2da3b478edf2ee2b53d700a48fbdc 100644 --- a/toardb/stationmeta/crud.py +++ b/toardb/stationmeta/crud.py @@ -96,6 +96,7 @@ def create_stationmeta(db: Session, stationmeta: StationmetaCreate): stationmeta_core_id = db_stationmeta.id # store roles and update association table if roles_data: + print("====roles_data============", roles_data) for r in roles_data: db_role = models.StationmetaRole(**r) # check whether role is already present in database @@ -150,20 +151,18 @@ def create_stationmeta(db: Session, stationmeta: StationmetaCreate): db.refresh(db_aux_url) # store globalmeta if globalmeta_data: - for g in globalmeta_data: - db_global = models.StationmetaGlobal(**g) - db_global.station_id = stationmeta_core_id - db.add(db_global) - db.commit() - db.refresh(db_global) + db_global = models.StationmetaGlobal(**globalmeta_data) + db_global.station_id = stationmeta_core_id + db.add(db_global) + db.commit() + db.refresh(db_global) # store globalservice if globalservice_data: - for s in globalservice_data: - db_globalservice = models.StationmetaGlobalService(**s) - db_globalservice.station_id = stationmeta_core_id - db.add(db_globalservice) - db.commit() - db.refresh(db_globalservice) + db_globalservice = models.StationmetaGlobalService(**globalservice) + db_globalservice.station_id = stationmeta_core_id + db.add(db_globalservice) + db.commit() + db.refresh(db_globalservice) # there's a mismatch with coordinates --> how to automatically switch back and forth?! db_stationmeta.coordinates = tmp_coordinates return db_stationmeta diff --git a/toardb/stationmeta/models_global.py b/toardb/stationmeta/models_global.py index 899984f090fdae6c7c51499e61822e60c2725c92..9347d32e2b8db87e9c827ae9c7043d11687ba5ef 100644 --- a/toardb/stationmeta/models_global.py +++ b/toardb/stationmeta/models_global.py @@ -4,7 +4,7 @@ class StationmetaGlobal (Base) ============================== """ from sqlalchemy import Column, DateTime, Float, ForeignKey, Integer, String, \ - Text, CheckConstraint, Sequence + Text, text, CheckConstraint, Sequence from geoalchemy2.types import Geometry from sqlalchemy.orm import relationship from sqlalchemy.dialects.postgresql import JSONB, ARRAY @@ -20,37 +20,37 @@ class StationmetaGlobal(Base): +======================================+==================+===========+==========+================================================+ | id | integer | | not null | nextval('stationmeta_global_id_seq'::regclass) | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | population_density_year2010 | double precision | | not null | | + | population_density_year2010 | double precision | | not null | '-1.0'::numeric | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | max_population_density_25km_year2010 | double precision | | not null | | + | max_population_density_25km_year2010 | double precision | | not null | '-1.0'::numeric | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | climatic_zone | integer | | not null | | + | climatic_zone | integer | | not null | '-1'::integer | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | nightlight_1km_year2013 | double precision | | not null | | + | nightlight_1km_year2013 | double precision | | not null | '-1.0'::numeric | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | nightlight_5km_year2013 | double precision | | not null | | + | nightlight_5km_year2013 | double precision | | not null | '-1.0'::numeric | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | max_nightlight_25km_year2013 | double precision | | not null | | + | max_nightlight_25km_year2013 | double precision | | not null | '-1.0'::numeric | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | wheat_production_year2000 | double precision | | not null | | + | wheat_production_year2000 | double precision | | not null | '-1.0'::numeric | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | rice_production_year2000 | double precision | | not null | | + | rice_production_year2000 | double precision | | not null | '-1.0'::numeric | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | edgar_htap_v2_nox_emissions_year2010 | double precision | | not null | | + | edgar_htap_v2_nox_emissions_year2010 | double precision | | not null | '-1.0'::numeric | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | omi_no2_column_years2011to2015 | double precision | | not null | | + | omi_no2_column_years2011to2015 | double precision | | not null | '-1.0'::numeric | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | htap_region_tier1 | integer | | not null | | + | htap_region_tier1 | integer | | not null | '-1'::integer | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | etopo_alt | double precision | | not null | | + | etopo_alt | double precision | | not null | '-1.0'::numeric | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | etopo_min_alt_5km | double precision | | not null | | + | etopo_min_alt_5km | double precision | | not null | '-1.0'::numeric | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | etopo_relative_alt | double precision | | not null | | + | etopo_relative_alt | double precision | | not null | '-1.0'::numeric | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | dominant_landcover_year2012 | integer | | not null | | + | dominant_landcover_year2012 | integer | | not null | '-1'::integer | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ - | toar1_category | integer | | not null | | + | toar1_category | integer | | not null | '-1'::integer | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ | station_id | integer | | not null | | +--------------------------------------+------------------+-----------+----------+------------------------------------------------+ @@ -66,30 +66,31 @@ class StationmetaGlobal(Base): "stationmeta_global_station_id_29ff53dd_fk_stationmeta_core_id" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) DEFERRABLE INITIALLY DEFERRED """ __tablename__ = 'stationmeta_global' - __table_args__ = ( - CheckConstraint('climatic_zone >= 0'), - CheckConstraint('dominant_landcover_year2012 >= 0'), - CheckConstraint('htap_region_tier1 >= 0'), - CheckConstraint('toar1_category >= 0') - ) +# Default values do not fit CheckConstraints == > Check, how both can be done!! +# __table_args__ = ( +# CheckConstraint('climatic_zone >= 0'), +# CheckConstraint('dominant_landcover_year2012 >= 0'), +# CheckConstraint('htap_region_tier1 >= 0'), +# CheckConstraint('toar1_category >= 0') +# ) id = Column(Integer, STATIONMETA_GLOBAL_ID_SEQ, primary_key=True, server_default=STATIONMETA_GLOBAL_ID_SEQ.next_value()) - population_density_year2010 = Column(Float(53), nullable=False) - max_population_density_25km_year2010 = Column(Float(53), nullable=False) - climatic_zone = Column(Integer, nullable=False) - nightlight_1km_year2013 = Column(Float(53), nullable=False) - nightlight_5km_year2013 = Column(Float(53), nullable=False) - max_nightlight_25km_year2013 = Column(Float(53), nullable=False) - wheat_production_year2000 = Column(Float(53), nullable=False) - rice_production_year2000 = Column(Float(53), nullable=False) - edgar_htap_v2_nox_emissions_year2010 = Column(Float(53), nullable=False) - omi_no2_column_years2011to2015 = Column(Float(53), nullable=False) - htap_region_tier1 = Column(Integer, nullable=False) - etopo_alt = Column(Float(53), nullable=False) - etopo_min_alt_5km = Column(Float(53), nullable=False) - etopo_relative_alt = Column(Float(53), nullable=False) - dominant_landcover_year2012 = Column(Integer, nullable=False) - toar1_category = Column(Integer, nullable=False) + population_density_year2010 = Column(Float(53), nullable=False, server_default=text("'-1.0'::numeric")) + max_population_density_25km_year2010 = Column(Float(53), nullable=False, server_default=text("'-1.0'::numeric")) + climatic_zone = Column(Integer, nullable=False, server_default=text("'-1'::integer")) + nightlight_1km_year2013 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) + nightlight_5km_year2013 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) + max_nightlight_25km_year2013 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) + wheat_production_year2000 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) + rice_production_year2000 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) + edgar_htap_v2_nox_emissions_year2010 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) + omi_no2_column_years2011to2015 = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) + htap_region_tier1 = Column(Integer, nullable=False, server_default=text("'-1'::integer")) + etopo_alt = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) + etopo_min_alt_5km = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) + etopo_relative_alt = Column(Float(53), nullable=False, server_default=text("'-999.0'::numeric")) + dominant_landcover_year2012 = Column(Integer, nullable=False, server_default=text("'-1'::integer")) + toar1_category = Column(Integer, nullable=False, server_default=text("'-1'::integer")) # do not use string declaration here (not working for pytest) # use the explicit class name here, # see: https://groups.google.com/forum/#!topic/sqlalchemy/YjGhE4d6K4U diff --git a/toardb/stationmeta/schemas.py b/toardb/stationmeta/schemas.py index b4a984f4a1f104e992b3421173e9a2ad3dd358d7..0d65a3a392e5902921dcb5f99fda616368f6e823 100644 --- a/toardb/stationmeta/schemas.py +++ b/toardb/stationmeta/schemas.py @@ -178,6 +178,38 @@ class StationmetaGlobal(StationmetaGlobalBase): class Config: orm_mode = True + +class StationmetaGlobalBaseNested(BaseModel): + id: int = None + population_density_year2010: float = None + max_population_density_25km_year2010: float = None + climatic_zone: int = None + nightlight_1km_year2013: float = None + nightlight_5km_year2013: float = None + max_nightlight_25km_year2013: float = None + wheat_production_year2000: float = None + rice_production_year2000: float = None + edgar_htap_v2_nox_emissions_year2010: float = None + omi_no2_column_years2011to2015: float = None + htap_region_tier1: int = None + etopo_alt: float = None + etopo_min_alt_5km: float = None + etopo_relative_alt: float = None + dominant_landcover_year2012: int = None + toar1_category: int = None + + +class StationmetaGlobalNestedCreate(StationmetaGlobalBaseNested): + pass + + +class StationmetaGlobalNested(StationmetaGlobalBaseNested): + id: int + + class Config: + orm_mode = True + + # ======== StationmetaGlobalService ========= class StationmetaGlobalServiceBase(BaseModel): @@ -238,7 +270,7 @@ class StationmetaCreate(StationmetaCoreBase): aux_images: List[StationmetaAuxImage] = None aux_docs: List[StationmetaAuxDoc] = None aux_urls: List[StationmetaAuxUrl] = None - globalmeta: StationmetaGlobal = None + globalmeta: StationmetaGlobalNestedCreate = None globalservice: StationmetaGlobalService = None class Config: diff --git a/toardb/timeseries/models_core.py b/toardb/timeseries/models_core.py index b69d0e0627b573e347a84d582ba8d6a7a7e9e22b..eb1fdca582cff018b12d794728d0e592013907a7 100644 --- a/toardb/timeseries/models_core.py +++ b/toardb/timeseries/models_core.py @@ -41,9 +41,9 @@ class Timeseries(Base): +---------------------+--------------------------+-----------+----------+----------------------------------------+ | additional_metadata | jsonb | | not null | | +---------------------+--------------------------+-----------+----------+----------------------------------------+ - | date_added | timestamp with time zone | | not null | | + | date_added | timestamp with time zone | | not null | now() | +---------------------+--------------------------+-----------+----------+----------------------------------------+ - | date_modified | timestamp with time zone | | not null | | + | date_modified | timestamp with time zone | | not null | now() | +---------------------+--------------------------+-----------+----------+----------------------------------------+ | station_id | integer | | | | +---------------------+--------------------------+-----------+----------+----------------------------------------+ @@ -89,12 +89,8 @@ class Timeseries(Base): measurement_method = Column(String(128), nullable=False) sampling_height = Column(Float(53), nullable=False) additional_metadata = Column(JSONB(astext_type=Text()), nullable=True) - -# date_added, date_modified: -# how to have them changed automatically - - date_added = Column(DateTime(True), nullable=False) - date_modified = Column(DateTime(True), nullable=False) + date_added = Column(DateTime(True), nullable=False, server_default=text("now()")) + date_modified = Column(DateTime(True), nullable=False, server_default=text("now()")) # do not use string declaration here (not working for pytest) # use the explicit class name here, # see: https://groups.google.com/forum/#!topic/sqlalchemy/YjGhE4d6K4U