def check_view_restrictions(document, request): """ Check whether there are any view restrictions on this document which are not fulfilled by the given request object. If there are, return an HttpResponse that will notify the user of that restriction (and possibly include a password / login form that will allow them to proceed). If there are no such restrictions, return None """ for restriction in document.collection.get_view_restrictions(): if not restriction.accept_request(request): if restriction.restriction_type == BaseViewRestriction.PASSWORD: from wagtail.core.forms import PasswordViewRestrictionForm form = PasswordViewRestrictionForm(instance=restriction, initial={'return_url': request.get_full_path()}) action_url = reverse('wagtaildocs_authenticate_with_password', args=[restriction.id]) password_required_template = getattr(settings, 'DOCUMENT_PASSWORD_REQUIRED_TEMPLATE', 'wagtaildocs/password_required.html') context = { 'form': form, 'action_url': action_url } return TemplateResponse(request, password_required_template, context) elif restriction.restriction_type in [BaseViewRestriction.LOGIN, BaseViewRestriction.GROUPS]: return require_wagtail_login(next=request.get_full_path())
def check_view_restrictions(document, request): """ Check whether there are any view restrictions on this document which are not fulfilled by the given request object. If there are, return an HttpResponse that will notify the user of that restriction (and possibly include a password / login form that will allow them to proceed). If there are no such restrictions, return None """ for restriction in document.collection.get_view_restrictions(): if not restriction.accept_request(request): if restriction.restriction_type == BaseViewRestriction.PASSWORD: from wagtail.core.forms import PasswordViewRestrictionForm form = PasswordViewRestrictionForm( instance=restriction, initial={'return_url': request.get_full_path()}) action_url = reverse('wagtaildocs_authenticate_with_password', args=[restriction.id]) password_required_template = getattr( settings, 'DOCUMENT_PASSWORD_REQUIRED_TEMPLATE', 'wagtaildocs/password_required.html') context = {'form': form, 'action_url': action_url} return TemplateResponse(request, password_required_template, context) elif restriction.restriction_type in [ BaseViewRestriction.LOGIN, BaseViewRestriction.GROUPS ]: return require_wagtail_login(next=request.get_full_path())
def listing_view(self, request): page, args, kwargs = self.get_object() for restriction in page.get_view_restrictions(): if not restriction.accept_request(request): if restriction.restriction_type == PageViewRestriction.PASSWORD: return Response({ "component_name": "PasswordProtectedPage", "component_props": { "restriction_id": restriction.id, "page_id": page.id, "csrf_token": csrf_middleware.get_token(request), }, }) elif restriction.restriction_type in [ PageViewRestriction.LOGIN, PageViewRestriction.GROUPS, ]: site = Site.find_for_request(self.request) resp = require_wagtail_login( next=page.relative_url(site, request)) return Response({ "redirect": { "destination": resp.url, "is_permanent": False, } }) return page.serve(request, *args, **kwargs)