コード例 #1
0
ファイル: migrate.py プロジェクト: zhangbufan/cronmon
def sample():
    """插入样例数据"""
    Business.insert_many(business_data, fields=business_fields).execute()
    Notifier.insert_many(notifier_data, fields=notifier_fields).execute()
    BusinessNotifier.insert_many(businessnotifier_data, fields=businessnotifier_fields).execute()
    TaskMonitor.insert_many(taskmonitor_data, fields=taskmonitor_fields).execute()
    User.insert_many(user_data, fields=user_fields).execute()
    Permission.insert_many(permission_data, fields=permission_fields).execute()
コード例 #2
0
    def test_column_default_value(self):
        """Test column default value."""
        business = Business(business_name='biz2')
        business.save()
        assert business.status is True

        notifier = Notifier(notify_name='nfy2', notify_email='*****@*****.**', notify_tel='23912347777')
        notifier.save()
        assert notifier.status is True
コード例 #3
0
 def __init__(self, *args, **kwargs):
     super(TaskFormNew, self).__init__(*args, **kwargs)
     query_all = Business.select(
         Business.id, Business.business_name).where(Business.status == True)
     query = perm_check(query_all)
     self.business.choices = [(business.id, business.business_name)
                              for business in query]
コード例 #4
0
    def test_get_by_id(self):
        """Get business by ID."""
        business = Business(business_name='biz1', status=True)
        business.save()
        retrieved = Business.get_by_id(business.id)
        assert retrieved == business

        notifier = Notifier(notify_name='nfy1', notify_email='*****@*****.**', notify_tel='13912347777', status=True)
        notifier.save()
        retrieved = Notifier.get_by_id(notifier.id)
        assert retrieved == notifier

        business_notifer = BusinessNotifier(business=business.id, notifier=notifier.id)
        business_notifer.save()
        retrieved = BusinessNotifier.get_by_id(business_notifer.id)
        assert retrieved == business_notifer
コード例 #5
0
    def test_column_type(self):
        """Test columns type."""
        business = Business(business_name='biz3')
        business.save()
        assert isinstance(business.business_name, str)
        assert isinstance(business.status, bool)

        notifier = Notifier(notify_name='nfy3', notify_email='*****@*****.**', notify_tel='33912347777')
        notifier.save()
        assert isinstance(notifier.notify_name, str)
        assert isinstance(notifier.notify_email, str)
        assert isinstance(notifier.notify_tel, str)
        assert isinstance(notifier.status, bool)

        business_notifier = BusinessNotifier(business=business.id, notifier=notifier.id)
        business_notifier.save()
        assert isinstance(business_notifier.business, Business)
        assert isinstance(business_notifier.notifier, Notifier)
コード例 #6
0
    def __init__(self, *args, **kwargs):
        super(BusinessNotifierForm, self).__init__(*args, **kwargs)
        query_all = Business.select(Business.id, Business.business_name)
        query = perm_check(query_all)

        self.business.choices = [(business.id, business.business_name)
                                 for business in query]
        self.notifier.choices = [
            (notifier.id, notifier.notify_name)
            for notifier in Notifier.select(Notifier.id, Notifier.notify_name)
        ]
コード例 #7
0
ファイル: taskcyclecheck.py プロジェクト: zhangbufan/cronmon
def emptybusinesscheck():
    """获取没有联系人的业务,并同时发给系统管理员和对应的业务管理员"""

    # 获取空联系人业务信息,如果结果为空,则退出后续检查
    subq = BusinessNotifier.select().where(BusinessNotifier.business_id == Business.id)
    query1 = Business.select().where((~fn.EXISTS(subq)) & (Business.status == True))
    if not query1:
        return

    # 获取管理员列表
    query2 = User.select().where((User.admin == True) & (User.status == True))

    # 列表生成(系统管理员)
    # stridlist用作和perm_list进行交叉比对
    stridlist = []
    strlist = []
    infolist = []

    subject = 'Empty Business - SystemAdmin'

    for i in query1:
        biz = str(i.id) + ' : ' + i.business_name
        strlist.append(biz)
        bizid = str(i.id)
        stridlist.append(bizid)
    mailstring = "\n".join(strlist)

    for item in query2:
        subinfolist = []
        notifier = item.email
        subinfolist.append(notifier)
        subinfolist.append(mailstring)
        subinfolist.append(subject)
        infolist.append(subinfolist)

    # 发送告警信息给系统管理员
    send_email(infolist)

    # 列表生成(业务管理员)
    strlist = []
    infolist = []

    subject = 'Empty Business - BizAdmin'

    user_perm_list = Permission.select().join(User)\
        .where((Permission.perm_list != '0') & (Permission.perm_list != '') & (User.status == 1))
    for item in user_perm_list:
        user_perm_list_item = item.perm_list.split()
        # perm_list和无联系人业务id列表进行交叉比对
        business_intersection = [x for x in user_perm_list_item if x in set(stridlist)]
        if business_intersection:
            subinfolist = []
            notifier = item.perm_user.email
            subinfolist.append(notifier)
            # 根据id获取业务名称作为邮件正文
            for perm_item in business_intersection:
                biz = Business.select().where(Business.id == perm_item).get().business_name
                strlist.append(biz)
            mailstring = "\n".join(strlist)
            subinfolist.append(mailstring)
            subinfolist.append(subject)
            infolist.append(subinfolist)

    # 发送告警信息给业务管理员
    send_email(infolist)
