Skip to content

Commit

Permalink
Move menu to shopinvader and add purge
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu.saison committed Jul 10, 2023
1 parent 28e3920 commit ecadb58
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 0 deletions.
1 change: 1 addition & 0 deletions shopinvader_cache_purge/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
27 changes: 27 additions & 0 deletions shopinvader_cache_purge/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Matthieu SAISON <[email protected]>
# License AGPL-3.0 or later (https: //www.gnu.org/licenses/agpl).

{
"name": "Custom Shopinvader Cache Purge",
"summary": "List url to purge and manage purge",
"version": "14.0.1.0.0",
"category": "Uncategorized",
"website": "www.akretion.com",
"author": " Akretion",
"license": "AGPL-3",
"application": False,
"installable": True,
"external_dependencies": {
"python": [],
"bin": [],
},
"depends": ["connector_search_engine", "connector", "shopinvader"],
"data": [
"data/ir_cron.xml",
"security/ir.model.access.csv",
"views/shopinvader_url_purge.xml",
"views/shopinvader_backend.xml",
],
"demo": [],
}
15 changes: 15 additions & 0 deletions shopinvader_cache_purge/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record forcecreate="True" id="ir_cron_cache_purge" model="ir.cron">
<field name="name">Purge search engine cache from listed urls.</field>
<field name="active" eval="True" />
<field name="user_id" ref="base.user_root" />
<field name="interval_number">1</field>
<field name="interval_type">hours</field>
<field name="numbercall">-1</field>
<field name="doall" eval="False" />
<field name="model_id" ref="model_shopinvader_url_purge" />
<field name="state">code</field>
<field name="code">model.search([("to_purge", "=", True)])._purge_url()</field>
</record>
</odoo>
3 changes: 3 additions & 0 deletions shopinvader_cache_purge/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import se_binding
from . import shopinvader_url_purge
from . import shopinvader_backend
32 changes: 32 additions & 0 deletions shopinvader_cache_purge/models/se_binding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Matthieu SAISON <[email protected]>
# License AGPL-3.0 or later (https: //www.gnu.org/licenses/agpl).
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Matthieu SAISON <[email protected]>
# License AGPL-3.0 or later (https: //www.gnu.org/licenses/agpl).

import logging

from odoo import models

_logger = logging.getLogger(__name__)


class SeBinding(models.AbstractModel):
_inherit = "se.binding"

def synchronize(self):
res = super().synchronize()
# some fields "url_key" are missing in self.data
url_keys = {binding.data.get("url_key") for binding in self}

for url_key in url_keys:
for sbackend in self.shopinvader_backend_ids:
if url_key:
self.env["shopinvader.url.purge"]._request_purge(
url_key,
self._name,
self._description,
sbackend.id,
)
return res
19 changes: 19 additions & 0 deletions shopinvader_cache_purge/models/shopinvader_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Matthieu SAISON <[email protected]>
# License AGPL-3.0 or later (https: //www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class ShopinvaderBackend(models.Model):
_inherit = "shopinvader.backend"

purge_ids = fields.One2many(
"shopinvader.url.purge", "backend_id", string="Urls to purge"
)
purge_nbr = fields.Integer(compute="_compute_purge_nbr", store=True, readonly=True)

@api.depends("purge_ids")
def _compute_purge_nbr(self):
for record in self:
record.purge_nbr = len(record.purge_ids)
67 changes: 67 additions & 0 deletions shopinvader_cache_purge/models/shopinvader_url_purge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Matthieu SAISON <[email protected]>
# License AGPL-3.0 or later (https: //www.gnu.org/licenses/agpl).
import requests

from odoo import fields, models


class ShopinvaderUrlPurge(models.Model):
_name = "shopinvader.url.purge"
_description = "List of url to purge from cache"

url = fields.Char(required=True)
to_purge = fields.Boolean(default=True)
purge_type_id = fields.Many2one("shopinvader.url.purge.type", string="Purge Type")
backend_id = fields.Many2one("shopinvader.backend", string="Backend")
date_last_purge = fields.Datetime("Last Purge Date", readonly=True)
manual = fields.Boolean(default=True, readonly=True)
status = fields.Char(readonly=True)
request_info = fields.Char(readonly=True)

