diff --git a/toardb/stationmeta/crud.py b/toardb/stationmeta/crud.py index 5c21dddf39449dc119b0f22db1dacc89161a7ef8..b6035fd381ccb583362c5c4f5c94135beeb2cda9 100644 --- a/toardb/stationmeta/crud.py +++ b/toardb/stationmeta/crud.py @@ -35,6 +35,15 @@ def get_all_stationmeta_core(db: Session, skip : int = 0, limit: int = None): return db_objects +def get_all_stationmeta(db: Session, skip : int = 0, limit: int = None): + db_objects = db.query(models.StationmetaCore).offset(skip).limit(limit).all() + for db_object in db_objects: + # there is a mismatch with coordinates and additional_metadata + db_object.coordinates = get_coordinates_from_geom(db_object.coordinates) + db_object.additional_metadata = str(db_object.additional_metadata) + return db_objects + + def create_stationmeta_core(db: Session, stationmeta_core: StationmetaCoreCreate): db_stationmeta_core = models.StationmetaCore(**stationmeta_core.dict()) # there's a mismatch with coordinates --> how to automatically switch back and forth?! diff --git a/toardb/stationmeta/models_annotation.py b/toardb/stationmeta/models_annotation.py index c7f91b67ab8e9a1a6fccbe0ce962e801c6e1e4f7..2a7fb421586c04d0e2a3b6e40f0ff4f21666bba4 100644 --- a/toardb/stationmeta/models_annotation.py +++ b/toardb/stationmeta/models_annotation.py @@ -61,7 +61,7 @@ class StationmetaAnnotation(Base): # how to reactivate the following two lines?! # contributor = relationship('AuthUser') - stationmeta_core = relationship("StationmetaCore", + station = relationship("StationmetaCore", secondary=stationmeta_core_stationmeta_annotations_table, backref="annotations") diff --git a/toardb/stationmeta/models_aux.py b/toardb/stationmeta/models_aux.py index eb42b8675e94fe0bb2b242641ec2f4b2c27b7556..52e96b92b826d4771813a214ef6c8aea0d1c97e1 100644 --- a/toardb/stationmeta/models_aux.py +++ b/toardb/stationmeta/models_aux.py @@ -13,7 +13,7 @@ class StationMetaAuxImage (Base) -------------------------------- """ from sqlalchemy import CheckConstraint, Column, DateTime, ForeignKey, Integer, String, Text, Sequence -#from sqlalchemy.orm import relationship +from sqlalchemy.orm import relationship from .models_core import StationmetaCore from toardb.base import Base @@ -49,8 +49,8 @@ class StationmetaAuxDoc(Base): date_added = Column(DateTime(True), nullable=False) resource = Column(String(100), nullable=False) station_id = Column(ForeignKey(StationmetaCore.id, deferrable=True, initially='DEFERRED'), index=True) -# how to reactivate the following? -# station = relationship('StationmetaCore') + + station = relationship('StationmetaCore') STATIONMETA_AUX_IMAGE_ID_SEQ = Sequence('stationmeta_aux_image_id_seq') #define sequence explicitly @@ -97,8 +97,8 @@ class StationmetaAuxImage(Base): image_height = Column(Integer, nullable=False) image_width = Column(Integer, nullable=False) station_id = Column(ForeignKey(StationmetaCore.id, deferrable=True, initially='DEFERRED'), index=True) -# how to reactivate the following? -# station = relationship('StationmetaCore') + + station = relationship('StationmetaCore') STATIONMETA_AUX_URL_ID_SEQ = Sequence('stationmeta_aux_url_id_seq') #define sequence explicitly @@ -132,6 +132,6 @@ class StationmetaAuxUrl(Base): date_added = Column(DateTime(True), nullable=False) resource = Column(String(200), nullable=False) station_id = Column(ForeignKey(StationmetaCore.id, deferrable=True, initially='DEFERRED'), index=True) -# how to reactivate the following? -# station = relationship('StationmetaCore') + + station = relationship('StationmetaCore') diff --git a/toardb/stationmeta/models_core.py b/toardb/stationmeta/models_core.py index 7b4d23c67cf541e17157297416a633da16da236a..f63cd95f9e7422e2d396e09d654e8b82eb95c386 100644 --- a/toardb/stationmeta/models_core.py +++ b/toardb/stationmeta/models_core.py @@ -92,6 +92,12 @@ class StationmetaCore(Base): timezone = Column(String(64), nullable=False) additional_metadata = Column(JSONB(astext_type=Text()), nullable=True) coordinate_validator_id = Column(ForeignKey(AuthUser.id, deferrable=True, initially='DEFERRED'), nullable=False, index=True) - #how to reactivate the following? -# coordinate_validator = relationship('AuthUser') + + coordinate_validator = relationship('AuthUser') + + aux_images = relationship('StationmetaAuxImage', back_populates='station') + aux_docs = relationship('StationmetaAuxDoc', back_populates='station') + aux_urls = relationship('StationmetaAuxUrl', back_populates='station') + globalmeta = relationship('StationmetaGlobal', uselist=False, back_populates='station') + globalservice = relationship('StationmetaGlobalService', uselist=False, back_populates='station') diff --git a/toardb/stationmeta/models_global.py b/toardb/stationmeta/models_global.py index f9cea75e62769dcba3eb36516631d36edda66b6a..899984f090fdae6c7c51499e61822e60c2725c92 100644 --- a/toardb/stationmeta/models_global.py +++ b/toardb/stationmeta/models_global.py @@ -93,6 +93,9 @@ class StationmetaGlobal(Base): # 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 - station_id = Column(ForeignKey(StationmetaCore.id, deferrable=True, initially='DEFERRED'), nullable=False, unique=True) - #how to reactivate the following? +# station_id = Column(ForeignKey('stationmeta_core.id', deferrable=True, initially='DEFERRED'), nullable=False, unique=True) + station_id = Column(ForeignKey(StationmetaCore.id)) + + #uselist=False ==> 1:1 relationship # station = relationship('StationmetaCore', uselist=False) + station = relationship('StationmetaCore', back_populates='globalmeta') diff --git a/toardb/stationmeta/models_global_services.py b/toardb/stationmeta/models_global_services.py index 5f8f7daf0f9688e52c3aaefe25c3caa387965a2c..8db8f4c190b97186ecde1df7c1387c5e750ba5de 100644 --- a/toardb/stationmeta/models_global_services.py +++ b/toardb/stationmeta/models_global_services.py @@ -3,7 +3,9 @@ class StationmetaGlobalService (Base) ===================================== """ -from sqlalchemy import CheckConstraint, Column, Integer, String, Sequence +from sqlalchemy import CheckConstraint, Column, Integer, String, Sequence, ForeignKey +from sqlalchemy.orm import relationship +from .models_core import StationmetaCore from toardb.base import Base STATIONMETA_GLOBAL_SERVICES_ID_SEQ = Sequence('stationmeta_global_services_id_seq') # define sequence explicitly @@ -48,3 +50,6 @@ class StationmetaGlobalService(Base): result_nvalues = Column(Integer, nullable=False) service_valid_year = Column(Integer) service_url = Column(String(200), nullable=False, unique=True) + station_id = Column(ForeignKey(StationmetaCore.id)) + + station = relationship('StationmetaCore', back_populates='globalservice') diff --git a/toardb/stationmeta/models_role.py b/toardb/stationmeta/models_role.py index 01fd6d3d751fa4ade7c9daba983d0c35938ec819..c49f9b3ae17175733032942bd0e50593106cba5b 100644 --- a/toardb/stationmeta/models_role.py +++ b/toardb/stationmeta/models_role.py @@ -11,7 +11,7 @@ from toardb.base import Base # many-to-many relationships -stationmeta_core_stationmeta_roles_table = Table('stationmeta_core_timeseries_roles', Base.metadata, +stationmeta_core_stationmeta_roles_table = Table('stationmeta_core_stationmeta_roles', Base.metadata, Column('station_id', Integer, ForeignKey('stationmeta_core.id')), Column('role_id', Integer, ForeignKey('stationmeta_roles.id')) ) @@ -58,7 +58,7 @@ class StationmetaRole(Base): # how to reactivate the following two lines?! # person = relationship('Person') - stationmeta_core = relationship("StationmetaCore", + station = relationship("StationmetaCore", secondary=stationmeta_core_stationmeta_roles_table, backref="roles") diff --git a/toardb/stationmeta/schemas.py b/toardb/stationmeta/schemas.py index ec29ce1e1ab62566ad53798e9164a945fa59e485..ea08658db4fca632360124e58aeb702da8a58810 100644 --- a/toardb/stationmeta/schemas.py +++ b/toardb/stationmeta/schemas.py @@ -220,3 +220,36 @@ class StationmetaRole(StationmetaRoleBase): class Config: orm_mode = True +# ======== for nested view/upload ========= + +class StationmetaBase(StationmetaCoreBase): + roles: List[StationmetaRole] = None + annotations: List[StationmetaAnnotation] = None + aux_images: List[StationmetaAuxImage] = None + aux_docs: List[StationmetaAuxDoc] = None + aux_urls: List[StationmetaAuxUrl] = None + globalmeta: StationmetaGlobal = None + globalservice: StationmetaGlobalService = None + + class Config: + orm_mode = True + +class StationmetaCreate(StationmetaCoreBase): + roles: List[StationmetaRoleBase] = None + annotations: List[StationmetaAnnotation] = None + aux_images: List[StationmetaAuxImage] = None + aux_docs: List[StationmetaAuxDoc] = None + aux_urls: List[StationmetaAuxUrl] = None + globalmeta: StationmetaGlobal = None + globalservice: StationmetaGlobalService = None + + class Config: + orm_mode = True + + +class Stationmeta(StationmetaBase): + id: int + + class Config: + orm_mode = True + diff --git a/toardb/stationmeta/stationmeta.py b/toardb/stationmeta/stationmeta.py index a3c100336d13ee89d2df9fa6e598e321178ca0a4..823d07d61364352d5a549903cdc354f7b8cc92a0 100644 --- a/toardb/stationmeta/stationmeta.py +++ b/toardb/stationmeta/stationmeta.py @@ -19,6 +19,13 @@ def get_all_stationmeta_core(skip: int = 0, limit: int = None, db: Session = Dep stationmeta_core = crud.get_all_stationmeta_core(db, skip=skip, limit=limit) return stationmeta_core +# the same as above, but nested view +#get all entries of table stationmeta +@router.get('/stationmeta/', response_model=List[schemas.Stationmeta]) +def get_all_stationmeta(skip: int = 0, limit: int = None, db: Session = Depends(get_db)): + stationmeta = crud.get_all_stationmeta(db, skip=skip, limit=limit) + return stationmeta + #get all core metadata of one station @router.get('/stationmeta_core/{station_code}', response_model=schemas.StationmetaCore) def get_stationmeta_core(station_code: str, db: Session = Depends(get_db)):