jrief / django-sass-processor

SASS processor to compile SCSS files into *.css, while rendering, or offline.
MIT License
403 stars 65 forks source link

Compilescss failed with a lot of "non sense" errors #155

Open JustDevZero opened 3 years ago

JustDevZero commented 3 years ago

When I try to run compilescss it just explodes with a bunch of errors, i'm trying to add an ipdb in order to find where it's exploding but i ind in a almost endless loop in walk_nodes method.


Error parsing template /home/django_project/virtualenv/lib/python3.7/site-packages/my_resources_cms/templates/custom_plugins/subs_newsletter/sub_newsletter.html: 'NoneType' object has no attribute 'engine'

Error parsing template /home/django_project/virtualenv/lib/python3.7/site-packages/aldryn_newsblog/templates/aldryn_newsblog/article_detail.html: Invalid template name in 'extends' tag: ''. Got this from the 'CMS_TEMPLATE' variable.

Error parsing template /home/django_project/virtualenv/lib/python3.7/site-packages/adminsortable/templates/adminsortable/change_form.html: Invalid template name in 'extends' tag: ''. Got this from the 'change_form_template_extends' variable.

The content using the sass_src is like this on the first one:


{% addtoblock "css" %}
    {% custom_sass_src 'default/sub_newsletter/sub_newsletter.scss' as scss %}
    <link rel="stylesheet" type="text/css" href="{% sass_src scss %}" >
{% endaddtoblock %}

Where custom_sass_src basically builds a different, given some data on the database.

But then, it also happends the " 'NoneType' object has no attribute 'engine'" error on files that even had no sass_src tag.

jrief commented 3 years ago

In order to replace sass_src against their compiled counterparts, django-sass-processor has to parse all templates. If Django's internal template parser finds something it can't understand, it complains with the above errors.

You can ignore them.

However, maybe a little investigation on your templates might give you a reason, why that happens. Does this make sense?

JustDevZero commented 3 years ago

Well, django template parser can normall understand this through templatetags... But somehow any custom templatetag is being ignored here.

JustDevZero commented 3 years ago

OK, basically it looks like Django doesn't like to use database on templatetags. Even it works...

jrief commented 3 years ago

Is your database up, running and is migrated, before you invoke ./manage compilescss?

JustDevZero commented 3 years ago

Yup, it is.

But if i try to put an ipdb into the templatetag it doesn't get triggered.

On the other hand if I try to change from a @register.simple_tag to @register.tag it gets indeed triggered (but of course don't worry because normal tags work a lot different)

Maybe the code will need to be changed after all...

JustDevZero commented 3 years ago

Ok, finally figure out how to solve my problem, for anyone interested:

from sass_processor.templatetags.sass_tags import SassSrcNode

class CustomSassProcessor(SassProcessor):
    def resolve_path(self, context=None):
        from resources.models import SiteConfiguration
        from resources.models import ThemesModel
        from django.conf import settings
        if context is None:
            context = Context()
        path = self._path.resolve(context)
        conf = SiteConfiguration.get_solo()
        rh = ThemesModel.get_solo().refresh_hash
        if not conf.customer_id:
            return '%s' % path
        static_root, static_file = path.rsplit('/', 1)
        static_file = static_file.split('.')[0]
        for item in settings.SASS_PROCESSOR_INCLUDE_DIRS:
            full_static_route = '%s/%s/%s_%s_%s.scss' % (item, static_root, conf.customer_id, static_file, rh)
            if os.path.isfile(full_static_route):
                return '%s/%s_%s_%s.scss' % (static_root, conf.customer_id, static_file, rh)
        return '%s' % path

class CustomSassSrc(SassSrcNode):
    def __init__(self, path):
        self.sass_processor = CustomSassProcessor(path)

@register.tag(name='custom_sass_src')
def render_sass_src(parser, token):
    return CustomSassSrc.handle_token(parser, token)

Maybe it's not the best way, but with this I can I achieve what i needed, having the scss replaced by the different colored versions.

And then, I just call directly custom_sass_src instead of a mix of sass_src and something else.

PetrDlouhy commented 2 years ago

This issue bugs me a bit. The output is huge in my case:

Invalid template /home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.10/lib/python3.10/site-packages/categories/templates/admin/edit_inline/gen_coll_tabular.html: No named cycles in template. 'row1,row2' is not defined

Error parsing template /home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.10/lib/python3.10/site-packages/admin_tools/dashboard/templates/admin_tools/dashboard/dummy.html: Invalid template name in 'extends' tag: ''. Got this from the 'template' variable.

Invalid template /home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.10/lib/python3.10/site-packages/nested_admin/templates/nesting/admin/includes/grappelli_inline.html: Invalid filter: 'prettylabel'

Error parsing template /home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.10/lib/python3.10/site-packages/admin_tools/menu/templates/admin_tools/menu/dummy.html: Invalid template name in 'extends' tag: ''. Got this from the 'template' variable.

Error parsing template /home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.10/lib/python3.10/site-packages/wagtail/admin/templates/wagtailadmin/bulk_actions/confirmation/form_with_fields.html: home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.10/lib/python3.10/site-packages/wagtail/admin/templates/wagtailadmin/bulk_actions/confirmation/form.html

Invalid template /home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.10/lib/python3.10/site-packages/django_filters/templates/django_filters/rest_framework/crispy_form.html: 'crispy_forms_tags' is not a registered tag library. Must be one of:
admin_chart_tags
...(many lines with all the tags)...
yandex_metrica

Error parsing template /home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.10/lib/python3.10/site-packages/adminsortable2/templates/adminsortable2/change_list.html: Invalid template name in 'extends' tag: ''. Got this from the 'original_change_list_template' variable.

I think there were some cases in the past where the compilescss command did help me to identify some error in the templates. But these errors are just false alarms by third libraries that I even can't fix.

Wouldn't it be possible to blacklist those templates as they don't contain sass_src tags?

jrief commented 2 years ago

This is because sass-processor makes a static code analysis including templates. In the worst case, the templatetag sass_processor is not found in one of the extended or included templates. This can be fixed by adding that template folder to the SASS_PROCESSOR_INCLUDE_DIRS.