Mercurial > hg > MPIWGWeb
changeset 49:e40ff9829108
improved old project inline image handling.
author | casties |
---|---|
date | Mon, 29 Apr 2013 18:00:46 +0200 |
parents | f59bdd5f4890 |
children | e30a4bd074db |
files | MPIWGProjects.py SrvTxtUtils.py zpt/project/edit_basic.zpt zpt/project/project_template.zpt |
diffstat | 4 files changed, 95 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/MPIWGProjects.py Mon Apr 29 16:02:24 2013 +0200 +++ b/MPIWGProjects.py Mon Apr 29 18:00:46 2013 +0200 @@ -27,7 +27,9 @@ import logging import time -from SrvTxtUtils import getInt, unicodify, utf8ify, refreshingImageFileIndexHtml +import xml.etree.ElementTree as ET + +from SrvTxtUtils import getInt, unicodify, utf8ify, serialize, refreshingImageFileIndexHtml from Products.MPIWGBibliography.BibliographyManager import BibliographyManager from bibliography import * from Products.ZDBInterface.ZDBInterfaceFolder import ZDBInterfaceFolder @@ -412,23 +414,31 @@ logging.debug("len(publ)" + repr(ret)) return ret; + + + def hasInlineImage(self): + """Return the number of inline images in the description.""" + text = self.getDescription() + cnt = text.count('<p class="picture">') + return cnt + def copyImageToMargin(self, RESPONSE=None): """copy inline images to marginal images""" # getImages from WEB_project_description - description = self.getContent('WEB_project_description') + description = self.getDescription() text2 = description splitted = text2.split("""<p class="picture">""") imageURLs = [] imageCaptions = [] - for split in splitted[1:]: + for split in splitted[1:]: tmp = split.split("</p>") # return repr(splitted[1]) try: - imageURLs.append(tmp[0].split("\"")[1].encode('utf-8')) + imageURLs.append(tmp[0].split("\"")[1].encode('utf-8')) except: try: @@ -437,8 +447,6 @@ imageURLs.append("") split2 = "</p>".join(tmp[1:]) - - splitted = split2.split("""<p class="picturetitle">""") if len(splitted) > 1: tmp = splitted[1].split("</p>") @@ -446,17 +454,24 @@ else: # keine caption - imageCaptions.append("") # eintragen: for imageURL in imageURLs: + if not imageURL: + # no URL - no image + continue + filename = imageURL.split("/")[-1] # lege neues images object an, mit leerem bild - if self.ZopeFind(self, obj_ids=[filename]): + if filename in self: # existiert das bild schon, dann neuen filenamen filename = "project_image_" + filename + if filename in self: + # exists too - assume its already converted + logging.warn("copyImageToMargin: image %s exists - skipping!"%filename) + continue self.addImage(None, imageCaptions[imageURLs.index(imageURL)], filename=filename) # hole die bilddaten aus der url @@ -469,11 +484,33 @@ try: # absolute data = urllib.urlopen(self.imageURL).read() except: - logger("MPIWG Project", logging.ERROR, "can't open: %s" % url) + logging.error("copyImageToMargin: can't open: %s" % url) obj = getattr(self, filename) obj.update_data(data) + # clean description + logging.debug("copyImageToMargin: description:%s"%repr(description)) + dom = ET.fromstring("<html>%s</html>"%description) + for e in dom.findall(".//p[@class='picture']"): + # remove contents + e.clear() + # remove tag + e.tag = None + + for e in dom.findall(".//p[@class='picturetitle']"): + # remove contents + e.clear() + # remove tag + e.tag = None + + # remove html tag + dom.tag = None + # set as new description + description = serialize(dom) + logging.debug("copyImageToMargin: new description:%s"%repr(description)) + setattr(self, 'WEB_project_description', description) + if RESPONSE: self.redirect(RESPONSE, 'manageImages') @@ -842,13 +879,13 @@ def getDescription(self, filter=False): """returns the project description""" - t = getattr(self, 'WEB_project_description', None) - if isinstance(t, list): + text = getattr(self, 'WEB_project_description', None) + if isinstance(text, list): # compat with old lists - return t[0] - else: - return t - + text = text[0] + + return text + def getSuperProjects(self): """returns a list of ancestor projects to the root""" @@ -1715,14 +1752,13 @@ return returnNamesDict - def editMPIWGProject(self, RESPONSE=None, fromEdit=None): + def editMPIWGProject(self, fromEdit=None, createNewVersion=True, RESPONSE=None): """edit the project and archive the old version""" - - self.copyObjectToArchive() # archive the object - self.ZCacheable_invalidate() + logging.debug("editMPIWGProject(fromEdit=%s, createNewVersion=%s)"%(fromEdit,createNewVersion)) + if createNewVersion: + self.copyObjectToArchive() # archive the object formdata = self.REQUEST.form - logging.debug("REQUEST.form=%s" % repr(formdata)) # set all definedFields for x in definedFields: @@ -2029,6 +2065,7 @@ def updateAllProjects(self, updateResponsibleScientistsList=False): """Patch all current projects for legacy problems.""" cnt = 0 + log = "" # go through all projects for (id, project) in self.ZopeFind(self, obj_metatypes=['MPIWGProject'], search_sub=1): cnt += 1 @@ -2036,6 +2073,7 @@ # hasRelatedPublicationsOldVersion # if project.hasRelatedPublicationsOldVersion(): + log += "%s: update relatedPublicationsOldVersion!\n"%project.getId() logging.debug("updateAllProjects(%s): update relatedPublicationsOldVersion!"%project.getId()) project.copyPublicationsToList() @@ -2044,6 +2082,7 @@ # memberlist = project.getResponsibleScientistsList() if len(memberlist) > 0 and isinstance(memberlist[0], tuple): + log += "%s: updating memberlist!\n"%project.getId() logging.debug("updateAllProjects(%s): updating memberlist" % project.getId()) newScientists = {} for m in memberlist: @@ -2065,9 +2104,23 @@ project.setResponsibleScientistsList(newScientists) # + # old inline images + # + if project.hasInlineImage(): + log += "%s: has inlineImage!\n"%project.getId() + logging.debug("updateAllProjects(%s): has inlineImage!"%project.getId()) + try: + project.copyImageToMargin() + except Exception, e: + log += "%s: ERROR in copyImageToMargin!\n"%project.getId() + logging.debug("updateAllProjects(%s): ERROR in copyImageToMargin: %s"%(project.getId(), e)) + + + # # remove old attributes # if hasattr(project, 'definedFields'): + log += "%s: has definedFields!\n"%project.getId() logging.debug("updateAllProjects(%s): has definedFields!"%project.getId()) delattr(project, 'definedFields') @@ -2075,13 +2128,13 @@ # update extended bibliography # if hasattr(project, 'publicationList'): + log += "%s: has publicationList!\n"%project.getId() logging.debug("updateAllProjects(%s): has publicationList!"%project.getId()) extpub = project.publicationList if hasattr(extpub, 'connection_id'): logging.debug("updateAllProjects(%s): extended publication %s has connection_id=%s!"%(project.getId(),extpub.getId(),extpub.connection_id)) - - return "updated %s projects!" % cnt + return log + "\n DONE! updated %s projects!" % cnt
--- a/SrvTxtUtils.py Mon Apr 29 16:02:24 2013 +0200 +++ b/SrvTxtUtils.py Mon Apr 29 18:00:46 2013 +0200 @@ -10,7 +10,9 @@ import urllib2 import logging -srvTxtUtilsVersion = "1.6" +import xml.etree.ElementTree as ET + +srvTxtUtilsVersion = "1.7" map_months = {'en': [u"", u"January", @@ -76,7 +78,7 @@ return s.encode('utf-8') def getText(node, recursive=0): - """returns all text content of a node and its subnodes""" + """returns all text content of a (etree) node and its subnodes""" if node is None: return '' @@ -99,6 +101,18 @@ return text + +def serialize(node): + """returns a string containing an XML snippet of (etree) node""" + s = ET.tostring(node, 'UTF-8') + # snip off XML declaration + if s.startswith('<?xml'): + i = s.find('?>') + return s[i+3:] + + return s + + def getMonthName(mon, lang): """returns the name of the month mon in the language lang""" return map_months[lang][mon]
--- a/zpt/project/edit_basic.zpt Mon Apr 29 16:02:24 2013 +0200 +++ b/zpt/project/edit_basic.zpt Mon Apr 29 18:00:46 2013 +0200 @@ -65,6 +65,9 @@ <h2>Names</h2> <table tal:define="global count python:0"> + <tr> + <th>Name in project</th><th>MPIWG member object</th> + </tr> <tr tal:repeat="identifiedName python:options.get('identifiedNames',{}).items()"> <tal:x tal:define="global count python:count+1" /> <td><input type="hidden"
--- a/zpt/project/project_template.zpt Mon Apr 29 16:02:24 2013 +0200 +++ b/zpt/project/project_template.zpt Mon Apr 29 18:00:46 2013 +0200 @@ -60,7 +60,7 @@ </div> <!-- inline image --> - <div tal:content="structure python:here.getDescription()">Project description</div> + <div tal:content="structure python:here.getDescription(filter=True)">Project description</div> </div>