Skip to content

Commit

Permalink
change_compare
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbieHan committed Feb 12, 2019
1 parent f1dde18 commit b58be33
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/cmdb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Meta:
class Code(AbstractMode):
key = models.CharField(max_length=80, verbose_name='键')
value = models.CharField(max_length=80, verbose_name='值')
desc = models.BooleanField(default=True, verbose_name='备注')
desc = models.CharField(max_length=100, blank=True, default='', verbose_name='备注')

class Meta:
verbose_name = '字典'
Expand Down
32 changes: 29 additions & 3 deletions apps/cmdb/signals.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
import os

from django.dispatch import receiver
from django.db.models.signals import post_delete
from django.db.models.signals import post_delete, post_save

from .models import DeviceFile
from .models import DeviceFile, DeviceInfo
from utils.db_utils import MongodbDriver


@receiver(post_delete, sender=DeviceFile)
def auto_delete_file(sender, instance, **kwargs):
if instance.file_content:
if os.path.isfile(instance.file_content.path):
os.remove(instance.file_content.path)
os.remove(instance.file_content.path)


@receiver(post_save, sender=DeviceInfo)
def auto_compare_diff(sender, instance, **kwargs):
record = instance.history.latest()
prev_record = record.prev_record
ope_type = {'~': 'update', '+': 'create', '-': 'delete'}
compare_result = {
'id': record.id,
'changed_by': record.changed_by.name,
'history_type': ope_type[record.history_type],
'history_date': record.history_date
}
changes = {}
if prev_record is not None:
delta = record.diff_against(prev_record)
for change in delta.changes:
changes[change.field] = [change.old, change.new]
compare_result['changes'] = changes
if compare_result['changes'] or compare_result['history_type'] == 'create':
try:
mongo = MongodbDriver(collection='change_compare')
mongo.insert(compare_result)
except Exception as e:
pass
18 changes: 18 additions & 0 deletions apps/utils/db_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pymongo


class MongodbDriver(object):

def __init__(self, db='device', collection='change_compare'):
self.client = pymongo.MongoClient('127.0.0.1', 27017)
self.db = self.client[db]
self.col = self.db[collection]

def insert(self, content):
return self.col.insert(content)

def find(self, sort_by, **filters,):
data = self.col.find(filters)
if sort_by:
data.sort(sort_by, pymongo.DESCENDING)
return data
2 changes: 1 addition & 1 deletion templates/cmdb/code_update.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h4>修改字典</h4>
</div>
<label class="col-sm-2 control-label">描述信息</label>
<div class="col-sm-3">
<input class="form-control" id="desc" name="desc" type="text" />
<input class="form-control" id="desc" name="desc" type="text" value="{{ code.desc }}"/>
</div>
</div>

Expand Down

0 comments on commit b58be33

Please sign in to comment.