宜兴通达竭诚为您服务。

通达科技 - 宜兴电脑维护及IT服务外包服务商

Managing Sitemaps for the Odoo Website
  • 上传者: Administrator
  • 上传时间:2024年02月24日 06时02分40秒
摘要:
A sitemap is important for any website. It is a file that contains information about the website's pages and other files on the website. Search engine ......
正文 相关文章 请喝咖啡

    A sitemap is important for any website. It is a file that contains information about the website's pages and other files on the website. Search engines use sitemaps to index the pages of your website.

    This blog describes how to modify an existing sitemap for the Odoo website. We can use the sitemap_xml_index method for changing the already existing sitemap. Ie,

    @http.route('/sitemap.xml', type='http', auth="public", website=True, multilang=False, sitemap=False)
    def sitemap_xml_index(self, **kwargs):
       current_website = request.website
       Attachment = request.env['ir.attachment'].sudo()
       View = request.env['ir.ui.view'].sudo()
       mimetype = 'application/xml;charset=utf-8'
       content = None
       def create_sitemap(url, content):
           return Attachment.create({
               'raw': content.encode(),
               'mimetype': mimetype,
               'type': 'binary',
               'name': url,
               'url': url,
           })
       dom = [('url', '=', '/sitemap-%d.xml' % current_website.id), ('type', '=', 'binary')]
       sitemap = Attachment.search(dom, limit=1)
       if sitemap:
           # Check if stored version is still valid
           create_date = fields.Datetime.from_string(sitemap.create_date)
           delta = datetime.datetime.now() - create_date
           if delta < SITEMAP_CACHE_TIME:
               content = base64.b64decode(sitemap.datas)
       if not content:
           # Remove all sitemaps in ir.attachments as we're going to regenerated them
           dom = [('type', '=', 'binary'), '|', ('url', '=like', '/sitemap-%d-%%.xml' % current_website.id),
                  ('url', '=', '/sitemap-%d.xml' % current_website.id)]
           sitemaps = Attachment.search(dom)
           sitemaps.unlink()
           pages = 0
           locs = request.website.with_user(request.website.user_id)._enumerate_pages()
           while True:
               values = {
                   'locs': islice(locs, 0, LOC_PER_SITEMAP),
                   'url_root': request.httprequest.url_root[:-1],
               }
               urls = View._render_template('website.sitemap_locs', values)
               if urls.strip():
                   content = View._render_template('website.sitemap_xml', {'content': urls})
                   pages += 1
                   last_sitemap = create_sitemap('/sitemap-%d-%d.xml' % (current_website.id, pages), content)
               else:
                   break
           if not pages:
               return request.not_found()
           elif pages == 1:
               # rename the -id-page.xml => -id.xml
               last_sitemap.write({
                   'url': "/sitemap-%d.xml" % current_website.id,
                   'name': "/sitemap-%d.xml" % current_website.id,
               })
           else:
               # TODO: in master/saas-15, move current_website_id in template directly
               pages_with_website = ["%d-%d" % (current_website.id, p) for p in range(1, pages + 1)]
               # Sitemaps must be split in several smaller files with a sitemap index
               content = View._render_template('website.sitemap_index_xml', {
                   'pages': pages_with_website,
                   'url_root': request.httprequest.url_root,
               })
               create_sitemap('/sitemap-%d.xml' % current_website.id, content)
       return request.make_response(content, [('Content-Type', mimetype)])
    

    This native function is defined within the website module controller. This function is called each time a sitemap file is generated.

    locs = request.website.with_user(request.website.user_id)._enumerate_pages()

    This code will generate the site URL for the sitemap. Here it is defined using the _enumerate_pages function in the sample website.

    def _enumerate_pages(self, query_string=None, force=False):
       """ Available pages in the website/CMS. This is mostly used for links
           generation and can be overridden by modules setting up new HTML
           controllers for dynamic pages (e.g. blog).
           By default, returns template views marked as pages.
           :param str query_string: a (user-provided) string, fetches pages
                                    matching the string
           :returns: a list of mappings with two keys: ``name`` is the displayable
                     name of the resource (page), ``url`` is the absolute URL
                     of the same.
           :rtype: list({name: str, url: str})
       """
       router = self.env['ir.http'].routing_map()
       url_set = set()
       sitemap_endpoint_done = set()
       for rule in router.iter_rules():
           if 'sitemap' in rule.endpoint.routing and rule.endpoint.routing['sitemap'] is not True:
               if rule.endpoint in sitemap_endpoint_done:
                   continue
               sitemap_endpoint_done.add(rule.endpoint)
               func = rule.endpoint.routing['sitemap']
               if func is False:
                   continue
               for loc in func(self.env, rule, query_string):
                   yield loc
               continue
           if not self.rule_is_enumerable(rule):
               continue
           if 'sitemap' not in rule.endpoint.routing:
               logger.warning('No Sitemap value provided for controller %s (%s)' %
                              (rule.endpoint.method, ','.join(rule.endpoint.routing['routes'])))
           converters = rule._converters or {}
           if query_string and not converters and (query_string not in rule.build({}, append_unknown=False)[1]):
               continue
           values = [{}]
           # converters with a domain are processed after the other ones
           convitems = sorted(
               converters.items(),
               key=lambda x: (hasattr(x[1], 'domain') and (x[1].domain != '[]'), rule._trace.index((True, x[0]))))
           for (i, (name, converter)) in enumerate(convitems):
               if 'website_id' in self.env[converter.model]._fields and (not converter.domain or converter.domain == '[]'):
    converter.domain = "[('website_id', 'in', (False, current_website_id))]"
               newval = []
               for val in values:
                   query = i == len(convitems) - 1 and query_string
                   if query:
                       r = "".join([x[1] for x in rule._trace[1:] if not x[0]])  # remove model converter from route
                       query = sitemap_qs2dom(query, r, self.env[converter.model]._rec_name)
                       if query == FALSE_DOMAIN:
                           continue
                   for rec in converter.generate(self.env, args=val, dom=query):
                       newval.append(val.copy())
                       newval[-1].update({name: rec})
               values = newval
           for value in values:
               domain_part, url = rule.build(value, append_unknown=False)
               if not query_string or query_string.lower() in url.lower():
                   page = {'loc': url}
                   if url in url_set:
                       continue
                   url_set.add(url)
                   yield page
       # '/' already has a http.route & is in the routing_map so it will already have an entry in the xml
       domain = [('url', '!=', '/')]
       if not force:
           domain += [('website_indexed', '=', True), ('visibility', '=', False)]
           # is_visible
           domain += [
               ('website_published', '=', True), ('visibility', '=', False),
               '|', ('date_publish', '=', False), ('date_publish', '<=', fields.Datetime.now())
           ]
       if query_string:
           domain += [('url', 'like', query_string)]
       pages = self._get_website_pages(domain)
       for page in pages:
           record = {'loc': page['url'], 'id': page['id'], 'name': page['name']}
           if page.view_id and page.view_id.priority != 16:
               record['priority'] = min(round(page.view_id.priority / 32.0, 1), 1)
           if page['write_date']:
               record['lastmod'] = page['write_date'].date()
           yield record
    

    You can use this function to switch between pages of your website in your sitemap. This function can be used when you need to add a new page or a new URL to your sitemap. You can use this function.

    Adding a Records Page to Your Sitemap

    To add a Records page to your sitemap, you must first import the following method.

    Import slug from odoo.addons.http_routing.models.ir_http
    Import from odoo.addons.website.models.ir_http sitemap_qs2dom

    The slug is used to generate user-friendly URLs and created on the basis of sitemap_qs2dom is primarily used to create domains based on routes and query strings.

    Next, create a new method in main.py.

    class Main(http.Controller):
        def sitemap_records(env, rule, qs):
            records = env[your.model]
            dom = sitemap_qs2dom(qs, '/url', records._rec_name)
            for r in records.search(dom):
                loc = '/url/%s' % slug(r)
                if not qs or qs.lower() in loc:
                    yield {'loc': loc}
    

    where sitemap_records is a Python generator function. This function is called each time a sitemap is generated. This function generates a domain with sitemap_qs2dom. And then used that generated domain to search for records. The location is then generated by the slug() method. Get a user-friendly URL by using the slug method.

    Then you can add the sitemap_records function reference to the record detail root. in short,

    @http.route('/url/', type='http', auth="user", website=True, sitemap=sitemap_records)
        def records_detail(self, record):

    Here we passed a reference to the sitemap_records() function to the root using the sitemap keyword.

    This allows you to add record pages to sitemap.xml. If you don't need to filter records and just want to list all records in the sitemap, you can pass True instead of a function reference.

    Finally, update the module with your changes. The sitemap is updated every 12 hours. So if you want to see the changes, go to attachments and delete the existing sitemap.xml. Open /sitemap.xml in a browser to see the changes.

    本文章从网上收集,如有侵权请联系tderp@tderp.com删除
  • 微信扫一扫,一分也是爱:
  • 微信

服务原则及地区范围

宜兴通达网络科技有限公司,地处中国宜兴环科园内,是一家高新技术企业。公司在企业网络维护和企业信息化建设与咨询方面,有10多年经验。

我公司愿与客户一道,力求彻底解决客户问题!
我们不是在给企业提供“头痛医头、脚痛医脚”的暂时解决方案,而是在部署根本性安全与稳定服务!!
我们愿携手客户,建立企业IT规划;杜绝随意安装系统、软件等操作;力求共同维护有序、安全、稳定的网络办公环境!!!
IT服务,服务是根本,客户是上帝;我们提供快速响应、快速上门、快速排查,提供优质高效的服务!!!!

通达科技提供全国范围内的服务,服务形式包括远程协助、电话咨询、电子邮件咨询、传真咨询、问答平台的问题解决等。

宜兴地区提供上门服务:

  • 市区服务:宜城街道、城北街道(屺亭街道)、新街街道、新庄街道、环科园、渚桥开发区
  • 市郊服务:张渚镇、西渚镇、太华镇、徐舍镇、官林镇、杨巷镇、新建镇、和桥镇、高塍镇、万石镇、周铁镇、芳桥镇、丁蜀镇、湖父镇。
  • 联系电话:189-21-343434
  • 在线沟通: