# HG changeset patch # User ymh # Date 1383216972 -3600 # Node ID d8d4c721523bb995b2363e46c534cc5073d4e2a1 # Parent 500f75283cadb11fa42ceedaa8e27d182ec08100 Remove xml declaration from ldt diff -r 500f75283cad -r d8d4c721523b src/ldt/ldt/ldt_utils/models.py --- a/src/ldt/ldt/ldt_utils/models.py Mon Oct 28 12:28:03 2013 +0100 +++ b/src/ldt/ldt/ldt_utils/models.py Thu Oct 31 11:56:12 2013 +0100 @@ -1,4 +1,10 @@ -from .events import post_project_save +import datetime +import mimetypes +import os.path +import re +from shutil import move +import uuid + from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.auth.models import Group @@ -6,6 +12,11 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ from guardian.shortcuts import assign, remove_perm, get_perms +import lxml.etree #@UnresolvedImport +from sorl.thumbnail import ImageField +import tagging.fields +from tagging.models import Tag + from ldt.core.models import Document from ldt.security import (get_current_user_or_admin, set_current_user, get_current_user) @@ -13,18 +24,11 @@ from ldt.security.models import SafeModel from ldt.utils import generate_hash, url as url_utils from ldt.utils.web_url_management import get_web_url -from shutil import move -from sorl.thumbnail import ImageField -from tagging.models import Tag from utils import (create_ldt, copy_ldt, create_empty_iri, update_iri, generate_uuid) -import datetime -import lxml.etree #@UnresolvedImport -import mimetypes -import os.path -import re -import tagging.fields -import uuid + +from .events import post_project_save + User = get_user_model() @@ -625,6 +629,9 @@ if name == "ldt" and hasattr(self, "__ldt_encoded"): del self.__ldt_encoded + def get_xml_doc(self): + #remove the xml header declaration + return lxml.etree.fromstring(re.sub(r"^<\?\s*xml .*\?>", "", self.ldt)) def __unicode__(self): return unicode(self.id) + u"::" + unicode(self.ldt_id) + u"::" + unicode(self.title) @@ -636,7 +643,7 @@ def get_description(self, doc=None): if doc is None: - doc = lxml.etree.fromstring(self.ldt) #@UndefinedVariable + doc = self.get_xml_doc()#@UndefinedVariable res = doc.xpath("/iri/project") if len(res) > 0: @@ -756,7 +763,7 @@ def has_annotations(self): nb_annot = 0 - doc = lxml.etree.fromstring(self.ldt) #@UndefinedVariable + doc = self.get_xml_doc() res = doc.xpath("/iri/annotations/content/ensemble/decoupage") for r in res: nb_annot = nb_annot + r.find('elements').__len__()