def _request_purge(self, url_key, model_name, model_description, backend_id):
record = self.search([("url", "=", url_key)])
if record:
record.to_purge = True
else:
purge_type = self.env["shopinvader.url.purge.type"].get_or_create(
model_name, model_description
)

record = self.create(
{
"url": url_key,
"to_purge": True,
"purge_type_id": purge_type.id,
"backend_id": backend_id,
"manual": False,
}
)

# use by cron
def _purge_url(self):
for url_key in self:
url = f"{url_key.backend_id.location}/{url_key.url}/"
try:
response = requests.get(
url,
# headers={"TODO"},
)
self.last_request_status = response.status_code
except Exception as e:
self.request_info = str(e)
self.status = "Failed"
continue


class ShopinvaderUrlPurgeType(models.Model):
_name = "shopinvader.url.purge.type"

code = fields.Char()
name = fields.Char()

def get_or_create(self, model_name, model_description):
res = self.search([("code", "=", model_name)])
if not res:
res = self.create({"code": model_name, "name": model_description})
return res
3 changes: 3 additions & 0 deletions shopinvader_cache_purge/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_custom_shopinvader_cache_purge,full access cache_purge,model_shopinvader_url_purge,base.group_user,1,1,1,1
access_custom_shopinvader_cache_purge_type,full access cache_purge_type,model_shopinvader_url_purge_type,base.group_user,1,1,1,1
23 changes: 23 additions & 0 deletions shopinvader_cache_purge/views/shopinvader_backend.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2013 Akretion (http://www.akretion.com)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>

<record model="ir.ui.view" id="shopinvader_backend_form_view">
<field name="model">shopinvader.backend</field>
<field name="inherit_id" ref="shopinvader.shopinvader_backend_view_form" />
<field name="arch" type="xml">
<div class="oe_button_box" position="inside">
<button
type="action"
class="oe_stat_button"
name="%(action_view_url_purge)d"
icon="fa-th-list"
>
<field name="purge_nbr" string="purge lines" widget="statinfo" />
</button>
</div>
</field>
</record>

</odoo>
57 changes: 57 additions & 0 deletions shopinvader_cache_purge/views/shopinvader_url_purge.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="action_view_url_purge" model="ir.actions.act_window">
<field name="type">ir.actions.act_window</field>
<field name="name">Liste des URL</field>
<field name="res_model">shopinvader.url.purge</field>
<field name="view_mode">tree</field>
<field name="domain">[('backend_id', '=', active_id)]</field>
</record>


<record model="ir.ui.view" id="shopinvader_url_purge_tree_view">
<field name="name">shopinvader.url.purge.tree.view</field>
<field name="model">shopinvader.url.purge</field>
<field name="arch" type="xml">
<tree >
<field name="url" />
<field name="to_purge" />
<field name="purge_type_id" />
<field name="backend_id" />
</tree>
</field>
</record>

<record model="ir.actions.act_window" id="shopinvader_url_purge_act_window">
<field name="res_model">shopinvader.url.purge</field>
<field name="view_mode">tree,form</field>
<field name="domain">[]</field>
<field name="context">{}</field>
</record>

<record model="ir.actions.server" id="action_purge">
<field name="name">Purger</field>
<field name="model_id" ref="model_shopinvader_url_purge" />
<field name="binding_type">action</field>
<!-- <field name="binding_model_id" ref="model_shopinvader_url_purge" /> -->
<field name="state">code</field>
<field name="code">
records._purge_url()
</field>
<field
name="groups_id"
eval="[(4, ref('base.group_user'))]"
/>
</record>

<menuitem
id="shopinvader_url_purge_root_menu"
parent="shopinvader.menu_shopinvader_root"
name="Url Purge List"
sequence="120"
action="shopinvader_url_purge_act_window"
/>



</odoo>

0 comments on commit ecadb58

Please sign in to comment.