Parcourir la source

Introduced ability to manually create Modules

Jeremy Stretch il y a 9 ans
Parent
commit
7428b83c90

+ 1 - 0
netbox/dcim/urls.py

@@ -142,6 +142,7 @@ urlpatterns = [
     url(r'^interfaces/(?P<pk>\d+)/delete/$', views.interface_delete, name='interface_delete'),
 
     # Modules
+    url(r'^devices/(?P<pk>\d+)/modules/add/$', views.module_add, name='module_add'),
     url(r'^modules/(?P<pk>\d+)/edit/$', views.module_edit, name='module_edit'),
     url(r'^modules/(?P<pk>\d+)/delete/$', views.module_delete, name='module_delete'),
 

+ 25 - 0
netbox/dcim/views.py

@@ -1503,6 +1503,31 @@ def ipaddress_assign(request, pk):
 # Modules
 #
 
+@permission_required('dcim.add_module')
+def module_add(request, pk):
+
+    device = get_object_or_404(Device, pk=pk)
+
+    if request.method == 'POST':
+        form = forms.ModuleForm(request.POST)
+        if form.is_valid():
+            module = form.save(commit=False)
+            module.device = device
+            module.save()
+            messages.success(request, "Added module {} to {}".format(module.name, module.device.name))
+            return redirect('dcim:device_inventory', pk=module.device.pk)
+
+    else:
+        form = forms.ModuleForm()
+
+    return render(request, 'dcim/module_edit.html', {
+        'device': device,
+        'form': form,
+        'cancel_url': reverse('dcim:device_inventory', kwargs={'pk': device.pk}),
+    })
+
+
+
 @permission_required('dcim.change_module')
 def module_edit(request, pk):
 

+ 11 - 5
netbox/templates/dcim/device_inventory.html

@@ -45,8 +45,8 @@
             <table class="table table-hover table-condensed panel-body" id="hardware">
                 <thead>
                     <tr>
-                        <th></th>
                         <th>Module</th>
+                        <th></th>
                         <th>Part Number</th>
                         <th>Serial Number</th>
                         <th></th>
@@ -55,8 +55,8 @@
                 <tbody>
                     {% for m in modules %}
                         <tr>
-                            <td>{% if not m.discovered %}<i class="fa fa-asterisk" title="Manually created"></i>{% endif %}</td>
                             <td>{{ m.name }}</td>
+                            <td>{% if not m.discovered %}<i class="fa fa-asterisk" title="Manually created"></i>{% endif %}</td>
                             <td>{{ m.part_id }}</td>
                             <td>{{ m.serial }}</td>
                             <td class="text-right">
@@ -70,8 +70,8 @@
                         </tr>
                         {% for m2 in m.submodules.all %}
                             <tr>
-                                <td>{% if not m.discovered %}<i class="fa fa-asterisk" title="Manually created"></i>{% endif %}</td>
                                 <td style="padding-left: 20px">{{ m2.name }}</td>
+                                <td>{% if not m2.discovered %}<i class="fa fa-asterisk" title="Manually created"></i>{% endif %}</td>
                                 <td>{{ m2.part_id }}</td>
                                 <td>{{ m2.serial }}</td>
                                 <td class="text-right">
@@ -85,8 +85,8 @@
                             </tr>
                             {% for m3 in m2.submodules.all %}
                                 <tr>
-                                    <td>{% if not m.discovered %}<i class="fa fa-asterisk" title="Manually created"></i>{% endif %}</td>
                                     <td style="padding-left: 40px">{{ m3.name }}</td>
+                                    <td>{% if not m3.discovered %}<i class="fa fa-asterisk" title="Manually created"></i>{% endif %}</td>
                                     <td>{{ m3.part_id }}</td>
                                     <td>{{ m3.serial }}</td>
                                     <td class="text-right">
@@ -100,8 +100,8 @@
                                 </tr>
                                 {% for m4 in m3.submodules.all %}
                                     <tr>
-                                        <td>{% if not m.discovered %}<i class="fa fa-asterisk" title="Manually created"></i>{% endif %}</td>
                                         <td style="padding-left: 60px">{{ m4.name }}</td>
+                                        <td>{% if not m4.discovered %}<i class="fa fa-asterisk" title="Manually created"></i>{% endif %}</td>
                                         <td>{{ m4.part_id }}</td>
                                         <td>{{ m4.serial }}</td>
                                         <td class="text-right">
@@ -120,6 +120,12 @@
                 </tbody>
             </table>
         </div>
+        {% if perms.dcim.add_module %}
+            <a href="{% url 'dcim:module_add' pk=device.pk %}" class="btn btn-success">
+                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
+                Add a Module
+            </a>
+        {% endif %}
     </div>
 </div>
 {% endblock %}

+ 3 - 3
netbox/templates/dcim/module_edit.html

@@ -1,7 +1,7 @@
 {% extends '_base.html' %}
 {% load form_helpers %}
 
-{% block title %}Editing {{ module.device }} {{ module }}{% endblock %}
+{% block title %}{% if module %}Editing {{ module.device }} {{ module }}{% else %}Add a Module to {{ device }}{% endif %}{% endblock %}
 
 {% block content %}
 <form action="." method="post" class="form form-horizontal">
@@ -18,13 +18,13 @@
             {% endif %}
             <div class="panel panel-default">
                 <div class="panel-heading">
-                    <strong>Editing {{ module.device }} {{ module }}</strong>
+                    <strong>{% if module %}Editing {{ module.device }} {{ module }}{% else %}Add a Module to {{ device }}{% endif %}</strong>
                 </div>
                 <div class="panel-body">
                     <div class="form-group">
                         <label class="col-md-3 control-label required">Device</label>
                         <div class="col-md-9">
-                            <p class="form-control-static">{{ module.device }}</p>
+                            <p class="form-control-static">{% if module %}{{ module.device }}{% else %}{{ device }}{% endif %}</p>
                         </div>
                     </div>
                     {% render_form form %}