def _import_image_by_url(self, url, session, field, line_number): """ Imports an image by URL :param str url: the original field value :param requests.Session session: :param str field: name of the field (for logging/debugging) :param int line_number: 0-indexed line number within the imported file (for logging/debugging) :return: the replacement value :rtype: bytes """ maxsize = int( config.get("import_image_maxbytes", DEFAULT_IMAGE_MAXBYTES)) try: response = session.get(url, timeout=int( config.get("import_image_timeout", DEFAULT_IMAGE_TIMEOUT))) response.raise_for_status() if response.headers.get('Content-Length') and int( response.headers['Content-Length']) > maxsize: raise ValueError( _("File size exceeds configured maximum (%s bytes)") % maxsize) content = bytearray() for chunk in response.iter_content(DEFAULT_IMAGE_CHUNK_SIZE): content += chunk if len(content) > maxsize: raise ValueError( _("File size exceeds configured maximum (%s bytes)") % maxsize) image = Image.open(io.BytesIO(content)) w, h = image.size if w * h > 42e6: # Nokia Lumia 1020 photo resolution raise ValueError( u"Image size excessive, imported images must be smaller " u"than 42 million pixel") return base64.b64encode(content) except Exception as e: raise ValueError( _("Could not retrieve URL: %(url)s [%(field_name)s: L%(line_number)d]: %(error)s" ) % { 'url': url, 'field_name': field, 'line_number': line_number + 1, 'error': e })
def _geoip_setup_resolver(cls): # Lazy init of GeoIP resolver if coffice._geoip_resolver is not None: return geofile = config.get('geoip_database') try: coffice._geoip_resolver = GeoIPResolver.open(geofile) or False except Exception as e: _logger.warning('Cannot load GeoIP: %s', ustr(e))
def _get_sys_logs(self): """ Utility method to send a publisher warranty get logs messages. """ msg = self._get_message() arguments = {'arg0': ustr(msg), "action": "update"} url = config.get("publisher_warranty_url") r = requests.post(url, data=arguments, timeout=30) r.raise_for_status() return literal_eval(r.text)
def _parse_import_data_recursive(self, model, prefix, data, import_fields, options): # Get fields of type date/datetime all_fields = self.env[model].fields_get() for name, field in all_fields.items(): name = prefix + name if field['type'] in ('date', 'datetime') and name in import_fields: index = import_fields.index(name) self._parse_date_from_data(data, index, name, field['type'], options) # Check if the field is in import_field and is a relational (followed by /) # Also verify that the field name exactly match the import_field at the correct level. elif any(name + '/' in import_field and name == import_field.split('/')[prefix.count('/')] for import_field in import_fields): # Recursive call with the relational as new model and add the field name to the prefix self._parse_import_data_recursive(field['relation'], name + '/', data, import_fields, options) elif field['type'] in ('float', 'monetary') and name in import_fields: # Parse float, sometimes float values from file have currency symbol or () to denote a negative value # We should be able to manage both case index = import_fields.index(name) self._parse_float_from_data(data, index, name, options) elif field['type'] == 'binary' and field.get('attachment') and any( f in name for f in IMAGE_FIELDS) and name in import_fields: index = import_fields.index(name) with requests.Session() as session: session.stream = True for num, line in enumerate(data): if re.match( config.get("import_image_regex", DEFAULT_IMAGE_REGEX), line[index]): if not self.env.user._can_import_remote_urls(): raise AccessError( _("You can not import images via URL, check with your administrator or support for the reason." )) line[index] = self._import_image_by_url( line[index], session, name, num) else: try: base64.b64decode(line[index], validate=True) except binascii.Error: raise ValueError( _("Found invalid image data, images should be imported as either URLs or base64-encoded data." )) return data
def registries(cls): """ A mapping from database names to registries. """ size = config.get('registry_lru_size', None) if not size: # Size the LRU depending of the memory limits if os.name != 'posix': # cannot specify the memory limit soft on windows... size = 42 else: # A registry takes 10MB of memory on average, so we reserve # 10Mb (registry) + 5Mb (working memory) per registry avgsz = 15 * 1024 * 1024 size = int(config['limit_memory_soft'] / avgsz) return LRU(size)
# -*- coding: utf-8 -*- import importlib.util import os import sys from coffice.tools import config from coffice.modules.module import get_resource_path for path in config.get("upgrades_paths", "").split(","): if os.path.exists(os.path.join(path, "__init__.py")): break else: # failback to legacy "maintenance/migrations" package path = get_resource_path("base", "maintenance", "migrations") if not path: raise ImportError("No package found in `upgrades_paths`") spec = importlib.util.spec_from_file_location( "coffice.upgrades", os.path.join(path, "__init__.py")) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) # shadow module and register under legacy name sys.modules["coffice.upgrades"] = sys.modules[ "coffice.addons.base.maintenance.migrations"] = module
import uuid import logging from coffice import api, fields, models from coffice.tools import config, ormcache, mute_logger _logger = logging.getLogger(__name__) """ A dictionary holding some configuration parameters to be initialized when the database is created. """ _default_parameters = { "database.secret": lambda: str(uuid.uuid4()), "database.uuid": lambda: str(uuid.uuid1()), "database.create_date": fields.Datetime.now, "web.base.url": lambda: "http://localhost:%s" % config.get('http_port'), "base.login_cooldown_after": lambda: 10, "base.login_cooldown_duration": lambda: 60, } class IrConfigParameter(models.Model): """Per-database storage of configuration key-value pairs.""" _name = 'ir.config_parameter' _description = 'System Parameter' _rec_name = 'key' _order = 'key' key = fields.Char(required=True, index=True) value = fields.Text(required=True)