Skip to content

Commit 886f03a

Browse files
committed
🎉 1️⃣3️⃣ database_limit_size
1 parent 0fe13c4 commit 886f03a

File tree

8 files changed

+172
-0
lines changed

8 files changed

+172
-0
lines changed

database_limit_size/README.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
.. image:: https://img.shields.io/badge/license-MIT-blue.svg
2+
:target: https://opensource.org/licenses/MIT
3+
:alt: License: MIT
4+
5+
=====================
6+
Database Limit Size
7+
=====================
8+
9+
This module allows blocking backend access when database limit is exceeded
10+
11+
On loading backend page, module fetches size of database (including filestore) and compares it with value, that
12+
is defined in "System Parameters" as ``database_limit_size``. Value is expected to be in bytes.
13+
14+
If ``database_limit_size`` is not given or zero, there is no limit.
15+
16+
Credits
17+
=======
18+
19+
Contributors
20+
------------
21+
* `Eugene Molotov <https://it-projects.info/team/em230418>`__:
22+
23+
Sponsors
24+
--------
25+
* `IT-Projects LLC <https://it-projects.info>`__
26+
27+
Maintainers
28+
-----------
29+
* `IT-Projects LLC <https://it-projects.info>`__
30+
31+
To get a guaranteed support
32+
you are kindly requested to purchase the module
33+
at `odoo apps store <https://apps.odoo.com/apps/modules/13.0/database_limit_size/>`__.
34+
35+
Thank you for understanding!
36+
37+
`IT-Projects Team <https://www.it-projects.info/team>`__
38+
39+
Further information
40+
===================
41+
42+
Demo: http://runbot.it-projects.info/demo/access-addons/13.0
43+
44+
HTML Description: https://apps.odoo.com/apps/modules/13.0/database_limit_size/
45+
46+
Usage instructions: `<doc/index.rst>`_
47+
48+
Changelog: `<doc/changelog.rst>`_
49+
50+
Notifications on updates: `via Atom <https://github.com/it-projects-llc/access-addons/commits/13.0/database_limit_size.atom>`_, `by Email <https://blogtrottr.com/?subscribe=https://github.com/it-projects-llc/access-addons/commits/13.0/database_limit_size.atom>`_
51+
52+
Tested on Odoo 13.0 41f72d9628b467f82da0fafc15651002a49b10ad

database_limit_size/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# License MIT (https://opensource.org/licenses/MIT).
2+
3+
from . import models
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2020 Eugene Molotov <https://it-projects.info/team/em230418>
2+
# License MIT (https://opensource.org/licenses/MIT).
3+
4+
{
5+
"name": """Database Limit Size""",
6+
"summary": """This module allows blocking backend access when database limit is exceeded""",
7+
"category": "Extra Tools",
8+
# "live_test_url": "http://apps.it-projects.info/shop/product/DEMO-URL?version=13.0",
9+
"images": [],
10+
"version": "13.0.1.0.0",
11+
"application": False,
12+
"author": "IT-Projects LLC, Eugene Molotov",
13+
"support": "apps@it-projects.info",
14+
"website": "https://apps.odoo.com/apps/modules/13.0/database_limit_size/",
15+
"license": "Other OSI approved licence", # MIT
16+
# "price": 9.00,
17+
# "currency": "EUR",
18+
"depends": ["database_block"],
19+
"external_dependencies": {"python": [], "bin": []},
20+
"data": [],
21+
"demo": [],
22+
"qweb": [],
23+
"post_load": None,
24+
"pre_init_hook": None,
25+
"post_init_hook": None,
26+
"uninstall_hook": None,
27+
"auto_install": False,
28+
"installable": True,
29+
# "demo_title": "Database Limit Size",
30+
# "demo_addons": [
31+
# ],
32+
# "demo_addons_hidden": [
33+
# ],
34+
# "demo_url": "DEMO-URL",
35+
# "demo_summary": "This module allows to block user to backend when database limit is exceed",
36+
# "demo_images": [
37+
# "images/MAIN_IMAGE",
38+
# ]
39+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
`1.0.0`
2+
-------
3+
4+
- **Init version**

database_limit_size/doc/index.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=====================
2+
Database Limit Size
3+
=====================
4+
5+
Installation
6+
============
7+
8+
* `Install <https://odoo-development.readthedocs.io/en/latest/odoo/usage/install-module.html>`__ this module in a usual way
9+
10+
Configuration
11+
=============
12+
13+
* `Log in as admin
14+
* `Activate Developer Mode <https://odoo-development.readthedocs.io/en/latest/odoo/usage/debug-mode.html>`__
15+
* Open menu ``[[ Settings ]] >> Technical >> System Parameter``
16+
* Edit existing record by key ``database_limit_size`` or create new one
17+
* Set integer value, click "Save" and reload web page
18+
* If you set small nonzero value (for example "1" without quotes), "Database size exceed" will appear and will disable navigating
19+
* If you set value that is less than actual database size, but greater than 90% of actual database size and ``web_responsive`` is installed, you will see warning message "Database size is about to be exceed"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# License MIT (https://opensource.org/licenses/MIT).
2+
3+
from . import ir_http
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2020 Eugene Molotov <https://it-projects.info/team/em230418>
2+
# License MIT (https://opensource.org/licenses/MIT).
3+
4+
import os
5+
6+
from odoo import models
7+
from odoo.tools import human_size
8+
9+
10+
def get_directory_size(start_path):
11+
total_size = 0
12+
for dirpath, _dirnames, filenames in os.walk(start_path):
13+
for f in filenames:
14+
fp = os.path.join(dirpath, f)
15+
total_size += os.path.getsize(fp)
16+
return total_size
17+
18+
19+
class IrHttp(models.AbstractModel):
20+
21+
_inherit = "ir.http"
22+
23+
def session_info(self):
24+
res = super(IrHttp, self).session_info()
25+
26+
Config = self.env["ir.config_parameter"].sudo()
27+
try:
28+
database_limit_size = int(Config.get_param("database_limit_size", 0))
29+
except ValueError:
30+
return res
31+
32+
if not database_limit_size:
33+
return res
34+
35+
self.env.cr.execute("select pg_database_size(%s)", [self.env.cr.dbname])
36+
database_size = self.env.cr.fetchone()[0]
37+
38+
filestore_size = get_directory_size(self.env["ir.attachment"]._filestore())
39+
40+
total_size = database_size + filestore_size
41+
if total_size > database_limit_size:
42+
res["database_block_message"] = "Database size exceed ({} / {})".format(
43+
human_size(total_size), human_size(database_limit_size),
44+
)
45+
elif total_size > database_limit_size * 0.9:
46+
res["database_block_message"] = (
47+
"Database size is about to be exceed (%s / %s)"
48+
% (human_size(total_size), human_size(database_limit_size))
49+
)
50+
res["database_block_is_warning"] = True
51+
52+
return res
6.87 KB
Loading

0 commit comments

Comments
 (0)