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:

www.mysite.com/++plone++static/++unique++2017-08-08%2016%3A33%3A53.779007/resourceregistry-compiled.css
www.mysite.com/++plone++static/components/jquery/dist/jquery.min.js

(skipping the www.mysite.com will also do....

I just took a brief look at https://github.com/plone/Products.CMFPlone/blob/master/Products/CMFPlone/resources/browser/styles.py and https://github.com/plone/Products.CMFPlone/blob/master/Products/CMFPlone/resources/browser/scripts.py 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)

and

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

and then

      def get_scripts(self):
          result = [{
                'src': '%s/++plone++%s' % (
                    self.site_url,
                    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)
                        else:
                            src = "%s" % (data.js)

                        data = {
                            'bundle': 'none',
                            'conditionalcomment': '',  # noqa
                            'src': src}
                        result.append(data)

        # 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:
            result.append({
                '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...