Skip to content

Commit

Permalink
feat(bonde): add implementation to create widget
Browse files Browse the repository at this point in the history
  • Loading branch information
igr-santos committed Oct 27, 2023
1 parent 5f02b06 commit 24b8266
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 33 deletions.
6 changes: 3 additions & 3 deletions app/contrib/actions/pressure/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from cms.plugin_pool import plugin_pool

from contrib.bonde.plugin_base import BondeWidgetPluginBase
from .forms import PressurePluginForm, PressureAjaxForm
from .forms import CreatePressurePluginForm, PressurePluginForm, PressureAjaxForm
from .models import PressurePluginModel


Expand All @@ -12,8 +12,8 @@ class PressurePlugin(BondeWidgetPluginBase):
module = "Estrategia"
render_template = "pressure/pressure_plugin.html"
model = PressurePluginModel
form = PressurePluginForm
# cache = False
edit_form_class = PressurePluginForm
add_form_class = CreatePressurePluginForm
fieldsets = [
(
"Pressão por e-mail",
Expand Down
16 changes: 12 additions & 4 deletions app/contrib/actions/pressure/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from django import forms
from django.conf import settings

from contrib.bonde.forms import ReferenceBaseModelForm
from contrib.bonde.models import WidgetKind
from contrib.bonde.forms import ChangeReferenceBaseModelForm, CreateReferenceBaseModelForm
from tailwind.forms import StyledBaseForm

from .models import PressurePluginModel
Expand All @@ -33,10 +34,17 @@ def __init__(self, attrs={}):

super().__init__(attrs=attrs)

class CreatePressurePluginForm(CreateReferenceBaseModelForm):
# Settings Widget Kind Form
action_kind = WidgetKind.pressure

class Meta(CreateReferenceBaseModelForm.Meta):
abstract = False
model = PressurePluginModel

class PressurePluginForm(ReferenceBaseModelForm):
class PressurePluginForm(ChangeReferenceBaseModelForm):
# Settings Widget Kind Form
action_kind = "pressure"
action_kind = WidgetKind.pressure

# Extra fields
targets = forms.CharField(
Expand All @@ -51,7 +59,7 @@ class PressurePluginForm(ReferenceBaseModelForm):
label="Corpo do e-mail de pressão", widget=forms.Textarea, required=False
)

class Meta(ReferenceBaseModelForm.Meta):
class Meta(ChangeReferenceBaseModelForm.Meta):
abstract = False
model = PressurePluginModel

Expand Down
93 changes: 74 additions & 19 deletions app/contrib/bonde/forms.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,98 @@
from typing import Any
from django import forms
from django.db import transaction
from colorfield.fields import ColorWidget

from .models import Widget, Block, Mobilization
from .widgets import ActionSelectWidget, ActionChoices


class ReferenceBaseModelForm(forms.ModelForm):
""" """
# Settings Widget Kind Form
action_kind = None

class Meta:
abstract = True
fields = ["reference_id"]

class Media:
js = (
"//ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js",
"bonde/js/edit-widget-form.js",
)
css = {"all": ["bonde/css/edit-widget-form.css"]}
js = ("//ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js",)

# Reference Widget ID
reference_id = forms.ChoiceField(
label="Selecione a widget criada no BONDE",
widget=ActionSelectWidget,
required=False,
)

def __init__(self, *args, **kwargs):
super(ReferenceBaseModelForm, self).__init__(*args, **kwargs)

# Configurar tipo de ação para filtrar widgets
self.fields["reference_id"].choices = ActionChoices(self.action_kind)

if (
self.instance
and self.instance.reference_id
and hasattr(self, "prepare_fields")
):
self.prepare_fields()


class CreateReferenceBaseModelForm(ReferenceBaseModelForm):
""" """

class Meta:
abstract = True
fields = ["reference_id"]

# Create Mobilization and Widget Fields
community_id = forms.IntegerField(widget=forms.HiddenInput)
name = forms.CharField()
# block_id = forms.IntegerField(widget=forms.HiddenInput)
# kind = forms.CharField(widget=forms.HiddenInput)
# settings = forms.JSONField()

@transaction.atomic
def save(self, commit=False) -> Any:
self.instance = super().save(commit=False)

# Criando a Widget no Bonde que se tornará a reference_id
mob = Mobilization.objects.create(
name=self.cleaned_data["name"],
community_id=self.cleaned_data["community_id"],
)
block = Block.objects.create(mobilization=mob)
widget = Widget.objects.create(
block=block,
kind=self.action_kind,
settings=dict(targets=[]),
)

self.instance.reference_id = widget.id

if commit:
self.instance.save()

# TODO: Verificar salvar sem necessidade
# self.update_widget_settings(widget=self.instance.widget, commit=True)

return self.instance


class ChangeReferenceBaseModelForm(ReferenceBaseModelForm):
""" """

class Meta:
abstract = True
fields = ["reference_id"]

class Media:
js = (
"//ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js",
"bonde/js/edit-widget-form.js",
)
css = {"all": ["bonde/css/edit-widget-form.css"]}

# Settings Fields
title = forms.CharField(label="Título do formulário", required=False)
button_text = forms.CharField(label="Texto do botão", required=False)
Expand All @@ -48,18 +115,6 @@ class Media:
label="Corpo do e-mail", widget=forms.Textarea, required=False
)

# Settings Widget Kind Form
action_kind = None

def __init__(self, *args, **kwargs):
super(ReferenceBaseModelForm, self).__init__(*args, **kwargs)

# Configurar tipo de ação para filtrar widgets
self.fields["reference_id"].choices = ActionChoices(self.action_kind)

if self.instance and self.instance.reference_id:
self.prepare_fields()

def prepare_fields(self):
obj = self.instance.widget

Expand Down
12 changes: 6 additions & 6 deletions app/contrib/bonde/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class MobilizationStatus(models.TextChoices):

class Mobilization(models.Model):
name = models.CharField(max_length=266, blank=True, null=True)
created_at = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)
# user_id = models.IntegerField(blank=True, null=True)
# color_scheme = models.CharField(max_length=-1, blank=True, null=True)
# google_analytics_code = models.CharField(max_length=-1, blank=True, null=True)
Expand All @@ -167,7 +167,7 @@ class Mobilization(models.Model):
# traefik_host_rule = models.CharField(max_length=-1, blank=True, null=True)
# traefik_backend_address = models.CharField(max_length=-1, blank=True, null=True)
language = models.CharField(max_length=5, blank=True, null=True)
updated_at = models.DateTimeField(blank=True, null=True)
updated_at = models.DateTimeField(blank=True, null=True, auto_now=True)
# theme = models.ForeignKey('Themes', models.DO_NOTHING, blank=True, null=True)

