Need some way to figure out if the page is being generated statically
danizen opened this issue · 4 comments
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.
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
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.
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.
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