@admin_bp.route('/template/<path:template>/apply', methods=['POST'], strict_slashes=False)
@login_required
def apply_records(template):
    try:
        jdata = request.json
        records = []
        for j in jdata['records']:
            name = ('@' if (j['record_name'] in ['@', '']) else j['record_name'])
            type = j['record_type']
            data = j['record_data']
            comment = j['record_comment']
            status = (0 if (j['record_status'] == 'Disabled') else 1)
            ttl = (int(j['record_ttl']) if j['record_ttl'] else 3600)
            dtr = DomainTemplateRecord(name=name, type=type, data=data, comment=comment, status=status, ttl=ttl)
            records.append(dtr)
        t = DomainTemplate.query.filter((DomainTemplate.name == template)).first()
        result = t.replace_records(records)
        if (result['status'] == 'ok'):
            jdata.pop('_csrf_token', None)
            history = History(msg='Apply domain template record changes to domain template {0}'.format(template), detail=json.dumps(jdata), created_by=current_user.username)
            history.add()
            return make_response(jsonify(result), 200)
        else:
            return make_response(jsonify(result), 400)
    except Exception as e:
        current_app.logger.error('Cannot apply record changes to the template. 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/<path:template>/apply', methods=['POST'], strict_slashes=False)
@login_required
def apply_records(template):
    try:
        jdata = request.json
        records = []
        for j in jdata['records']:
            name = ('@' if (j['record_name'] in ['@', ]) else j['record_name'])
            type = j['record_type']
            data = j['record_data']
            comment = j['record_comment']
            status = (0 if (j['record_status'] == 'Disabled') else 1)
            ttl = (int(j['record_ttl']) if j['record_ttl'] else 3600)
            dtr = DomainTemplateRecord(name=name, type=type, data=data, comment=comment, status=status, ttl=ttl)
            records.append(dtr)
        t = DomainTemplate.query.filter((DomainTemplate.name == template)).first()
        result = t.replace_records(records)
        if (result['status'] == 'ok'):
            jdata.pop('_csrf_token', None)
            history = History(msg='Apply domain template record changes to domain template {0}'.format(template), detail=json.dumps(jdata), created_by=current_user.username)
            history.add()
            return make_response(jsonify(result), 200)
        else:
            return make_response(jsonify(result), 400)
    except Exception as e:
        current_app.logger.error('Cannot apply record changes to the template. Error: {0}'.format(e))
        current_app.logger.debug(traceback.format_exc())
        return make_response(jsonify({'status': 'error', 'msg': 'Error when applying new changes'}), 500)