@admin_bp.route('/template/create-from-zone', methods=['POST'])
@login_required
@operator_role_required
def create_template_from_zone():
    try:
        jdata = request.json
        name = jdata['name']
        description = jdata['description']
        domain_name = jdata['domain']
        if ((' ' in name) or (not name) or (not type)):
            return make_response(jsonify({'status': 'error', 'msg': 'Please correct template name'}), 400)
        if DomainTemplate.query.filter((DomainTemplate.name == name)).first():
            return make_response(jsonify({'status': 'error', 'msg': 'A template with the name {0} already exists!'.format(name)}), 409)
        t = DomainTemplate(name=name, description=description)
        result = t.create()
        if (result['status'] == 'ok'):
            history = History(msg='Add domain template {0}'.format(name), detail=json.dumps({'name': name, 'description': description}), created_by=current_user.username)
            history.add()
            records = []
            domain = Domain.query.filter((Domain.name == domain_name)).first()
            if domain:
                rrsets = Record().get_rrsets(domain.name)
                if rrsets:
                    for r in rrsets:
                        name = ('@' if (r['name'] == (domain_name + '.')) else r['name'].replace('.{}.'.format(domain_name), ''))
                        for record in r['records']:
                            t_record = DomainTemplateRecord(name=name, type=r['type'], status=(False if record['disabled'] else True), ttl=r['ttl'], data=record['content'])
                            records.append(t_record)
            result = t.replace_records(records)
            if (result['status'] == 'ok'):
                return make_response(jsonify({'status': 'ok', 'msg': result['msg']}), 200)
            else:
                t.delete_template()
                return make_response(jsonify({'status': 'error', 'msg': result['msg']}), 500)
        else:
            return make_response(jsonify({'status': 'error', 'msg': result['msg']}), 500)
    except Exception as e:
        current_app.logger.error('Cannot create template from zone. Error: {0}'.format(e))
        current_app.logger.debug(traceback.format_exc())
        return make_response(jsonify({'status': 'error', 'msg': 'Error when applying new changes'}), 500)
@admin_bp.route('/template/create-from-zone', methods=['POST'])
@login_required
@operator_role_required
def create_template_from_zone():
    try:
        jdata = request.json
        name = jdata['name']
        description = jdata['description']
        domain_name = jdata['domain']
        if ((' ' in name) or (not name) or (not type)):
            return make_response(jsonify({'status': 'error', 'msg': 'Please correct template name'}), 400)
        if DomainTemplate.query.filter((DomainTemplate.name == name)).first():
            return make_response(jsonify({'status': 'error', 'msg': 'A template with the name {0} already exists!'.format(name)}), 409)
        t = DomainTemplate(name=name, description=description)
        result = t.create()
        if (result['status'] == 'ok'):
            history = History(msg='Add domain template {0}'.format(name), detail=json.dumps({'name': name, 'description': description}), created_by=current_user.username)
            history.add()
            records = []
            domain = Domain.query.filter((Domain.name == domain_name)).first()
            if domain:
                rrsets = Record().get_rrsets(domain.name)
                if rrsets:
                    for r in rrsets:
                        name = ('@' if (r['name'] == (domain_name + '.')) else r['name'].replace('.{}.'.format(domain_name), ))
                        for record in r['records']:
                            t_record = DomainTemplateRecord(name=name, type=r['type'], status=(False if record['disabled'] else True), ttl=r['ttl'], data=record['content'])
                            records.append(t_record)
            result = t.replace_records(records)
            if (result['status'] == 'ok'):
                return make_response(jsonify({'status': 'ok', 'msg': result['msg']}), 200)
            else:
                t.delete_template()
                return make_response(jsonify({'status': 'error', 'msg': result['msg']}), 500)
        else:
            return make_response(jsonify({'status': 'error', 'msg': result['msg']}), 500)
    except Exception as e:
        current_app.logger.error('Cannot create template from zone. Error: {0}'.format(e))
        current_app.logger.debug(traceback.format_exc())
        return make_response(jsonify({'status': 'error', 'msg': 'Error when applying new changes'}), 500)