Exemple #1
0
    note = models.OneToOneField(Note, primary_key=True)
    markdown = models.TextField(blank=True, null=True)
    html = models.TextField(blank=True, null=True)

    def save(self, *args, **kwargs):
        if self.markdown and not self.html:
            self.html = markdown.markdown(self.markdown)
        if self.note.is_editable():
            self.html = sanitizer.sanitize_html_to_editable(self.html)
        else:
            self.html = sanitizer.sanitize_html_preserve_formatting(self.html)

        super(NoteMarkdown, self).save(*args, **kwargs)


auto_add_check_unique_together(Note)


def update_note_counts(note_instance):
    try:
        # test if the course still exists, or if this is a cascade delete.
        note_instance.course
    except Course.DoesNotExist:
        # this is a cascade delete. there is no course to update
        pass
    else:
        # course exists
        note_instance.course.update_thank_count()
        note_instance.course.update_note_count()
        if note_instance.course.school:
            note_instance.course.school.update_note_count()
Exemple #2
0
        # Run through all associated professors and concatenate their names.
        return ','.join(self.professor.values_list('email', flat=True))


reversion.register(Course)


@register_channel_name('course_name_by_name')
class CourseNameLookup(FieldLookupChannel):
    """
    Handles AJAX lookups against the course model's name field.
    Returns just the matching field values.
    """
    model = Course
    field_lookup = 'name'

    def get_query(self, q, request):
        """ Return only the list of name fields. """
        # Find the matching objects.
        results = super(CourseNameLookup, self).get_query(q, request)
        # Only return the name field, not the object.
        return results.values_list(self.field_lookup, flat=True)


# Enforce unique constraints even when we're using a database like
# SQLite that doesn't understand them
auto_add_check_unique_together(Course)
auto_add_check_unique_together(School)
auto_add_check_unique_together(Department)
auto_add_check_unique_together(Professor)
Exemple #3
0
        if self.uploaded_at and self.uploaded_at > self.course.updated_at:
            self._update_parent_updated_at()
        super(Note, self).save(*args, **kwargs)

    def has_markdown(self):
        return hasattr(self, "notemarkdown")

    def is_pdf(self):
        return self.mimetype in Note.PDF_MIMETYPES


class NoteMarkdown(models.Model):
    note     = models.OneToOneField(Note, primary_key=True)
    markdown = models.TextField(blank=True, null=True)

auto_add_check_unique_together(Note)


def update_note_counts(note_instance):
    try:
        # test if the course still exists, or if this is a cascade delete.
        note_instance.course
    except Course.DoesNotExist:
        # this is a cascade delete. there is no course to update
        pass
    else:
        # course exists
        note_instance.course.update_thank_count()
        note_instance.course.update_note_count()
        if note_instance.course.school:
            note_instance.course.school.update_note_count()
Exemple #4
0
            return str(self.instructor_email)
        # Run through all associated professors and concatenate their names.
        return ','.join(self.professor.values_list('email', flat=True))

reversion.register(Course)


@register_channel_name('course_name_by_name')
class CourseNameLookup(FieldLookupChannel):
    """
    Handles AJAX lookups against the course model's name field.
    Returns just the matching field values.
    """
    model = Course
    field_lookup = 'name'

    def get_query(self, q, request):
        """ Return only the list of name fields. """
        # Find the matching objects.
        results = super(CourseNameLookup, self).get_query(q, request)
        # Only return the name field, not the object.
        return results.values_list(self.field_lookup, flat=True)


# Enforce unique constraints even when we're using a database like
# SQLite that doesn't understand them
auto_add_check_unique_together(Course)
auto_add_check_unique_together(School)
auto_add_check_unique_together(Department)
auto_add_check_unique_together(Professor)
Exemple #5
0
    def convert_to_note(self):
        """ polymorph this object into a note.models.Note object  """
        # TODO move this to Note. superclasses should not care about subclasses,
        # but subclasses should care about parents.

        # Note inherits all fields of Document as does RawDocument.
        # Dynamically refer to all fields of RawDocument found within Document
        # and also Note.
        initdict = {}
        for field in Document._meta.get_all_field_names():
            if field in ('tags',):
                # TaggableManager does not play well with init()
                continue
            initdict[field] = getattr(self,field)
        # Create a new Note using all fields from the Document
        note = Note(**initdict)
        note.save()
        for tag in self.tags.all():
            note.tags.add(tag)
        return note

    def save(self, user=None, *args, **kwargs):
        super(RawDocument, self).save(*args, **kwargs)

    def process_document(self, user=None):
        if not self.is_processed:
            tasks.process_raw_document.delay(self, user)


auto_add_check_unique_together(RawDocument)
Exemple #6
0
    def convert_to_note(self):
        """ polymorph this object into a note.models.Note object  """
        # TODO move this to Note. superclasses should not care about subclasses,
        # but subclasses should care about parents.

        # Note inherits all fields of Document as does RawDocument.
        # Dynamically refer to all fields of RawDocument found within Document
        # and also Note.
        initdict = {}
        for field in Document._meta.get_all_field_names():
            if field in ('tags', ):
                # TaggableManager does not play well with init()
                continue
            initdict[field] = getattr(self, field)
        # Create a new Note using all fields from the Document
        note = Note(**initdict)
        note.save()
        for tag in self.tags.all():
            note.tags.add(tag)
        return note

    def save(self, user=None, *args, **kwargs):
        super(RawDocument, self).save(*args, **kwargs)

    def process_document(self, user=None):
        if not self.is_processed:
            tasks.process_raw_document.delay(self, user)


auto_add_check_unique_together(RawDocument)