Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 1 | # Copyright (c) 2012 Web Notes Technologies Pvt Ltd. |
| 2 | # License: GNU General Public License (v3). For more information see license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | frame_xml = """<?xml version="1.0" encoding="UTF-8"?> |
| 6 | <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">%s |
| 7 | </urlset>""" |
| 8 | |
| 9 | link_xml = """\n<url><loc>%s</loc><lastmod>%s</lastmod></url>""" |
| 10 | |
| 11 | # generate the sitemap XML |
| 12 | def generate(domain): |
| 13 | global frame_xml, link_xml |
| 14 | import urllib, os |
| 15 | import webnotes |
Rushabh Mehta | 9db1a68 | 2013-04-02 10:41:37 +0530 | [diff] [blame] | 16 | import webnotes.webutils |
Anand Doshi | 09480e2 | 2013-04-16 20:57:09 +0530 | [diff] [blame] | 17 | from webnotes.utils import nowdate |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 18 | |
| 19 | # settings |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 20 | max_items = 1000 |
Anand Doshi | 09480e2 | 2013-04-16 20:57:09 +0530 | [diff] [blame] | 21 | count = 0 |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 22 | |
| 23 | site_map = '' |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 24 | if domain: |
Anand Doshi | 09480e2 | 2013-04-16 20:57:09 +0530 | [diff] [blame] | 25 | today = nowdate() |
| 26 | |
| 27 | # generated pages |
| 28 | for doctype, opts in webnotes.webutils.get_generators().items(): |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 29 | pages = webnotes.conn.sql("""select page_name, `modified` |
| 30 | from `tab%s` where ifnull(%s,0)=1 |
Anand Doshi | 09480e2 | 2013-04-16 20:57:09 +0530 | [diff] [blame] | 31 | order by modified desc""" % (doctype, opts.get("condition_field"))) |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 32 | |
| 33 | for p in pages: |
Anand Doshi | 09480e2 | 2013-04-16 20:57:09 +0530 | [diff] [blame] | 34 | if count >= max_items: break |
Rushabh Mehta | 5f18398 | 2013-01-01 10:55:58 +0530 | [diff] [blame] | 35 | page_url = os.path.join(domain, urllib.quote(p[0])) |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 36 | modified = p[1].strftime('%Y-%m-%d') |
| 37 | site_map += link_xml % (page_url, modified) |
Anand Doshi | 09480e2 | 2013-04-16 20:57:09 +0530 | [diff] [blame] | 38 | count += 1 |
| 39 | |
| 40 | if count >= max_items: break |
| 41 | |
| 42 | # standard pages |
| 43 | for page, opts in webnotes.get_config()["web"]["pages"].items(): |
| 44 | if "no_cache" in opts: |
| 45 | continue |
| 46 | |
| 47 | if count >= max_items: break |
| 48 | page_url = os.path.join(domain, urllib.quote(page)) |
| 49 | modified = today |
| 50 | site_map += link_xml % (page_url, modified) |
| 51 | count += 1 |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 52 | |
Anand Doshi | 09480e2 | 2013-04-16 20:57:09 +0530 | [diff] [blame] | 53 | return frame_xml % site_map |