class Foo: bar: Union[str, float, bool] = doc.field()
class Foo: bar: str = doc.field(required=True)
class Foo: bar: Any = doc.field()
class Foo: bar: Union[str, None] = doc.field()
class AfterFilter(doc.Model): after: datetime = doc.field( description='To filter all messages after a datetime in isoformat')
class Bulk(doc.Model): name = 'bulk_request' bulk_request: bool = doc.field()
class DocMessagePut(doc.Model): """ Model to show in generated swagger(for put) """ text: str = doc.field(description='The text of the message')
class DocMessageGet(doc.Model): """ Model to show in generated swagger(for get) """ id: str = doc.field(description='The message unique identifier') text: str = doc.field(description='Message\'s text') timestamp: datetime = doc.field(description='The message\'s timestmap')
class UserList(HTTPMethodView): """ Show a list of all the users or insert user in list """ @doc.summary('List all users') @doc.produces(doc.field(type=typing.List[DocUserGet])) async def get(self, request: Request): """Endpoint for GET method (list all users) :param request: request made :type request: Request """ with request.app.dbi.context() as db_ctx: try: users = [ user async for user in db_ctx.User.find( projection=request.app.dbi.user_schema.__mongoload__) ] data, error = request.app.dbi.user_schema.dump(users, many=True) if error: print(error) return json({ 'message': 'Impossible to dump some data' }, 500) return json(data, 200) except Exception as err: print(err) return json({ 'message': 'Impossible to get the data requested' }, 500) @doc.summary('Create a single user') @doc.consumes(DocUserPut, location='body', required=True) @doc.produces(DocUserGet) async def put(self, request: Request): """Endpoint for PUT method (creates a single user) :param request: request made :type request: Request """ if request.json is None: return json({'message': 'invalid schema format(json)'}, 400) try: data, errors = request.app.dbi.user_schema.load(request.json) except marshmallow.ValidationError as e: e.messages['message'] = 'ValidationError' return json(e.messages, 400) if errors: errors['message'] = 'ValidationError' return json(errors, 400) with request.app.dbi.context() as db_ctx: user = db_ctx.User(**data) user.googleSessionId = uuid.uuid4() try: await user.commit() except mongoengine.OperationError as oe: print(oe) return json({'message': 'Impossible to commit changes'}, 500) paramFirst = db_ctx.Params( ofTopic=request.app.default_topic, values=request.json, startTime=datetime.utcnow(), priority=0) context = db_ctx.Context( ofUser=user, timestamp=datetime.utcnow(), params=[paramFirst], message=db_ctx.Message(text='Hi!')) try: await context.commit() responses = await request.app.ai.get_message( db_ctx, user.id, None, request.app.fallback_rule) if responses is not None: for r in responses: await r.commit() except mongoengine.OperationError as oe: print(oe) return json({'message': 'Impossible to commit changes'}, 500) # TODO: Gestire caso in cui user รจ comunque salvato data, error = request.app.dbi.user_schema.dump(user) if error: print(error) return json({'message': 'Impossible to serialize user'}, 500) return json(data, 200)
class DocUserPut(doc.Model): """ Model of User to send to the api """ username: str = doc.field(description='The user displayed username')
class DocUserGet(doc.Model): """ Model of User returned by the api """ id: str = doc.field(description='The user unique identifier') username: str = doc.field(description='The user displayed username')