Пример #1
0
class BasePage(Orderable, Displayable):
    """
    Exists solely to store ``PageManager`` as the main manager.
    If it's defined on ``Page``, a concrete model, then each
    ``Page`` subclass loses the custom manager.
    """

    objects = wrapped_manager(PageManager)

    class Meta:
        abstract = True
class AbstractBaseField(Orderable):
    """
    A field for a user-built form.
    """

    label = models.TextField(_("Label"))
    field_type = models.IntegerField(_("Type"), choices=fields.NAMES)
    required = models.BooleanField(_("Required"), default=True)
    visible = models.BooleanField(_("Visible"), default=True)
    choices = models.CharField(
        _("Choices"),
        max_length=1000,
        blank=True,
        help_text=_(
            "Comma separated options where applicable. If an option "
            "itself contains commas, surround the option with `backticks`."),
    )
    default = models.CharField(_("Default value"),
                               blank=True,
                               max_length=settings.FORMS_FIELD_MAX_LENGTH)
    placeholder_text = models.CharField(_("Placeholder Text"),
                                        blank=True,
                                        max_length=100)
    help_text = models.TextField(_("Help text"), blank=True)

    objects = wrapped_manager(FieldManager)

    class Meta:
        abstract = True
        verbose_name = _("Field")
        verbose_name_plural = _("Fields")

    def __str__(self):
        return self.label

    def get_choices(self):
        """
        Parse a comma separated choice string into a list of choices taking
        into account quoted choices.
        """
        choice = ""
        (quote, unquote) = ("`", "`")
        quoted = False
        for char in self.choices:
            if not quoted and char == quote:
                quoted = True
            elif quoted and char == unquote:
                quoted = False
            elif char == "," and not quoted:
                choice = choice.strip()
                if choice:
                    yield choice, choice
                choice = ""
            else:
                choice += char
        choice = choice.strip()
        if choice:
            yield choice, choice

    def is_a(self, *args):
        """
        Helper that returns ``True`` if the field's type is given in any arg.
        """
        return self.field_type in args