...
Just my blog

Blog about everything, mostly about tech stuff I made. Here is the list of stuff I'm using at my blog. Feel free to ask me about implementations.

Soft I recommend
Py lib I recommend

I'm using these libraries so you can ask me about them.

Django middleware for the rescue!

Now I can save visitors locally without external tools and also track a bad acting requests.

Finally, I can have a better working middleware which can catch HTTP status errors, make a redirect-response to the main page and also save a visitor.

Now I can also catch HTTP status codes in the database to see what a bad actor wants to achieve.

Thanks to: LINK

I can now save site visits without external analytics and save a bad example to later expose them in a fancy table.

Only I need to mask IP addresses before I show requests and paths to the public.

TBH I also need to add some load balancing.

    def __call__(self, request: HttpRequest) -> typing.Optional[HttpResponse]:
        """
        Check request for validity here and response with correct answers.
        Use bad codes when needed.
        Save visitor now with status code relation.
        :param request:
        :return:
        """
        try:
            response = self.get_response(request)
        except SuspiciousOperation as e:
            log.error(f"SuspiciousOperation:"
                      f"\nException:\n{e}\n")
            save_visit_task(request, status='SUS')
            return HttpResponseForbidden('CSRF verification failed.')
        except Exception as e:
            log.error(f"General Exception: returning the main page by default."
                      f"\nException:\n{e}\n")
            save_visit_task(request, status='ERR')
            return render(request, 'main/main_body.html', {
                'error_message': 'Something went wrong!',
                'error_code': f'CODE: ERR'
            })

        # Save with status code:
        save_visit_task(request, status=response.status_code)
        # Indicate status code and errors at main page alert section
        # Redirects to the main page!
        bad_codes = [400, 401, 403, 404, 500]
        if response.status_code in bad_codes:
            if not const.is_dev():
                return render(request, 'main/main_body.html', {
                    'error_message': 'Something went wrong!',
                    'error_code': f'CODE: {response.status_code}'
                })
        return response

 

Probably, I should also describe my full path later too, It may be useful for random readers.