forked from RobbieHan/sandboxMP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
3,916 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,46 @@ | ||
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, ConnectionInfo | ||
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 | ||
|
||
|
||
@receiver(post_delete, sender=DeviceInfo) | ||
def auto_delete_connection(sender, instance, **kwargs): | ||
dev_connection = getattr(instance, 'dev_connection') | ||
if dev_connection: | ||
ConnectionInfo.objects.filter(id=dev_connection).delete() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @Time : 2019/2/17 21:28 | ||
# @Author : RobbieHan | ||
# @File : __init__.py.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from django import template | ||
from django.db.models.query import QuerySet | ||
from django.contrib.auth import get_user_model | ||
|
||
from cmdb.models import Code, Cabinet | ||
|
||
register = template.Library() | ||
|
||
User = get_user_model() | ||
|
||
|
||
@register.simple_tag | ||
def get_con(context, arg, field): | ||
if isinstance(context, QuerySet): | ||
context = context.values() | ||
instance = [con for con in context if con['id'] == arg] | ||
if instance: | ||
return instance[0][field] | ||
return '' | ||
|
||
|
||
@register.filter(name='compare_result') | ||
def get_change_compare(changes): | ||
change_compare = [] | ||
for key, value in changes.items(): | ||
if key in ['network_type', 'service_type', 'operation_type']: | ||
log = replace_log(key, value, Code, 'value') | ||
elif key == 'dev_cabinet': | ||
log = replace_log(key, value, Cabinet, 'number') | ||
elif key == 'leader': | ||
log = replace_log(key, value, User, 'name') | ||
else: | ||
log = '字段:"%(field)s",由:"%(old)s",变更为:"%(new)s"。' % { | ||
'field': key, | ||
'old': value[0], | ||
'new': value[1] | ||
} | ||
change_compare.append(log) | ||
return ','.join(str(i) for i in change_compare) | ||
|
||
|
||
def replace_log(key, value, model, field): | ||
old = value[0] | ||
new = value[1] | ||
log_format = '字段:"%(field)s",由:"%(old)s",变更为:"%(new)s"。' | ||
try: | ||
data = model.objects.filter(id=old).values()[0] | ||
old_data = data[field] | ||
except Exception: | ||
old_data = old | ||
try: | ||
data = model.objects.filter(id=new).values()[0] | ||
new_data = data[field] | ||
except Exception: | ||
new_data = new | ||
return log_format % { | ||
'field': key, | ||
'old': old_data, | ||
'new': new_data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.