Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][IMP] Improvements about conciseness in code #5

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion website_catch_500/models/ir_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 12 additions & 21 deletions website_catch_500/models/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -33,23 +15,32 @@ class Website(models.Model):
)

def _catch_500_error(self, request):
url = remove_domain_and_protocol(request.url)
url = request.url
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 = (
self.env["website.500.errors"]
.sudo()
.search([("name", "=", url), ("website_id", "=", website_id)])
.search(
[
("url", "=", url),
("website_id", "=", website_id),
("form_data", "=", form_data_str or ""),
]
)
)

if error:
error.hit_count += 1
else:
self.env["website.500.errors"].sudo().create(
{
"name": url,
"url": url,
"request_method": request_method,
"hit_count": 1,
"website_id": website_id,
"form_data": form_data_str or "",
}
)
self.env.cr.commit()
Expand Down
9 changes: 3 additions & 6 deletions website_catch_500/models/website_500_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -21,15 +22,11 @@ class Website500Errors(models.Model):
],
string="Request Method",
)
form_data = fields.Text(string="Form Data")
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.")
10 changes: 6 additions & 4 deletions website_catch_500/views/website_500_errors_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
<field name="model">website.500.errors</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
<field name="url"/>
<field name="request_method"/>
<field name="hit_count"/>
<field name="website_id"/>
<field name="form_data"/>
<field name="hit_count"/>
</tree>
</field>
</record>
Expand All @@ -19,10 +20,11 @@
<form string="Website 500 Error">
<sheet>
<group>
<field name="name" readonly="True"/>
<field name="url" readonly="True" />
<field name="request_method" readonly="True"/>
<field name="hit_count" readonly="True"/>
<field name="website_id" readonly="True"/>
<field name="form_data" readonly="True"/>
<field name="hit_count"/>
</group>
</sheet>
</form>
Expand Down
Loading