objects = RequestManager(lookup_field="community")
Expand All @@ -181,8 +181,8 @@ class Block(models.Model):
mobilization = models.ForeignKey(
Mobilization, models.DO_NOTHING, blank=True, null=True
)
created_at = models.DateTimeField()
updated_at = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# bg_class = models.CharField(max_length=-1, blank=True, null=True)
# position = models.IntegerField(blank=True, null=True)
# hidden = models.BooleanField(blank=True, null=True)
Expand Down Expand Up @@ -238,8 +238,8 @@ class Widget(models.Model):
kind = models.CharField(
max_length=50, choices=WidgetKind.choices, blank=True, null=True
)
created_at = models.DateTimeField()
updated_at = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# sm_size = models.IntegerField(blank=True, null=True)
# md_size = models.IntegerField(blank=True, null=True)
# lg_size = models.IntegerField(blank=True, null=True)
Expand Down
23 changes: 22 additions & 1 deletion app/contrib/bonde/plugin_base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
from cms.plugin_base import CMSPluginBase

from .models import Community


class BondeWidgetPluginBase(CMSPluginBase):
edit_form_class = None
add_form_class = None

def get_form(self, request, obj, change, **kwargs):
if not change:
self.form = self.add_form_class
form = super().get_form(request, obj, change, **kwargs)
# Add default values
c = Community.objects.on_site(request).first()
form.base_fields["community_id"].initial = c.id
else:
self.form = self.edit_form_class
form = super().get_form(request, obj, change, **kwargs)

return form

def get_fieldsets(self, request, obj):
if obj and obj.reference_id:
return [
Expand Down Expand Up @@ -30,4 +48,7 @@ def get_fieldsets(self, request, obj):
),
] + self.fieldsets

return [(None, {"fields": ["reference_id"]})]
return [
(None, {"fields": ["reference_id"]}),
("Ou cria uma nova widget", {"fields": ["name", "community_id"]}),
]

0 comments on commit 24b8266

Please sign in to comment.