Ejemplo n.º 1
0
 def get(self):
     """获取用户,并返回用户信息"""
     user = UserProfile.query.get(g.user["uid"])
     user_dict = user_schema.dump(user)
     user_dict[
         "avatar"] = "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1016301861,2773103463&fm=26&gp=0.jpg"
     return generate_response(data=user_dict)
Ejemplo n.º 2
0
    def post(self):
        # 获取用户传过来的数据
        data = request.json

        # 验证参数有效性
        form = UserForm(data=data)
        if form.validate():
            # # 创建用户
            # user = UserProfile(user_profile_email=data.get("email"),
            #                    user_profile_name=data.get("name"),
            #                    password=data.get("password"))
            # db.session.add(user)
            # db.session.commit()
            # result = user_schema.dump(user)
            UserProfile.create_user(user_profile_email=form.email.data,
                                    user_profile_name=form.name.data,
                                    password=form.password.data)
            user = UserProfile.query.filter_by(user_profile_email=data.get("email")).first()
            result = user_schema.dump(user)

            # 返回结果
            return generate_response(data=result)
        else:
            result = form.errors
            raise FormValidateException(message=result)
Ejemplo n.º 3
0
    def post(self):
        # 获取用户传过来的数据
        data = request.json

        # 验证参数有效性
        # RequestParse: 验证参数类型 => 弱
        # wtforms:更灵活,参数类型、参数值... => 推荐!!
        # 构建表单 => 设置参数的要求 => data与表单绑定 => 验证数据有效性
        form = RegisterForm(data=data)
        if form.validate():

            # 注意: form.email.data
            UserProfile.create_user(
                user_profile_email=form.email.data,
                user_profile_name=form.username.data,
                password=form.password.data,
                user_profile_mobile=form.phone.data,
                user_height=form.height.data,
                user_weight=form.weight.data,
                user_age=form.age.data,
            )
            user = UserProfile.query.filter_by(
                user_profile_email=data.get("email")).first()
            result = user_schema.dump(user)
            # 返回结果
            return generate_response(data=result)
        else:
            result = form.errors
            raise FormValidateException(message=result)
Ejemplo n.º 4
0
 def post(self):
     """新建一个idc信息"""
     params = request.json
     idc_name = params.get("name", "")
     idc = IDC(idc_name=idc_name, idc_name_cn=idc_name)
     db.session.add(idc)
     db.session.commit()
     return generate_response(data=idc_schema.dump(idc))
Ejemplo n.º 5
0
 def delete(self):
     params = request.json
     # 凡是获取客户端数据 => 都使用forms来进行数据检验和检查
     task_id = params.get("task_id", "")
     task = Task.query.get(task_id)
     db.session.delete(task)
     db.session.commit()
     return generate_response(data=[])
Ejemplo n.º 6
0
 def get(self):
     tasklogs = TaskLog.query.all()
     for tasklog in tasklogs:
         res = AsyncResult(tasklog.tasklog_tid, app=celery)
         if not tasklog.tasklog_result and res.result:
             tasklog.tasklog_result = res.result
             db.session.add(tasklog)
     db.session.commit()
     return generate_response(data=tasklogs_schema.dump(tasklogs),
                              total=len(tasklogs))
Ejemplo n.º 7
0
 def post(self):
     x = request
     # 获取用户传过来的参数(api的参数为json数据)
     data = request.json
     # 将数据与LoginForm进行绑定
     form = LoginForm(data=data)
     # 检查用户是否合法
     user = form.validate()
     # 生成token
     token = create_token(uid=user.user_profile_id)
     return generate_response(data={"token":token})
Ejemplo n.º 8
0
    def post(self):
        """获取用户,并返回用户信息"""
        # user = UserProfile.query.get(g.user["uid"])
        data = request.json
        user_name = data['user_name']
        user = UserProfile.query.filter_by(user_profile_name=user_name).first()
        weight = user.user_profile_weight
        height = user.user_profile_height
        age = user.user_profile_age
        sample = [[1, age, height, weight]]
        sample = transformer.transform(sample)
        user_type = classifier.predict(sample)[0]

        prefer = [
            user.prefer_1,
            user.prefer_2,
            user.prefer_3,
            user.prefer_4,
        ]

        # videos = Exercise.query.filter(Exercise.video_type_id.in_(user_ex_mapping[user_type])).all()

        recommends = {'user_type': user_type}
        random_pick = list(
            choice(user_ex_mapping[user_type], size=4, replace=False))
        temp = {}
        i = 0
        other_prob = list(
            set(full_type_list).difference(set(user_ex_mapping[user_type])))
        random_pick.extend(choice(other_prob, 1))
        for video_type in random_pick:
            video_list = []
            videos = Exercise.query.filter(Exercise.video_type_id == video_type)\
                .order_by(Exercise.advertise_charge.desc()).all()
            if video_type in prefer:
                video_list.extend(choice(videos, 2))
            else:
                video_list.extend(choice(videos, 1))
            for video in video_list:
                temp["re" + str(i)] = {
                    'exercise_name': video.exercise_name,
                    'exercise_length': video.exercise_length,
                    'description': video.description,
                    'video_link': video.video_link,
                    'image_url': video.image_url,
                    'category': Category.query.get(video.category_id).name
                }
                i += 1
            if i == 6:
                break
        recommends['recommend'] = temp
        # reco_dict = reco_schema.dump(recommends)

        return generate_response(data=recommends)
