Skip to content

Commit e3490b3

Browse files
committed
tags&filters
1 parent b58be33 commit e3490b3

File tree

7 files changed

+279
-1
lines changed

7 files changed

+279
-1
lines changed

apps/cmdb/templatetags/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @Time : 2019/2/17 21:28
2+
# @Author : RobbieHan
3+
# @File : __init__.py.py

apps/cmdb/templatetags/extra_tags.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from django import template
2+
from django.db.models.query import QuerySet
3+
from django.contrib.auth import get_user_model
4+
5+
from cmdb.models import Code, Cabinet
6+
7+
register = template.Library()
8+
9+
User = get_user_model()
10+
11+
12+
@register.simple_tag
13+
def get_con(context, arg, field):
14+
if isinstance(context, QuerySet):
15+
context = context.values()
16+
instance = [con for con in context if con['id'] == arg]
17+
if instance:
18+
return instance[0][field]
19+
return ''
20+
21+
22+
@register.filter(name='compare_result')
23+
def get_change_compare(changes):
24+
change_compare = []
25+
for key, value in changes.items():
26+
if key in ['network_type', 'service_type', 'operation_type']:
27+
log = replace_log(key, value, Code, 'value')
28+
elif key == 'dev_cabinet':
29+
log = replace_log(key, value, Cabinet, 'number')
30+
elif key == 'leader':
31+
log = replace_log(key, value, User, 'name')
32+
else:
33+
log = '字段:"%(field)s",由:"%(old)s",变更为:"%(new)s"。' % {
34+
'field': key,
35+
'old': value[0],
36+
'new': value[1]
37+
}
38+
change_compare.append(log)
39+
return ','.join(str(i) for i in change_compare)
40+
41+
42+
def replace_log(key, value, model, field):
43+
old = value[0]
44+
new = value[1]
45+
log_format = '字段:"%(field)s",由:"%(old)s",变更为:"%(new)s"。'
46+
try:
47+
data = model.objects.filter(id=old).values()[0]
48+
old_data = data[field]
49+
except Exception:
50+
old_data = old
51+
try:
52+
data = model.objects.filter(id=new).values()[0]
53+
new_data = data[field]
54+
except Exception:
55+
new_data = new
56+
return log_format % {
57+
'field': key,
58+
'old': old_data,
59+
'new': new_data
60+
}

apps/cmdb/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
path('eam/device/list/', views_eam.DeviceListView.as_view(), name='eam-device-list'),
3434
path('eam/device/delete/', views_eam.DeviceDeleteView.as_view(), name='eam-device-delete'),
3535
path('eam/device/device2connection/', views_eam.Device2ConnectionView.as_view(), name='eam-device-device2connection'),
36+
path('eam/device/detail/', views_eam.DeviceDetailView.as_view(), name='eam-device-detail'),
3637
]

apps/cmdb/views_eam.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SandboxListView, SandboxUpdateView, SandboxCreateView)
1212
from .models import Cabinet, DeviceInfo, Code, ConnectionInfo
1313
from .forms import DeviceCreateForm, DeviceUpdateForm, ConnectionInfoForm
14+
from utils.db_utils import MongodbDriver
1415

1516
User = get_user_model()
1617

@@ -157,4 +158,20 @@ def post(self, request):
157158
form_errors = str(form.errors)
158159
errors = re.findall(pattern, form_errors)
159160
res['error'] = errors[0]
160-
return JsonResponse(res)
161+
return JsonResponse(res)
162+
163+
164+
class DeviceDetailView(LoginRequiredMixin, BreadcrumbMixin, TemplateView):
165+
template_name = 'cmdb/deviceinfo_detail.html'
166+
167+
def get_context_data(self, **kwargs):
168+
device = get_object_or_404(DeviceInfo, pk=int(self.request.GET['id']))
169+
mongo = MongodbDriver()
170+
logs = mongo.find(id=int(self.request.GET['id']), sort_by='history_date')
171+
all_file = device.devicefile_set.all()
172+
device_public = get_device_public()
173+
kwargs['device'] = device
174+
kwargs['logs'] = logs
175+
kwargs['all_file'] = all_file
176+
kwargs.update(device_public)
177+
return super().get_context_data(**kwargs)

