Merge branch 'master' of github.com:FinalsClub/karmaworld
[oweals/karmaworld.git] / karmaworld / apps / notes / models.py
index 5b34fdc4a11ea6dc13b02fcf5ea4fe088afda03e..f34bb333deb06c67bdce09d312544c842f17f83e 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
 
@@ -78,9 +79,24 @@ class Note(models.Model):
         # TODO: If self.name isn't set, generate one based on uploaded_name
         # if we fail to set the Note.name earlier than this, use the saved filename
 
+        # only generate a slug if the name has been set, and slug hasn't
         if not self.slug and self.name:
-            # only generate a slug if the name has been set, and slug hasn't
-            self.slug = defaultfilters.slugify(self.name)
+            slug = defaultfilters.slugify(self.name)
+            cursor = Note.objects.filter(slug=slug)
+            # If there are no other notes with this slug, then the slug does not need an id
+            if cursor.count() == 0:
+                self.slug = slug
+            else:
+                super(Note, self).save(*args, **kwargs) # generate self.id
+                self.slug = defaultfilters.slugify("%s %s" % (self.name, self.id))
+            super(Note, self).save(*args, **kwargs)
+
+        # Check if Note.uploaded_at is after Course.updated_at
+        if self.uploaded_at and self.uploaded_at > self.course.updated_at:
+            self.course.updated_at = self.uploaded_at
+            # if it is, update Course.updated_at
+            self.course.save()
+
         super(Note, self).save(*args, **kwargs)
 
     def get_absolute_url(self):
@@ -94,6 +110,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