From 1708dc40808ac2c8155ab7bf0ff3bbe62d295b0c Mon Sep 17 00:00:00 2001 From: Don Mitchell Date: Tue, 8 Jan 2013 15:13:37 -0500 Subject: [PATCH] Use html rather than xml parsing for course updates --- .../contentstore/course_info_model.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/cms/djangoapps/contentstore/course_info_model.py b/cms/djangoapps/contentstore/course_info_model.py index c2e8348a66..8d488c2550 100644 --- a/cms/djangoapps/contentstore/course_info_model.py +++ b/cms/djangoapps/contentstore/course_info_model.py @@ -1,7 +1,7 @@ from xmodule.modulestore.exceptions import ItemNotFoundError from xmodule.modulestore import Location from xmodule.modulestore.django import modulestore -from lxml import etree +from lxml import html import re from django.http import HttpResponseBadRequest import logging @@ -24,9 +24,9 @@ def get_course_updates(location): # purely to handle free formed updates not done via editor. Actually kills them, but at least doesn't break. try: - course_html_parsed = etree.fromstring(course_updates.definition['data']) - except etree.XMLSyntaxError: - course_html_parsed = etree.fromstring("
    ") + course_html_parsed = html.fromstring(course_updates.definition['data']) + except: + course_html_parsed = html.fromstring("
      ") # Confirm that root is
        , iterate over
      1. , pull out

        subs and then rest of val course_upd_collection = [] @@ -39,7 +39,7 @@ def get_course_updates(location): # could enforce that update[0].tag == 'h2' content = update[0].tail else: - content = "\n".join([etree.tostring(ele) for ele in update[1:]]) + content = "\n".join([html.tostring(ele) for ele in update[1:]]) # make the id on the client be 1..len w/ 1 being the oldest and len being the newest course_upd_collection.append({"id" : location_base + "/" + str(len(course_html_parsed) - idx), @@ -61,12 +61,12 @@ def update_course_updates(location, update, passed_id=None): # purely to handle free formed updates not done via editor. Actually kills them, but at least doesn't break. try: - course_html_parsed = etree.fromstring(course_updates.definition['data']) - except etree.XMLSyntaxError: - course_html_parsed = etree.fromstring("
          ") + course_html_parsed = html.fromstring(course_updates.definition['data']) + except: + course_html_parsed = html.fromstring("
            ") # No try/catch b/c failure generates an error back to client - new_html_parsed = etree.fromstring('
          1. ' + update['date'] + '

            ' + update['content'] + '
          2. ') + new_html_parsed = html.fromstring('
          3. ' + update['date'] + '

            ' + update['content'] + '
          4. ') # Confirm that root is
              , iterate over
            1. , pull out

              subs and then rest of val if course_html_parsed.tag == 'ol': @@ -82,7 +82,7 @@ def update_course_updates(location, update, passed_id=None): passed_id = course_updates.location.url() + "/" + str(idx) # update db record - course_updates.definition['data'] = etree.tostring(course_html_parsed) + course_updates.definition['data'] = html.tostring(course_html_parsed) modulestore('direct').update_item(location, course_updates.definition['data']) return {"id" : passed_id, @@ -105,9 +105,9 @@ def delete_course_update(location, update, passed_id): # TODO use delete_blank_text parser throughout and cache as a static var in a class # purely to handle free formed updates not done via editor. Actually kills them, but at least doesn't break. try: - course_html_parsed = etree.fromstring(course_updates.definition['data']) - except etree.XMLSyntaxError: - course_html_parsed = etree.fromstring("
                ") + course_html_parsed = html.fromstring(course_updates.definition['data']) + except: + course_html_parsed = html.fromstring("
                  ") if course_html_parsed.tag == 'ol': # ??? Should this use the id in the json or in the url or does it matter? @@ -118,7 +118,7 @@ def delete_course_update(location, update, passed_id): course_html_parsed.remove(element_to_delete) # update db record - course_updates.definition['data'] = etree.tostring(course_html_parsed) + course_updates.definition['data'] = html.tostring(course_html_parsed) store = modulestore('direct') store.update_item(location, course_updates.definition['data'])