Skip to content

Commit

Permalink
Merge pull request #2 from milleniumkid/16.0
Browse files Browse the repository at this point in the history
[16.0][ADD] website_catch_500
  • Loading branch information
yibudak authored Dec 11, 2024
2 parents aa6243e + 945ba34 commit 4c73ae2
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 0 deletions.
3 changes: 3 additions & 0 deletions website_catch_500/__init__.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions website_catch_500/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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,
}
5 changes: 5 additions & 0 deletions website_catch_500/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions website_catch_500/models/ir_http.py
Original file line number Diff line number Diff line change
@@ -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)
56 changes: 56 additions & 0 deletions website_catch_500/models/website.py
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions website_catch_500/models/website_500_errors.py
Original file line number Diff line number Diff line change
@@ -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.")
2 changes: 2 additions & 0 deletions website_catch_500/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -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
Binary file added website_catch_500/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions website_catch_500/views/website_500_errors_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_website_500_errors" model="ir.ui.view">
<field name="name">website.500.errors.tree</field>
<field name="model">website.500.errors</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
<field name="request_method"/>
<field name="hit_count"/>
<field name="website_id"/>
</tree>
</field>
</record>
<record id="view_website_500_errors_form" model="ir.ui.view">
<field name="name">website.500.errors.form</field>
<field name="model">website.500.errors</field>
<field name="arch" type="xml">
<form string="Website 500 Error">
<sheet>
<group>
<field name="name" readonly="True"/>
<field name="request_method" readonly="True"/>
<field name="hit_count" readonly="True"/>
<field name="website_id" readonly="True"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="action_website_500_errors" model="ir.actions.act_window">
<field name="name">Website 500 Errors</field>
<field name="res_model">website.500.errors</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="menu_website_500_errors" name="500 Errors"
parent="website.menu_website_global_configuration"
action="action_website_500_errors" sequence="99"/>
</odoo>
13 changes: 13 additions & 0 deletions website_catch_500/views/website_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<data>
<record id="website_cache_500_errors_form" model="ir.ui.view">
<field name="name">website.catch.500.errors.form.inherit</field>
<field name="model">website</field>
<field name="inherit_id" ref="website.view_website_form"/>
<field name="arch" type="xml">
<field name="default_lang_id" position="after">
<field name="catch_500_errors"/>
</field>
</field>
</record>
</data>

0 comments on commit 4c73ae2

Please sign in to comment.