'This will extract the HTML of a rendered XBlock from a
page. This should be simple. This should just be (in lxml):
usage_id = self.xblocks[block_urlname].scope_ids.usage_id
encoded_id = usage_id.replace(";_", "/")
Followed by:
page_xml = defusedxml.ElementTree.parse(StringIO.StringIO(response_content))
page_xml.find("//[@data-usage-id={usage}]".format(usage=encoded_id))
or
soup_html = BeautifulSoup(response_content, \'html.parser\')
soup_html.find(**{"data-usage-id": encoded_id})
Why isn\'t it? Well, the blocks are stored in a rather funky
way in learning sequences. Ugh. Easy enough, populate the
course with just verticals. Well, that doesn\'t work
either. The whole test infrastructure populates courses with
Studio AJAX calls, and Studio has broken support for anything
other than course/sequence/vertical/block.
So until we either fix Studio to support most course
structures, fix learning sequences to not have HTML-in-JS
(which causes many other problems as well -- including
user-facing bugs), or fix the test infrastructure to
create courses from OLX, we\'re stuck with this little hack.'

def extract_block_html(self, content, urlname):

    usage_id = self.xblocks[urlname].scope_ids.usage_id
    soup_html = BeautifulSoup(content)
    xblock_html = unicode(soup_html.find(id='seq_contents_0'))
    try:
        escaped_html = xblock_html.split('<')[1].split('>')[1]
    except IndexError:
        print >>sys.stderr, 'XBlock    page    could    not    render'
        print >>sys.stderr, '(Often,    a    redirect    if    e.g.    not    logged    in)'
        print >>sys.stderr, 'URL    Name:', repr(urlname)
        print >>sys.stderr, 'Usage    ID', repr(usage_id)
        print >>sys.stderr, 'Content', repr(content)
        print >>sys.stderr, 'Split    1', repr(xblock_html.split('<'))
        print >>sys.stderr, 'Dice    1:', repr(xblock_html.split('<')[1])
        print >>sys.stderr, 'Split    2', repr(xblock_html.split('<')[1].split('>'))
        print >>sys.stderr, 'Dice    2', repr(xblock_html.split('<')[1].split('>')[1])
        raise
    decoded_html = HTMLParser.HTMLParser().unescape(escaped_html).strip()
    return decoded_html
