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. Linux(CentOS)下安装git

    上个月把VPS迁到budgetVM,终于不用再受digitalOcean的气了,入手很方便,重点是支持支付宝付款——paypal的界面真是不习惯,开通速度挺快的,1G的内存够我折腾一段时间了~,额外送 ...

  2. BZOJ-2748 音量调节 DP+背包(脑残)

    水DP,一开始竟然想错了...水了半天....真可怕 2748: [HAOI2012]音量调节 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 1156 ...

  3. BZOJ-3524 Couriers 可持久化线段树

    可持久化线段树,其实就是类主席树了.. 3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 128 MB Submit: 1124 Sol ...

  4. 【poj1716】 Integer Intervals

    http://poj.org/problem?id=1716 (题目链接) 题意 给出n个区间,要求取出最少数量的不同的自然数,使每个区间中至少包含2个取出的数. Solution 差分约束. 运用前 ...

  5. codeforces 375D:Tree and Queries

    Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  6. Windows Server 2008系统如何取消登录时要按Ctrl+Alt+Delete组合键

    1.点桌面任务栏的“开始-->运行”在弹出的窗口中输入gpedit.msc . 2.输入gpedit.msc后,点击确定即打开了组策略编辑器.在组策略编辑器的左框内依次序展开(点前面的“+”号) ...

  7. PL/0编译器(java version) - MainFrame.java

    1: /* 2: * To change this license header, choose License Headers in Project Properties. 3: * To chan ...

  8. 在使用easyui,datagrid时,JSON中的如果含有换行符,则不能显示数据

    http://www.xuebuyuan.com/2103538.html 每项值需处理换行符 item = item.Replace("\r\n", ""); ...

  9. Java初学(二)

    一.数据类型 在定义Long或者Float类型变量的时候,要加L或f(大小写无关,只是便于识别,建议不要小写L) 整数默认是int,浮点数默认是double 二.java字符 java语言采用的是Un ...

  10. ewebeditor编辑器ASP/ASPX/PHP/JSP版本漏洞利用总结及解决方法

    这个编辑器按脚本分主要有4个版本,ASP/ASPX/PHP/JSP 每个版本都有可以利用的漏洞.判断网站是否使用了eWebEditor查看程序源代码,看看源码中是否存在类似”ewebeditor.as ...