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()
--- /dev/null
+#!/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)
+
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
# 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