Skip to content

Commit

Permalink
refactor: add related_name to carousel
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelzinh3 committed Oct 20, 2023
1 parent 9cebc0d commit 78a1cf8
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 43 deletions.
11 changes: 4 additions & 7 deletions app/contrib/frontend/landpage/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from contrib.bonde.models import Community

from .models import Navbar, CarouselBlock, CarouselItem, Footer
from .models import Navbar, CarouselPlugin, CarouselItem, Footer
from .forms import LayoutBlockForm, LayoutBlockPressureForm

# from .layouts import Layout
Expand Down Expand Up @@ -86,25 +86,22 @@ def render(self, context, instance, placeholder):

class CarouselItemInline(admin.StackedInline):
model = CarouselItem
fk_name = 'block'


@plugin_pool.register_plugin
class CarouselBlockPlugin(CMSPluginBase):
model = CarouselBlock
class CarouselPlugin(CMSPluginBase):
model = CarouselPlugin
name = "Bloco de Carousel"
inlines = [CarouselItemInline]
render_template = "frontend/landpage/plugins/carousel.html"

def render(self, context, instance, placeholder):
ctx = super().render(context, instance, placeholder)
items = instance.carousel_items.all()
items = instance.carousel_item.all()

print("Items in render:", items)

ctx.update({
"title": instance.title,
"description": instance.description,
'items': items,
})
return ctx
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2 on 2023-10-19 23:00
# Generated by Django 3.2 on 2023-10-20 18:41

import colorfield.fields
from django.conf import settings
Expand All @@ -10,16 +10,16 @@
class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.FILER_IMAGE_MODEL),
('cms', '0022_auto_20180620_1551'),
migrations.swappable_dependency(settings.FILER_IMAGE_MODEL),
('landpage', '0007_merge_0006_auto_20230821_2039_0006_auto_20230822_1436'),
]

operations = [
migrations.CreateModel(
name='CarouselBlock',
name='CarouselPlugin',
fields=[
('cmsplugin_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='landpage_carouselblock', serialize=False, to='cms.cmsplugin')),
('cmsplugin_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='landpage_carouselplugin', serialize=False, to='cms.cmsplugin')),
('title', models.CharField(max_length=120, verbose_name='Título')),
('description', models.CharField(blank=True, max_length=120, null=True, verbose_name='Descrição')),
],
Expand Down Expand Up @@ -56,14 +56,10 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='CarouselItem',
fields=[
('cmsplugin_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='landpage_carouselitem', serialize=False, to='cms.cmsplugin')),
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.TextField(verbose_name='Texto do item')),
('block', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='carousel_items', to='landpage.carouselblock')),
('image', filer.fields.image.FilerImageField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.FILER_IMAGE_MODEL, verbose_name='Imagem do item')),
('plugin', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='carousel_item', to='landpage.carouselplugin')),
],
options={
'abstract': False,
},
bases=('cms.cmsplugin',),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2 on 2023-10-20 18:53

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('landpage', '0008_auto_20231020_1841'),
]

operations = [
migrations.RemoveField(
model_name='carouselplugin',
name='description',
),
migrations.RemoveField(
model_name='carouselplugin',
name='title',
),
]
15 changes: 6 additions & 9 deletions app/contrib/frontend/landpage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,18 @@ class Meta:
abstract = False


class CarouselBlock(CMSPlugin):
title = models.CharField("Título", max_length=120)
description = models.CharField("Descrição", max_length=120, null=True, blank=True)

class CarouselPlugin(CMSPlugin):
def copy_relations(self, oldinstance):
self.carousel_items.all().delete()
self.carousel_item.all().delete()

for carousel_item in oldinstance.carousel_items.all():
for carousel_item in oldinstance.carousel_item.all():
carousel_item.pk = None
carousel_item.block = self
carousel_item.plugin = self
carousel_item.save()


