classActive_file_protect_rule_view (Apiview): Renderer_classes=(Jsonrenderer, browsableapirenderer) parser_classes=(Jsonparser,) def post (self, request): fromdatetime import DateTime fromdjango.utils Import TimeZone fromDjango.utils.timezone import UTC Import Time import Xml.etree.ElementTree asET fromxml.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) forRuleinchRule_list:tmp_rule= Element ('Rule', { 'ID': Str (rule.id),'enabled':'true' ifRule.enableElse 'false', 'Status':'true' ifRule.apply_statusElse 'false', 'Log': Rule.log,'opertion':','. Join (Json.loads (rule.operation)),'Recover':'true' ifRule.recoverElse 'false', 'Protectdir': Rule.protectdir,'Action':' Allow' ifRule.actionElse 'Deny', 'Protectfiletype':','. Join (Json.loads (rule.file_type_list)),'Comment': Rule.commont}) Rule.apply_status=1Rule.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) # First Storageif(len (tmp_commit_rule_list) = =0): 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 () # Subsequent modification of the XML content and version number (timestamp)Else: Tmp_commit_rule= Models. File_protect_rule_commit.objects.Get(device_hash=Device_hash)if(Tmp_commit_rule.rule_xml_text = =rule_xml): PassElse: Tmp_commit_rule.rule_xml_text=Rule_xml tmp_commit_rule.version=Timezone.now () tmp_commit_rule.save ( ) fromdjango.forms.models Import model_to_dict version=tmp_commit_rule.version Tmp_commit_rule=model_to_dict (tmp_commit_rule)" "From datetime import datetime fromdjango.utils Import TimeZone fromdjango.utils.timezone import UTC Import time" "#time. Mktime (Timezone.now (). Timetuple ()) version=Time.mktime (Version.timetuple ()) tmp_commit_rule['version'] =versionreturnApiresponse (status=status_code.success, Data=tmp_commit_rule)
Resources:
XML Read-write
Recommendation: 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 Time Issue
The JSON serialization tool that resolves Python does not serialize datetime type data problems: http://www.au92.com/archives/resove-python-can-not-serialize-datetime.html
http://smilejay.com/2014/06/django-datetimefield-timezone-issue/
Three-dimensional expression:
http://blog.csdn.net/lanyuanershe/article/details/8083425
JOIN list:http://www.jb51.net/article/63598.htm
"Python" python XML read-write