【Python】Python XML 读写
2022/1/10 20:03:31
本文主要是介绍【Python】Python XML 读写,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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 = 1 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) == 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() # 后续修改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)
这篇关于【Python】Python XML 读写的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门