def post(self, request, *args, **kwargs): """ Clear the logs before a POST-request. :param request: :param args: :param kwargs: :return: """ Log.clear() return super().post(request, *args, **kwargs)
def get(self, request, *args, **kwargs): """ Only clear the log AFTER a GET-request. This way, POST-requests are included in the network logs. :param request: :param args: :param kwargs: :return: """ if not self.keep_logs and not request.GET.get('keep-logs', False): Log.clear() return super().get(request, *args, **kwargs)
def dispatch(self, request, *args, **kwargs): clear_logs = not self.keep_logs and not request.GET.get('keep-logs', False) if clear_logs: Log.clear() try: self._pre_dispatch(request, *args, **kwargs) result = super().dispatch(request, *args, **kwargs) self._post_dispatch(request, *args, **kwargs) except (ClientError, HTTPError) as e: return render_exception_to_response(request, e) return result
def render_exception_to_response(request, exc, context=None): """ Shortcut function to render a template with the exception nicely formatted. :param request: The request passed to the view. :param exc: The `Exception` or `ClientError`. :param context: Additional context as a `dict` to pass to the template. :return: A stringified response. """ if context is None: context = {} error = None if type(exc) is ClientError: try: error = exc.args[0] error['invalid_params'] = error.get('invalid-params', None) except Exception: pass context.update({ 'view': { 'title': _('Foutmelding'), 'subtitle': _('Er ging iets mis...'), }, 'error': error, 'exception': exc, 'log_entries': Log.entries() }) return render(request, 'demo/error.html', context)
def get_context_data(self, **kwargs): """ Include log entries in the response. :param kwargs: :return: """ context = super().get_context_data(**kwargs) context.update({ 'log_entries': Log.entries(), }) return context