def test_03_get_image_info(self): gif_base64 = "R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=" self.authenticate('admin', 'admin') # Upload document. response = self.url_open( '/web_editor/attachment/add_data', headers={'Content-Type': 'application/json'}, data=json_safe.dumps({'params': { 'name': 'test.gif', 'data': gif_base64, 'is_image': True, }}) ).json() self.assertFalse('error' in response, 'Upload failed: %s' % response.get('error', {}).get('message')) attachment_id = response['result']['id'] image_src = response['result']['image_src'] mimetype = response['result']['mimetype'] self.assertEqual('image/gif', mimetype, "Wrong mimetype") # Ensure image info can be retrieved. response = self.url_open('/web_editor/get_image_info', headers={'Content-Type': 'application/json'}, data=json_safe.dumps({ "params": { "src": image_src, } }), ).json() self.assertEqual(attachment_id, response['result']['original']['id'], "Wrong id") self.assertEqual(image_src, response['result']['original']['image_src'], "Wrong image_src") self.assertEqual(mimetype, response['result']['original']['mimetype'], "Wrong mimetype")
def write(self, vals): if 'cover_properties' not in vals: return super().write(vals) cover_properties = json_safe.loads(vals['cover_properties']) resize_classes = cover_properties.get('resize_class', '').split() classes = [ 'o_half_screen_height', 'o_full_screen_height', 'cover_auto' ] if not set(resize_classes).isdisjoint(classes): # Updating cover properties and the given 'resize_class' set is # valid, normal write. return super().write(vals) # If we do not receive a valid resize_class via the cover_properties, we # keep the original one (prevents updates on list displays from # destroying resize_class). copy_vals = dict(vals) for item in self: old_cover_properties = json_safe.loads(item.cover_properties) cover_properties['resize_class'] = old_cover_properties.get( 'resize_class', classes[0]) copy_vals['cover_properties'] = json_safe.dumps(cover_properties) super(WebsiteCoverPropertiesMixin, item).write(copy_vals) return True
class WebsiteCoverPropertiesMixin(models.AbstractModel): _name = 'website.cover_properties.mixin' _description = 'Cover Properties Website Mixin' cover_properties = fields.Text( 'Cover Properties', default=lambda s: json_safe.dumps(s._default_cover_properties())) def _default_cover_properties(self): return { "background_color_class": "o_cc3", "background-image": "none", "opacity": "0.2", "resize_class": "o_half_screen_height", } def _get_background(self, height=None, width=None): self.ensure_one() properties = json_safe.loads(self.cover_properties) img = properties.get('background-image', "none") if img.startswith('url(/web/image/'): suffix = "" if height is not None: suffix += "&height=%s" % height if width is not None: suffix += "&width=%s" % width if suffix: suffix = '?' not in img and "?%s" % suffix or suffix img = img[:-1] + suffix + ')' return img
class WebsiteCoverPropertiesMixin(models.AbstractModel): _name = 'website.cover_properties.mixin' _description = 'Cover Properties Website Mixin' cover_properties = fields.Text( 'Cover Properties', default=lambda s: json_safe.dumps(s._default_cover_properties())) def _default_cover_properties(self): return { "background_color_class": "o_cc3", "background-image": "none", "opacity": "0.2", "resize_class": "o_half_screen_height", } def _get_background(self, height=None, width=None): self.ensure_one() properties = json_safe.loads(self.cover_properties) img = properties.get('background-image', "none") if img.startswith('url(/web/image/'): suffix = "" if height is not None: suffix += "&height=%s" % height if width is not None: suffix += "&width=%s" % width if suffix: suffix = '?' not in img and "?%s" % suffix or suffix img = img[:-1] + suffix + ')' return img def write(self, vals): if 'cover_properties' not in vals: return super().write(vals) cover_properties = json_safe.loads(vals['cover_properties']) resize_classes = cover_properties.get('resize_class', '').split() classes = [ 'o_half_screen_height', 'o_full_screen_height', 'cover_auto' ] if not set(resize_classes).isdisjoint(classes): # Updating cover properties and the given 'resize_class' set is # valid, normal write. return super().write(vals) # If we do not receive a valid resize_class via the cover_properties, we # keep the original one (prevents updates on list displays from # destroying resize_class). copy_vals = dict(vals) for item in self: old_cover_properties = json_safe.loads(item.cover_properties) cover_properties['resize_class'] = old_cover_properties.get( 'resize_class', classes[0]) copy_vals['cover_properties'] = json_safe.dumps(cover_properties) super(WebsiteCoverPropertiesMixin, item).write(copy_vals) return True
def test_01_upload_document(self): self.authenticate('admin', 'admin') # Upload document. response = self.url_open( '/web_editor/attachment/add_data', headers={'Content-Type': 'application/json'}, data=json_safe.dumps({'params': { 'name': 'test.txt', 'data': 'SGVsbG8gd29ybGQ=', # base64 Hello world 'is_image': False, }}) ).json() self.assertFalse('error' in response, 'Upload failed: %s' % response.get('error', {}).get('message')) attachment_id = response['result']['id'] checksum = response['result']['checksum'] # Download document and check content. response = self.url_open( '/web/content/%s?unique=%s&download=true' % (attachment_id, checksum) ) self.assertEqual(200, response.status_code, 'Expect response') self.assertEqual(b'Hello world', response.content, 'Expect raw content')
def google_map(self, *arg, **post): clean_ids = [] for partner_id in post.get('partner_ids', "").split(","): try: clean_ids.append(int(partner_id)) except ValueError: pass partners = request.env['res.partner'].sudo().search([ ("id", "in", clean_ids), ('website_published', '=', True), ('is_company', '=', True) ]) partner_data = {"counter": len(partners), "partners": []} for partner in partners.with_context(show_address=True): partner_data["partners"].append({ 'id': partner.id, 'name': partner.name, 'address': '\n'.join(partner.name_get()[0][1].split('\n')[1:]), 'latitude': str(partner.partner_latitude) if partner.partner_latitude else False, 'longitude': str(partner.partner_longitude) if partner.partner_longitude else False, }) if 'customers' in post.get('partner_url', ''): partner_url = '/customers/' else: partner_url = '/partners/' google_maps_api_key = request.website.google_maps_api_key values = { 'partner_url': partner_url, 'partner_data': scriptsafe.dumps(partner_data), 'google_maps_api_key': google_maps_api_key, } return request.render("website_google_map.google_map", values)
class WebsiteCoverPropertiesMixin(models.AbstractModel): _name = 'website.cover_properties.mixin' _description = 'Cover Properties Website Mixin' cover_properties = fields.Text('Cover Properties', default=lambda s: json_scriptsafe.dumps(s._default_cover_properties())) def _default_cover_properties(self): return { "background_color_class": "o_cc3", "background-image": "none", "opacity": "0.2", "resize_class": "o_half_screen_height", } def write(self, vals): if 'cover_properties' not in vals: return super().write(vals) cover_properties = json_scriptsafe.loads(vals['cover_properties']) resize_classes = cover_properties.get('resize_class', '').split() classes = ['o_half_screen_height', 'o_full_screen_height', 'cover_auto'] if not set(resize_classes).isdisjoint(classes): # Updating cover properties and the given 'resize_class' set is # valid, normal write. return super().write(vals) # If we do not receive a valid resize_class via the cover_properties, we # keep the original one (prevents updates on list displays from # destroying resize_class). copy_vals = dict(vals) for item in self: old_cover_properties = json_scriptsafe.loads(item.cover_properties) cover_properties['resize_class'] = old_cover_properties.get('resize_class', classes[0]) copy_vals['cover_properties'] = json_scriptsafe.dumps(cover_properties) super(WebsiteCoverPropertiesMixin, item).write(copy_vals) return True
def donation_pay(self, **kwargs): """ Behaves like PaymentPortal.payment_pay but for donation :param dict kwargs: As the parameters of in payment_pay, with the additional: - str donation_options: The options settled in the donation snippet - str donation_descriptions: The descriptions for all prefilled amounts :return: The rendered donation form :rtype: str :raise: werkzeug.exceptions.NotFound if the access token is invalid """ kwargs['is_donation'] = True kwargs['currency_id'] = int( kwargs.get('currency_id', request.env.company.currency_id.id)) kwargs['amount'] = float(kwargs.get('amount', 25)) kwargs['donation_options'] = kwargs.get( 'donation_options', json_safe.dumps(dict(customAmount="freeAmount"))) if request.env.user._is_public(): kwargs['partner_id'] = request.env.user.partner_id.id kwargs['access_token'] = payment_utils.generate_access_token( kwargs['partner_id'], kwargs['amount'], kwargs['currency_id']) return self.payment_pay(**kwargs)