from flask_appbuilder.models.sqla.interface import SQLAInterface from flask_babel import gettext as __ from flask_babel import lazy_gettext as _ from sqlalchemy import or_ from flask_appbuilder.actions import action from wtforms.validators import DataRequired from myapp import app, appbuilder from wtforms import StringField, SelectField from flask_appbuilder.fieldwidgets import BS3TextFieldWidget, Select2Widget from myapp.forms import MyBS3TextAreaFieldWidget,MySelect2Widget from .baseApi import ( MyappModelRestApi ) from flask import ( abort, flash, g ) from .base import ( MyappFilter, ) from myapp.models.model_metadata_metric import Metadata_metric conf = app.config logging = app.logger class Metadata_Metrics_table_Filter(MyappFilter): # @pysnooper.snoop() def apply(self, query, func): if g.user.is_admin(): return query # 公开的,或者责任人包含自己的 return query.filter( or_( self.model.public==True, self.model.metric_responsible.contains(g.user.username) ) ) class Metadata_metric_ModelView_base(): label_title='指标' datamodel = SQLAInterface(Metadata_metric) base_permissions = ['can_add','can_show','can_edit','can_list','can_delete'] base_order = ("id", "desc") order_columns = ['id'] search_columns=['metric_data_type','metric_responsible','app','name','label','describe','metric_type','metric_level','task_id','caliber'] show_columns=['id','app','metric_data_type','name','label','describe','metric_type','metric_level','metric_dim','metric_responsible','caliber','task_id','public'] list_columns = ['app','metric_data_type','name','label','describe','metric_level','metric_responsible','public','metric_type','task_id'] cols_width = { "name":{"type": "ellip2", "width": 200}, "label": {"type": "ellip2", "width": 200}, "describe": {"type": "ellip2", "width": 400}, "metric_responsible": {"type": "ellip2", "width": 300} } spec_label_columns={ "name":"指标英文名", "label": "指标中文名", "describe":"指标描述", "metric_data_type":"指标模块", "task_id":"任务id" } add_columns = ['app','metric_data_type','name','label','describe','metric_type','metric_level','metric_dim','metric_responsible','caliber','task_id','public'] # show_columns = ['project','name','describe','config_html','dag_json_html','created_by','changed_by','created_on','changed_on','expand_html'] edit_columns = add_columns base_filters = [["id", Metadata_Metrics_table_Filter, lambda: []]] add_form_extra_fields = { "app": SelectField( label=_(datamodel.obj.lab('app')), description='产品', widget=MySelect2Widget(can_input=True,conten2choices=True), choices=[[x,x] for x in ['产品1',"产品2","产品3"]] ), "name":StringField( label=_(datamodel.obj.lab('name')), description='指标英文名', widget=BS3TextFieldWidget(), validators=[DataRequired()] ), "label": StringField( label=_(datamodel.obj.lab('label')), description='指标中文名', widget=BS3TextFieldWidget(), validators=[DataRequired()] ), "describe": StringField( label=_(datamodel.obj.lab('describe')), description='指标描述', widget=BS3TextFieldWidget(), validators=[DataRequired()] ), "metric_type": SelectField( label=_(datamodel.obj.lab('metric_type')), description='指标类型', default='原子指标', widget=Select2Widget(), choices=[[x,x] for x in ['原子指标', '衍生指标']] ), "metric_data_type": SelectField( label=_(datamodel.obj.lab('metric_data_type')), description='指标归属模块', widget=MySelect2Widget(can_input=True,conten2choices=True), choices=[[x,x] for x in ['模块1',"模块2","模块3"]] ), "metric_level":SelectField( label=_(datamodel.obj.lab('metric_level')), description='指标重要级别', default='普通', widget=Select2Widget(), choices=[[x,x] for x in ['普通','重要','核心']] ), "metric_dim": SelectField( label=_(datamodel.obj.lab('metric_dim')), description='指标维度1', default='天', widget=Select2Widget(), choices=[[x, x] for x in ['天', '周', '月']] ), "metric_responsible": StringField( label=_(datamodel.obj.lab('metric_responsible')), description='指标负责人,逗号分隔', widget=BS3TextFieldWidget(), validators=[DataRequired()] ), "status": SelectField( label=_(datamodel.obj.lab('status')), description='指标状态', widget=Select2Widget(), choices=[[x,x] for x in [ "下线","待审批",'创建中',"上线",]] ), "caliber": StringField( label=_(datamodel.obj.lab('caliber')), description='指标口径描述,代码和计算公式', widget=MyBS3TextAreaFieldWidget(rows=3), validators=[DataRequired()] ), "task_id": StringField( label=_(datamodel.obj.lab('task_id')), description='任务id', widget=BS3TextFieldWidget() ), } edit_form_extra_fields = add_form_extra_fields import_data=True download_data=True def pre_upload(self,data): data['public'] = bool(int(data.get('public', 1))) return data @action( "muldelete", __("Delete"), __("Delete all Really?"), "fa-trash", single=False ) def muldelete(self, items): if not items: abort(404) for item in items: try: if item.created_by.username==g.user.username: self.pre_delete(item) self.datamodel.delete(item, raise_exception=True) self.post_delete(item) except Exception as e: flash(str(e), "danger") class Metadata_metric_ModelView_Api(Metadata_metric_ModelView_base,MyappModelRestApi): datamodel = SQLAInterface(Metadata_metric) route_base = '/metadata_metric_modelview/api' label_title='指标' appbuilder.add_api(Metadata_metric_ModelView_Api)