Permalink
Browse files

Showing OPML errors and fixing unicode decode error by sanitizing OPM…

…L uploads.
  • Loading branch information...
1 parent 687c044 commit 4b5fce6ce7adae499b0eca97e9a7d75b67cb09a7 @samuelclay committed Aug 8, 2013
Showing with 30 additions and 21 deletions.
  1. +2 −1 apps/feed_import/models.py
  2. +18 −19 apps/feed_import/views.py
  3. +10 −1 media/js/newsblur/reader/reader_intro.js
@@ -127,7 +127,8 @@ def try_processing(self):
def process(self):
# self.clear_feeds()
- outline = opml.from_string(str(self.opml_xml))
+
+ outline = opml.from_string(self.opml_xml)
folders = self.get_folders()
try:
folders = self.process_outline(outline, folders)
View
@@ -35,33 +35,32 @@ def opml_upload(request):
if 'file' in request.FILES:
logging.user(request, "~FR~SBOPML upload starting...")
file = request.FILES['file']
- xml_opml = file.read()
+ xml_opml = str(file.read().decode('utf-8', 'ignore'))
try:
- uploaded_opml = UploadedOPML.objects.create(user_id=request.user.pk, opml_file=xml_opml)
+ UploadedOPML.objects.create(user_id=request.user.pk, opml_file=xml_opml)
except (UnicodeDecodeError, InvalidStringData):
- uploaded_opml = None
folders = None
code = -1
message = "There was a Unicode decode error when reading your OPML file."
- if uploaded_opml:
- opml_importer = OPMLImporter(xml_opml, request.user)
- try:
- folders = opml_importer.try_processing()
- except TimeoutError:
- folders = None
- ProcessOPML.delay(request.user.pk)
- feed_count = opml_importer.count_feeds_in_opml()
- logging.user(request, "~FR~SBOPML pload took too long, found %s feeds. Tasking..." % feed_count)
- payload = dict(folders=folders, delayed=True, feed_count=feed_count)
- code = 2
- message = ""
- except AttributeError:
- code = -1
- message = "OPML import failed. Couldn't parse XML file."
- folders = None
+ opml_importer = OPMLImporter(xml_opml, request.user)
+ try:
+ folders = opml_importer.try_processing()
+ except TimeoutError:
+ folders = None
+ ProcessOPML.delay(request.user.pk)
+ feed_count = opml_importer.count_feeds_in_opml()
+ logging.user(request, "~FR~SBOPML pload took too long, found %s feeds. Tasking..." % feed_count)
+ payload = dict(folders=folders, delayed=True, feed_count=feed_count)
+ code = 2
+ message = ""
+ except AttributeError:
+ code = -1
+ message = "OPML import failed. Couldn't parse XML file."
+ folders = None
if folders:
+ code = 1
feeds = UserSubscription.objects.filter(user=request.user).values()
payload = dict(folders=folders, feeds=feeds)
logging.user(request, "~FR~SBOPML Upload: ~SK%s~SN~SB~FR feeds" % (len(feeds)))
@@ -608,10 +608,14 @@ _.extend(NEWSBLUR.ReaderIntro.prototype, {
$('.NB-intro-import-delayed', self.$modal).show();
$('.NB-intro-import-restart', self.$modal).hide();
$('.NB-intro-import-message', self.$modal).hide();
+ } else if (data.code < 0) {
+ $('.NB-intro-import-delayed', self.$modal).hide();
+ $('.NB-intro-import-restart', self.$modal).hide();
+ $('.NB-intro-import-message', self.$modal).show().addClass('NB-error').text(data.message);
} else {
$('.NB-intro-import-delayed', self.$modal).hide();
$('.NB-intro-import-restart', self.$modal).show();
- $('.NB-intro-import-message', self.$modal).show().text("All done!");
+ $('.NB-intro-import-message', self.$modal).show().removeClass('NB-error').text("All done!");
}
}, this));
},
@@ -631,6 +635,7 @@ _.extend(NEWSBLUR.ReaderIntro.prototype, {
dataType: 'json',
success: function (data, status) {
NEWSBLUR.assets.load_feeds(function() {
+ console.log(["opml upload", data, status]);
$loading.removeClass('NB-active');
self.advance_import_carousel(2);
if (data.payload.delayed) {
@@ -639,6 +644,10 @@ _.extend(NEWSBLUR.ReaderIntro.prototype, {
$('.NB-intro-import-delayed', self.$modal).show();
$('.NB-intro-import-restart', self.$modal).hide();
$('.NB-intro-import-message', self.$modal).hide();
+ } else if (data.code < 0) {
+ $('.NB-intro-import-delayed', self.$modal).hide();
+ $('.NB-intro-import-restart', self.$modal).show();
+ $('.NB-intro-import-message', self.$modal).addClass('NB-error').show().text(data.message);
} else {
$('.NB-intro-import-message', self.$modal).text("All done!").removeClass('NB-error').show();
$('.NB-intro-import-delayed', self.$modal).hide();

0 comments on commit 4b5fce6

Please sign in to comment.