Explorar o código

实现创建虚拟机磁盘表单校验

James Iter %!s(int64=8) %!d(string=hai) anos
pai
achega
4d062e1314
Modificáronse 2 ficheiros con 53 adicións e 3 borrados
  1. 9 1
      api/disk.py
  2. 44 2
      templates/disk_create.html

+ 9 - 1
api/disk.py

@@ -97,7 +97,15 @@ def r_create():
             }
 
             if disk.on_host == 'shared_storage':
-                message['hostname'] = Guest.get_lightest_host()['hostname']
+                available_hosts = Guest.get_available_hosts()
+
+                if available_hosts.__len__() == 0:
+                    ret['state'] = ji.Common.exchange_state(50351)
+                    return ret
+
+                # 在可用计算节点中平均分配任务
+                chosen_host = available_hosts[quantity % available_hosts.__len__()]
+                message['hostname'] = chosen_host['hostname']
 
             Guest.emit_instruction(message=json.dumps(message, ensure_ascii=False))
 

+ 44 - 2
templates/disk_create.html

@@ -42,6 +42,48 @@
         {% else %}
             $('#on_host_form').remove();
         {% endif %}
+
+        $('#create_disk_form').formValidation({
+            framework: 'bootstrap4',
+            icon: {
+                valid: 'fa fa-check',
+                invalid: 'fa fa-times',
+                validating: 'fa fa-refresh'
+            },
+            // Since the Bootstrap Button hides the radio and checkbox
+            // We exclude the disabled elements only
+            excluded: ':disabled',
+            fields: {
+                size: {
+                    validators: {
+                        between: {
+                            min: 1,
+                            max: 10240
+                        }
+                    }
+                },
+                quantity: {
+                    validators: {
+                        between: {
+                            min: 1,
+                            max: 20
+                        }
+                    }
+                },
+                remark: {
+                    validators: {
+                        stringLength: {
+                            max: 64
+                        }
+                    }
+                }
+            }
+        })
+        .on('success.field.fv', function(e, data) {
+            if (data.fv.getInvalidFields().length > 0) {    // There is invalid field
+                data.fv.disableSubmitButtons(true);
+            }
+        })
     });
 
     $(function() { "use strict";
@@ -100,7 +142,7 @@
                 </span>
             </a>
             <div class="example-box-wrapper">
-                <form class="form-horizontal bordered-row" action="/disks/create" method="post">
+                <form id="create_disk_form" class="form-horizontal bordered-row" action="/disks/create" method="post">
                     <div class="form-group">
                         <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-hdd"></span>&nbsp;&nbsp;磁盘大小 (GB)</label>
                         <div class="col-sm-6">
@@ -135,7 +177,7 @@
                     <div class="form-group">
                         <label class="col-sm-2 control-label"></label>
                         <div class="col-sm-3 pull-right">
-                            <button class="btn btn-blue-alt" style="width: 180px; height: 40px; font-size: 16px;">创建</button>
+                            <button type="submit" class="btn btn-blue-alt" style="width: 180px; height: 40px; font-size: 16px;">创建</button>
                         </div>
                     </div>
                 </form>