sandboxMP/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
'simple_history',
4545
'system',
4646
'cmdb',
47+
'cmdb.templatetags',
4748
]
4849

4950
MIDDLEWARE = [

templates/cmdb/deviceinfo.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,5 +339,9 @@
339339
}
340340
});
341341
}
342+
343+
function doDetail(id){
344+
window.location.href="{% url 'cmdb:eam-device-detail' %}?id="+id;
345+
}
342346
</script>
343347
{% endblock %}

templates/cmdb/deviceinfo_detail.html

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
{% extends "base-left.html" %}
2+
{% load staticfiles %}
3+
{% load extra_tags %}
4+
5+
{% block css %}
6+
<link rel="stylesheet" href="{% static 'js/plugins/layer/skin/layer.css' %}">
7+
{% endblock %}
8+
9+
{% block content %}
10+
11+
<section class="content">
12+
13+
<div class="nav-tabs-custom">
14+
<ul class="nav nav-tabs">
15+
<li class="active"><a href="#activity" data-toggle="tab">设备详情</a></li>
16+
<li><a href="#history" data-toggle="tab">历史纪录</a></li>
17+
</ul>
18+
<div class="tab-content">
19+
<div class="active tab-pane" id="activity">
20+
<div class="box-body no-padding">
21+
<div class="btn-group pull-right margin">
22+
<button type="button" class="btn btn-primary btn-sm margin-r-5"
23+
onclick="doUpload({{ device.id }})">
24+
<i class="fa fa-cloud-upload"> 上传资料</i>
25+
</button>
26+
<button type="button" class="btn btn-primary btn-sm margin-r-5" title="认证管理" onclick="doDevice2Connection({{ device.id }})">
27+
<i class="fa fa-user"> 认证管理</i>
28+
</button>
29+
<button type="button" class="btn btn-primary btn-sm margin-r-5" title="自动更新" onclick="doAutoUpdate({{ device.id }})">
30+
<i class="fa fa-circle-o-notch"> 自动更新</i>
31+
</button>
32+
<button type="button" class="btn btn-primary btn-sm margin-r-5" title="编辑" onclick="doUpdate({{ device.id }})">
33+
<i class="fa fa-pencil"> 编辑</i>
34+
</button>
35+
<button type="button" id="btnReturn" class="btn btn-primary btn-sm">
36+
<i class="fa fa-arrow-left"></i> 返回
37+
</button>
38+
</div>
39+
</div>
40+
<div class="table-responsive mailbox-messages">
41+
<table class="table" id="tbWorkList" style="white-space: nowrap;">
42+
<tbody>
43+
<tr class="info">
44+
<td width="10%"><strong>主机名</strong></td>
45+
<td class="text-left">{{ device.sys_hostname }}</td>
46+
<td width="10%"><strong>SN编号</strong></td>
47+
<td class="text-left">{{ device.sn_number }}</td>
48+
</tr>
49+
<tr>
50+
<td><strong>系统类型</strong></td>
51+
<td>{{ device.os_type }}</td>
52+
<td><strong>设备类型</strong></td>
53+
<td>{{ device.device_type }}</td>
54+
</tr>
55+
<tr class="info">
56+
<td><strong>设备地址</strong></td>
57+
<td>{{ device.hostname }}</td>
58+
<td><strong>MAC地址</strong></td>
59+
<td>{{ device.mac_address }}</td>
60+
</tr>
61+
<tr>
62+
<td><strong>网络类型</strong></td>
63+
<td>{% get_con all_code device.network_type 'value' %}</td>
64+
<td><strong>服务类型</strong></td>
65+
<td>{% get_con all_code device.service_type 'value' %}</td>
66+
</tr>
67+
<tr class="info">
68+
<td><strong>业务类型</strong></td>
69+
<td>{% get_con all_code device.operation_type 'value' %}</td>
70+
<td><strong>机柜信息</strong></td>
71+
<td>{% get_con all_cabinet device.dev_cabinet 'number' %}</td>
72+
</tr>
73+
<tr>
74+
<td><strong>购买日期</strong></td>
75+
<td>{{ device.buyDate }}</td>
76+
<td><strong>质保日期</strong></td>
77+
<td>{{ device.warrantyDate }}</td>
78+
</tr>
79+
<tr class="info">
80+
<td><strong>所属</strong></td>
81+
<td>{% if device.parent %}
82+
{{ device.parent.sys_hostname }}({{ device.parent.hostname }})
83+
{% endif %}
84+
</td>
85+
<td><strong>责任人</strong></td>
86+
<td>{% get_con all_user device.leader 'name' %}</td>
87+
</tr>
88+
<tr>
89+
<td><strong>入库时间</strong></td>
90+
<td>{{ device.add_time }}</td>
91+
<td><strong>最后变更</strong></td>
92+
<td>{{ device.modify_time }}</td>
93+
</tr>
94+
<tr class="info">
95+
<td><strong>最后操作人</strong></td>
96+
<td>{{ device.changed_by.name }}</td>
97+
<td><strong></strong></td>
98+
<td></td>
99+
</tr>
100+
<tr>
101+
<td><strong>备注信息</strong></td>
102+
<td colspan="3">{{ device.desc }}</td>
103+
</tr>
104+
</tbody>
105+
</table>
106+
</div>
107+
<br>
108+
<div class="box-footer">
109+
110+
<ul class="mailbox-attachments clearfix" id="imageContainer">
111+
{% for file in all_file %}
112+
<li class="imageItem">
113+
<div class="mailbox-attachment-info">
114+
<a href="/media/{{ file.file_content }}" target="_blank"><i
115+
class="fa fa-file-text"></i>
116+
<small>{{ file.file_content|cut:'asset_file/' }}</small>
117+
</a>
118+
<span class="mailbox-attachment-size">
119+
<b>上传人</b>:{{ file.upload_user }}
120+
<a href="/media/{{ file.file_content }}" download="{{ file.file_content }}"
121+
class="btn btn-primary btn-xs pull-right">
122+
<i class="fa fa-cloud-download" title="下载文件"></i>
123+
</a>
124+
<button class="btn btn-adn btn-xs pull-right margin-r-5"
125+
onclick="doDelete({{ file.id }})">
126+
<i class="fa fa-trash" title="删除文件"> </i>
127+
</button>
128+
</span>
129+
</div>
130+
</li>
131+
{% endfor %}
132+
</ul>
133+
</div>
134+
</div>
135+
<!-- /.tab-pane -->
136+
<div class="tab-pane" id="history">
137+
<div class="box-body">
138+
<ul class="todo-list">
139+
{% for log in logs %}
140+
<li>
141+
<!-- drag handle -->
142+
<span class="handle">
143+
<small class="text-maroon">
144+
<i class="glyphicon glyphicon-time"></i>
145+
{{ log.history_date }}
146+
&nbsp;&nbsp;
147+
{{ log.changed_by }}
148+
&nbsp;&nbsp;
149+
{{ log.history_type }}
150+
</small>
151+
</span>
152+
<span class="text-sm">
153+
{{ log.changes | compare_result }}
154+
</span>
155+
<!--
156+
<button class="btn btn-xs btn-danger pull-right">还原数据</button>
157+
-->
158+
</li>
159+
{% endfor %}
160+
</ul>
161+
</div>
162+
</div>
163+
164+
<!-- /.tab-pane -->
165+
</div>
166+
<!-- /.tab-content -->
167+
</div>
168+
</section>
169+
170+
<!-- /.content -->
171+
172+
{% endblock %}
173+
174+
{% block javascripts %}
175+
<script src="{% static 'js/plugins/layer/layer.js' %}"></script>
176+
<script src="{% static 'plugins/masonry/masonry.js' %}"></script>
177+
<script type="text/javascript">
178+
$(function () {
179+
$('#CMDB-EAM').addClass('active');
180+
$('#CMDB-EAM-DEVICE').addClass('active');
181+
182+
});
183+
184+
$("#btnReturn").click(function(){
185+
history.back();
186+
});
187+
188+
189+
190+
</script>
191+
192+
{% endblock %}

0 commit comments

Comments
 (0)