【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 读写的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程