From 94932989783aaf56f4f3f12c7744c1575536deb6 Mon Sep 17 00:00:00 2001 From: Ismail Cagan Yilmaz Date: Fri, 13 Dec 2024 11:50:13 +0300 Subject: [PATCH 1/2] [16.0][IMP] Improvements about conciseness in code --- website_catch_500/models/ir_http.py | 2 +- website_catch_500/models/website.py | 31 +++++++------------ .../models/website_500_errors.py | 1 + .../views/website_500_errors_views.xml | 8 ++--- 4 files changed, 17 insertions(+), 25 deletions(-) diff --git a/website_catch_500/models/ir_http.py b/website_catch_500/models/ir_http.py index 965feadf..35212cee 100644 --- a/website_catch_500/models/ir_http.py +++ b/website_catch_500/models/ir_http.py @@ -10,7 +10,7 @@ class HttpInherit(models.AbstractModel): @classmethod def _get_error_html(cls, env, code, values): if code == 500: - # if code == 500 or code == "page_500" + # if code == 500 or code == "page_500" website = request.website if website and website.catch_500_errors: website._catch_500_error(request.httprequest) diff --git a/website_catch_500/models/website.py b/website_catch_500/models/website.py index c67b748f..3ad8a1c1 100644 --- a/website_catch_500/models/website.py +++ b/website_catch_500/models/website.py @@ -4,24 +4,6 @@ from urllib.parse import urlparse, urlunparse -def remove_domain_and_protocol(url): - """ - Removes domain and protocol from the url. - """ - parsed_url = urlparse(url) - modified_url = urlunparse( - ( - "", - "", - parsed_url.path, - parsed_url.params, - parsed_url.query, - parsed_url.fragment, - ) - ) - return modified_url - - class Website(models.Model): _inherit = "website" @@ -33,14 +15,22 @@ class Website(models.Model): ) def _catch_500_error(self, request): - url = remove_domain_and_protocol(request.url) + url = request.url + http_param = request.query_string request_method = request.method website_id = self.id error = ( self.env["website.500.errors"] .sudo() - .search([("name", "=", url), ("website_id", "=", website_id)]) + .search( + [ + ("name", "=", url), + ("website_id", "=", website_id), + ("http_param", "=", http_param or ""), + ] + ) ) + if error: error.hit_count += 1 else: @@ -50,6 +40,7 @@ def _catch_500_error(self, request): "request_method": request_method, "hit_count": 1, "website_id": website_id, + "http_param": http_param or "", } ) self.env.cr.commit() diff --git a/website_catch_500/models/website_500_errors.py b/website_catch_500/models/website_500_errors.py index 8e52dd56..2de21c00 100644 --- a/website_catch_500/models/website_500_errors.py +++ b/website_catch_500/models/website_500_errors.py @@ -21,6 +21,7 @@ class Website500Errors(models.Model): ], string="Request Method", ) + http_param = fields.Char(string="HTTP Request Param") hit_count = fields.Integer(string="Hit Count") website_id = fields.Many2one( comodel_name="website", diff --git a/website_catch_500/views/website_500_errors_views.xml b/website_catch_500/views/website_500_errors_views.xml index 73fff738..b96d3fbe 100644 --- a/website_catch_500/views/website_500_errors_views.xml +++ b/website_catch_500/views/website_500_errors_views.xml @@ -5,10 +5,10 @@ website.500.errors - - + + @@ -19,10 +19,10 @@
- - + +
From 5a059c2a5673e9a05db602234ddd418e69f9d486 Mon Sep 17 00:00:00 2001 From: Ismail Cagan Yilmaz Date: Sat, 14 Dec 2024 16:49:53 +0300 Subject: [PATCH 2/2] [FIX] website_catch_500: Capturing form data Add the support to capture form data on POST requests. --- website_catch_500/models/ir_http.py | 1 - website_catch_500/models/website.py | 10 +++++----- website_catch_500/models/website_500_errors.py | 10 +++------- website_catch_500/views/website_500_errors_views.xml | 6 ++++-- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/website_catch_500/models/ir_http.py b/website_catch_500/models/ir_http.py index 35212cee..9349d571 100644 --- a/website_catch_500/models/ir_http.py +++ b/website_catch_500/models/ir_http.py @@ -10,7 +10,6 @@ class HttpInherit(models.AbstractModel): @classmethod def _get_error_html(cls, env, code, values): if code == 500: - # if code == 500 or code == "page_500" website = request.website if website and website.catch_500_errors: website._catch_500_error(request.httprequest) diff --git a/website_catch_500/models/website.py b/website_catch_500/models/website.py index 3ad8a1c1..15eda61d 100644 --- a/website_catch_500/models/website.py +++ b/website_catch_500/models/website.py @@ -16,7 +16,7 @@ class Website(models.Model): def _catch_500_error(self, request): url = request.url - http_param = request.query_string + form_data_str = "\n".join("%s: %s" % (k,v) for k,v in dict(request.form).items()) request_method = request.method website_id = self.id error = ( @@ -24,9 +24,9 @@ def _catch_500_error(self, request): .sudo() .search( [ - ("name", "=", url), + ("url", "=", url), ("website_id", "=", website_id), - ("http_param", "=", http_param or ""), + ("form_data", "=", form_data_str or ""), ] ) ) @@ -36,11 +36,11 @@ def _catch_500_error(self, request): else: self.env["website.500.errors"].sudo().create( { - "name": url, + "url": url, "request_method": request_method, "hit_count": 1, "website_id": website_id, - "http_param": http_param or "", + "form_data": form_data_str or "", } ) self.env.cr.commit() diff --git a/website_catch_500/models/website_500_errors.py b/website_catch_500/models/website_500_errors.py index 2de21c00..6f1dc80e 100644 --- a/website_catch_500/models/website_500_errors.py +++ b/website_catch_500/models/website_500_errors.py @@ -7,8 +7,9 @@ class Website500Errors(models.Model): _name = "website.500.errors" _description = "Base Model for Website 500 Errors" + _rec_name = "url" - name = fields.Char(string="URL") + url = fields.Char(string="URL") request_method = fields.Selection( selection=[ ("GET", "GET"), @@ -21,7 +22,7 @@ class Website500Errors(models.Model): ], string="Request Method", ) - http_param = fields.Char(string="HTTP Request Param") + form_data = fields.Text(string="Form Data") hit_count = fields.Integer(string="Hit Count") website_id = fields.Many2one( comodel_name="website", @@ -29,8 +30,3 @@ class Website500Errors(models.Model): ondelete="cascade", ) - @api.constrains("url") - def _check_url(self): - for record in self: - if self.search_count([("name", "=", record.name)]) > 1: - raise ValidationError("URL must be unique.") diff --git a/website_catch_500/views/website_500_errors_views.xml b/website_catch_500/views/website_500_errors_views.xml index b96d3fbe..abda2269 100644 --- a/website_catch_500/views/website_500_errors_views.xml +++ b/website_catch_500/views/website_500_errors_views.xml @@ -5,9 +5,10 @@ website.500.errors + - + @@ -19,9 +20,10 @@
+ - +