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

【Python】Python XML 读写的更多相关文章

  1. Python之xml读写

    遇到问题xml文件读写,没有子节点需要新建ChildNode. # -*- coding: utf-8 -*- import os import shutil import xml.dom.minid ...

  2. Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...

  3. 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    [转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...

  4. python 生成 xml文件 属性的顺序问题

    需求很奇葩. 文档示例 <ITEM key="username" eng="User Name" chn="用户名" val=&quo ...

  5. python读取xml文件

    关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...

  6. python 解析XML python模块xml.dom解析xml实例代码

    分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...

  7. python解析xml模块封装代码

    在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...

  8. python解析xml之lxml

    虽然python解析xml的库很多,但是,由于lxml在底层是用C语言实现的,所以lxml在速度上有明显优势.除了速度上的优势,lxml在使用方面,易用性也非常好.这里将以下面的xml数据为例,介绍l ...

  9. python处理xml的常用包(lib.xml、ElementTree、lxml)

    python处理xml的三种常见机制 dom(随机访问机制) sax(Simple APIs for XML,事件驱动机制) etree python处理xml的三种包 标准库中的xml Fredri ...

随机推荐

  1. java编程

    Java编程:五子棋游戏源代码 import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing ...

  2. Docker-2 的创建、启动、终止、删除、迁移等

    学习博客地址:http://www.dwhd.org/20151115_140935.html

  3. Yii2请求,报400错误

    出现400错误是yii2.0的csrf防范策略导致 在components里面添加request配置如下: 'request' => [ // !!! insert a secret key i ...

  4. BZOJ-1061 志愿者招募 线性规划转最小费用最大流+数学模型 建模

    本来一眼建模,以为傻逼题,然后发现自己傻逼...根本没想到神奇的数学模型..... 1061: [Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 ...

  5. firefox与chrome中对select下拉框中的option支持问题

    firefox可以直接修改option的字体样式,但是chrome中option的字体样式是继承select的,这个是在项目中遇到的,具体的可以看一下 http://www.cnblogs.com/r ...

  6. javaScript基础练习题-下拉框制作

    1.基础回顾 如何让一个段javascript在文档加载后执行,(因为自己忘了,所以顺便复习一下) window.onload = function(){}; <!DOCTYPE html PU ...

  7. Azure怎么使用ftp登录

    1.下载配置文件 2.拷贝FTP的地址 3.查看配置文件里面的用户名和密码 4.登录

  8. hdu 1575 矩阵快速幂模板

    #include "iostream" #include "vector" #include "cstring" using namespa ...

  9. BZOJ1588 HNOI2002 营业额统计 [Splay入门题]

    [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4128  Solved: 1305 Description 营业额统计 ...

  10. codeforce626C.Block Towers(二分)

    C. Block Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...