process note html for a target=_blank on file upload and adding managment command...
authorSeth Woodworth <seth@sethish.com>
Thu, 18 Apr 2013 21:28:35 +0000 (17:28 -0400)
committerSeth Woodworth <seth@sethish.com>
Thu, 18 Apr 2013 21:28:35 +0000 (17:28 -0400)
karmaworld/apps/notes/gdrive.py
karmaworld/apps/notes/management/commands/process_note_html.py [new file with mode: 0644]
karmaworld/apps/notes/models.py

index 4c4f5fb9d9b2cd386f427420ae5dbc860198b1b5..e5a8b877dc1d4211cfdbdad42add375d1949c8ce 100644 (file)
@@ -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 (file)
index 0000000..9f44198
--- /dev/null
@@ -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 <a>s" % (note.id, data)
+
index 8cf83b34738839793b78e49b9ac195769767c720..a9606211aac9a20afc4a12937cb15d155a466384 100644 (file)
@@ -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 <a> 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