Esempio n. 1
0
class MultiLessonForm(ModelForm):
    class Meta:
        model = Lesson
        fields = [
            'title', 'week', 'number', 'pacing_weight', 'unplugged', 'keywords'
        ]

    keywords = KeywordsField()
Esempio n. 2
0
class LessonInline(TabularDynamicInlineAdmin):
    model = Lesson
    sortable_field_name = "number"
    fields = [
        'title', 'week', 'number', 'pacing_weight', 'unplugged', 'keywords'
    ]

    keywords = KeywordsField()
Esempio n. 3
0
class MetaData(models.Model):
    """
    Abstract model that provides meta data for content.
    """

    description = models.TextField(_("Description"), blank=True)
    keywords = KeywordsField(verbose_name=_("Keywords"))

    class Meta:
        abstract = True
Esempio n. 4
0
class LessonInline(TabularDynamicInlineAdmin):
    model = Lesson
    fk_name = 'unit'
    fields = [
        'number', 'title', 'week', 'pacing_weight', 'unplugged', 'keywords'
    ]

    keywords = KeywordsField()

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False
Esempio n. 5
0
class Dump(Slugged, TimeStamped, Ownable):
    """A Dump is a link to be shared with the world.

    .. attribute:: link

        The link of the Dump.

    .. attribute:: description

        A brief description about the Dump.

    .. attribute:: views

        The number of views this Dump has received.

    .. attribute:: category

        The :class:`DumpCategory` the Dump belongs to.

    .. attribute:: tags

        Any tags the Dump is labelled with.

    """
    link = models.URLField(verbose_name=_("External URL"))
    description = models.CharField(max_length=200, blank=True)
    views = models.IntegerField(default=0, editable=False)
    category = models.ForeignKey("DumpCategory",
                                 verbose_name=_("Category"),
                                 related_name="dumps")
    tags = KeywordsField(verbose_name=_("Tags"))

    class Meta:
        verbose_name = _("Link")
        verbose_name_plural = _("Links")
        ordering = ("title", )

    def save(self, *args, **kwargs):
        """Randomly generate a slug if one is not entered."""
        if not self.slug:
            self.slug = _generate_random_slug()
        super(Dump, self).save(*args, **kwargs)

    def get_absolute_url(self):
        """Return the redirecting URL."""
        return reverse("linkdump.views.link_dump_redirect",
                       kwargs={'dump_slug': self.slug})
Esempio n. 6
0
class MetaData(models.Model):
    """
    Abstract model that provides meta data for content.
    """

    _meta_title = models.CharField(
        _("Title"),
        null=True,
        blank=True,
        max_length=500,
        help_text=_("Optional title to be used in the HTML title tag. "
                    "If left blank, the main title field will be used."))
    description = models.TextField(_("Description"), blank=True)
    gen_description = models.BooleanField(
        _("Generate description"),
        help_text=_("If checked, the description will be automatically "
                    "generated from content. Uncheck if you want to manually "
                    "set a custom description."),
        default=True)
    keywords = KeywordsField(verbose_name=_("Keywords"))

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        """
        Set the description field on save.
        """
        if self.gen_description:
            self.description = strip_tags(self.description_from_content())
        super(MetaData, self).save(*args, **kwargs)

    def meta_title(self):
        """
        Accessor for the optional ``_meta_title`` field, which returns
        the string version of the instance if not provided.
        """
        return self._meta_title or str(self)

    def description_from_content(self):
        """
        Returns the first block or sentence of the first content-like
        field.
        """
        description = ""
        # Use the first RichTextField, or TextField if none found.
        for field_type in (RichTextField, models.TextField):
            if not description:
                for field in self._meta.fields:
                    if isinstance(field, field_type) and \
                        field.name != "description":
                        description = getattr(self, field.name)
                        if description:
                            from mezzanine.core.templatetags.mezzanine_tags \
                            import richtext_filters
                            description = richtext_filters(description)
                            break
        # Fall back to the title if description couldn't be determined.
        if not description:
            description = str(self)
        # Strip everything after the first block or sentence.
        ends = ("</p>", "<br />", "<br/>", "<br>", "</ul>", "\n", ". ", "! ",
                "? ")
        for end in ends:
            pos = description.lower().find(end)
            if pos > -1:
                description = TagCloser(description[:pos]).html
                break
        else:
            description = truncatewords_html(description, 100)
        return description
Esempio n. 7
0
class Displayable(Slugged):
    """
    Abstract model that provides features of a visible page on the 
    website such as publishing fields and meta data.
    """

    status = models.IntegerField(_("Status"),
                                 choices=CONTENT_STATUS_CHOICES,
                                 default=CONTENT_STATUS_DRAFT)
    publish_date = models.DateTimeField(
        _("Published from"),
        help_text=_("With published checked, won't be shown until this time"),
        blank=True,
        null=True)
    expiry_date = models.DateTimeField(
        _("Expires on"),
        help_text=_("With published checked, won't be shown after this time"),
        blank=True,
        null=True)
    description = models.TextField(_("Description"), blank=True)
    keywords = KeywordsField(verbose_name=_("Keywords"))
    short_url = models.URLField(blank=True, null=True)

    objects = DisplayableManager()
    search_fields = {"keywords": 10, "title": 5}

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        """
        Set default for ``publsh_date`` and ``description`` if none 
        given.
        """
        if self.publish_date is None:
            # publish_date will be blank when a blog post is created
            # from the quick blog form in the admin dashboard.
            self.publish_date = datetime.now()
        if not self.description:
            self.description = strip_tags(self.description_from_content())
        super(Displayable, self).save(*args, **kwargs)

    def description_from_content(self):
        """
        Returns the first paragraph of the first content-like field.
        """
        description = ""
        # Use the first HTMLField, or TextField if none found.
        for field_type in (HtmlField, models.TextField):
            if not description:
                for field in self._meta.fields:
                    if isinstance(field, field_type) and \
                        field.name != "description":
                        description = getattr(self, field.name)
                        if description:
                            break
        # Fall back to the title if description couldn't be determined.
        if not description:
            description = self.title
        # Strip everything after the first paragraph or sentence.
        for end in ("</p>", "<br />", "\n", ". "):
            if end in description:
                description = description.split(end)[0] + end
                break
        else:
            description = truncatewords_html(description, 100)
        return description

    def admin_link(self):
        return "<a href='%s'>%s</a>" % (self.get_absolute_url(),
                                        ugettext("View on site"))

    admin_link.allow_tags = True
    admin_link.short_description = ""