Exemple #1
0
async def error_middleware(request, handler):
    try:
        return await handler(request)
    except web.HTTPException as ex:
        return json_response(status=ex.status, text_status=ex.text, data={})
    except Exception as e:
        request.app.logging.error(traceback.format_exc())
        return json_response(status=500, text_status=str(e), data={})
Exemple #2
0
async def error_middleware(request, handler):
    try:
        return await handler(request)
    except web.HTTPException as ex:
        return json_response(status=ex.status, text_status=ex.text, data={})
    except Exception as e:
        request.app.logger.exception("Exception: {}".format(str(e)),
                                     exc_info=e)
        return json_response(status=500, text_status=str(e), data={})
Exemple #3
0
    async def get(self):
        query = self.request.query
        accomodation_id: int = int(query.get("id"))

        store: AioMongoAccessor = self.request.app.get("store").get("db")
        cur = await store.collection("accomodation").aggregate([
            {
                "$match": {
                    "id": accomodation_id
                }
            },
            {
                "$lookup": {
                    "from": "review",
                    "localField": "id",
                    "foreignField": "listing_id",
                    "as": "reviews",
                }
            },
        ])

        accomodation: Optional[Accomodation] = None
        async for d in cur:
            accomodation = Accomodation.Schema().load(d)
            break

        if accomodation is None:
            raise HTTPNotFound

        return json_response(data=Accomodation.Schema().dump(accomodation))
    async def delete(self):
        question_id: int = int(self.request.match_info["question"])
        question = await models.Question.get(question_id)
        if not question:
            raise web.HTTPNotFound()
        await question.delete()

        return json_response(data=None)
    async def delete(self):
        theme_id: int = int(self.request.match_info["theme"])
        theme: models.Theme = await models.Theme.get(theme_id)
        if not theme:
            raise web.HTTPNotFound()

        await theme.delete()
        return json_response(data=None)
Exemple #6
0
 async def get(self):
     query = self.request.query
     limit: str = query.get("limit", 100)
     offset: str = query.get("offset", 0)
     airports_plenty = await Airport.query.limit(limit).offset(offset
                                                               ).gino.all()
     return json_response(data=AirportListDC.Schema().dump(
         AirportListDC(airports=[a.to_dc() for a in airports_plenty])))
 async def post(self):
     question: models.Question = await models.Question.create(
         content=self.request["data"]["content"],
         correct_answer=self.request["data"]["correct_answer"],
         score=self.request["data"]["score"],
         theme_id=self.request["data"]["theme_id"],
     )
     return json_response(data=QuestionResponseSchema().dump(question))
    async def patch(self):
        theme_id: int = int(self.request.match_info["theme"])
        theme: models.Theme = await models.Theme.get(theme_id)
        if not theme:
            raise web.HTTPNotFound()
        await theme.update(title=self.request["data"]["title"]).apply()

        return json_response(data=ThemeResponseSchema().dump(theme))
    async def patch(self):
        question_id: int = int(self.request.match_info["question"])
        question = await models.Question.get(question_id)
        if not question:
            raise web.HTTPNotFound()

        data = self.request["data"]
        await question.update(**data).apply()

        return json_response(data=QuestionResponseSchema().dump(question))
Exemple #10
0
    async def get(self):
        query = self.request.query
        airport_code: str = query.get("airport_code")

        airport: Optional[Airport] = (await Airport.query.where(
            Airport.airport_code == airport_code, ).gino.load(Airport).first())
        if airport is None:
            raise HTTPNotFound

        return json_response(data=AirportDC.Schema().dump(airport.to_dc()))
Exemple #11
0
    async def get(self):
        query = self.request.query
        limit: str = query.get("limit", 100)
        offset: str = query.get("offset", 0)

        store: AioMongoAccessor = self.request.app.get("store").get("db")
        accomodations = (
            await store.collection("accomodation").find().limit(10).to_list())

        return json_response(
            data={
                "accomodations":
                BaseAccomodation.Schema().dump(BaseAccomodation.Schema().load(
                    accomodations, many=True),
                                               many=True)
            })
