def _cleanse_tags(self, message):
        """Using BeautifulSoup, remove or modify improper tags & attributes"""
        soup = BeautifulSoup(message, "lxml")

        # Find all the tags in the HTML code
        for tag in soup.findAll():
            if tag.name not in self.VALID_TAGS:
                tag.hidden = True
            for attr, value in dict(tag.attrs).iteritems():
                if attr not in self.VALID_ATTRS:
                    del tag.attrs[attr]
                # Make sure any src attributes are on an allowed domain
                if attr == 'src':
                    parsed_src = urlparse(value)
                    valid_netlocs = settings.ALLOWED_HOSTS
                    valid_netlocs.append('')
                    if parsed_src.netloc not in valid_netlocs:
                        tag.hidden = True
                if attr == 'href':
                    parsed_src = urlparse(value)
                    if parsed_src.scheme not in self.VALID_SCHEMES:
                        tag.hidden = True

            # All image tags should have a max-width style attribute to ensure
            # they always fit inside any template or email client they're
            # inserted into.
            if self.ADD_MAX_WIDTH and tag.name == 'img':
                tag.attrs['style'] = 'max-width: 100%;'

        # Grab the HTML from BeautifulSoup, return it in UTF8
        return unicode(soup).encode("utf-8", errors="ignore")
dataset/ETHPy150Open ofa/connect/open_connect/connect_core/utils/mixins.py/SanitizeHTMLMixin._cleanse_tags/original