class ACTIVE_FILE_PROTECT_RULE_VIEW(APIView): renderer_classes = (JSONRenderer, BrowsableAPIRenderer)
parser_classes = (JSONParser,) def post(self, request):
from datetime import datetime
from django.utils import timezone
from django.utils.timezone import utc
import time import xml.etree.ElementTree as ET
from xml.etree.ElementTree import ElementTree,Element
root = ET.fromstring(RULE_XML_TPL)
fileprotect = root.find('fileprotect')
# print fileprotect.tag, fileprotect.attrib user_info = request.session.get('user_info')
customer_id = user_info.get('customer_id') body_data = request.body
request_data = json.loads(body_data)
device_hash = request_data['device_hash'] with transaction.atomic():
device = models.FILE_PROTECT_INSTANCE.objects.get(device_hash=device_hash)
assert(device.customer_id == customer_id) rule_list = models.FILE_PROTECT_RULE_UPDATE.objects.filter(device_hash=device_hash)
for rule in rule_list:
tmp_rule = Element('rule', {
'id': str(rule.id),
'enabled': 'true' if rule.enable else 'false',
'status': 'true' if rule.apply_status else 'false',
'log': rule.log,
'opertion': ','.join(json.loads(rule.operation)),
'recover': 'true' if rule.recover else 'false',
'protectdir': rule.protectdir,
'action': 'allow' if rule.action else 'deny',
'protectfiletype': ','.join(json.loads(rule.file_type_list)),
'comment': rule.commont
})
rule.apply_status =
rule.save() fileprotect.append(tmp_rule)
# ET.dump(root)
tmp_xml = ET.tostring(root, encoding="utf-8", method="xml")
rule_xml = '<?xml version="1.0" encoding="utf-8"?>\n' + tmp_xml tmp_commit_rule_list = models.FILE_PROTECT_RULE_COMMIT.objects.filter(device_hash=device_hash).filter(customer_id=customer_id)
# 首次入库
if(len(tmp_commit_rule_list) == ):
tmp_commit_rule = models.FILE_PROTECT_RULE_COMMIT(customer_id=customer_id, device_hash=device_hash, rule_xml_text=rule_xml)
tmp_commit_rule.save()
# 后续修改xml内容和版本号(时间戳)
else:
tmp_commit_rule = models.FILE_PROTECT_RULE_COMMIT.objects.get(device_hash=device_hash)
if(tmp_commit_rule.rule_xml_text == rule_xml):
pass
else:
tmp_commit_rule.rule_xml_text = rule_xml
tmp_commit_rule.version = timezone.now()
tmp_commit_rule.save() from django.forms.models import model_to_dict
version = tmp_commit_rule.version
tmp_commit_rule = model_to_dict(tmp_commit_rule) '''from datetime import datetime
from django.utils import timezone
from django.utils.timezone import utc
import time'''
#time.mktime(timezone.now().timetuple())
version = time.mktime(version.timetuple())
tmp_commit_rule['version'] = version return APIResponse(status=status_code.success, data=tmp_commit_rule)
参考资料:
XML读写
推荐:http://blog.csdn.net/gingerredjade/article/details/21944675
http://bbs.csdn.net/topics/350027413
http://bbs.csdn.net/topics/390194606
http://python.jobbole.com/82775/
http://www.jb51.net/article/67190.htm
http://blog.csdn.net/shomy_liu/article/details/37929181
http://www.python tab.com/html/2013/pythonjichu_0618/451.html
http://blog.csdn.net/xibuzhihun/article/details/6950142
http://www.cnblogs.com/CheeseZH/p/4026686.html
http://www.jb51.net/article/17687.htm
http://blog.csdn.net/kiki113/article/details/4052584
http://www.jb51.net/article/67120.htm
Django UTC时间问题
解决Python自带的json序列化工具不能序列化datetime类型数据问题:http://www.au92.com/archives/resove-python-can-not-serialize-datetime.html
http://smilejay.com/2014/06/django-datetimefield-timezone-issue/
三元表达式:
http://blog.csdn.net/lanyuanershe/article/details/8083425
JOIN LIST:http://www.jb51.net/article/63598.htm