Ejemplo n.º 1
0
 def _compute_images(self):
     for record in self:
         images = tools.image_get_resized_images(record.image_raw_original, big_name=False)
         record.image_raw_big = tools.image_get_resized_images(record.image_raw_original,
             large_name=False, medium_name=False, small_name=False)['image']
         record.image_raw_large = images['image_large']
         record.image_raw_medium = images['image_medium']
         record.image_raw_small = images['image_small']
         record.can_image_raw_be_zoomed = tools.is_image_size_above(record.image_raw_original)
Ejemplo n.º 2
0
 def _compute_images(self):
     for record in self:
         images = tools.image_get_resized_images(record.image_original, big_name=False)
         record.image_big = tools.image_get_resized_images(record.image_original,
             large_name=False, medium_name=False, small_name=False, preserve_aspect_ratio=True)['image']
         record.image_large = images['image_large']
         record.image_medium = images['image_medium']
         record.image_small = images['image_small']
         record.can_image_be_zoomed = tools.is_image_size_above(record.image_original)
 def write(self, vals):
     tools.image_resize_images(vals)
     if 'image01' in vals:
         vals.update(tools.image_get_resized_images(vals['image01'],
                     return_big=True,
                     big_name='image01'))
     if 'image02' in vals:
         vals.update(tools.image_get_resized_images(vals['image02'],
                     return_big=True,
                     big_name='image02'))
     return super(FleetVehicleClaims, self).write(vals)
Ejemplo n.º 4
0
 def _compute_images(self):
     for record in self:
         image = record.image_raw_original
         # for performance: avoid calling unnecessary methods when falsy
         images = image and tools.image_get_resized_images(image, big_name=False)
         record.image_raw_big = image and tools.image_get_resized_images(image,
             large_name=False, medium_name=False, small_name=False)['image']
         record.image_raw_large = image and images['image_large']
         record.image_raw_medium = image and images['image_medium']
         record.image_raw_small = image and images['image_small']
         record.can_image_raw_be_zoomed = image and tools.is_image_size_above(image)
 def create(self, vals):
     tools.image_resize_images(vals)
     if 'image01' in vals:
         vals.update(tools.image_get_resized_images(vals['image01'],
                     return_big=True,
                     big_name='image01'))
     if 'image02' in vals:
         vals.update(tools.image_get_resized_images(vals['image02'],
                     return_big=True,
                     big_name='image02'))
     if not vals.get('name', False) or vals['name'] == _('New'):
         vals['name'] = self.env['ir.sequence'].next_by_code(
             'fleet.vehicle.claims') or _('New')
     result = super(FleetVehicleClaims, self).create(vals)
     return result
Ejemplo n.º 6
0
 def _compute_images(self):
     products = self.filtered(
         lambda p: p.odoo_module_version_id and p.image_module)
     other_products = self.filtered(lambda p: p not in products)
     for product in products:
         if self._context.get('bin_size'):
             product.image_medium = product.image_module
             product.image_small = product.image_module
             product.image = product.image_module
         else:
             resized_images = tools.image_get_resized_images(
                 product.image_module,
                 return_big=True,
                 avoid_resize_medium=True)
             product.image_medium = resized_images.get('image_medium')
             product.image_small = resized_images.get('image_small')
             product.image = resized_images.get('image')
         if not product.image_medium:
             product.image_medium = product.product_tmpl_id.image_medium
         if not product.image_small:
             product.image_small = product.product_tmpl_id.image_small
         if not product.image:
             product.image = product.product_tmpl_id.image
     if other_products:
         super(ProductProduct, other_products)._compute_images()
Ejemplo n.º 7
0
    def get_variant_vals(self, value_ids, custom_values=None, **kwargs):
        """ Hook to alter the values of the product variant before creation

            :param value_ids: list of product.attribute.values ids
            :param custom_values: dict {product.attribute.id: custom_value}

            :returns: dictionary of values to pass to product.create() method
         """
        self.ensure_one()

        image = self.get_config_image_obj(value_ids).image
        all_images = tools.image_get_resized_images(image,
                                                    avoid_resize_medium=True)
        vals = {
            'product_tmpl_id': self.id,
            'attribute_value_ids': [(6, 0, value_ids)],
            'taxes_id': [(6, 0, self.taxes_id.ids)],
            'image': image,
            'image_variant': image,
            'image_medium': all_images['image_medium'],
            'image_small': all_images['image_medium'],
        }

        if custom_values:
            vals.update(
                {'value_custom_ids': self.encode_custom_values(custom_values)})

        return vals
