示例#1
0
 def _get_bra_by_uuid_or_404(self, bra_id: UUID) -> _Element:
     with connection_scope() as con:
         query = select([BraRecordTable.c.br_raw_xml
                         ]).where(BraRecordTable.c.br_id == bra_id)
         res = con.execute(query).first()
     if not res:
         bra_api.abort(HTTPStatus.NOT_FOUND)
     return res.br_raw_xml
示例#2
0
 def _get_bra_by_date_or_404(self, massif: str, date: str) -> _Element:
     with session_scope() as sess:
         try:
             parsed_date = datetime.strptime(date, "%Y-%m-%d")
             query = (sess.query(BraRecord.br_raw_xml).join(Massif).filter(
                 Massif.m_name == massif.upper()).filter(
                     cast(BraRecord.br_production_date, Date) ==
                     parsed_date))
             return query.one().br_raw_xml
         except NoResultFound:
             bra_api.abort(HTTPStatus.NOT_FOUND,
                           "BRA for this date or name cannot be found.")
         except ValueError:
             # strptime fail
             bra_api.abort(HTTPStatus.NOT_FOUND,
                           "BRA for this date or name cannot be found.")
示例#3
0
 def _transform_bra(self, bra: _Element) -> str:
     try:
         with resource_stream("nivo_api", "static/bra.xslt") as fp:
             xslt = ET.parse(fp)
         transform = ET.XSLT(xslt)
         return str(transform(bra))
     except LxmlError:
         return bra_api.abort(
             HTTPStatus.INTERNAL_SERVER_ERROR,
             "Something went wrong in BRA generation 😭",
         )
示例#4
0
 def get(self, massif_id, bra_date) -> Dict:
     parsed_date = date.fromisoformat(bra_date)
     with session_scope() as sess:
         global_rec = sess.query(
             BraRecord,
             func.lag(BraRecord.br_id).over(
                 order_by=BraRecord.br_production_date,
                 partition_by=BraRecord.br_massif,
             ).label("previous_bra_id"),
             func.lead(BraRecord.br_id).over(
                 order_by=BraRecord.br_production_date,
                 partition_by=BraRecord.br_massif,
             ).label("next_bra_id"),
         ).subquery()
         result_filtered = (sess.query(global_rec).filter(
             global_rec.c.br_production_date.cast(Date) == parsed_date
         ).filter(global_rec.c.br_massif == massif_id).first())
         if result_filtered is None:
             return bra_api.abort(
                 404,
                 f"Record for massif id {massif_id} for the date {bra_date} could not be found."
             )
         record_as_dict = result_filtered._asdict()
         record_as_dict["massif"] = (sess.query(Massif).filter(
             Massif.m_id == result_filtered.br_massif).first())
         record_as_dict["risks"] = sess.query(Risk).filter(
             Risk.r_record_id == result_filtered.br_id)
         record_as_dict["snow_records"] = sess.query(SnowRecord).filter(
             SnowRecord.s_bra_record == result_filtered.br_id)
         record_as_dict["fresh_snow_records"] = sess.query(
             FreshSnowRecord).filter(
                 FreshSnowRecord.fsr_bra_record == result_filtered.br_id)
         record_as_dict["weather_forecasts"] = sess.query(
             WeatherForecast).filter(
                 WeatherForecast.wf_bra_record == result_filtered.br_id)
         record_as_dict["risk_forecasts"] = sess.query(RiskForecast).filter(
             RiskForecast.rf_bra_record == result_filtered.br_id)
         return marshal(record_as_dict, bra_model)