コード例 #8
0
 def __init__(self, *args, **kwargs):
     super(PermissionBizForm, self).__init__(*args, **kwargs)
     query = Business.select(Business.id, Business.business_name)
     self.business.choices = [(business.id, business.business_name)
                              for business in query]
コード例 #9
0
def form_edit(db_model, form, template, form2=False):
    """通用编辑模版(新增和修改)

    :param db_model: 数据库model
    :param form: 表单
    :param template: 模版
    :param form2: 第二个表单,进行权限分离时需要用到
    :return: 渲染后的模版
    """

    # 获取参数已经生成编辑和新增路径
    action, id, bid, page, length, search_content, search_by, perm_list = get_parm(
    )
    redirect_path_edit = ('main.' + template.split('.')[0]).replace(
        'edit', 'list')
    redirect_path_add = ('main.' + template.split('.')[0])

    # 修改操作(id存在)
    if id:
        # 如果是指定model,则记录操作前对应字段值
        if db_model == Business or db_model == TaskMonitor:
            try:
                status_old = db_model.select().where(
                    db_model.id == id).get().status
            except:
                abort(500)
        if db_model == User:
            try:
                admin_old = db_model.select().where(
                    db_model.id == id).get().admin
            except:
                abort(500)
        # 权限检查
        if not (bid in perm_list or perm_list == ['0']):
            abort(403)
        # 模型转表单
        model = db_model.get(db_model.id == id)
        if request.method == 'GET':
            utils.model_to_form(model, form)
        # 提交修改
        if request.method == 'POST':
            try:
                if form.validate_on_submit():
                    # 如果业务状态为禁用,则不允许操作
                    if db_model == TaskMonitor or db_model == BusinessNotifier:
                        biz_status = Business.select().join(db_model).where(
                            Business.id == bid).get().status
                        if not biz_status:
                            flash('关联业务为禁用状态')
                            return redirect(url_for(redirect_path_edit))
                    # 如果是指定model,则记录表单提交前指定字段值
                    if db_model == Business or db_model == TaskMonitor:
                        status_new = form.status.data
                    if db_model == User:
                        admin_new = form.admin.data
                    # 提交数据
                    utils.form_to_model(form, model)
                    model.save()
                    # 如果业务状态从启用变为禁用,则对应的所有监控任务会被禁用
                    if db_model == Business:
                        if status_old != status_new and status_new is False:
                            toupdate = (TaskMonitor.update({
                                TaskMonitor.status:
                                False
                            }).where(TaskMonitor.business == id))
                            toupdate.execute()
                    # 如果监控任务状态从启用变为禁用,则对应的告警状态会被重置
                    if db_model == TaskMonitor:
                        if status_old != status_new and status_new is False:
                            toupdate = (TaskMonitor.update({
                                TaskMonitor.warning:
                                False
                            }).where(TaskMonitor.id == id))
                            toupdate.execute()
                    # 如果修改用户角色,则进行关联表相关操作
                    # 如果从业务管理员到系统管理员,则将perm_list修改为0
                    if db_model == User and admin_old != admin_new:
                        if admin_new is True:
                            toupdate = (Permission.update({
                                Permission.perm_list:
                                '0'
                            }).where(Permission.perm_user == id))
                            toupdate.execute()
                    # 如果从系统管理员到业务管理员,则将perm_list修改为空值
                        else:
                            toupdate = (Permission.update({
                                Permission.perm_list:
                                ''
                            }).where(Permission.perm_user == id))
                            toupdate.execute()
                    flash('修改成功')
                    return redirect(url_for(redirect_path_edit))
                else:
                    utils.flash_errors(form)
            except peewee.IntegrityError as e:
                flash(e)
    # 新增操作(id不存在)
    else:
        # 如果form2存在,则将form替换成form2,目前限于2种表单操作(业务联系人和任务表单)
        # 此类表单进行新增操作时,会进行‘status’是否为真的判断,在编辑操作时则不进行
        if form2:
            form = form2
        try:
            if form.validate_on_submit():
                model = db_model()
                utils.form_to_model(form, model)
                model.save()
                # 如果为用户model,则进行修改权限model操作,如果是超级管理员,则将perm_list更新为‘0’,否则保留默认值
                if db_model == User:
                    user_id = User.select().where(
                        User.username == form.username.data).get().id
                    toadd = Permission()
                    toadd.perm_user = user_id
                    if form.admin.data:
                        toadd.perm_list = '0'
                    toadd.save()
                flash('保存成功')
                return redirect(url_for(redirect_path_add))
            else:
                utils.flash_errors(form)
        except peewee.IntegrityError as e:
            flash(e)

    return render_template(template, form=form, current_user=current_user)