Esempio n. 1
0
def create_rule(term_id):
    '''
    Add a rule to the database
    '''
    check_admin()

    term = Term.query.filter(Term.id == term_id).first_or_404()
    form = RuleForm()

    if form.validate_on_submit():
        rule = Rule()

        rule.name = form.name.data
        rule.identifier = form.identifier.data
        rule.description = form.description.data
        rule.notes = form.notes.data

        term.rules.append(rule)

        db.session.add(rule)
        db.session.commit()

        flash('You have successfully added the %s rule.' % rule.name)

        # Redirect to term page
        return redirect(url_for('main.show_term', selected_term=term.id))

    # Load term template
    return render_template('admin/rules/rule.html', action="Add", form=form)
Esempio n. 2
0
 def post(self):
     '''
     添加
     '''
     args = parse_base.parse_args()
     name = args.get('name')
     url = args.get('url')
     menu_id = args.get('menu_id')
     method = args.get('method')
     # 权限规则名称可以重复,但是URL&请求方法不能重复
     _data = Rule.query.filter_by(url=url, method=method,
                                  is_del='0').first()
     if _data:
         abort(RET.Forbidden, msg='权限规则已存在')
     model_data = Rule()
     model_data.name = name
     model_data.url = url
     model_data.menu_id = menu_id
     model_data.method = method
     model_data.last_editor = g.admin.username
     if model_data.add():
         data = {'status': RET.Created, 'msg': '添加成功', 'data': model_data}
         return marshal(data, sing_fields)
     abort(RET.BadRequest, msg='添加失败,请重试')