Esempio n. 1
0
    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,
                })

        page.write({
            'key': page_key,
            'name': data['name'],
            'url': url,
            'website_published': data['website_published'],
            'website_indexed': data['website_indexed'],
            'date_publish': data['date_publish'] or None,
            'is_homepage': data['is_homepage'],
        })

        # Create redirect if needed
        if data['create_redirect']:
            self.env['website.redirect'].create({
                'type': data['redirect_type'],
                'url_from': original_url,
                'url_to': url,
                'website_id': website.id,
            })

        return url
Esempio n. 2
0
    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,
                'track': True,
            })
            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
Esempio n. 3
0
    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('.')
        # completely arbitrary max_length
        page_url = '/' + slugify(name, max_length=200, path=True)
        page_key = self.get_unique_path(slugify(name, 50))
        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 = '%s.%s' % (template_module, page_key)
        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 ispage:
            page = self.env['website.page'].create({
                'url':
                page_url,
                'website_ids': [(6, None, [self.get_current_website().id])],
                'view_id':
                view.id
            })
            result['view_id'] = view.id
        if add_menu:
            self.env['website.menu'].create({
                'name':
                name,
                'url':
                page_url,
                'parent_id':
                self.get_current_website().menu_id.id,
                'page_id':
                page.id,
                'website_id':
                self.get_current_website().id,
            })
        return result
Esempio n. 4
0
    def save_page_info(self, website_id, data):
        website = self.env['website'].browse(website_id)

        if data['is_homepage'] and website.homepage_id.id != int(data['id']):
            # If page is set as the new homepage, set it on website (only page can be set as homepage)
            website.write({'homepage_id': data['id']})
        else:
            if not data['is_homepage'] and website.homepage_id.id == int(
                    data['id']):
                # If the page is not a homepage, check if it was the homepage
                website.write({'homepage_id': None})

        #If URL has been edited, slug it
        page = self.browse(int(data['id']))
        original_url = page.url
        url = data['url']
        if page.url != url:
            url = slugify(url, max_length=200, path=True)

        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,
                })

        page.write({
            'key': 'website.' + slugify(data['name'], 50),
            'name': data['name'],
            'url': url,
            'website_published': data['website_published'],
            'website_indexed': data['website_indexed'],
            'date_publish': data['date_publish'] or None
        })

        # Create redirect if needed
        if data['create_redirect']:
            self.env['website.redirect'].create({
                'type': data['redirect_type'],
                'url_from': original_url,
                'url_to': url,
                'website_id': website.id,
            })

        return True
Esempio n. 5
0
    def clone_page(self, page_id, page_name=None, clone_menu=True):
        """ Clone a page, given its identifier
            :param page_id : website.page identifier
        """
        page = self.browse(int(page_id))
        copy_param = dict(
            name=page_name or page.name,
            website_id=self.env['website'].get_current_website().id)
        if page_name:
            page_url = '/' + slugify(page_name, max_length=1024, path=True)
            copy_param['url'] = self.env['website'].get_unique_path(page_url)

        new_page = page.copy(copy_param)
        # Should not clone menu if the page was cloned from one website to another
        # Eg: Cloning a generic page (no website) will create a page with a website, we can't clone menu (not same container)
        if clone_menu and new_page.website_id == page.website_id:
            menu = self.env['website.menu'].search([('page_id', '=', page_id)],
                                                   limit=1)
            if menu:
                # If the page being cloned has a menu, clone it too
                menu.copy({
                    'url': new_page.url,
                    'name': new_page.name,
                    'page_id': new_page.id
                })

        return new_page.url + '?enable_editor=1'
Esempio n. 6
0
 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
Esempio n. 7
0
    def get_seo_data(self, res_id, res_model):
        if not request.env.user.has_group('website.group_website_publisher'):
            raise werkzeug.exceptions.Forbidden()

        fields = [
            'website_meta_title', 'website_meta_description',
            'website_meta_keywords', 'website_meta_og_img'
        ]
        if res_model == 'website.page':
            fields.extend(['website_indexed', 'website_id'])

        record = request.env[res_model].browse(res_id)
        res = record._read_format(fields)[0]
        res['has_social_default_image'] = request.website.has_social_default_image

        if res_model not in (
                'website.page',
                'ir.ui.view') and 'seo_name' in record:  # allow custom slugify
            res['seo_name_default'] = slugify(
                record.display_name)  # default slug, if seo_name become empty
            res['seo_name'] = record.seo_name and slugify(
                record.seo_name) or ''
        return res
Esempio n. 8
0
 def message_new(self, msg_dict, custom_values=None):
     custom_values = custom_values if custom_values is not None else {}
     parent_directory_id = custom_values.get("parent_id", None)
     parent_directory = self.sudo().browse(parent_directory_id)
     if not parent_directory_id or not parent_directory.exists():
         raise ValueError("No directory could be found!")
     if parent_directory.alias_process == "files":
         parent_directory._process_message(msg_dict)
         return parent_directory
     names = parent_directory.child_directory_ids.mapped("name")
     subject = slugify(msg_dict.get("subject", _("Alias-Mail-Extraction")))
     defaults = dict(
         {"name": unique_name(subject, names, escape_suffix=True)},
         **custom_values)
     directory = super().message_new(msg_dict, custom_values=defaults)
     directory._process_message(msg_dict)
     return directory
Esempio n. 9
0
    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'],
            'visibility': data['visibility'],
        }
        if page.visibility == 'restricted_group' and data[
                'visibility'] != "restricted_group":
            w_vals['groups_id'] = False
        elif 'group_id' in data:
            w_vals['groups_id'] = [data['group_id']]
        if 'visibility_pwd' in data:
            w_vals[
                'visibility_password_display'] = data['visibility_pwd'] or ''

        page.with_context(no_cow=True).write(w_vals)

        # Create redirect if needed
        if data['create_redirect']:
            self.env['website.rewrite'].create({
                'name':
                data['name'],
                'redirect_type':
                data['redirect_type'],
                'url_from':
                original_url,
                'url_to':
                url,
                'website_id':
                website.id,
            })

        return url
Esempio n. 10
0
 def test_unicode(self):
     self.assertEqual("heterogeneite", slugify(u"hétérogénéité"))
Esempio n. 11
0
 def test_numbers(self):
     self.assertEqual("article-1", slugify(u"Article 1"))
Esempio n. 12
0
 def test_all(self):
     self.assertEqual("do-you-know-martine-a-la-plage",
                      slugify(u"Do YOU know 'Martine à la plage' ?"))
Esempio n. 13
0
 def test_str_to_unicode(self):
     self.assertEqual("espana", slugify("España"))
Esempio n. 14
0
 def test_special_chars(self):
     self.assertEqual("o-d-o-o", slugify(u"o!#d{|\o/@~o&%^?"))
Esempio n. 15
0
 def test_caps(self):
     self.assertEqual("camelcase", slugify(u"CamelCase"))
Esempio n. 16
0
 def test_underscore(self):
     self.assertEqual("one-two", slugify(u"one_two"))
Esempio n. 17
0
 def test_spaces(self):
     self.assertEqual("spaces", slugify(u"   spaces   "))