diff --git a/website_catch_500/__init__.py b/website_catch_500/__init__.py new file mode 100644 index 000000000..d3aec5347 --- /dev/null +++ b/website_catch_500/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2024 Ismail Cagan Yilmaz (https://github.com/milleniumkid) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +from . import models diff --git a/website_catch_500/__manifest__.py b/website_catch_500/__manifest__.py new file mode 100644 index 000000000..9a371bd9e --- /dev/null +++ b/website_catch_500/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2024 Ismail Cagan Yilmaz (https://github.com/milleniumkid) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +{ + "name": "Website Catch 500 Errors", + "summary": "Catch All 500 Errors and Log Them", + "description": "This module is designed to handle HTTP 500 errors." + "It logs all HTTP 500 errors and displays them in the back-end.", + "development_status": "Beta", + "version": "16.0.1.0.1", + "author": "Ismail Cagan Yilmaz", + "license": "LGPL-3", + "website": "https://github.com/milleniumkid", + "category": "Extensions", + "depends": ["base", "website", "website_sale"], + "data": [ + "security/ir.model.access.csv", + "views/website_views.xml", + "views/website_500_errors_views.xml", + ], + "installable": True, +} diff --git a/website_catch_500/models/__init__.py b/website_catch_500/models/__init__.py new file mode 100644 index 000000000..f95f61224 --- /dev/null +++ b/website_catch_500/models/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2024 Ismail Cagan Yilmaz (https://github.com/milleniumkid) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +from . import ir_http +from . import website_500_errors +from . import website diff --git a/website_catch_500/models/ir_http.py b/website_catch_500/models/ir_http.py new file mode 100644 index 000000000..965feadf3 --- /dev/null +++ b/website_catch_500/models/ir_http.py @@ -0,0 +1,17 @@ +# Copyright 2024 Ismail Cagan Yilmaz (https://github.com/milleniumkid) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +from odoo import models +from odoo.http import request + + +class HttpInherit(models.AbstractModel): + _inherit = "ir.http" + + @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) + return super()._get_error_html(env, code, values) diff --git a/website_catch_500/models/website.py b/website_catch_500/models/website.py new file mode 100644 index 000000000..c67b748fd --- /dev/null +++ b/website_catch_500/models/website.py @@ -0,0 +1,56 @@ +# Copyright 2024 Ismail Cagan Yilmaz (https://github.com/milleniumkid) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +from odoo import models, fields +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" + + catch_500_errors = fields.Boolean(string="Catch 500 Errors") + catched_500_errors = fields.One2many( + comodel_name="website.500.errors", + inverse_name="website_id", + string="Caught 500 Errors", + ) + + def _catch_500_error(self, request): + url = remove_domain_and_protocol(request.url) + request_method = request.method + website_id = self.id + error = ( + self.env["website.500.errors"] + .sudo() + .search([("name", "=", url), ("website_id", "=", website_id)]) + ) + if error: + error.hit_count += 1 + else: + self.env["website.500.errors"].sudo().create( + { + "name": url, + "request_method": request_method, + "hit_count": 1, + "website_id": website_id, + } + ) + self.env.cr.commit() + return True diff --git a/website_catch_500/models/website_500_errors.py b/website_catch_500/models/website_500_errors.py new file mode 100644 index 000000000..8e52dd563 --- /dev/null +++ b/website_catch_500/models/website_500_errors.py @@ -0,0 +1,35 @@ +# Copyright 2024 Ismail Cagan Yilmaz (https://github.com/milleniumkid) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +from odoo import models, api, fields +from odoo.exceptions import ValidationError + + +class Website500Errors(models.Model): + _name = "website.500.errors" + _description = "Base Model for Website 500 Errors" + + name = fields.Char(string="URL") + request_method = fields.Selection( + selection=[ + ("GET", "GET"), + ("POST", "POST"), + ("PUT", "PUT"), + ("DELETE", "DELETE"), + ("HEAD", "HEAD"), + ("OPTIONS", "OPTIONS"), + ("PATCH", "PATCH"), + ], + string="Request Method", + ) + hit_count = fields.Integer(string="Hit Count") + website_id = fields.Many2one( + comodel_name="website", + string="Website", + 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/security/ir.model.access.csv b/website_catch_500/security/ir.model.access.csv new file mode 100644 index 000000000..4639eb9ab --- /dev/null +++ b/website_catch_500/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_website_500_errors,access_website_500_errors,model_website_500_errors,website.group_website_designer,1,1,1,1 \ No newline at end of file diff --git a/website_catch_500/static/description/icon.png b/website_catch_500/static/description/icon.png new file mode 100644 index 000000000..e21c89070 Binary files /dev/null and b/website_catch_500/static/description/icon.png differ diff --git a/website_catch_500/views/website_500_errors_views.xml b/website_catch_500/views/website_500_errors_views.xml new file mode 100644 index 000000000..73fff738c --- /dev/null +++ b/website_catch_500/views/website_500_errors_views.xml @@ -0,0 +1,39 @@ + + + + website.500.errors.tree + website.500.errors + + + + + + + + + + + website.500.errors.form + website.500.errors + +
+ + + + + + + + +
+
+
+ + Website 500 Errors + website.500.errors + tree,form + + +
\ No newline at end of file diff --git a/website_catch_500/views/website_views.xml b/website_catch_500/views/website_views.xml new file mode 100644 index 000000000..b9f417cf2 --- /dev/null +++ b/website_catch_500/views/website_views.xml @@ -0,0 +1,13 @@ + + + + website.catch.500.errors.form.inherit + website + + + + + + + +