From b7ebe2b1d390232a16618977fb3b19cfa790f7b9 Mon Sep 17 00:00:00 2001 From: Seth Woodworth Date: Thu, 18 Apr 2013 17:28:35 -0400 Subject: [PATCH] process note html for a target=_blank on file upload and adding managment command for old files --- karmaworld/apps/notes/gdrive.py | 3 +++ .../management/commands/process_note_html.py | 24 +++++++++++++++++ karmaworld/apps/notes/models.py | 27 ++++++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 karmaworld/apps/notes/management/commands/process_note_html.py diff --git a/karmaworld/apps/notes/gdrive.py b/karmaworld/apps/notes/gdrive.py index 4c4f5fb..e5a8b87 100644 --- a/karmaworld/apps/notes/gdrive.py +++ b/karmaworld/apps/notes/gdrive.py @@ -166,5 +166,8 @@ def convert_with_google_drive(note): new_note.html = content_dict['html'] new_note.text = content_dict['text'] + # before we save new html, sanitize a tags in note.html + new_note.sanitize_html(save=False) + # Finally, save whatever data we got back from google new_note.save() diff --git a/karmaworld/apps/notes/management/commands/process_note_html.py b/karmaworld/apps/notes/management/commands/process_note_html.py new file mode 100644 index 0000000..9f44198 --- /dev/null +++ b/karmaworld/apps/notes/management/commands/process_note_html.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# -*- coding:utf8 -*- +# Copyright (C) 2012 FinalsClub Foundation + +from lxml.html import fromstring, tostring + +from django.core.management.base import BaseCommand +from apps.notes.models import Note + +class Command(BaseCommand): + args = 'none' + help = "Process note.html and modify a tags to open in new window" + + def add_target(self, tag): + tag.attrib['target'] = '_blank' + + def handle(self, *args, **kwargs): + notes = Note.objects.filter(html__isnull=False) + + for note in notes: + succ, data = note.sanitize_html() + if succ: + print "Note %s contained %s s" % (note.id, data) + diff --git a/karmaworld/apps/notes/models.py b/karmaworld/apps/notes/models.py index 8cf83b3..a960621 100644 --- a/karmaworld/apps/notes/models.py +++ b/karmaworld/apps/notes/models.py @@ -12,8 +12,9 @@ from django.conf import settings from django.core.files.storage import FileSystemStorage from django.db import models from django.template import defaultfilters -from taggit.managers import TaggableManager +from lxml.html import fromstring, tostring from oauth2client.client import Credentials +from taggit.managers import TaggableManager from karmaworld.apps.courses.models import Course @@ -109,6 +110,30 @@ class Note(models.Model): # return a url ending in id return u"/{0}/{1}/{2}".format(self.course.school.slug, self.course.slug, self.id) + def sanitize_html(self): + """ if self contains html, find all tags and add target=_blank + takes self + returns True/False on succ/fail and error or count + """ + # build a tag sanitizer + def add_attribute_target(tag): + tag.attrib['target'] = '_blank' + + # if no html, return false + if not self.html: + return False, "Note has no html" + + _html = fromstring(self.html) + a_tags = _html.findall('.//a') # recursively find all a tags in document tree + # if there are a tags + if a_tags > 1: + #apply the add attribute function + map(add_attribute_target, a_tags) + self.html = _html + self.save() + return True, len(a_tags) + + class DriveAuth(models.Model): """ stored google drive authentication and refresh token -- 2.25.1