Finding all resources used for anon user

I want to have a browser view that shows all resources (js and css) served to anon users.

In other words, I need a list, something like this:

(skipping the will also do....

I just took a brief look at and which seems to be the places where the resources are collected. There is the ResourceBase class which can be used as a mixin for your own view which has the required methods (get_resources() and anonymous()).

The anonymous() method is used to attach logged-in.css and logged-in.js when the user is logged in. You could just exclude those entries :slight_smile:

There is also the ordered_bundles_result() method which might be helpful.

1 Like

I am not sure if it can be done that way in Plone 5.0.7 + (I have been struggling with it for a few hours)

The ResourceView is now a viewlet

class ResourceView(ViewletBase):
    """Information for script rendering.

But then you can still redefine the required methods from that viewlet in your view :wink:

Do you mean "copying all the code" ?

.... would be quite a lot(?), since the styles viewlet is based on the resorce viewlet (and the same with scripts viewlet)

No, I think all you would need is:

    def get_resources(self):
        return self.registry.collectionOfInterface(
            IResourceRegistry, prefix="plone.resources", check=False)


    def anonymous(self):
        return _getAuthenticatedUser(
            self.context).getUserName() == 'Anonymous User'

and then

      def get_scripts(self):
          result = [{
                'src': '%s/++plone++%s' % (
                    self.production_path + '/default.js'
                'conditionalcomment': None,
                'bundle': 'production'
            }, ]
        # no need for logged-in resources for you
        # ...
        # Add manual added resources
        if hasattr(self.request, 'enabled_resources'):
            resources = self.get_resources()
            for resource in self.request.enabled_resources:
                if resource in resources:
                    data = resources[resource]
                    if data.js:
                        url = urlparse(data.js)
                        if url.netloc == '':
                            # Local
                            src = "%s/%s" % (self.site_url, data.js)
                            src = "%s" % (data.js)

                        data = {
                            'bundle': 'none',
                            'conditionalcomment': '',  # noqa
                            'src': src}

        # Add diazo url
        origin = None
        if self.diazo_production_js and self.development is False:
            origin = self.diazo_production_js
        if self.diazo_development_js and self.development is True:
            origin = self.diazo_development_js
        if origin:
                'bundle': 'diazo',
                'conditionalcomment': '',
                'src': '%s/%s' % (
                    self.site_url, origin)

        return result

Same applied then for the CSS. If you don't need the diazo resources, you can skip that as well.

Edit: You don't need the def anonymous(self) method.

Thanks for you help.

I needed to add a bit more.
Got it kind of working... the code looks a bit ugly... but...

Plone Foundation Code of Conduct