def new_page(self, name=False, add_menu=False, template='website.default_page', ispage=True, namespace=None): """ Create a new website page, and assign it a xmlid based on the given one :param name : the name of the page :param template : potential xml_id of the page to create :param namespace : module part of the xml_id if none, the template module name is used """ if namespace: template_module = namespace else: template_module, _ = template.split('.') page_url = '/' + slugify(name, max_length=1024, path=True) page_url = self.get_unique_path(page_url) page_key = slugify(name) result = dict({'url': page_url, 'view_id': False}) if not name: name = 'Home' page_key = 'home' template_record = self.env.ref(template) website_id = self._context.get('website_id') key = self.get_unique_key(page_key, template_module) view = template_record.copy({'website_id': website_id, 'key': key}) view.with_context(lang=None).write({ 'arch': template_record.arch.replace(template, key), 'name': name, }) if view.arch_fs: view.arch_fs = False website = self.get_current_website() if ispage: page = self.env['website.page'].create({ 'url': page_url, 'website_id': website.id, # remove it if only one website or not? 'view_id': view.id, }) result['view_id'] = view.id if add_menu: self.env['website.menu'].create({ 'name': name, 'url': page_url, 'parent_id': website.menu_id.id, 'page_id': page.id, 'website_id': website.id, }) return result
def search_pages(self, needle=None, limit=None): name = slugify(needle, max_length=50, path=True) res = [] for page in self.enumerate_pages(query_string=name, force=True): res.append(page) if len(res) == limit: break return res
def test_all(self): self.assertEqual( "do-you-know-martine-a-la-plage", slugify(u"Do YOU know 'Martine à la plage' ?") )
def test_numbers(self): self.assertEqual( "article-1", slugify(u"Article 1") )
def test_str_to_unicode(self): self.assertEqual( "espana", slugify("España") )
def test_special_chars(self): self.assertEqual( "o-d-o-o", slugify(u"o!#d{|\o/@~o&%^?") )
def test_caps(self): self.assertEqual( "camelcase", slugify(u"CamelCase") )
def test_underscore(self): self.assertEqual( "one-two", slugify(u"one_two") )
def test_unicode(self): self.assertEqual( "heterogeneite", slugify(u"hétérogénéité") )
def test_spaces(self): self.assertEqual( "spaces", slugify(u" spaces ") )
def save_page_info(self, website_id, data): website = self.env['website'].browse(website_id) page = self.browse(int(data['id'])) # If URL has been edited, slug it original_url = page.url url = data['url'] if not url.startswith('/'): url = '/' + url if page.url != url: url = '/' + slugify(url, max_length=1024, path=True) url = self.env['website'].get_unique_path(url) # If name has changed, check for key uniqueness if page.name != data['name']: page_key = self.env['website'].get_unique_key(slugify( data['name'])) else: page_key = page.key menu = self.env['website.menu'].search([('page_id', '=', int(data['id']))]) if not data['is_menu']: # If the page is no longer in menu, we should remove its website_menu if menu: menu.unlink() else: # The page is now a menu, check if has already one if menu: menu.write({'url': url}) else: self.env['website.menu'].create({ 'name': data['name'], 'url': url, 'page_id': data['id'], 'parent_id': website.menu_id.id, 'website_id': website.id, }) # Edits via the page manager shouldn't trigger the COW # mechanism and generate new pages. The user manages page # visibility manually with is_published here. w_vals = { 'key': page_key, 'name': data['name'], 'url': url, 'is_published': data['website_published'], 'website_indexed': data['website_indexed'], 'date_publish': data['date_publish'] or None, 'is_homepage': data['is_homepage'], } page.with_context(no_cow=True).write(w_vals) # Create redirect if needed if data['create_redirect']: self.env['website.rewrite'].create({ 'redirect_type': data['redirect_type'], 'url_from': original_url, 'url_to': url, 'website_id': website.id, }) return url