Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activator styles not applied on Python versions less than 3.11 #187

Open
Amos-Cacha opened this issue Jan 18, 2025 · 4 comments
Open

Activator styles not applied on Python versions less than 3.11 #187

Amos-Cacha opened this issue Jan 18, 2025 · 4 comments
Labels
bug Something isn't working

Comments

@Amos-Cacha
Copy link

Amos-Cacha commented Jan 18, 2025

Outdated (see second comment), I've also changed the issue title to describe my other issue instead of the one here (see first comment)

Show outdated info Hello, I managed to recreate this issue in the testapp where the text of an activator button is set to None when using a default_renderer. I have only tested the bootstrap renderer so far.

Image

It works properly when you use {% render_form form "bootstrap" %} in the template code instead of {{ form }}.

Image

Steps to recreate:

  1. I added a mytest.py and mytest.html with the following code
    (I wasn't really sure how to edit the views.py)

mytest.py

class TestView(FormView):
    form_class = AddressForm
    template_name = "testapp/mytest.html"
    success_url = "/"

mytest.html

{% extends "bootstrap/base.html" %}
{% load render_form from formsetify %}

{% block main-content %}
<django-formset endpoint="{{ request.path }}" csrf-token="{{ csrf_token }}">
    {% comment %} Set this to True to test the appropriate behavior {% endcomment %}
    {% if False %}
        {% render_form form "bootstrap" %}
    {% else %}
        {{ form }}
    {% endif %}
</django-formset>
{% endblock %}
  1. Then I modified address.py by adding this code default_renderer = FormRenderer(form_css_classes='row mt-4', ) and I also changed the label of one of the activators just to confirm that my changes were reflected label="Should be able to see this ",
  2. I also modified urls.py by adding
from .mytest import TestView
...
path('mytest', TestView.as_view(), name='address'),
  1. Finally, you can see the error by running the server and going to the "/mytest" URL

In my own code where I first discovered the bug, I also encountered an issue where the button variant style is not applied. When I inspected the rendered html code of the buttons, the class is being set to "btn btn-1" where the number after the dash corresponds to the appropriate button variant. I haven't been able to recreate this in the testapp though, so I am still testing what could be different.

Image

Workaround
If anybody is also encountering this issue, I've been using this JS code as a workaround it's kind of fragile though depending on how you render your form.

const buttons = ["btn-primary", "btn-secondary", "btn-success", "btn-danger", "btn-warning", "btn-info"];
for (let i = 0; i < buttons.length; i++) {
    document.querySelectorAll(".btn-" + (i + 1)).forEach(function(button) {
        button.classList.add(buttons[i]);
        if (button.innerText == "None")
            button.childNodes[0].textContent = document.querySelector(`label[for="${button.id}"`).getAttribute("content");
});
@Amos-Cacha
Copy link
Author

Amos-Cacha commented Jan 19, 2025

I figured out the difference between my local environment and the testapp that's causing the button variant's style to not apply. It is specifically this code from formset/renderers/__init__.py that is causing the issue.

try:
    from enum import StrEnum
except ImportError:
    from enum import Enum

    class StrEnum(str, Enum):
        pass

StrEnum is only available on Python version 3.11+ while my local environment is on Python 3.10. For versions under 3.11, a custom StrEnum is used which inherits from str and Enum, but this implementation lacks the capability to automatically return the lower-cased version of its member name as its string value using auto(). It just returns the integer value of the enum member as a string.

Python 3.10 interpreter output:

>>> class StrEnum(str, Enum):
...     pass
... 
>>> class ButtonVariant(StrEnum):
...     PRIMARY = auto()
...     SECONDARY = auto()
...     SUCCESS = auto()
...     DANGER = auto()
...     WARNING = auto()
...     INFO = auto()
... 
>>> ButtonVariant.PRIMARY
<ButtonVariant.PRIMARY: '1'>
>>> f"Bluh bluh {ButtonVariant.PRIMARY}"
'Bluh bluh 1'

One fix could be to copy the StrEnum definition in the except clause from here. Another method would be to define the string values of ButtonVariant manually.

Python 3.10 interpreter output:

>>> class ButtonVariant(StrEnum):
...     PRIMARY = "primary"
...     SECONDARY = "secondary"
...     SUCCESS = "success"     
...     DANGER = "danger"   
...     WARNING = "warning" 
...     INFO = "info"       
... 
>>> f"Bluh bluh {ButtonVariant.PRIMARY}"
'Bluh bluh primary'

@Amos-Cacha
Copy link
Author

Hello, I got more time to test and I realized that the reason the button labels were being set to None was just because I was missing the FormMixin as an inheritance 😅.

@Amos-Cacha Amos-Cacha changed the title Activator label set to None when using default_renderer Activator styles not applied on Python versions less than 3.11 Jan 20, 2025
@jrief
Copy link
Owner

jrief commented Jan 20, 2025

can this issue then be closed?

@Amos-Cacha
Copy link
Author

hello, the issue with strEnum is still there so i haven't closed the ticket yet, should i have created a different incident for that instead of changing this one?

@jrief jrief added the bug Something isn't working label Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants