2021-08-17 17:00:34 +08:00
|
|
|
|
from flask_appbuilder.models.sqla.interface import SQLAInterface
|
|
|
|
|
from flask_babel import lazy_gettext as _
|
|
|
|
|
from myapp.models.model_team import Project,Project_User
|
2022-10-11 14:25:25 +08:00
|
|
|
|
from wtforms import SelectField, StringField
|
2021-08-17 17:00:34 +08:00
|
|
|
|
from myapp.utils import core
|
2022-10-11 14:25:25 +08:00
|
|
|
|
from myapp import appbuilder
|
2021-08-17 17:00:34 +08:00
|
|
|
|
from wtforms.ext.sqlalchemy.fields import QuerySelectField
|
|
|
|
|
from flask_appbuilder.fieldwidgets import Select2Widget
|
|
|
|
|
from myapp.exceptions import MyappException
|
2022-10-11 14:25:25 +08:00
|
|
|
|
from myapp import db, security_manager
|
2022-08-02 16:02:22 +08:00
|
|
|
|
from myapp.forms import MyBS3TextFieldWidget,MyBS3TextAreaFieldWidget
|
2022-10-11 14:25:25 +08:00
|
|
|
|
from wtforms.validators import DataRequired
|
2021-08-17 17:00:34 +08:00
|
|
|
|
from flask import (
|
|
|
|
|
flash,
|
2022-10-10 11:44:53 +08:00
|
|
|
|
g
|
2021-08-17 17:00:34 +08:00
|
|
|
|
)
|
|
|
|
|
from .base import (
|
|
|
|
|
get_user_roles,
|
|
|
|
|
MyappFilter,
|
|
|
|
|
MyappModelView,
|
|
|
|
|
)
|
|
|
|
|
from .baseApi import (
|
|
|
|
|
MyappModelRestApi
|
|
|
|
|
)
|
2022-10-11 14:25:25 +08:00
|
|
|
|
import json
|
|
|
|
|
from flask_appbuilder import CompactCRUDMixin
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
|
|
|
|
# table show界面下的
|
|
|
|
|
class Project_User_ModelView_Base():
|
|
|
|
|
label_title='组成员'
|
|
|
|
|
datamodel = SQLAInterface(Project_User)
|
|
|
|
|
add_columns = ['project','user','role']
|
|
|
|
|
edit_columns = add_columns
|
|
|
|
|
list_columns = ['user','role']
|
|
|
|
|
|
|
|
|
|
add_form_extra_fields = {
|
|
|
|
|
"project": QuerySelectField(
|
|
|
|
|
"项目组",
|
|
|
|
|
query_factory=lambda: db.session.query(Project),
|
|
|
|
|
allow_blank=True,
|
|
|
|
|
widget=Select2Widget(extra_classes="readonly"),
|
|
|
|
|
description='只有creator可以添加修改组成员,可以添加多个creator'
|
2022-08-02 16:02:22 +08:00
|
|
|
|
),
|
|
|
|
|
"role": SelectField(
|
|
|
|
|
"成员角色",
|
|
|
|
|
widget=Select2Widget(),
|
|
|
|
|
default='dev',
|
|
|
|
|
choices=[[x, x] for x in ['dev', 'ops','creator']],
|
|
|
|
|
description='只有creator可以添加修改组成员,可以添加多个creator',
|
|
|
|
|
validators=[DataRequired()]
|
2021-08-17 17:00:34 +08:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
edit_form_extra_fields = add_form_extra_fields
|
|
|
|
|
|
|
|
|
|
# @pysnooper.snoop()
|
|
|
|
|
def pre_add(self, item):
|
|
|
|
|
user_roles = [role.name.lower() for role in list(get_user_roles())]
|
|
|
|
|
if "admin" in user_roles:
|
|
|
|
|
return
|
|
|
|
|
if not g.user.username in item.project.get_creators():
|
|
|
|
|
raise MyappException('just creator can add/edit user')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pre_update(self, item):
|
|
|
|
|
user_roles = [role.name.lower() for role in list(get_user_roles())]
|
|
|
|
|
if "admin" in user_roles:
|
|
|
|
|
return
|
|
|
|
|
if not g.user.username in item.project.get_creators():
|
|
|
|
|
raise MyappException('just creator can add/edit user')
|
|
|
|
|
|
|
|
|
|
class Project_User_ModelView(Project_User_ModelView_Base,CompactCRUDMixin, MyappModelView):
|
|
|
|
|
datamodel = SQLAInterface(Project_User)
|
|
|
|
|
|
|
|
|
|
appbuilder.add_view_no_menu(Project_User_ModelView)
|
|
|
|
|
|
2022-08-08 20:11:53 +08:00
|
|
|
|
|
2021-08-17 17:00:34 +08:00
|
|
|
|
class Project_User_ModelView_Api(Project_User_ModelView_Base,MyappModelRestApi):
|
|
|
|
|
datamodel = SQLAInterface(Project_User)
|
|
|
|
|
route_base = '/project_user_modelview/api'
|
2022-08-02 16:02:22 +08:00
|
|
|
|
# add_columns = ['user', 'role']
|
|
|
|
|
add_columns = ['project', 'user', 'role']
|
|
|
|
|
edit_columns = add_columns
|
|
|
|
|
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
|
|
|
|
appbuilder.add_api(Project_User_ModelView_Api)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-02-26 22:36:57 +08:00
|
|
|
|
# 获取某类project分组
|
2021-08-17 17:00:34 +08:00
|
|
|
|
class Project_Filter(MyappFilter):
|
|
|
|
|
# @pysnooper.snoop()
|
|
|
|
|
def apply(self, query, value):
|
|
|
|
|
# user_roles = [role.name.lower() for role in list(get_user_roles())]
|
|
|
|
|
# if "admin" in user_roles:
|
|
|
|
|
# return query.filter(Project.type == value).order_by(Project.id.desc())
|
|
|
|
|
return query.filter(self.model.type==value).order_by(self.model.id.desc())
|
|
|
|
|
|
|
|
|
|
|
2021-09-07 18:09:47 +08:00
|
|
|
|
|
2022-02-26 22:36:57 +08:00
|
|
|
|
# 获取自己参加的某类project分组
|
2021-09-07 18:09:47 +08:00
|
|
|
|
class Project_Join_Filter(MyappFilter):
|
|
|
|
|
# @pysnooper.snoop()
|
|
|
|
|
def apply(self, query, value):
|
|
|
|
|
if g.user.is_admin():
|
|
|
|
|
return query.filter(self.model.type == value).order_by(self.model.id.desc())
|
|
|
|
|
join_projects_id = security_manager.get_join_projects_id(db.session)
|
|
|
|
|
return query.filter(self.model.id.in_(join_projects_id)).filter(self.model.type==value).order_by(self.model.id.desc())
|
|
|
|
|
|
2022-02-26 22:36:57 +08:00
|
|
|
|
|
|
|
|
|
|
2022-08-08 20:11:53 +08:00
|
|
|
|
# query joined project
|
2021-09-07 18:09:47 +08:00
|
|
|
|
def filter_join_org_project():
|
|
|
|
|
query = db.session.query(Project)
|
2022-02-26 22:36:57 +08:00
|
|
|
|
# user_roles = [role.name.lower() for role in list(get_user_roles())]
|
|
|
|
|
# if "admin" in user_roles:
|
|
|
|
|
if g.user.is_admin():
|
2021-09-07 18:09:47 +08:00
|
|
|
|
return query.filter(Project.type=='org').order_by(Project.id.desc())
|
|
|
|
|
|
|
|
|
|
my_user_id = g.user.get_id() if g.user else 0
|
|
|
|
|
owner_ids_query = db.session.query(Project_User.project_id).filter(Project_User.user_id == my_user_id)
|
|
|
|
|
|
|
|
|
|
return query.filter(Project.id.in_(owner_ids_query)).filter(Project.type=='org').order_by(Project.id.desc())
|
|
|
|
|
|
|
|
|
|
|
2021-08-17 17:00:34 +08:00
|
|
|
|
class Project_ModelView_Base():
|
|
|
|
|
label_title='项目组'
|
|
|
|
|
datamodel = SQLAInterface(Project)
|
2022-08-08 20:11:53 +08:00
|
|
|
|
base_permissions = ['can_add', 'can_edit', 'can_delete', 'can_list', 'can_show']
|
2021-08-17 17:00:34 +08:00
|
|
|
|
base_order = ('id', 'desc')
|
|
|
|
|
list_columns = ['name','user','type']
|
2022-08-02 16:02:22 +08:00
|
|
|
|
cols_width = {
|
|
|
|
|
"name":{"type": "ellip2", "width": 200},
|
|
|
|
|
"user": {"type": "ellip3", "width": 700},
|
|
|
|
|
"type": {"type": "ellip2", "width": 200},
|
|
|
|
|
}
|
2021-08-17 17:00:34 +08:00
|
|
|
|
related_views = [Project_User_ModelView,]
|
2022-08-02 16:02:22 +08:00
|
|
|
|
add_columns = ['name','describe','expand']
|
2021-08-17 17:00:34 +08:00
|
|
|
|
edit_columns = add_columns
|
2023-04-06 23:04:34 +08:00
|
|
|
|
project_type = 'org'
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
|
|
|
|
# @pysnooper.snoop()
|
2022-11-23 17:01:10 +08:00
|
|
|
|
def pre_add_web(self):
|
2021-08-17 17:00:34 +08:00
|
|
|
|
self.edit_form_extra_fields['type'] = StringField(
|
|
|
|
|
_(self.datamodel.obj.lab('type')),
|
|
|
|
|
description="项目分组",
|
|
|
|
|
widget=MyBS3TextFieldWidget(value=self.project_type,readonly=1),
|
|
|
|
|
default=self.project_type,
|
|
|
|
|
)
|
|
|
|
|
self.add_form_extra_fields = self.edit_form_extra_fields
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pre_update(self, item):
|
2022-08-02 16:02:22 +08:00
|
|
|
|
if not item.type:
|
|
|
|
|
item.type = self.project_type
|
2021-08-17 17:00:34 +08:00
|
|
|
|
if item.expand:
|
|
|
|
|
core.validate_json(item.expand)
|
|
|
|
|
item.expand = json.dumps(json.loads(item.expand),indent=4,ensure_ascii=False)
|
|
|
|
|
user_roles = [role.name.lower() for role in list(get_user_roles())]
|
|
|
|
|
if "admin" in user_roles:
|
|
|
|
|
return
|
|
|
|
|
if not g.user.username in item.get_creators():
|
|
|
|
|
raise MyappException('just creator can add/edit')
|
|
|
|
|
|
2022-08-08 20:11:53 +08:00
|
|
|
|
# before update, check permission
|
2022-11-23 17:01:10 +08:00
|
|
|
|
def pre_update_web(self, item):
|
|
|
|
|
self.pre_add_web()
|
2022-08-02 16:02:22 +08:00
|
|
|
|
self.check_item_permissions(item)
|
|
|
|
|
if not self.user_permissions['edit']:
|
2021-08-17 17:00:34 +08:00
|
|
|
|
flash('just creator can add/edit user','warning')
|
|
|
|
|
raise MyappException('just creator can add/edit user')
|
|
|
|
|
|
2022-08-08 20:11:53 +08:00
|
|
|
|
# check permission
|
2022-08-02 16:02:22 +08:00
|
|
|
|
def check_item_permissions(self,item):
|
|
|
|
|
if g.user.is_admin() or g.user.username in item.get_creators():
|
|
|
|
|
self.user_permissions = {
|
|
|
|
|
"add": True,
|
|
|
|
|
"edit": True,
|
|
|
|
|
"delete": True,
|
|
|
|
|
"show": True
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
self.user_permissions = {
|
|
|
|
|
"add": True,
|
|
|
|
|
"edit": False,
|
|
|
|
|
"delete": False,
|
|
|
|
|
"show": True
|
|
|
|
|
}
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-08-08 20:11:53 +08:00
|
|
|
|
# add project user
|
2021-08-17 17:00:34 +08:00
|
|
|
|
def post_add(self, item):
|
2022-08-02 16:02:22 +08:00
|
|
|
|
if not item.type:
|
|
|
|
|
item.type = self.project_type
|
2021-08-17 17:00:34 +08:00
|
|
|
|
creator = Project_User(role='creator',user=g.user,project=item)
|
|
|
|
|
db.session.add(creator)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# @pysnooper.snoop()
|
|
|
|
|
def post_list(self,items):
|
|
|
|
|
return core.sort_expand_index(items,db.session)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-08-02 16:02:22 +08:00
|
|
|
|
# class Project_ModelView_job_template(Project_ModelView_Base,MyappModelView):
|
|
|
|
|
# project_type = 'job-template'
|
2022-08-08 20:11:53 +08:00
|
|
|
|
# base_filters = [["id", Project_Filter, project_type]]
|
2022-08-02 16:02:22 +08:00
|
|
|
|
# datamodel = SQLAInterface(Project)
|
|
|
|
|
# label_title = '模板分类'
|
|
|
|
|
#
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
|
|
|
|
|
2021-09-07 18:09:47 +08:00
|
|
|
|
class Project_ModelView_job_template_Api(Project_ModelView_Base,MyappModelRestApi):
|
|
|
|
|
route_base = '/project_modelview/job_template/api'
|
|
|
|
|
datamodel = SQLAInterface(Project)
|
|
|
|
|
project_type = 'job-template'
|
2022-08-08 20:11:53 +08:00
|
|
|
|
base_filters = [["id", Project_Filter, project_type]]
|
2022-08-02 16:02:22 +08:00
|
|
|
|
related_views = [Project_User_ModelView_Api, ]
|
|
|
|
|
label_title = '模板分类'
|
|
|
|
|
edit_form_extra_fields={
|
|
|
|
|
'type': StringField(
|
|
|
|
|
_(datamodel.obj.lab('type')),
|
|
|
|
|
description="模板分类",
|
|
|
|
|
widget=MyBS3TextFieldWidget(value=project_type, readonly=1),
|
|
|
|
|
default=project_type,
|
|
|
|
|
),
|
|
|
|
|
'expand': StringField(
|
|
|
|
|
_(datamodel.obj.lab('expand')),
|
|
|
|
|
description='扩展参数。示例参数:<br>"index": 0 表示在pipeline编排中的模板列表的排序位置',
|
|
|
|
|
widget=MyBS3TextAreaFieldWidget(),
|
|
|
|
|
default='{}',
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
add_form_extra_fields=edit_form_extra_fields
|
2021-09-07 18:09:47 +08:00
|
|
|
|
|
|
|
|
|
appbuilder.add_api(Project_ModelView_job_template_Api)
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
|
|
|
|
|
2022-08-02 16:02:22 +08:00
|
|
|
|
# class Project_ModelView_org(Project_ModelView_Base,MyappModelView):
|
|
|
|
|
# project_type='org'
|
2022-08-08 20:11:53 +08:00
|
|
|
|
# base_filters = [["id", Project_Filter, project_type]]
|
2022-08-02 16:02:22 +08:00
|
|
|
|
# datamodel = SQLAInterface(Project)
|
|
|
|
|
# label_title = '项目分组'
|
|
|
|
|
#
|
|
|
|
|
#
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
2022-08-08 20:11:53 +08:00
|
|
|
|
|
2021-09-07 18:09:47 +08:00
|
|
|
|
class Project_ModelView_org_Api(Project_ModelView_Base,MyappModelRestApi):
|
|
|
|
|
route_base = '/project_modelview/org/api'
|
|
|
|
|
datamodel = SQLAInterface(Project)
|
|
|
|
|
project_type = 'org'
|
2022-08-08 20:11:53 +08:00
|
|
|
|
base_filters = [["id", Project_Filter, project_type]]
|
2022-08-02 16:02:22 +08:00
|
|
|
|
related_views = [Project_User_ModelView_Api, ]
|
|
|
|
|
label_title = '项目分组'
|
|
|
|
|
edit_form_extra_fields={
|
|
|
|
|
'type': StringField(
|
|
|
|
|
_(datamodel.obj.lab('type')),
|
|
|
|
|
description="项目分组",
|
|
|
|
|
widget=MyBS3TextFieldWidget(value=project_type, readonly=1),
|
|
|
|
|
default=project_type,
|
|
|
|
|
),
|
|
|
|
|
'expand': StringField(
|
|
|
|
|
_(datamodel.obj.lab('expand')),
|
2023-04-06 23:04:34 +08:00
|
|
|
|
description='扩展参数。示例参数:<br>"cluster": "dev"<br>"org": "public"<br>"volume_mount": "kubeflow-user-workspace(pvc):/mnt/;/data/k8s/../group1(hostpath):/mnt1"<br>"SERVICE_EXTERNAL_IP":"xx.内网.xx.xx|xx.公网.xx.xx"',
|
2022-08-02 16:02:22 +08:00
|
|
|
|
widget=MyBS3TextAreaFieldWidget(),
|
2022-08-04 14:58:08 +08:00
|
|
|
|
default=json.dumps({"cluster": "dev"},indent=4,ensure_ascii=False),
|
2022-08-02 16:02:22 +08:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
add_form_extra_fields=edit_form_extra_fields
|
2021-09-07 18:09:47 +08:00
|
|
|
|
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
2022-08-02 16:02:22 +08:00
|
|
|
|
appbuilder.add_api(Project_ModelView_org_Api)
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
2022-05-31 14:16:55 +08:00
|
|
|
|
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
2022-08-02 16:02:22 +08:00
|
|
|
|
# class Project_ModelView_train_model(Project_ModelView_Base,MyappModelView):
|
|
|
|
|
# project_type = 'model'
|
2022-08-08 20:11:53 +08:00
|
|
|
|
# base_filters = [["id", Project_Filter, project_type]]
|
2022-08-02 16:02:22 +08:00
|
|
|
|
# datamodel = SQLAInterface(Project)
|
|
|
|
|
# label_title = '模型分组'
|
|
|
|
|
#
|
|
|
|
|
#
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
2022-08-08 20:11:53 +08:00
|
|
|
|
|
2021-09-07 18:09:47 +08:00
|
|
|
|
class Project_ModelView_train_model_Api(Project_ModelView_Base,MyappModelRestApi):
|
|
|
|
|
route_base = '/project_modelview/model/api'
|
|
|
|
|
datamodel = SQLAInterface(Project)
|
|
|
|
|
project_type = 'model'
|
2022-08-02 16:02:22 +08:00
|
|
|
|
label_title = '模型分组'
|
2022-08-08 20:11:53 +08:00
|
|
|
|
base_filters = [["id", Project_Filter, project_type]]
|
2022-08-02 16:02:22 +08:00
|
|
|
|
related_views = [Project_User_ModelView_Api, ]
|
|
|
|
|
edit_form_extra_fields={
|
|
|
|
|
'type': StringField(
|
|
|
|
|
_(datamodel.obj.lab('type')),
|
|
|
|
|
description="模型分组",
|
|
|
|
|
widget=MyBS3TextFieldWidget(value=project_type, readonly=1),
|
|
|
|
|
default=project_type,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
add_form_extra_fields=edit_form_extra_fields
|
2021-09-07 18:09:47 +08:00
|
|
|
|
|
|
|
|
|
|
2022-08-02 16:02:22 +08:00
|
|
|
|
appbuilder.add_api(Project_ModelView_train_model_Api)
|
2021-09-07 18:09:47 +08:00
|
|
|
|
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Project_ModelView_Api(Project_ModelView_Base,MyappModelRestApi):
|
|
|
|
|
datamodel = SQLAInterface(Project)
|
|
|
|
|
route_base = '/project_modelview/api'
|
2022-08-02 16:02:22 +08:00
|
|
|
|
related_views=[Project_User_ModelView_Api,]
|
2021-08-17 17:00:34 +08:00
|
|
|
|
|
|
|
|
|
appbuilder.add_api(Project_ModelView_Api)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|