コード例 #1
0
ファイル: album.py プロジェクト: emencia/cmsplugin-blocks
class AlbumFactory(factory.django.DjangoModelFactory):
    """
    Factory to create instance of a Album.
    """
    template = get_album_default_template()
    title = factory.Faker("text", max_nb_chars=20)

    class Meta:
        model = Album
コード例 #2
0
def test_album_factory(db):
    """
    Factory should correctly create a new object without any errors
    """
    instance = AlbumFactory()
    assert instance.template == get_album_default_template()

    instance = AlbumFactory(template="foo")
    assert instance.template == "foo"
コード例 #3
0
ファイル: album.py プロジェクト: emencia/cmsplugin-blocks
class Album(CMSPlugin):
    """
    Album container for items.
    """
    title = models.CharField(
        _("Title"),
        blank=False,
        max_length=150,
        default="",
    )
    """
    A required title string.
    """

    template = models.CharField(
        _("Template"),
        blank=False,
        max_length=150,
        choices=get_album_template_choices(),
        default=get_album_default_template(),
        help_text=_("Used template for content formatting."),
    )
    """
    Template choice from available plugin templates in setting
    ``BLOCKS_ALBUM_TEMPLATES``. Default to the first choice item.
    """
    def __str__(self):
        return Truncator(strip_tags(self.title)).words(
            settings.BLOCKS_MODEL_TRUNCATION_LENGTH,
            truncate=settings.BLOCKS_MODEL_TRUNCATION_CHR)

    def copy_relations(self, oldinstance):
        """
        Copy FK relations when plugin object is copied as another object

        See:

        http://docs.django-cms.org/en/latest/how_to/custom_plugins.html#for-foreign-key-relations-from-other-objects

        :meta private:
        """
        self.album_item.all().delete()

        for album_item in oldinstance.album_item.all():
            album_item.pk = None
            album_item.album = self
            album_item.save()

    class Meta:
        verbose_name = _("Album")
        verbose_name_plural = _("Albums")
コード例 #4
0
ファイル: album.py プロジェクト: emencia/cmsplugin-blocks
class AlbumPlugin(CMSPluginBase):
    """
    Album interface is able to add/edit/remove items within inline forms.

    Also used template is dynamically retrieved from "template" value.
    """
    module = _("Blocks")
    name = _("Album")
    model = Album
    form = AlbumForm
    inlines = (AlbumItemAdmin, )
    render_template = get_album_default_template()
    cache = True
    fieldsets = ((None, {
        "fields": (
            "title",
            (
                "template",
                "mass_upload",
            ),
        ),
    }), )

    def render(self, context, instance, placeholder):
        context = super().render(context, instance, placeholder)
        self.render_template = instance.template
        ressources = instance.album_item.all().order_by("order")
        context.update({
            "instance": instance,
            "ressources": ressources,
        })
        return context

    def save_model(self, request, obj, form, change):
        result = super().save_model(request, obj, form, change)

        # Save awaiting item in memory
        for item in getattr(obj, "_awaiting_items", []):
            item.album = obj
            item.save()

        return result
コード例 #5
0
class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('cms', '0016_auto_20160608_1535'),
    ]

    operations = [
        migrations.CreateModel(
            name='Album',
            fields=[
                ('cmsplugin_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='cmsplugin_blocks_album', serialize=False, to='cms.CMSPlugin')),
                ('title', models.CharField(default='', max_length=150, verbose_name='Title')),
                ('template', models.CharField(choices=get_album_template_choices(), default=get_album_default_template(), help_text='Used template for content formatting.', max_length=150, verbose_name='Template')),
            ],
            options={
                'verbose_name_plural': 'Albums',
                'verbose_name': 'Album',
            },
            bases=('cms.cmsplugin',),
        ),
        migrations.CreateModel(
            name='AlbumItem',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(blank=True, default='', max_length=150, verbose_name='Title')),
                ('order', models.IntegerField(default=0, verbose_name='Order')),
                ('image', models.ImageField(default=None, max_length=255, null=True, upload_to='blocks/album/%y/%m', verbose_name='Image')),
                ('album', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='album_item', to='cmsplugin_blocks.Album')),
            ],
            options={
                'verbose_name_plural': 'Album items',
                'verbose_name': 'Album item',
            },
        ),
        migrations.CreateModel(
            name='Card',
            fields=[
                ('cmsplugin_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='cmsplugin_blocks_card', serialize=False, to='cms.CMSPlugin')),
                ('alignment', models.CharField(choices=[('left', 'Content to the left, image to the right'), ('right', 'Image to the left, content to the reft')], default='left', max_length=15, verbose_name='Alignment')),
                ('template', models.CharField(choices=get_card_template_choices(), default=get_card_default_template(), help_text='Used template for content look.', max_length=150, verbose_name='Template')),
                ('image', models.ImageField(blank=True, default=None, max_length=255, null=True, upload_to='blocks/card/%y/%m', verbose_name='Image')),
                ('content', models.TextField(default='', verbose_name='Content')),
            ],
            options={
                'verbose_name_plural': 'Cards',
                'verbose_name': 'Card',
            },
            bases=('cms.cmsplugin',),
        ),
        migrations.CreateModel(
            name='Hero',
            fields=[
                ('cmsplugin_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='cmsplugin_blocks_hero', serialize=False, to='cms.CMSPlugin')),
                ('template', models.CharField(choices=get_hero_template_choices(), default=get_hero_default_template(), help_text='Used template for content look.', max_length=150, verbose_name='Template')),
                ('image', models.ImageField(blank=True, default=None, max_length=255, null=True, upload_to='blocks/hero/%y/%m', verbose_name='Image')),
                ('content', models.TextField(default='', verbose_name='Content')),
            ],
            options={
                'verbose_name_plural': 'Heros',
                'verbose_name': 'Hero',
            },
            bases=('cms.cmsplugin',),
        ),
        migrations.CreateModel(
            name='SlideItem',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('image', models.ImageField(default=None, max_length=255, null=True, upload_to='blocks/slider/%y/%m', verbose_name='Image')),
                ('content', models.TextField(blank=True, default='', verbose_name='Content')),
                ('link_name', models.CharField(blank=True, max_length=45, verbose_name='link name')),
                ('link_url', models.CharField(blank=True, max_length=255, verbose_name='link url')),
                ('link_open_blank', models.BooleanField(default=False, help_text='If checked the link will be open in a new window', verbose_name='open new window')),
            ],
            options={
                'verbose_name_plural': 'Slide items',
                'verbose_name': 'Slide item',
            },
        ),
        migrations.CreateModel(
            name='Slider',
            fields=[
                ('cmsplugin_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='cmsplugin_blocks_slider', serialize=False, to='cms.CMSPlugin')),
                ('title', models.CharField(default='', max_length=150, verbose_name='Title')),
                ('template', models.CharField(choices=get_slider_template_choices(), default=get_slider_default_template(), help_text='Used template for content look.', max_length=150, verbose_name='Template')),
            ],
            options={
                'verbose_name_plural': 'Sliders',
                'verbose_name': 'Slider',
            },
            bases=('cms.cmsplugin',),
        ),
        migrations.AddField(
            model_name='slideitem',
            name='slider',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='slide_item', to='cmsplugin_blocks.Slider'),
        ),
    ]