Просмотр исходного кода

[minor] [fix] [website] sitemap and rss

version-14
Anand Doshi 12 лет назад
Родитель
Сommit
d3eebd5d0a
4 измененных файлов: 31 добавлений и 54 удалений
  1. +2
    -4
      public/html/rss.xml
  2. +2
    -2
      public/html/sitemap.xml
  3. +12
    -5
      webnotes/webutils.py
  4. +15
    -43
      website/sitemap.py

+ 2
- 4
public/html/rss.xml Просмотреть файл

@@ -17,7 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import cgi, cgitb, os, sys
import cgi, cgitb, sys
cgitb.enable()

# import libs
@@ -28,12 +28,10 @@ sys.path.append('../app')
import webnotes
import webnotes.auth

import conf

if __name__=='__main__':
# webnotes.http_request = webnotes.auth.HTTPRequest()
webnotes.connect()
from website.helpers import blog_feed
from website.doctype.blog_post import blog_feed
try:
print 'Content-Type: text/xml'
print


+ 2
- 2
public/html/sitemap.xml Просмотреть файл

@@ -31,7 +31,7 @@ if __name__=='__main__':
webnotes.http_request = webnotes.auth.HTTPRequest()
domain = os.environ.get('HTTP_HOST')
protocol = os.environ.get('HTTPS') and 'https://' or 'http://'
from website.helpers import sitemap
import website.sitemap
print 'Content-Type: text/xml'
print
print sitemap.generate(protocol + domain + '/')
print website.sitemap.generate(protocol + domain + '/')

+ 12
- 5
webnotes/webutils.py Просмотреть файл

@@ -58,17 +58,21 @@ def build_page(page_name):
if not webnotes.conn:
webnotes.connect()

sitemap = webnotes.cache().get_value("website_sitemap", build_sitemap)
sitemap = get_website_sitemap()
page_options = sitemap.get(page_name)
if not page_options:
if page_name=="index":
# page not found, try home page
page_options = sitemap.get(get_home_page())
home_page = get_home_page()
page_options = sitemap.get(home_page)
if not page_options:
raise PageNotFoundError
page_options["page_name"] = home_page
else:
raise PageNotFoundError
else:
page_options["page_name"] = page_name
basepath = webnotes.utils.get_base_path()
module = None
@@ -82,8 +86,8 @@ def build_page(page_name):
if page_options.get("is_generator"):
if not module:
raise Exception("Generator controller not defined")
name = webnotes.conn.get_value(module.doctype, {"page_name": page_name})
name = webnotes.conn.get_value(module.doctype, {"page_name": page_options["page_name"]})
obj = webnotes.get_obj(module.doctype, name, with_children=True)

if hasattr(obj, 'get_context'):
@@ -237,9 +241,12 @@ def clear_cache(page_name=None):
cache.delete_value("page:" + p)
cache.delete_value("website_sitemap")
cache.delete_value("website_sitemap_config")
def get_website_sitemap():
return webnotes.cache().get_value("website_sitemap", build_sitemap)

def get_all_pages():
return webnotes.cache().get_value("website_sitemap", build_sitemap).keys()
return get_website_sitemap().keys()

def delete_page_cache(page_name):
if page_name:


+ 15
- 43
website/sitemap.py Просмотреть файл

@@ -2,54 +2,26 @@
# MIT License. See license.txt

from __future__ import unicode_literals
import urllib
import webnotes
import webnotes.webutils
from webnotes.utils import nowdate

frame_xml = """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">%s
</urlset>"""

link_xml = """\n<url><loc>%s</loc><lastmod>%s</lastmod></url>"""

# generate the sitemap XML
def generate(domain):
global frame_xml, link_xml
import urllib, os
import webnotes
import webnotes.webutils
from webnotes.utils import nowdate
"""generate the sitemap XML"""

# settings
max_items = 1000
count = 0
frame_xml = """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">%s
</urlset>"""

link_xml = """\n<url><loc>%s</loc><lastmod>%s</lastmod></url>"""
site_map = ''
site_map = ""
if domain:
today = nowdate()
# generated pages
for doctype, opts in webnotes.webutils.get_generators().items():
pages = webnotes.conn.sql("""select page_name, `modified`
from `tab%s` where ifnull(%s,0)=1
order by modified desc""" % (doctype, opts.get("condition_field")))
for p in pages:
if count >= max_items: break
if p[0]:
page_url = os.path.join(domain, urllib.quote(p[0]))
modified = p[1].strftime('%Y-%m-%d')
site_map += link_xml % (page_url, modified)
count += 1
if count >= max_items: break
# standard pages
for page, opts in webnotes.get_config()["web"]["pages"].items():
if "no_cache" in opts:
continue
if count >= max_items: break
page_url = os.path.join(domain, urllib.quote(page))
modified = today
site_map += link_xml % (page_url, modified)
count += 1

for page_name, page_options in webnotes.webutils.get_website_sitemap().items():
url = urllib.basejoin(domain, urllib.quote(page_name))
site_map += link_xml % (url, today)
return frame_xml % site_map

Загрузка…
Отмена
Сохранить