[IMP] base,website*: do not store country flags.

1) Avoid the storage of all country flags as ir_attachment (230+
ir_attachment in a new db) to reduce the filestore of databases.

The country flags are nearly static and not expected to be modified on
Odoo instances.

This change is based on the new "image_url" widget logic (see previous commits).

2) Extend the flag coverage for countries

Add the missing country flags & specify a mapping to provide flags
for overseas administrated countries/territories.
This commit is contained in:
Victor Feyens
2020-09-02 16:43:45 +00:00
parent f8b901d04b
commit 2f3dcb8947
13 changed files with 49 additions and 252 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ class WebsiteVisitor(models.Model):
# localisation and info
country_id = fields.Many2one('res.country', 'Country', readonly=True)
country_flag = fields.Binary(related="country_id.image", string="Country Flag")
country_flag = fields.Char(related="country_id.image_url", string="Country Flag")
lang_id = fields.Many2one('res.lang', string='Language', help="Language from the website when visitor has been created")
timezone = fields.Selection(_tz_get, string='Timezone')
email = fields.Char(string='Email', compute='_compute_email_phone')
-2
View File
@@ -33,8 +33,6 @@ class UtilPerf(HttpCase):
class TestStandardPerformance(UtilPerf):
def test_10_perf_sql_img_controller(self):
self.authenticate('demo', 'demo')
url = '/web/image/res.country/4/image'
self.assertEqual(self._get_url_hot_query(url), 4)
url = '/web/image/res.users/2/image_256'
self.assertEqual(self._get_url_hot_query(url), 8)
+11 -7
View File
@@ -82,6 +82,7 @@
<kanban class="o_wvisitor_kanban" sample="1">
<field name="id"/>
<field name="country_id"/>
<field name="country_flag"/>
<field name="email"/>
<field name="is_connected"/>
<field name="display_name"/>
@@ -109,10 +110,10 @@
<span class="fa fa-circle text-success" t-if="record.is_connected.raw_value" aria-label="Online" title="Online"/>
<span class="fa fa-circle text-danger" t-else="" aria-label="Offline" title="Offline"/>
</div>
<div>
<img t-if="record.country_id.raw_value"
t-att-src="kanban_image('res.country', 'image', record.country_id.raw_value)"
class="o_country_flag" alt="Country"/>
<!-- Double check is necessary for sample view (or error image are shown) -->
<div t-if="record.country_id.raw_value and record.country_flag.raw_value">
<img t-att-src="record.country_flag.raw_value"
class="o_country_flag" alt="Country"/>
</div>
</div>
</div>
@@ -147,7 +148,8 @@
</div>
<strong>
<img t-if="record.country_id.raw_value"
t-att-src="kanban_image('res.country', 'image', record.country_id.raw_value)" class="o_country_flag" alt="Country"/>
t-att-src="record.country_flag.raw_value"
class="o_country_flag" alt="Country"/>
<field name="display_name"/>
</strong>
<div class="mb-2">Active <field name="time_since_last_action"/></div>
@@ -196,7 +198,9 @@
<field name="visitor_page_count" widget="statinfo" string="Page views"/>
</button>
</div>
<div class="float-right" attrs="{'invisible': [('country_id', '=', False)]}"><field name="country_flag" widget="image" options='{"size": [32, 32]}'/></div>
<div class="float-right" attrs="{'invisible': [('country_id', '=', False)]}">
<field name="country_flag" widget="image_url" options='{"size": [32, 32]}'/>
</div>
<div class="oe_title">
<h1><field name="display_name"/></h1>
</div>
@@ -227,7 +231,7 @@
<field name="arch" type="xml">
<tree string="Web Visitors" decoration-success="is_connected" decoration-danger="not is_connected" sample="1">
<!--TODO DBE : Handle no_label in treeview-->
<field name="country_flag" widget="image" options='{"size": [20, 20]}' string=" "/>
<field name="country_flag" widget="image_url" options='{"size": [20, 20]}' string=" "/>
<field name="display_name"/>
<field name="create_date"/>
<field name="last_connection_datetime"/>
@@ -92,11 +92,11 @@ class EventSponsor(models.Model):
sponsor.is_in_opening_hours = opening_from <= now_tz < opening_to
@api.depends('partner_id.country_id.image')
@api.depends('partner_id.country_id.image_url')
def _compute_country_flag_url(self):
for sponsor in self:
if sponsor.partner_id.country_id:
sponsor.country_flag_url = self.env['website'].image_url(sponsor.partner_id.country_id, 'image', size=256)
sponsor.country_flag_url = sponsor.partner_id.country_id.image_url
else:
sponsor.country_flag_url = False
+1 -1
View File
@@ -1641,7 +1641,7 @@
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" t-attf-id="country_#{country.id}" t-att-value="country.id"/>
<label class="custom-control-label" t-attf-for="country_#{country.id}">
<span t-field="country.image" t-options='{"widget": "image", "class": "country_flag"}' class="mr-2"/>
<span t-field="country.image_url" t-options='{"widget": "image_url", "class": "country_flag"}' class="mr-2"/>
<span t-esc="country.name"/>
</label>
</div>
File diff suppressed because it is too large Load Diff
+31 -1
View File
@@ -10,6 +10,24 @@ from odoo.tools.translate import _
_logger = logging.getLogger(__name__)
FLAG_MAPPING = {
"GF": "fr",
"BV": "no",
"BQ": "nl",
"GP": "fr",
"HM": "au",
"YT": "fr",
"RE": "fr",
"MF": "fr",
"UM": "us",
}
NO_FLAG_COUNTRIES = [
"AQ", #Antarctica
"SJ", #Svalbard + Jan Mayen : separate jurisdictions : no dedicated flag
]
class Country(models.Model):
_name = 'res.country'
_description = 'Country'
@@ -37,7 +55,10 @@ class Country(models.Model):
"(in reports for example), while this field is used to modify the input form for "
"addresses.")
currency_id = fields.Many2one('res.currency', string='Currency')
image = fields.Binary(attachment=True)
image_url = fields.Char(
compute="_compute_image_url", string="Flag",
help="Url of static flag image",
)
phone_code = fields.Integer(string='Country Calling Code')
country_group_ids = fields.Many2many('res.country.group', 'res_country_res_country_group_rel',
'res_country_id', 'res_country_group_id', string='Country Groups')
@@ -90,6 +111,15 @@ class Country(models.Model):
self.ensure_one()
return re.findall(r'\((.+?)\)', self.address_format)
@api.depends('code')
def _compute_image_url(self):
for country in self:
if not country.code or country.code in NO_FLAG_COUNTRIES:
country.image_url = False
else:
code = FLAG_MAPPING.get(country.code, country.code.lower())
country.image_url = "/base/static/img/country_flags/%s.png" % code
class CountryGroup(models.Model):
_description = "Country Group"
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

@@ -7,8 +7,8 @@
<t t-if="not object.name and object.parent_name">
<span itemprop="name" t-esc="object.parent_name"/>
</t>
<t t-if="options.get('country_image') and 'country_id' in fields and object.country_id and object.country_id.image">
<span t-field="object.country_id.image" t-options='{"widget": "image", "class": "country_flag"}'/>
<t t-if="options.get('country_image') and 'country_id' in fields and object.country_id and object.country_id.image_url">
<span t-field="object.country_id.image_url" t-options='{"widget": "image_url", "class": "country_flag"}'/>
</t>
</div>
</template>
+1 -1
View File
@@ -23,7 +23,7 @@
<sheet>
<div class="oe_button_box">
</div>
<field name="image" widget="image" class="oe_avatar"/>
<field name="image_url" widget="image_url" class="oe_avatar"/>
<group name="main_group">
<group name="country_details">
<field name="name"/>