#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ # @Version : Python 3.11.4 # @Software : Sublime Text 4 # @Author : StudentCWZ # @Email : StudentCWZ@outlook.com # @Date : 2023/10/29 00:08 # @File : user.py # @Description : """ import json from flask import Blueprint, current_app, request, jsonify from flask_restful import Api from application.services import UserService from marshmallow import ValidationError from application.schemas import UserSchema from application.utils import ElasticsearchUtil from application.common import ConfigHelper user_api = Blueprint('user_api', __name__) api_users = Api(user_api) @user_api.route('/users', methods=['GET']) def get_users(): users = UserService.get_all_users() return jsonify([user.to_dict() for user in users]) @user_api.route('/users/', methods=['GET']) def get_user(user_id): user = UserService.get_user_by_id(user_id) if user is None: return jsonify({'error': 'User not found'}), 404 return jsonify(user.to_dict()) @user_api.route('/users', methods=['POST']) def create_user(): user_schema = UserSchema() try: data = user_schema.load(request.json) except ValidationError as e: return jsonify(e.messages), 400 username = data.username password = data.password email = data.email user = UserService.create_user(username, password, email) return jsonify(user_schema.dump(user)), 201 @user_api.route('/tests', methods=['POST']) def test_user(): data = request.get_json() start = data.get('start') end = data.get('end') dsl = ElasticsearchUtil.dsl(start, end) index = current_app.config.Elasticsearch.Index res = ElasticsearchUtil.search(index, dsl) print(len(res)) data = { "user_name": "libai", "user_age": 18, } res_json = json.dumps(data) return res_json, 200