Ejemplo n.º 9
0
 def get(self):
     """获取所有idc信息"""
     # Log(user=xxx, method=request.method, url=request.url, ...)
     # commit()
     # 封装成装饰器
     params = request.args
     keywords = params.get("key", "")
     if keywords:
         idcs = IDC.query.filter_by(idc_name=keywords).all()
     else:
         idcs = IDC.query.all()
     return generate_response(data=idcs_schema.dump(idcs), total=len(idcs))
Ejemplo n.º 10
0
 def post(self):
     # 获取用户传过来的参数(api的参数为json数据)
     data = request.json
     # 检查参数的合法性(RequestParser/WTForms)
     form = RegisterForm(data=data)
     # 如果合法,创建
     if form.validate():
         # 创建用户UserProfile.create_user()
         user = UserProfile.create_user(user_profile_email=form.email.data,
                                 user_profile_name=form.name.data,
                                 password=form.password.data)
         return generate_response(data=user_schema.dump(user))
     else:
         raise ArgsTypeException(message=form.errors)
Ejemplo n.º 11
0
 def post(self):
     params = request.json
     task_name = params.get("command_name", "")
     task_command = params.get("command", "")
     task_args = params.get("args", "")
     task_host = params.get("force_host", "")
     # 这里请同学们自己进行优化 => forms . validate
     task = Task(task_name=task_name,
                 task_args=task_args,
                 task_command=task_command,
                 task_host=task_host)
     db.session.add(task)
     db.session.commit()
     return generate_response(data=task_schema.dump(task))
Ejemplo n.º 12
0
 def post(self):
     data = request.json
     if not data:
         raise ArgsTypeException
     # 将接收的数据绑定form,由form验证
     form = LoginForm(data=data)
     # validate函数返回了合法的用户
     user = form.validate()
     if user:
         # 生成token
         token = create_token(uid=user.user_profile_id)
         return generate_response(data={"token":token})
     else:
         result = form.errors
         raise FormValidateException(message=result)
Ejemplo n.º 13
0
 def post(self):
     # 获取参数部分,每个函数都有类似代码,可以优化
     params = request.json
     task_id = params.get("task_id", "")
     # get根据id来查找数据
     task = Task.query.get(task_id)
     # 发布异步任务
     res = rum_cmd.delay(task.task_host, task.task_command)
     # 写日志
     tasklog = TaskLog(task_id=task.task_id,
                       tasklog_tid=res.task_id,
                       user=g.user.get("uid"))
     db.session.add(tasklog)
     db.session.commit()
     return generate_response(data=tasklog_schema.dump(tasklog))
Ejemplo n.º 14
0
    def post(self):
        # 接收用户数据
        data = request.json
        if not data:
            raise ArgsTypeException(message="传参的方式不对,或没有传参")

        # 验证用户输入数据合法性
        # 创建Form -> data绑定 -> validate
        form = LoginForm(data=data)
        # validate函数返回了合法的用户
        user = form.validate()
        # 生成token
        pic = "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1016301861,2773103463&fm=26&gp=0.jpg"
        token = create_token(uid=user.user_profile_id)
        return generate_response(data={"token": token, "pic": pic})
Ejemplo n.º 15
0
 def post(self):
     import time
     import random
     params = request.json
     # 添加Asset -> 主机名
     hostname = params.get("hostname", "")
     asset_sn = params.get("sn", "")
     asset_sn = asset_sn if asset_sn else hash(
         str(time.time()) + str(random.randint(1, 99999)))
     asset = Asset(asset_sn=asset_sn, asset_hostname=hostname)
     # 添加Server -> idc
     detail = params.get("detail", "")
     server = Server(asset=asset, note=detail)
     # 添加NIC -> ip
     ipaddr = params.get("ip")
     eth = "eth0"
     nic = Nic(nic_ipaddr=ipaddr, nic_name=eth, server=server)
     # 入库
     db.session.add_all([asset, server, nic])
     db.session.commit()
     return generate_response(data=server_parse(server))
Ejemplo n.º 16
0
 def get(self):
     """获取所有服务器信息"""
     params = request.args
     page = int(params.get("page", 1))
     per_page = int(params.get("limit", current_app.config["PER_PAGE"]))
     keywords = params.get("key", "")
     if keywords:
         # 根据条件查询指定的数据(key => sn, hostname)
         # 跨表查询
         result = db.session.query(Server).join(Asset).filter(
             or_(Asset.asset_sn.like(f"%{keywords}%"),
                 Asset.asset_hostname.like(f"%{keywords}%")))
     else:
         # 获取所有数据
         result = Server.query
     # 将查询到的数据进行分页
     paginate_servers = result.paginate(page,
                                        per_page=per_page,
                                        error_out=False)
     # 将数据序列化
     servers = server_parse(paginate_servers.items)
     # 返回数据(减少代码冗余)
     return generate_response(data=servers, total=paginate_servers.total)
Ejemplo n.º 17
0
 def get(self):
     user = UserProfile.query.get(g.user["uid"])
     user_dict = user_schema.dump(user)
     return generate_response(data=user_dict)
Ejemplo n.º 18
0
 def get(self):
     tasks = tasks_schema.dump(Task.query.all())
     return generate_response(data=tasks, total=len(tasks))
Ejemplo n.º 19
0
 def put(self):
     # 接收的数据[{},{}]
     return generate_response(data=[])