class CarouselItem(CMSPlugin):
block = models.ForeignKey(CarouselBlock, related_name='carousel_items', on_delete=models.CASCADE)
class CarouselItem(models.Model):
plugin = models.ForeignKey(CarouselPlugin, related_name='carousel_item', on_delete=models.CASCADE)
image = FilerImageField(verbose_name="Imagem do item", null=True, blank=True, on_delete=models.SET_NULL)
text = models.TextField("Texto do item")

Expand Down
3 changes: 2 additions & 1 deletion app/contrib/frontend/landpage/plugin_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class BlockPluginBase(CMSPluginBase):
"ButtonPlugin",
"VideoPlayerPlugin",
"SnippetPlugin",
"LeafletMapPlugin"
"LeafletMapPlugin",
"CarouselPlugin"
]
prepopulated_fields = {"slug": ("title",)}
fieldsets = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,13 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@10/swiper-bundle.min.css" />
{% endaddtoblock %}

<section class="bg-[#024C2B] relative">
<div class="container px-5 py-28 mx-auto md:px-4">
{% if title or description %}
<div class="mb-6">
{% if title %}
<h2 class="text-4xl text-white text-center max-w-[620px] mx-auto mb-4">{{ title }}</h2>
{% endif %}
{% if description %}
<p class="text-white text-center max-w-[815px] mx-auto">{{ description }}</p>
{% endif %}
</div>
{% endif %}

<div class="container px-5 mx-auto md:px-4">
<div class="swiper mySwiper md:min-h-[300px]">
<div class="swiper-wrapper">
<!-- Slides -->
{% for item in items %}
<div class="swiper-slide">
<img src="{{ item.image.url }}" alt="{{ item.text }}">
<img src="{{ item.image.url }}" alt="{{ item.image.default_alt_text }}">
<p>{{ item.text }}</p>
</div>
{% endfor %}
Expand All @@ -32,7 +20,7 @@ <h2 class="text-4xl text-white text-center max-w-[620px] mx-auto mb-4">{{ title
<div class="swiper-button-prev mt-14 md:mx-5 md:mt-10"></div>
<div class="swiper-button-next mt-14 md:mx-5 md:mt-10"></div>
</div>
</section>


{% addtoblock "js" %}
<script src="https://cdn.jsdelivr.net/npm/swiper@10/swiper-bundle.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion app/project/settings/cms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
CMS_PLACEHOLDER_CONF = {
"content": {
"name": "Blocos",
"plugins": ["BlockPlugin", "BlockPressurePlugin", "CarouselBlockPlugin", "EleicaoCandidateListPlugin", "EleicaoCarouselPlugin", "EleicaoVoterFormPlugin"],
"plugins": ["BlockPlugin", "BlockPressurePlugin", "EleicaoCandidateListPlugin", "EleicaoCarouselPlugin", "EleicaoVoterFormPlugin"],
},
"navigation": {
"name": "Navegação",
Expand Down
29 changes: 29 additions & 0 deletions app/tailwind/static/tailwind/tailwindcss/carousel.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

.swiper {
@apply w-full h-full;
}

.swiper-slide {
@apply flex flex-col gap-4 content-center items-center px-8 text-center md:px-4;
}

.swiper-button-next,
.swiper-button-prev {
@apply top-1/2 text-neutral-50;
}

.swiper-button-next::after,
.swiper-button-prev::after {
text-shadow: 1px 0px 5px rgba(52,52,52,0.6);
}


.swiper-pagination-bullet {
@apply bg-[rgba(250,250,250,1)];
}

.swiper-horizontal > .swiper-pagination-bullets,
.swiper-pagination-bullets.swiper-pagination-horizontal,
.swiper-pagination-custom, .swiper-pagination-fraction {
@apply bottom-auto;
}
1 change: 1 addition & 0 deletions app/tailwind/static/tailwind/tailwindcss/page.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
@import "tailwindcss/components";
@import "tailwindcss/utilities";
@import "./eleicao.css";
@import "./carousel.css";

0 comments on commit 78a1cf8

Please sign in to comment.