Merge branch 'ppt-gdrive' of https://github.com/FinalsClub/karmaworld into ppt-gdrive
[oweals/karmaworld.git] / karmaworld / apps / notes / models.py
index 8cf83b34738839793b78e49b9ac195769767c720..0552cc933966c433b750fd50621ccd06aedf945f 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
 
@@ -52,10 +53,15 @@ class Note(models.Model):
                             blank=True, null=True)
 
     # Upload files to MEDIA_ROOT/notes/YEAR/MONTH/DAY, 2012/10/30/filename
+    # FIXME: because we are adding files by hand in tasks.py, upload_to is being ignored for media files
     note_file       = models.FileField(
                             storage=fs,
                             upload_to="notes/%Y/%m/%j/",
                             blank=True, null=True)
+    pdf_file       = models.FileField(
+                            storage=fs,
+                            upload_to="notes/%Y/%m/%j/",
+                            blank=True, null=True)
 
     ## post gdrive conversion data
     embed_url       = models.URLField(max_length=1024, blank=True, null=True)
@@ -109,6 +115,31 @@ 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, save=True):
+        """ 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
+            if save:
+                self.save()
+            return True, len(a_tags)
+
+
 
 class DriveAuth(models.Model):
     """ stored google drive authentication and refresh token