def prearrange(self, obj):
     """
     Passive preprocessing of object data. It means adding attributes like
     title_normalized, looking for mime-types of file etc etc.
     This method doesnt touch the database, it works only on the object
     attributes.
     @return True if object was successfully preprocessed
     @return False if object doesnt fulfill table contraints or if object damaged
     """
     # add normalized title
     if "title_normalized" in [x for x in obj]:
         norm_method = getattr(Normalize, obj._table_name)
         # I'm still not sure if it is implemented yet
         assert callable(norm_method)
         if obj["title"] is None:
             return False
         obj["title_normalized"] = norm_method(obj["title"])
         if obj["title_normalized"] is None:
             return False
     # add missing mime type if needed
     if obj._table_name == "file":
         # if "type" not in obj:  TODO really??
         fn = obj["filename"]
         r = self._mime.start([fn])
         obj["type"] = r[fn]
     elif obj._table_name == "person":
         if obj["full_name"] is None:
             obj["full_name"] = " ".join(
                 [obj[x] for x in ("first_name", "middle_name", "last_name") if obj[x] is not None]
             )
         if obj["full_name_ascii"] is None:
             obj["full_name_ascii"] = Normalize.to_ascii(obj["full_name"])
     elif obj._table_name == "location":
         if "city" in obj:
             city = Normalize.location(obj["city"], True)
             if city is None:
                 obj.city = None
             else:
                 obj["city"] = city
         if "country" in obj:
             country = Normalize.location(obj["country"], True)
             if country is None:
                 obj.country = None
             else:
                 obj["country"] = country
         if "address" in obj:
             addr = Normalize.location(obj["address"])
             if addr is None:
                 obj.address = None
             else:
                 obj["address"] = addr
     elif obj._table_name == "publication_section":
         self._bind_publ_section_to_text(obj)
     return True
Beispiel #2
0
 def prearrange(self, obj):
     """
     Passive preprocessing of object data. It means adding attributes like
     title_normalized, looking for mime-types of file etc etc.
     This method doesnt touch the database, it works only on the object
     attributes.
     @return True if object was successfully preprocessed
     @return False if object doesnt fulfill table contraints or if object damaged
     """
     # add normalized title
     if 'title_normalized' in [x for x in obj]:
         norm_method = getattr(Normalize, obj._table_name)
         # I'm still not sure if it is implemented yet
         assert callable(norm_method)
         if obj['title'] is None:
             return False
         obj['title_normalized'] = norm_method(obj['title'])
         if obj['title_normalized'] is None:
             return False
     # add missing mime type if needed
     if obj._table_name == "file":
         #if "type" not in obj:  TODO really??
         fn = obj['filename']
         r = self._mime.start([fn])
         obj['type'] = r[fn]
     elif obj._table_name == "person":
         if obj['full_name'] is None:
             obj['full_name'] = " ".join([obj[x] for x in ('first_name', \
                         'middle_name', 'last_name') if obj[x] is not None])
         if obj['full_name_ascii'] is None:
             obj['full_name_ascii'] = Normalize.to_ascii(obj['full_name'])
     elif obj._table_name == "location":
         if 'city' in obj:
             city = Normalize.location(obj['city'], True)
             if city is None: obj.city = None
             else: obj['city'] = city
         if 'country' in obj:
             country = Normalize.location(obj['country'], True)
             if country is None: obj.country = None
             else: obj['country'] = country
         if 'address' in obj:
             addr = Normalize.location(obj['address'])
             if addr is None: obj.address = None
             else: obj['address'] = addr
     elif obj._table_name == "publication_section":
         self._bind_publ_section_to_text(obj)
     return True