Browse Source

模板镜像分离完毕

James Iter 8 năm trước cách đây
mục cha
commit
24ac2005b8

+ 96 - 1
api/os_template_image.py

@@ -2,7 +2,10 @@
 # -*- coding: utf-8 -*-
 
 
-from flask import Blueprint
+from math import ceil
+
+import requests
+from flask import Blueprint, url_for
 from flask import request
 import jimit as ji
 import json
@@ -295,3 +298,95 @@ def r_content_search():
             ret['data'][i]['guests'] = os_template_image_guest_mapping[os_template_image['id']]
 
     return ret
+
+
+@Utils.dumps2response
+def r_show():
+    args = list()
+    page = int(request.args.get('page', 1))
+    page_size = int(request.args.get('page_size', 1000))
+    keyword = request.args.get('keyword', None)
+    order_by = request.args.get('order_by', None)
+    order = request.args.get('order', None)
+
+    if page is not None:
+        args.append('page=' + page.__str__())
+
+    if page_size is not None:
+        args.append('page_size=' + page_size.__str__())
+
+    if keyword is not None:
+        args.append('keyword=' + keyword.__str__())
+
+    if order_by is not None:
+        args.append('order_by=' + order_by)
+
+    if order is not None:
+        args.append('order=' + order)
+
+    os_templates_image_url = url_for('api_os_templates_image.r_get_by_filter', _external=True)
+    if keyword is not None:
+        os_templates_image_url = url_for('api_os_templates_image.r_content_search', _external=True)
+
+    if args.__len__() > 0:
+        os_templates_image_url = os_templates_image_url + '?' + '&'.join(args)
+
+    os_templates_image_ret = requests.get(url=os_templates_image_url, cookies=request.cookies)
+    os_templates_image_ret = json.loads(os_templates_image_ret.content)
+
+    public_count = 0
+    custom_count = 0
+
+    for os_template_image in os_templates_image_ret['data']:
+        if os_template_image['kind'] == 0:
+            public_count += 1
+
+        elif os_template_image['kind'] == 1:
+            custom_count += 1
+
+    os_templates_profile, _ = OSTemplateProfile.get_by_filter()
+
+    os_templates_profile_mapping_by_id = dict()
+    for os_template_profile in os_templates_profile:
+        os_templates_profile_mapping_by_id[os_template_profile['id']] = os_template_profile
+
+    last_page = int(ceil(os_templates_image_ret['paging']['total'] / float(page_size)))
+    page_length = 5
+    pages = list()
+    if page < int(ceil(page_length / 2.0)):
+        for i in range(1, page_length + 1):
+            pages.append(i)
+            if i == last_page or last_page == 0:
+                break
+
+    elif last_page - page < page_length / 2:
+        for i in range(last_page - page_length + 1, last_page + 1):
+            if i < 1:
+                continue
+            pages.append(i)
+
+    else:
+        for i in range(page - page_length / 2, page + int(ceil(page_length / 2.0))):
+            pages.append(i)
+            if i == last_page or last_page == 0:
+                break
+
+    ret = dict()
+    ret['state'] = ji.Common.exchange_state(20000)
+
+    ret['data'] = {
+        'os_templates_image': os_templates_image_ret['data'],
+        'os_templates_profile_mapping_by_id': os_templates_profile_mapping_by_id,
+        'page': page,
+        'page_size': page_size,
+        'keyword': keyword,
+        'pages': pages,
+        'last_page': last_page,
+        'order_by': order_by,
+        'order': order,
+        'public_count': public_count,
+        'custom_count': custom_count
+    }
+
+    return ret
+

+ 1 - 0
api_route_table.py

