【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)
上一篇:Leetcode笔记-13 罗马数字转整数


下一篇:vue共用的 混淆