def update_tag_id(self, match, destination_ids_by_source): # Updates a specific tag's id from source to destination Wagtail instance, or removes the tag if no id mapping exists tag_body = match.group(1) attrs = extract_attrs(tag_body) try: handler = self.handlers[attrs[self.type_attribute]] target_model = get_base_model(handler.get_model()) new_id = destination_ids_by_source.get( (target_model, int(attrs['id']))) if new_id is None: # Return the tag's inner contents, effectively removing the tag try: return match.group(2) except IndexError: # The tag has no inner content, return a blank string instead return '' # Otherwise update the id and construct the new tag string new_tag_body = FIND_ID.sub('id="{0}"'.format(str(new_id)), tag_body) tag_body_offset = match.start(0) new_tag_string = match.group(0)[:( match.start(1) - tag_body_offset)] + new_tag_body + match.group(0)[ (match.end(1) - tag_body_offset):] return new_tag_string except KeyError: # If the relevant handler cannot be found, don't update the tag id pass return match.group(0)
def update_tag_id(self, match, destination_ids_by_source): # Updates a specific tag's id from source to destination Wagtail instance attrs = extract_attrs(match.group(1)) try: handler = self.handlers[attrs[self.type_attribute]] target_model = get_base_model(handler.get_model()) new_id = destination_ids_by_source.get((target_model, int(attrs['id'])), int(attrs['id'])) return FIND_ID.sub('id="{0}"'.format(str(new_id)), match.group(0)) except KeyError: # If the relevant handler cannot be found, don't update the tag id pass return match.group(0)
def get_objects(self, html): # Gets object references objects = set() if html: for match in self.tag_matcher.finditer(html): attrs = extract_attrs(match.group(1)) try: handler = self.handlers[attrs[self.type_attribute]] objects.add((get_base_model(handler.get_model()), int(attrs['id']))) except KeyError: # If no handler can be found, no object reference can be added. # This might occur when the link is a plain url pass return objects
def test_extract_attr(self): html = '<a foo="bar" baz="quux">snowman</a>' result = extract_attrs(html) self.assertEqual(result, {'foo': 'bar', 'baz': 'quux'})
def test_extract_attr(self): html = '<a foo="bar" baz="quux">snowman</a>' result = extract_attrs(html) self.assertEqual(result, {"foo": "bar", "baz": "quux"})