django-components: Not compatible with django-compressor

When I try to run python manage.py compress on a project that uses django-compressor (https://github.com/django-compressor/django-compressor/) I get the following error:

  File "C:\progr\py3\ehcg\venv\lib\site-packages\compressor\offline\django.py", line 84, in remove_block_nodes
    setattr(node, attr, sub_nodelist)
AttributeError: can't set attribute 'nodelist'

After some debugging it seems that django-compressor can’t handle the component_block for example like this:

      {% component_block "card" "Εμφάνιση στοιχείων" color="" header_color="bg-info" header_text_color="text-light" %}
        {% fill "body" %}
          {% for p in ac.object_lists %}
            <a class='btn btn-info mt-1' href='{% url "object-list-detail" p.0 %}'>{{ p.1 }}</a>
          {% endfor %}
        {% endfill %}
      {% endcomponent_block %}

I opend that issue on django-compressor (https://github.com/django-compressor/django-compressor/issues/1201) but it is probably easier to fix django-components instead ?

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 20 (12 by maintainers)

Most upvoted comments

@spapas @dylanjcastillo Great collab, 0.33 is now released with this fix!

Just looking at this now. I’ve yet to investigate it properly, but the exception seems to occur because django-compressor tries to reassign the nodelist attribute. (I’ve never used django-compressor, so I’ll have to read up on why it does this.) Anyway, the reassignment fails on ComponentNode.nodelist because this class’s nodelist is implemented as a getter method using the @property decorator. Since there is no corresponding setter method, it can’t be assigned to.

To fix it, the next steps are

  1. Figure out what django-compressor does and what the implications would be of letting it make attribute reassignments to our internal Node objects.
  2. Re-implement ComponentNode.nodelist as a bona fide public attribute, since this would appear to more in line with what other tooling expects.

Link to relevant LoC: https://github.com/EmilStenstrom/django-components/blob/3318bde9c36c237fcf88a965332b915340281780/django_components/templatetags/component_tags.py#L405C1-L407C1

Hello @dylanjcastillo I just tested it and it works fine!

Pinging @EmilStenstrom for merging the PR #364 and releasing a new ver.

Thank you for the help!

Hey @dylanjcastillo I’ve created a minimal repository that reproduces the problem: https://github.com/spapas/components_compress

Please notice that in order for the problem to appear the component block must be on an extended template (see base.html and home.html on the repo)