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
4 changed files
with
49 additions
and
5 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
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 |
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,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 |
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