예제 #1
0
class PollContent(models.Model):
    poll = models.ForeignKey(Poll, on_delete=models.CASCADE)
    language = models.TextField()
    text = models.TextField()
    placeholders = PlaceholderRelationField()

    @cached_property
    def placeholder(self):
        try:
            return self.placeholders.get(slot='content')
        except Placeholder.DoesNotExist:
            from cms.utils.placeholder import rescan_placeholders_for_obj
            rescan_placeholders_for_obj(self)
            return self.placeholders.get(slot='content')

    def __str__(self):
        return self.text

    def get_absolute_url(self):
        return reverse("admin:polls_pollcontent_changelist")

    def get_placeholders(self):
        return [self.placeholder]

    def get_template(self):
        return 'polls/poll_content.html'
예제 #2
0
class FancyPoll(models.Model):
    name = models.CharField(max_length=255)
    template = models.CharField(max_length=255, default='fancy_poll_app/detail.html')
    placeholders = PlaceholderRelationField()

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('fancy_poll_detail_view', args=[self.pk])

    def get_template(self):
        return self.template
예제 #3
0
class AliasContent(models.Model):
    alias = models.ForeignKey(
        Alias,
        on_delete=models.CASCADE,
        related_name='contents',
    )
    name = models.CharField(
        verbose_name=_('name'),
        max_length=120,
    )
    placeholders = PlaceholderRelationField()
    placeholder_slotname = 'content'
    language = models.CharField(
        max_length=10,
        choices=settings.LANGUAGES,
        default=get_current_language,
    )

    class Meta:
        verbose_name = _('alias content')
        verbose_name_plural = _('alias contents')

    def __str__(self):
        return '{} ({})'.format(self.name, self.language)

    @cached_property
    def placeholder(self):
        try:
            return self.placeholders.get(slot=self.placeholder_slotname)
        except Placeholder.DoesNotExist:
            from cms.utils.placeholder import rescan_placeholders_for_obj
            rescan_placeholders_for_obj(self)
            return self.placeholders.get(slot=self.placeholder_slotname)

    def get_placeholders(self):
        return [self.placeholder]

    def get_absolute_url(self):
        return get_object_preview_url(self)

    def get_template(self):
        return 'djangocms_alias/alias_content.html'

    @transaction.atomic
    def delete(self, *args, **kwargs):
        super().delete(*args, **kwargs)
        self.alias.cms_plugins.filter(language=self.language).delete()

    @transaction.atomic
    def populate(self,
                 replaced_placeholder=None,
                 replaced_plugin=None,
                 plugins=None):
        if not replaced_placeholder and not replaced_plugin:
            copy_plugins_to_placeholder(
                plugins,
                placeholder=self.placeholder,
            )
            return

        if replaced_placeholder:
            plugins = replaced_placeholder.get_plugins(self.language)
            placeholder = replaced_placeholder
            add_plugin_kwargs = {}
        else:
            plugins = CMSPlugin.objects.filter(
                id__in=[replaced_plugin.pk] +
                replaced_plugin._get_descendants_ids(), )
            placeholder = replaced_plugin.placeholder
            add_plugin_kwargs = {'position': 'left', 'target': replaced_plugin}

        copy_plugins_to_placeholder(
            plugins,
            placeholder=self.placeholder,
            language=self.language,
        )
        plugins.delete()
        placeholder._recalculate_plugin_positions(self.language)

        new_plugin = add_plugin(placeholder,
                                plugin_type='Alias',
                                language=self.language,
                                alias=self.alias,
                                **add_plugin_kwargs)
        if replaced_plugin:
            new_plugin.position = replaced_plugin.position
            new_plugin.save(update_fields=['position'])
        return new_plugin