@@ -51,6 +51,7 @@ add_rule_api(os_template_image.blueprints, '/<ids>', api_func='os_template_image
 add_rule_api(os_template_image.blueprints, '/<ids>', api_func='os_template_image.r_get', methods=['GET'])
 add_rule_api(os_template_image.blueprints, '', api_func='os_template_image.r_get_by_filter', methods=['GET'])
 add_rule_api(os_template_image.blueprints, '/_search', api_func='os_template_image.r_content_search', methods=['GET'])
+add_rule_api(os_template_image.blueprints, '/_show', api_func='os_template_image.r_show', methods=['GET'])
 
 # 系统模板描述文件操作
 add_rule_api(os_template_profile.blueprint, '', api_func='os_template_profile.r_create', methods=['POST'])

+ 43 - 7
templates/os_templates_image_show.html

@@ -212,11 +212,47 @@
             if (data.fv.getInvalidFields().length > 0) {    // There is invalid field
                 data.fv.disableSubmitButtons(true);
             }
+        }).on('success.form.fv', function (e, data) {
+            create();
         });
 
         nav_to_sheet_tag();
     });
 
+    function create() {
+        var data = {
+            label: $('#label').val(),
+            description: $('#description').val(),
+            path: $('#path').val(),
+            logo: $('#logo').val(),
+            active: $('#active').val(),
+            os_template_profile_id: parseInt($('#os_template_profile_id').val()),
+            kind: 0
+        };
+
+        var go_back_url = '/os_templates_image';
+
+        $.ajax({
+            url : '/api/os_template_image',
+            type : 'POST',
+            contentType: "application/json; charset=utf-8",
+            async: false,
+            dataType: "json",
+            data : JSON.stringify(data),
+            error : function(data, textStatus, xhr) {
+                alter_warning('创建模板镜像失败!');
+                alter_danger(data.responseText);
+            },
+            success : function(data, textStatus, xhr) {
+                alter_success('您所提交的模板镜像已创建。页面将在3秒钟后自动跳转到模板列表页面!');
+            }
+        });
+
+        setTimeout(function() {
+            window.location.href=go_back_url;
+        }, 3000);
+    }
+
     function refresh() {
         if (keyword !== undefined && keyword.length > 0) {
             cur_url = resource_path + '?keyword=' + keyword + '&r=' + new RandomPassword().create(16);
@@ -560,7 +596,7 @@
                 </tr>
                 </thead>
                 <tbody>
-                {% for item in os_templates_image_ret.data if item.kind == 0 %}
+                {% for item in os_templates_image if item.kind == 0 %}
                     <tr role="row" class="odd" onmouseover="row_onmouseover(this);" onmouseout="row_onmouseout(this);">
                         <td style="display: none;">{{ item.id }}</td>
                         <td><input title="选中" type="checkbox"></td>
@@ -590,12 +626,12 @@
                             <div>
                                 <a href="javascript:;" data-toggle="modal" data-target="#update_logo_modal">
                                     {% if item.logo == "" %}
-                                        <span class="{{ os_templates_profile_mapping_by_id[item.os_template_profile_id].icon }}"></span>
+                                        <span class="{{ os_templates_profile_mapping_by_id[item.os_template_profile_id | string].icon }}"></span>
                                     {% else %}
                                         <span class="{{ item.logo }}"></span>
                                     {% endif %}
                                 </a>
-                                <p style="display: inline-block;">{{ os_templates_profile_mapping_by_id[item.os_template_profile_id].os_product_name }}</p>
+                                <p style="display: inline-block;">{{ os_templates_profile_mapping_by_id[item.os_template_profile_id | string].os_product_name }}</p>
                             </div>
                         </td>
                         <td>
@@ -683,7 +719,7 @@
                 </tr>
                 </thead>
                 <tbody>
-                {% for item in os_templates_image_ret.data if item.kind == 1 %}
+                {% for item in os_templates_image if item.kind == 1 %}
                     <tr role="row" class="odd" onmouseover="row_onmouseover(this);" onmouseout="row_onmouseout(this);">
                         <td style="display: none;">{{ item.id }}</td>
                         <td><input title="选中" type="checkbox"></td>
@@ -717,12 +753,12 @@
                             <div>
                                 <a href="javascript:;" data-toggle="modal" data-target="#update_logo_modal">
                                     {% if item.logo == "" %}
-                                        <span class="{{ os_templates_profile_mapping_by_id[item.os_template_profile_id].icon }}"></span>
+                                        <span class="{{ os_templates_profile_mapping_by_id[item.os_template_profile_id | string].icon }}"></span>
                                     {% else %}
                                         <span class="{{ item.logo }}"></span>
                                     {% endif %}
                                 </a>
-                                <p style="display: inline-block;">{{ os_templates_profile_mapping_by_id[item.os_template_profile_id].os_product_name }}</p>
+                                <p style="display: inline-block;">{{ os_templates_profile_mapping_by_id[item.os_template_profile_id | string].os_product_name }}</p>
                             </div>
                         </td>
                         <td>
@@ -849,7 +885,7 @@
             </div>
             <div class="modal-body" style="padding-top: 0; padding-bottom: 0;">
                 <div class="example-box-wrapper">
-                    <form id="add_os_template_image_form" class="form-horizontal bordered-row" action="/os_template_image" method="post">
+                    <form id="add_os_template_image_form" class="form-horizontal bordered-row" action="javascript:;">
                         <input id="active" name="active" value="1" type="hidden" title="激活" class="form-control">
                         <div class="form-group">
                             <div class="col-sm-2"></div>

+ 15 - 114
views/os_template_image.py

@@ -3,9 +3,8 @@
 
 
 import json
-from flask import Blueprint, render_template, url_for, request, redirect
+from flask import Blueprint, render_template, url_for, request
 import requests
-from math import ceil
 
 
 __author__ = 'James Iter'
@@ -28,121 +27,23 @@ blueprints = Blueprint(
 
 
 def show():
-    args = list()
-    page = int(request.args.get('page', 1))
-    page_size = int(request.args.get('page_size', 1000))
-    keyword = request.args.get('keyword', None)
-    order_by = request.args.get('order_by', None)
-    order = request.args.get('order', None)
+    url = url_for('api_os_templates_image.r_show', _external=True)
+    if request.args.__len__() >= 1:
+        args = list()
 
-    if page is not None:
-        args.append('page=' + page.__str__())
+        for k, v in request.args.items():
+            args.append('='.join([k, v]))
 
-    if page_size is not None:
-        args.append('page_size=' + page_size.__str__())
+        url += '?' + '&'.join(args)
 
-    if keyword is not None:
-        args.append('keyword=' + keyword.__str__())
+    ret = requests.get(url=url, cookies=request.cookies)
+    ret = json.loads(ret.content)
 
-    if order_by is not None:
-        args.append('order_by=' + order_by)
+    return render_template('os_templates_image_show.html', os_templates_image=ret['data']['os_templates_image'],
+                           os_templates_profile_mapping_by_id=ret['data']['os_templates_profile_mapping_by_id'],
+                           page=ret['data']['page'], page_size=ret['data']['page_size'], keyword=ret['data']['keyword'],
+                           pages=ret['data']['pages'], order_by=ret['data']['order_by'], order=ret['data']['order'],
+                           last_page=ret['data']['last_page'], public_count=ret['data']['public_count'],
+                           custom_count=ret['data']['custom_count'])
 
-    if order is not None:
-        args.append('order=' + order)
 
-    host_url = request.host_url.rstrip('/')
-
-    os_templates_image_url = host_url + url_for('api_os_templates_image.r_get_by_filter')
-    if keyword is not None:
-        os_templates_image_url = host_url + url_for('api_os_templates_image.r_content_search')
-
-    os_templates_profile_url = host_url + url_for('api_os_templates_profile.r_get_by_filter')
-
-    if args.__len__() > 0:
-        os_templates_image_url = os_templates_image_url + '?' + '&'.join(args)
-
-    os_templates_image_ret = requests.get(url=os_templates_image_url, cookies=request.cookies)
-    os_templates_image_ret = json.loads(os_templates_image_ret.content)
-
-    public_count = 0
-    custom_count = 0
-
-    for os_template_image in os_templates_image_ret['data']:
-        if os_template_image['kind'] == 0:
-            public_count += 1
-
-        elif os_template_image['kind'] == 1:
-            custom_count += 1
-
-    os_templates_profile_ret = requests.get(url=os_templates_profile_url, cookies=request.cookies)
-    os_templates_profile_ret = json.loads(os_templates_profile_ret.content)
-    os_templates_profile_mapping_by_id = dict()
-    for os_template_profile in os_templates_profile_ret['data']:
-        os_templates_profile_mapping_by_id[os_template_profile['id']] = os_template_profile
-
-    last_page = int(ceil(os_templates_image_ret['paging']['total'] / float(page_size)))
-    page_length = 5
-    pages = list()
-    if page < int(ceil(page_length / 2.0)):
-        for i in range(1, page_length + 1):
-            pages.append(i)
-            if i == last_page or last_page == 0:
-                break
-
-    elif last_page - page < page_length / 2:
-        for i in range(last_page - page_length + 1, last_page + 1):
-            if i < 1:
-                continue
-            pages.append(i)
-
-    else:
-        for i in range(page - page_length / 2, page + int(ceil(page_length / 2.0))):
-            pages.append(i)
-            if i == last_page or last_page == 0:
-                break
-
-    return render_template('os_templates_image_show.html', os_templates_image_ret=os_templates_image_ret,
-                           os_templates_profile_mapping_by_id=os_templates_profile_mapping_by_id,
-                           page=page, page_size=page_size, keyword=keyword, pages=pages, order_by=order_by, order=order,
-                           last_page=last_page, public_count=public_count, custom_count=custom_count)
-
-
-def create():
-    host_url = request.host_url.rstrip('/')
-
-    if request.method == 'POST':
-        label = request.form.get('label')
-        description = request.form.get('description', '')
-        path = request.form.get('path')
-        logo = request.form.get('logo')
-        active = request.form.get('active', 1)
-        os_template_profile_id = request.form.get('os_template_profile_id')
-        kind = request.form.get('kind', 0)
-
-        payload = {
-            "label": label,
-            "description": description,
-            "path": path,
-            "logo": logo,
-            "active": active,
-            "os_template_profile_id": int(os_template_profile_id),
-            "kind": int(kind)
-        }
-
-        url = host_url + '/api/os_template_image'
-        headers = {'content-type': 'application/json'}
-        r = requests.post(url, data=json.dumps(payload), headers=headers, cookies=request.cookies)
-        j_r = json.loads(r.content)
-        if j_r['state']['code'] != '200':
-            return render_template('failure.html',
-                                   go_back_url='/os_templates_image',
-                                   timeout=10000, title='创建失败',
-                                   message_title='创建模板镜像失败',
-                                   message=j_r['state']['sub']['zh-cn'])
-
-        return render_template('success.html', go_back_url='/os_templates_image', timeout=10000, title='提交成功',
-                               message_title='添加模板镜像的请求已被接受',
-                               message='您所提交的模板镜像已创建。页面将在10秒钟后自动跳转到模板列表页面!')
-
-    else:
-        return redirect(url_for('v_os_templates_image.show'))

+ 0 - 1
views_route_table.py

@@ -43,7 +43,6 @@ add_rule_views(disk.blueprint, '/detail/<uuid>', views_func='disk.detail', metho
 add_rule_views(log.blueprints, '', views_func='log.show', methods=['GET'])
 
 add_rule_views(os_template_image.blueprints, '', views_func='os_template_image.show', methods=['GET'])
-add_rule_views(os_template_image.blueprint, '', views_func='os_template_image.create', methods=['POST'])
 
 add_rule_views(host.blueprints, '', views_func='host.show', methods=['GET'])
 add_rule_views(host.blueprint, '/detail/<node_id>', views_func='host.detail', methods=['GET'])