Ejemplo n.º 8
0
    def _compute_image_small(self):
        for folder in self:
            if folder.image_small_cache and not self.env.context.get(
                    'build_cache'):
                folder.image_small = folder.image_small_cache
                continue
            try:
                _logger.debug("Resizing image folder %s...", folder.path)
                resized_images = tools.image_get_resized_images(
                    folder.image_folder,
                    return_big=False,
                    return_medium=False,
                    return_small=True)
            except:
                _logger.warning('Error with image in folder "%s"', folder.path)
                continue

            folder.image_small = resized_images['image_small']

            # Save in cache
            with self.pool.cursor() as cr:
                new_self = self.with_env(self.env(cr=cr))
                new_self.env['oomusic.folder'].browse(folder.id).sudo().write({
                    'image_small_cache':
                    resized_images['image_small'],
                })
Ejemplo n.º 9
0
 def _resize_big_images(self, images, values):
     for image in images:
         if image in values:
             values.update(
                 tools.image_get_resized_images(values[image],
                                                return_big=True,
                                                big_name=image))
Ejemplo n.º 10
0
    def _compute_sp_image(self):
        build_cache = self.env.context.get("build_cache", False)
        force = self.env.context.get("force", False)
        for artist in self:
            if artist.sp_image_cache and not build_cache and not force:
                artist.sp_image = artist.sp_image_cache
                continue

            resized_images = {"image_medium": False}
            req_json = artist._spotify_artist_search(force=force)
            try:
                _logger.debug("Retrieving image for artist %s...", artist.name)
                item = req_json["artists"]["items"][0] if req_json["artists"][
                    "items"] else {}
                images = item.get("images")
                if not images:
                    _logger.info('No image found for artist "%s" (id: %s)',
                                 artist.name, artist.id)
                    continue
                for image in images:
                    image_content = urllib.request.urlopen(image["url"],
                                                           timeout=5).read()
                    resized_images = tools.image_get_resized_images(
                        base64.b64encode(image_content),
                        return_big=False,
                        return_medium=True,
                        return_small=False,
                    )
                    break
            except KeyError:
                _logger.info("No image found for artist '%s' (id: %s)",
                             artist.name,
                             artist.id,
                             exc_info=True)
            except urllib.error.HTTPError:
                _logger.info(
                    "HTTPError when retrieving image for artist '%s' (id: %s). "
                    "Forcing Spotify info refresh.",
                    artist.name,
                    artist.id,
                )
                artist._spotify_artist_search(force=True)

            # Avoid useless save in cache
            if resized_images["image_medium"] == artist.sp_image_cache:
                continue

            artist.sp_image = resized_images["image_medium"]

            # Save in cache
            try:
                artist.sudo().write(
                    {"sp_image_cache": resized_images["image_medium"]})
                self.env.cr.commit()
            except OperationalError:
                _logger.warning(
                    "Error when writing image cache for artist id: %s",
                    artist.id,
                    exc_info=True)
Ejemplo n.º 11
0
 def _get_image_sizes(self):
     for s in self:
         try:
             vals = tools.image_get_resized_images(
                 s.with_context(bin_size=False).image_main)
         except:
             vals = {"image_medium": False, "image_small": False}
         s.update(vals)
Ejemplo n.º 12
0
 def _compute_images(self):
     try:
         resized_images = tools.image_get_resized_images(
             self.image,
             return_big=False,
             return_medium=False,
         )
     except Exception:
         _logger.exception('Error while resizing image')
         self.image_small = False
     else:
         self.image_small = resized_images['image_small']
Ejemplo n.º 13
0
 def _compute_images(self):
     for project in self:
         if project._context.get('bin_size'):
             project.image_medium = project.cover_photo
             project.image_small = project.cover_photo
             project.image = project.cover_photo
         else:
             resized_images = tools.image_get_resized_images(
                 project.cover_photo, return_big=True, avoid_resize_medium=True)
             project.image_medium = resized_images['image_medium']
             project.image_small = resized_images['image_small']
             project.image = resized_images['image']
Ejemplo n.º 14
0
    def _compute_custom_thumbnail(self):
        """Generate small custom thumbnails.

        They will be empty if the main custom thumbnail is empty.
        """
        for record in self:
            record.update(
                tools.image_get_resized_images(
                    record.with_context(bin_size=False).custom_thumbnail,
                    medium_name="custom_thumbnail_medium",
                    small_name="custom_thumbnail_small",
                ))