Exemple #12
0
    async def get(self):
        flight_id: int = int(self.request.query.get("flight_id"))

        airport_from: Airport = Airport.alias("airport_from")
        airport_to: Airport = Airport.alias("airport_to")
        flight: Optional[Flight] = (
            await Flight.outerjoin(
                airport_from, airport_from.airport_code ==
                Flight.departure_airport).outerjoin(
                    airport_to,
                    airport_to.airport_code == Flight.arrival_airport
                ).select().where(Flight.flight_id == flight_id).gino.load(
                    # Flight
                    Flight.load(departure_airport_info=airport_from,
                                arrival_airport_info=airport_to)).all())
        if not flight:
            raise HTTPNotFound

        return json_response(data=FlightDC.Schema().dump(flight[0].to_dc()))
Exemple #13
0
    async def get(self):
        times_func = db.func.count(Ticket.passenger_id)
        amount_func = db.func.sum(TicketFlight.amount)

        query = (db.select(
            [Ticket.passenger_id, times_func, amount_func]).select_from(
                Ticket.outerjoin(
                    TicketFlight,
                    Ticket.ticket_no == TicketFlight.ticket_no)).group_by(
                        Ticket.passenger_id).gino.load((
                            Ticket.passenger_id,
                            ColumnLoader(times_func),
                            ColumnLoader(amount_func),
                        )))

        res = Stat()
        async with db.transaction():
            async for passenger_id, times, amount in query.iterate():
                res.passengers.append(
                    StatItem(passenger_id=passenger_id,
                             times=times,
                             amount=amount))

        return json_response(data=Stat.Schema().dump(res))
Exemple #14
0
    async def get(self):
        query = self.request.query
        accomodation_id: int = int(query.get("id"))

        store: AioMongoAccessor = self.request.app.get("store").get("db")
        cur = await store.collection("accomodation").aggregate([
            {
                "$match": {
                    "id": accomodation_id
                }
            },
            {
                "$lookup": {
                    "from": "review",
                    "localField": "id",
                    "foreignField": "listing_id",
                    "as": "reviews",
                }
            },
            {
                "$lookup": {
                    "from": "review",
                    "localField": "id",
                    "foreignField": "listing_id",
                    "as": "reviews",
                }
            },
            {
                "$unwind": "$reviews"
            },
            {
                "$addFields": {
                    "review_date": {
                        "$toDate": "$reviews.date"
                    },
                }
            },
            {
                "$group": {
                    "_id": "$reviews.reviewer_id",
                    "times": {
                        "$sum": 1
                    },
                    "first_date": {
                        "$min": "$review_date"
                    },
                    "last_date": {
                        "$max": "$review_date"
                    },
                    "comments": {
                        "$addToSet": "$reviews.comments"
                    },
                }
            },
            {
                "$match": {
                    "times": {
                        "$gt": 1
                    }
                }
            },
        ])

        cases: List[StatItem] = list()
        async for d in cur:
            item = StatItem.Schema().load({
                **d,
                "first_date":
                d.get("first_date").isoformat(),
                "last_date":
                d.get("last_date").isoformat(),
            })
            cases.append(item)

        return json_response(
            data={"cases": StatItem.Schema().dump(cases, many=True)})
Exemple #15
0
 async def post(self):
     theme = await models.Theme.create(title=self.request["data"]["title"])
     return json_response(data=ThemeResponseSchema().dump(theme))
Exemple #16
0
 async def get(self):
     questions: models.Question = await models.Question.query.gino.all()
     return json_response(
         data=QuestionResponseSchema().dump(questions, many=True))
Exemple #17
0
 async def get(self):
     theme_id: int = int(self.request.match_info["theme"])
     questions = await models.Question.query.where(
         models.Question.theme_id == theme_id).gino.all()
     return json_response(
         data=QuestionResponseSchema().dump(questions, many=True))
Exemple #18
0
 async def get(self):
     theme_id: int = int(self.request.match_info["theme"])
     theme = await models.Theme.get(theme_id)
     if not theme:
         raise web.HTTPNotFound()
     return json_response(data=ThemeResponseSchema().dump(theme))
Exemple #19
0
 async def get(self):
     themes = await models.Theme.query.gino.all()
     return json_response(
         data=ThemeResponseSchema().dump(themes, many=True))