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