KevinHuSh
commited on
Commit
·
4e603f5
1
Parent(s):
028acc4
rm wrongly uploaded py (#1073)
Browse files### What problem does this PR solve?
### Type of change
- [x] Refactoring
- api/apps/canvas_app.py +0 -112
- api/db/services/canvas_service.py +0 -26
api/apps/canvas_app.py
DELETED
|
@@ -1,112 +0,0 @@
|
|
| 1 |
-
#
|
| 2 |
-
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
| 3 |
-
#
|
| 4 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
-
# you may not use this file except in compliance with the License.
|
| 6 |
-
# You may obtain a copy of the License at
|
| 7 |
-
#
|
| 8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
-
#
|
| 10 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
-
# See the License for the specific language governing permissions and
|
| 14 |
-
# limitations under the License.
|
| 15 |
-
#
|
| 16 |
-
import json
|
| 17 |
-
|
| 18 |
-
from flask import request
|
| 19 |
-
from flask_login import login_required, current_user
|
| 20 |
-
|
| 21 |
-
from api.db.services.canvas_service import CanvasTemplateService, UserCanvasService
|
| 22 |
-
from api.utils import get_uuid
|
| 23 |
-
from api.utils.api_utils import get_json_result, server_error_response, validate_request
|
| 24 |
-
from graph.canvas import Canvas
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
@manager.route('/templates', methods=['GET'])
|
| 28 |
-
@login_required
|
| 29 |
-
def templates():
|
| 30 |
-
return get_json_result(data=[c.to_dict() for c in CanvasTemplateService.get_all()])
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
@manager.route('/list', methods=['GET'])
|
| 34 |
-
@login_required
|
| 35 |
-
def canvas_list():
|
| 36 |
-
|
| 37 |
-
return get_json_result(data=[c.to_dict() for c in UserCanvasService.query(user_id=current_user.id)])
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
@manager.route('/rm', methods=['POST'])
|
| 41 |
-
@validate_request("canvas_ids")
|
| 42 |
-
@login_required
|
| 43 |
-
def rm():
|
| 44 |
-
for i in request.json["canvas_ids"]:
|
| 45 |
-
UserCanvasService.delete_by_id(i)
|
| 46 |
-
return get_json_result(data=True)
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
@manager.route('/set', methods=['POST'])
|
| 50 |
-
@validate_request("dsl", "title")
|
| 51 |
-
@login_required
|
| 52 |
-
def save():
|
| 53 |
-
req = request.json
|
| 54 |
-
req["user_id"] = current_user.id
|
| 55 |
-
if not isinstance(req["dsl"], str):req["dsl"] = json.dumps(req["dsl"], ensure_ascii=False)
|
| 56 |
-
try:
|
| 57 |
-
Canvas(req["dsl"])
|
| 58 |
-
except Exception as e:
|
| 59 |
-
return server_error_response(e)
|
| 60 |
-
|
| 61 |
-
req["dsl"] = json.loads(req["dsl"])
|
| 62 |
-
if "id" not in req:
|
| 63 |
-
req["id"] = get_uuid()
|
| 64 |
-
if not UserCanvasService.save(**req):
|
| 65 |
-
return server_error_response("Fail to save canvas.")
|
| 66 |
-
else:
|
| 67 |
-
UserCanvasService.update_by_id(req["id"], req)
|
| 68 |
-
|
| 69 |
-
return get_json_result(data=req)
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
@manager.route('/get/<canvas_id>', methods=['GET'])
|
| 73 |
-
@login_required
|
| 74 |
-
def get(canvas_id):
|
| 75 |
-
e, c = UserCanvasService.get_by_id(canvas_id)
|
| 76 |
-
if not e:
|
| 77 |
-
return server_error_response("canvas not found.")
|
| 78 |
-
return get_json_result(data=c.to_dict())
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
@manager.route('/run', methods=['POST'])
|
| 82 |
-
@validate_request("id", "dsl")
|
| 83 |
-
@login_required
|
| 84 |
-
def run():
|
| 85 |
-
req = request.json
|
| 86 |
-
if not isinstance(req["dsl"], str): req["dsl"] = json.dumps(req["dsl"], ensure_ascii=False)
|
| 87 |
-
try:
|
| 88 |
-
canvas = Canvas(req["dsl"], current_user.id)
|
| 89 |
-
ans = canvas.run()
|
| 90 |
-
req["dsl"] = json.loads(str(canvas))
|
| 91 |
-
UserCanvasService.update_by_id(req["id"], dsl=req["dsl"])
|
| 92 |
-
return get_json_result(data=req["dsl"])
|
| 93 |
-
except Exception as e:
|
| 94 |
-
return server_error_response(e)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
@manager.route('/reset', methods=['POST'])
|
| 98 |
-
@validate_request("canvas_id")
|
| 99 |
-
@login_required
|
| 100 |
-
def reset():
|
| 101 |
-
req = request.json
|
| 102 |
-
try:
|
| 103 |
-
user_canvas = UserCanvasService.get_by_id(req["canvas_id"])
|
| 104 |
-
canvas = Canvas(req["dsl"], current_user.id)
|
| 105 |
-
canvas.reset()
|
| 106 |
-
req["dsl"] = json.loads(str(canvas))
|
| 107 |
-
UserCanvasService.update_by_id(req["canvas_id"], dsl=req["dsl"])
|
| 108 |
-
return get_json_result(data=req["dsl"])
|
| 109 |
-
except Exception as e:
|
| 110 |
-
return server_error_response(e)
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api/db/services/canvas_service.py
DELETED
|
@@ -1,26 +0,0 @@
|
|
| 1 |
-
#
|
| 2 |
-
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
| 3 |
-
#
|
| 4 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
-
# you may not use this file except in compliance with the License.
|
| 6 |
-
# You may obtain a copy of the License at
|
| 7 |
-
#
|
| 8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
-
#
|
| 10 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
-
# See the License for the specific language governing permissions and
|
| 14 |
-
# limitations under the License.
|
| 15 |
-
#
|
| 16 |
-
from datetime import datetime
|
| 17 |
-
import peewee
|
| 18 |
-
from api.db.db_models import DB, API4Conversation, APIToken, Dialog, CanvasTemplate, UserCanvas
|
| 19 |
-
from api.db.services.common_service import CommonService
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
class CanvasTemplateService(CommonService):
|
| 23 |
-
model = CanvasTemplate
|
| 24 |
-
|
| 25 |
-
class UserCanvasService(CommonService):
|
| 26 |
-
model = UserCanvas
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|