Ejemplo n.º 15
0
 def _compute_variant_image(self):
     for record in self:
         if record.image_variant:
             resized_images = tools.image_get_resized_images(
                 record.image_variant,
                 return_big=False,
                 return_small=False,
                 avoid_resize_medium=True)
             record.image_variant_medium = resized_images['image_medium']
             record.image_variant_big = record.image_variant
         else:
             record.image_variant_medium = record.product_tmpl_id.image_medium
             record.image_variant_big = record.product_tmpl_id.image
Ejemplo n.º 16
0
    def _compute_fm_image(self):
        for artist in self:
            if artist.fm_image_cache and not self.env.context.get(
                    'build_cache'):
                artist.fm_image = artist.fm_image_cache
                continue

            resized_images = {'image_medium': False}
            req_json = artist._lastfm_artist_getinfo()
            try:
                _logger.debug("Retrieving image for artist %s...", artist.name)
                for image in req_json['artist']['image']:
                    if image['size'] == 'large' and image['#text']:
                        image_content = urllib.request.urlopen(
                            image['#text'], timeout=5).read()
                        resized_images = tools.image_get_resized_images(
                            base64.b64encode(image_content),
                            return_big=False,
                            return_medium=True,
                            return_small=False)
                        break
            except KeyError:
                _logger.info('No image found for artist "%s" (id: %s)',
                             artist.name, artist.id)
            except urllib.error.HTTPError:
                _logger.info(
                    'HTTPError when retrieving image for artist "%s" (id: %s). '
                    'Forcing LastFM info refresh.', artist.name, artist.id)
                artist._lastfm_artist_getinfo(force=True)

            # Avoid useless save in cache
            if resized_images['image_medium'] == artist.fm_image_cache:
                continue

            artist.fm_image = resized_images['image_medium']

            # Save in cache
            with self.pool.cursor() as cr:
                new_self = self.with_env(self.env(cr=cr))
                try:
                    new_self.env['oomusic.artist'].browse(
                        artist.id).sudo().write({
                            'fm_image_cache':
                            resized_images['image_medium'],
                        })
                except OperationalError:
                    _logger.warning(
                        'Error when writing image cache for artist id: %s',
                        artist.id,
                        exc_info=True)
                    continue
Ejemplo n.º 17
0
    def _compute_fm_image(self):
        for artist in self:
            if artist.fm_image_cache and not self.env.context.get(
                    "build_cache"):
                artist.fm_image = artist.fm_image_cache
                continue

            resized_images = {"image_medium": False}
            req_json = artist._lastfm_artist_getinfo()
            try:
                _logger.debug("Retrieving image for artist %s...", artist.name)
                for image in req_json["artist"]["image"]:
                    if image["size"] == "large" and image["#text"]:
                        image_content = urllib.request.urlopen(
                            image["#text"], timeout=5).read()
                        resized_images = tools.image_get_resized_images(
                            base64.b64encode(image_content),
                            return_big=False,
                            return_medium=True,
                            return_small=False,
                        )
                        break
            except KeyError:
                _logger.info('No image found for artist "%s" (id: %s)',
                             artist.name, artist.id)
            except urllib.error.HTTPError:
                _logger.info(
                    'HTTPError when retrieving image for artist "%s" (id: %s). '
                    "Forcing LastFM info refresh.",
                    artist.name,
                    artist.id,
                )
                artist._lastfm_artist_getinfo(force=True)

            # Avoid useless save in cache
            if resized_images["image_medium"] == artist.fm_image_cache:
                continue

            artist.fm_image = resized_images["image_medium"]

            # Save in cache
            try:
                artist.sudo().write(
                    {"fm_image_cache": resized_images["image_medium"]})
                self.env.cr.commit()
            except OperationalError:
                _logger.warning(
                    "Error when writing image cache for artist id: %s",
                    artist.id,
                    exc_info=True)
Ejemplo n.º 18
0
 def _compute_images(self):
     if self._context.get('bin_size'):
         self.image_medium = self.image
         self.image_small = self.image
         
     else:
         resized_images = tools.image_get_resized_images(self.image, return_big=True, avoid_resize_medium=True)
         self.image_medium = resized_images['image_medium']
         self.image_small = resized_images['image_small']
        
     if not self.image_medium:
         self.image_medium = self.image_medium
     if not self.image_small:
         self.image_small = self.image_small
Ejemplo n.º 19
0
    def _get_image(self):
        """ calculate the images sizes and set the images to the corresponding
            fields
        """
        image = self.image

        # check if the context contains the magic `bin_size` key
        if self.env.context.get("bin_size"):
            # refetch the image with a clean context
            image = self.env[self._name].with_context({}).browse(self.id).image

        data = tools.image_get_resized_images(image)
        self.image_medium = data["image_medium"]
        self.image_small = data["image_small"]
        return True
Ejemplo n.º 20
0
 def _compute_images(self):
     if self._context.get('bin_size'):
         self.image_medium = self.image_variant
         self.image_small = self.image_variant
         self.image = self.image_variant
     else:
         resized_images = tools.image_get_resized_images(self.image_variant, return_big=True, avoid_resize_medium=True)
         self.image_medium = resized_images['image_medium']
         self.image_small = resized_images['image_small']
         self.image = resized_images['image']
     if not self.image_medium:
         self.image_medium = self.product_tmpl_id.image_medium
     if not self.image_small:
         self.image_small = self.product_tmpl_id.image_small
     if not self.image:
         self.image = self.product_tmpl_id.image
    def get_variant_vals(self, value_ids, custom_values={}, **kwargs):
        """ Hook to alter the values of the product variant before creation

            :param value_ids: list of product.attribute.values ids
            :param custom_values: dict {product.attribute.id: custom_value}

            :returns: dictionary of values to pass to product.create() method
         """
        self.ensure_one()

        image = self.get_config_image_obj(value_ids).image
        all_images = tools.image_get_resized_images(image,
                                                    avoid_resize_medium=True)
        vals = {
            'product_tmpl_id': self.id,
            'attribute_value_ids': [(6, 0, value_ids)],
            'taxes_id': [(6, 0, self.taxes_id.ids)],
            'image': image,
            'image_variant': image,
            'image_medium': all_images['image_medium'],
            'image_small': all_images['image_medium'],
        }

        binary_attribute_ids = self.env['product.attribute'].search([
            ('custom_type', '=', 'binary')
        ]).ids

        if not custom_values:
            return vals

        custom_lines = []

        for key, val in custom_values.iteritems():
            custom_vals = {'attribute_id': key}
            if key in binary_attribute_ids:
                custom_vals.update({'attachment_ids': [(6, 0, val.ids)]})
            else:
                custom_vals.update({'value': val})
            custom_lines.append((0, 0, custom_vals))
        vals.update({'value_custom_ids': custom_lines})

        return vals
Ejemplo n.º 22
0
    def confirm_import(self):
        images_data = self.load_images_from_folder()
        Product = self.env['product.product']
        bounce_imgs = images_data[1]

        for data in images_data[0]:
            product_id = Product.search(
                [('default_code', '=', data['default_code'])], limit=1)
            if product_id:
                data_img = tools.image_get_resized_images(data['image'],
                                                          return_big=True)
                image = data_img['image']
                product_id.image = image
            else:
                bounce_imgs.append([
                    'Product(Item number) not found: ' + str(data['filename'])
                ])

        ctx = {}
        if bounce_imgs:
            context = {}
            file = self._write_bounced_images('/tmp', bounce_imgs, context)
            # f = open(file['file_path'])
            # f.close()
            self.datas = file.get('file_data')
            # ctx.update({'default_is_bounced': True, 'default_file': file.get('fname'),
            #             'default_file_data': file.get('file_data')})

            _logger.info("Successfully completed import process.")
            self.filename = file.get('fname')
            self.is_bounced = True
        self.is_process_finished = True
        return {
            'view_mode': 'form',
            'res_id': self.id,
            'res_model': 'product.image.import',
            'view_type': 'form',
            'type': 'ir.actions.act_window',
            'context': self.env.context,
            'target': 'new',
        }
Ejemplo n.º 23
0
    def _compute_image_medium(self):
        for folder in self:
            if folder.image_medium_cache and not self.env.context.get(
                    'build_cache'):
                folder.image_medium = folder.image_medium_cache
                continue
            try:
                _logger.debug("Resizing image folder %s...", folder.path)
                resized_images = tools.image_get_resized_images(
                    folder.image_folder,
                    return_big=False,
                    return_medium=True,
                    return_small=False)
            except:
                _logger.warning('Error with image in folder "%s" (id: %s)',
                                folder.path,
                                folder.id,
                                exc_info=True)
                continue

            folder.image_medium = resized_images['image_medium']

            # Avoid useless save in cache
            if resized_images['image_medium'] == folder.image_medium_cache:
                continue

            # Save in cache
            with self.pool.cursor() as cr:
                new_self = self.with_env(self.env(cr))
                try:
                    new_self.env['oomusic.folder'].browse(
                        folder.id).sudo().write({
                            'image_medium_cache':
                            resized_images['image_medium'],
                        })
                except OperationalError:
                    _logger.warning(
                        'Error when writing image cache for folder id: %s',
                        folder.id,
                        exc_info=True)
                    continue
Ejemplo n.º 24
0
    def _compute_fm_image(self):
        for artist in self:
            if artist.fm_image_cache and not self.env.context.get(
                    'build_cache'):
                artist.fm_image = artist.fm_image_cache
                continue

            resized_images = {'image_medium': False}
            url = 'https://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=' + artist.name
            req_json = json.loads(self.env['oomusic.lastfm'].get_query(url))
            try:
                _logger.debug("Retrieving image for artist %s...", artist.name)
                for image in req_json['artist']['image']:
                    if image['size'] == 'large':
                        image_content = urllib2.urlopen(image['#text'],
                                                        timeout=5).read()
                        resized_images = tools.image_get_resized_images(
                            base64.b64encode(image_content),
                            return_big=False,
                            return_medium=True,
                            return_small=False)
                        break
            except:
                _logger.warning('Error with image for artist "%s"',
                                artist.name)
                continue

            # Avoid doing a write is nothing has to be done
            if not resized_images['image_medium'] and not artist.fm_image_cache:
                continue

            artist.fm_image = resized_images['image_medium']

            # Save in cache
            with self.pool.cursor() as cr:
                new_self = self.with_env(self.env(cr=cr))
                new_self.env['oomusic.artist'].browse(artist.id).sudo().write({
                    'fm_image_cache':
                    resized_images['image_medium'],
                })
Ejemplo n.º 25
0
    def _compute_image_small(self):
        for folder in self:
            if folder.image_small_cache and not self.env.context.get(
                    "build_cache"):
                folder.image_small = folder.image_small_cache
                continue
            try:
                _logger.debug("Resizing image folder %s...", folder.path)
                resized_images = tools.image_get_resized_images(
                    folder.image_folder,
                    return_big=False,
                    return_medium=False,
                    return_small=True)
            except:
                _logger.warning(
                    'Error with image in folder "%s" (id: %s)',
                    folder.path,
                    folder.id,
                    exc_info=True,
                )
                continue

            folder.image_small = resized_images["image_small"]

            # Avoid useless save in cache
            if resized_images["image_small"] == folder.image_small_cache:
                continue

            # Save in cache
            try:
                folder.sudo().write(
                    {"image_small_cache": resized_images["image_small"]})
                self.env.cr.commit()
            except OperationalError:
                _logger.warning(
                    "Error when writing image cache for folder id: %s",
                    folder.id,
                    exc_info=True)
Ejemplo n.º 26
0
	def _compute_images(self):
		if self._context.get('bin_size'):
			self.imagen_medium = self.imagen_variante
		else:
			resized_images = tools.image_get_resized_images(self.imagen_variante, return_big=True, avoid_resize_medium=True)
			self.imagen_medium = resized_images['image_medium']
Ejemplo n.º 27
0
 def _compute_image(self):
     return dict((rec.id, tools.image_get_resized_images(rec.image))
                 for rec in self)
Ejemplo n.º 28
0
 def _get_images(self):
     for i in self:
         image = i.image
         data = tools.image_get_resized_images(image)
         i.image_small = data["image_small"]
Ejemplo n.º 29
0
 def _get_image(self):
     return dict((p.id, tools.image_get_resized_images(p.img_tmp_proof_of_delivery)) for p in self)
Ejemplo n.º 30
0
 def _get_image(self):
     result = dict.fromkeys(self._ids, False)
     for obj in self:
         result[obj.id] = tools.image_get_resized_images(obj.image)
     return result
Ejemplo n.º 31
0
 def _get_image(self):
     image = self.image
     data = tools.image_get_resized_images(image)
     self.image_medium = data["image_medium"]
     self.image_small = data["image_small"]
     return True