def image_shape(self, module, filename, img_key, **kwargs): svg = self._get_shape_svg(module, 'image_shapes', filename) record = request.env['ir.binary']._find_record(img_key) stream = request.env['ir.binary']._get_image_stream_from(record) if stream.type == 'url': return stream.get_response() if stream.type == 'path': with file_open(stream.path, 'rb') as file: image = file.read() else: image = stream.data img = binary_to_image(image) width, height = tuple(str(size) for size in img.size) root = etree.fromstring(svg) root.attrib.update({'width': width, 'height': height}) # Update default color palette on shape SVG. svg, _ = self._update_svg_colors( kwargs, etree.tostring(root, pretty_print=True).decode('utf-8')) # Add image in base64 inside the shape. uri = image_data_uri(b64encode(image)) svg = svg.replace('<image xlink:href="', '<image xlink:href="%s' % uri) return request.make_response(svg, [ ('Content-type', 'image/svg+xml'), ('Cache-control', 'max-age=%s' % http.STATIC_CACHE_LONG), ])
def image_shape(self, module, filename, img_key, **kwargs): svg = self._get_shape_svg(module, 'image_shapes', filename) _, _, image = request.env['ir.http'].binary_content( xmlid=img_key, model='ir.attachment', field='datas', default_mimetype='image/png') if not image: image = request.env['ir.http']._placeholder() img = binary_to_image(image) width, height = tuple(str(size) for size in img.size) root = etree.fromstring(svg) root.attrib.update({'width': width, 'height': height}) # Update default color palette on shape SVG. svg, _ = self._update_svg_colors( kwargs, etree.tostring(root, pretty_print=True).decode('utf-8')) # Add image in base64 inside the shape. uri = image_data_uri(b64encode(image)) svg = svg.replace('<image xlink:href="', '<image xlink:href="%s' % uri) return request.make_response(svg, [ ('Content-type', 'image/svg+xml'), ('Cache-control', 'max-age=%s' % http.STATIC_CACHE_LONG), ])
def _get_qr_code_base64(self, qr_method, amount, currency, debtor_partner, free_communication, structured_communication): """ Hook for extension, to support the different QR generation methods. This function uses the provided qr_method to try generation a QR-code for the given data. It it succeeds, it returns QR code in base64 url; else None. :param qr_method: The QR generation method to be used to make the QR-code. :param amount: The amount to be paid :param currency: The currency in which amount is expressed :param debtor_partner: The partner to which this QR-code is aimed (so the one who will have to pay) :param free_communication: Free communication to add to the payment when generating one with the QR-code :param structured_communication: Structured communication to add to the payment when generating one with the QR-code """ # pylint: disable=E1137 # pylint: disable=E1134 # (PyLint doesn't get that we are not assigning to None here) params = self._get_qr_code_generation_params(qr_method, amount, currency, debtor_partner, free_communication, structured_communication) if params: try: barcode = self.env['ir.actions.report'].barcode(**params) except (ValueError, AttributeError): raise werkzeug.exceptions.HTTPException( description='Cannot convert into barcode.') return image_data_uri(base64.b64encode(barcode)) return None
def userinfo(self, **kw): user = request.env.user uid = request.session.uid result = { 'sub': uid, 'name': user.name, 'locale': user.lang, 'zoneinfo': user.tz, 'username': user.login, 'email': user.partner_id.email, 'website': user.partner_id.website, 'phone_number': user.partner_id.phone, 'address': { 'formatted': user.partner_id.contact_address, 'street_address': user.partner_id.street, 'locality': user.partner_id.city, 'postal_code': user.partner_id.zip, 'region': user.partner_id.state_id.display_name, 'country': user.partner_id.country_id.display_name, }, 'updated_at': user.partner_id.write_date, 'picture': image_data_uri(user.partner_id.image_medium), } content = json.dumps(result, sort_keys=True, indent=4, cls=ResponseEncoder) return Response(content, content_type='application/json;charset=utf-8', status=200)
def wkn_my_profile(self): user_id = self.env.user image = image_data_uri(user_id.partner_id.image_256 ) if user_id.partner_id.image_256 else '' profile = { 'partner_id': user_id.partner_id.id, 'name': user_id.partner_id.name, 'email': user_id.partner_id.email, 'image': image, 'phone': user_id.partner_id.phone, 'street': user_id.partner_id.street, 'product_uom_qty': 0, 'order_count': 0, 'amount_total': 0, } if user_id.partner_id.birthdate_date: profile['year'] = user_id.partner_id.birthdate_date.year profile['month'] = user_id.partner_id.birthdate_date.month profile['day'] = user_id.partner_id.birthdate_date.day if len(user_id.partner_id.stats_ids): profile['product_uom_qty'] = user_id.partner_id.stats_ids[0][ 'product_uom_qty'] profile['order_count'] = user_id.partner_id.stats_ids[0][ 'order_count'] profile['amount_total'] = user_id.partner_id.stats_ids[0][ 'amount_total'] return profile
def build_swiss_code_base64(self, amount, currency_name, not_used_anymore_1, debtor_partner, not_used_anymore_2, structured_communication, free_communication): qr_code_vals = self.build_swiss_code_vals(amount, currency_name, not_used_anymore_1, debtor_partner, not_used_anymore_2, structured_communication, free_communication) try: barcode = self.env['ir.actions.report'].barcode('QR_quiet', '\n'.join(qr_code_vals), width=256, height=256, humanreadable=1) except (ValueError, AttributeError): raise werkzeug.exceptions.HTTPException(description='Cannot convert into barcode.') return image_data_uri(base64.b64encode(barcode))
def build_qr_code_base64(self, amount, comment): try: barcode = self.env['ir.actions.report'].barcode( 'QR', self.build_qr_code_vals(amount, comment), width=128, height=128, humanreadable=1) except (ValueError, AttributeError): raise werkzeug.exceptions.HTTPException( description='Cannot convert into barcode.') return image_data_uri(base64.b64encode(barcode))
def _get_documents_src(self, employee): res = {} for field in [ 'id_card', 'image_1920', 'driving_license', 'mobile_invoice', 'sim_card', 'internet_invoice' ]: if employee[field]: if employee[field][:7] == b'JVBERi0': img_src = "data:application/pdf;base64,%s" % ( employee[field].decode()) else: img_src = image_data_uri(employee[field]) res[field] = img_src else: res[field] = False return res
def fix_image_data_uri(base64_source): if base64_source: return image_data_uri(base64_source) else: return ''
return super(FleetVehicleModelBrand, self).create(vals_list) ''' image.image_resize_and_sharpen(image=img, size=(500, 500)) image.image_get_resized_images(base64_source=img) ''' @api.one @api.depends('image_variant', 'product_tmpl_id.image') 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) ''' image.image_data_uri(base64_source=img) """This returns data URL scheme according RFC 2397 (https://tools.ietf.org/html/rfc2397) for all kind of supported images (PNG, GIF, JPG and SVG), defaulting on PNG type if not mimetype detected. """ # <img t-if="company.logo" t-att-src="image_data_uri(company.logo)" style="max-height: 45px;" alt="Logo"/> image.image_save_for_web(img) """ Save image optimized for web usage. """ image.crop_image(img) """ Used for cropping image and create thumbnail """
def _get_personal_infos(self, contract): initial_values = {} dropdown_options = {} targets = { 'employee': contract.employee_id, 'address': contract.employee_id.address_home_id, 'bank_account': contract.employee_id.bank_account_id, } countries = request.env['res.country'].search([]) states = request.env['res.country.state'].search([]) langs = request.env['res.lang'].search([]) # PERSONAL INFOS personal_infos = request.env['hr.contract.salary.personal.info'].sudo( ).search([ '|', ('structure_type_id', '=', False), ('structure_type_id', '=', contract.structure_type_id.id) ]) mapped_personal_infos = [ defaultdict(lambda: request.env['hr.contract.salary.personal.info'] ), # Main Panel request.env['hr.contract.salary.personal.info'], # Side Panel ] for personal_info in personal_infos: if personal_info.position == 'left': mapped_personal_infos[0][ personal_info.info_type_id.name] |= personal_info else: mapped_personal_infos[1] |= personal_info target = targets[personal_info.applies_on] if personal_info.display_type == 'document': if personal_info.field in target and target[ personal_info.field]: if target[personal_info.field][:7] == b'JVBERi0': content = "data:application/pdf;base64,%s" % ( target[personal_info.field].decode()) else: content = image_data_uri(target[personal_info.field]) else: content = False initial_values[personal_info.field] = content else: initial_values[personal_info.field] = target[ personal_info. field] if personal_info.field in target else '' if personal_info.display_type == 'dropdown': # Set record id instead of browse record as value if isinstance(initial_values[personal_info.field], models.BaseModel): initial_values[personal_info.field] = initial_values[ personal_info.field].id if personal_info.dropdown_selection == 'specific': values = [(value.value, value.name) for value in personal_info.value_ids] elif personal_info.dropdown_selection == 'country': values = [(country.id, country.name) for country in countries] elif personal_info.dropdown_selection == 'state': values = [(state.id, state.name) for state in states] elif personal_info.dropdown_selection == 'lang': values = [(lang.id, lang.name) for lang in langs] dropdown_options[personal_info.field] = values return mapped_personal_infos, dropdown_options, initial_values