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

Need some way to figure out if the page is being generated statically #144

Open
danizen opened this issue Feb 19, 2020 · 4 comments
Open

Comments

@danizen
Copy link

danizen commented Feb 19, 2020

CURRENT BEHAVIOR

The project includes no additional context processors or request headers which I can re-skin the pages to look different when generated statically.

DESIRED BEHAVIOR

Custom request.META such as "HTTP_X_BAKERY: True" or something.

POTENTIAL SOLUTIONS

settings.BAKERY_REQUEST_HEADER defaults to 'X-Generator'. The request will have the request.META[settings.BAKERY_REQUEST_HEADER] set to positive during build process.

bakery.context_processors.baking provides template context "baking" set to True if BAKERY_REQUEST_HEADER is set, and to False otherwise.

MOTIVATING EXAMPLE

The internal reports/dashboards will have different navigation options than their public, static counterparts.

@danizen
Copy link
Author

danizen commented Feb 20, 2020

Work-around below allows a developer using one DJANGO_SETTINGS_MODULE, or QA using a "preview" button, to see what the page will look like when generated "for the public". This is made a bit orthogonal to django-bakery, but remains a work-around.

class IsPublicMixin:
    def create_request(self, path):
        request = RequestFactory().get(path)
        request.GET = QueryDict('public=%s' % str(settings.IS_PUBLIC))
        return request

    def is_public(self):
        public = self.request.GET.get('public', 'false').lower()
        if public not in {'1', '0', 'yes', 'no', 'false', 'true'}:
            logger.warning('public CGI parameter is not truthy')
        return settings.IS_PUBLIC or public in {'1', 'yes', 'true'}

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update({
            'public': self.is_public()
        })
        return context

@danizen
Copy link
Author

danizen commented Mar 24, 2020

So, my plan is to shadow the "build" management command with one of my own which overrides the setings.IS_PUBLIC. I think a context manager that returns whether you are in a static context would also be a good idea here.

@danizen
Copy link
Author

danizen commented Mar 24, 2020

I don't need to do that - I've overridden create_request already... I guess its been awhile since I looked at this issue. I've been waiting for operations to make a mount point on the batch server so I can add this to my scheduled tasks.

@danizen
Copy link
Author

danizen commented Mar 24, 2020

So, to generalize this, we need to send custom meta-data to the request by default, by sending a custom header X-Bakery: 1 here https://github.com/datadesk/django-bakery/blob/2c9c495e4e8faca7b81fa57635d1631933f14171/bakery/views/base.py#L44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant