Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
Added sitemap by template, fix get_http_url for channel
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Dec 17, 2015
1 parent 9bd7557 commit e82340b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
10 changes: 5 additions & 5 deletions quokka/core/models/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
# -*- coding: utf-8 -*-

import logging
from flask import request
from flask.ext.mistune import markdown

from quokka.core.db import db
from quokka.core.models.custom_values import HasCustomValue
from quokka.core.models.signature import (
Tagged, Publishable, LongSlugged, ContentFormat, TemplateType
)
from quokka.core.models.config import Config
from quokka.core.admin.utils import _l

from quokka.utils.settings import get_site_url
logger = logging.getLogger()


Expand Down Expand Up @@ -161,8 +159,10 @@ def get_canonical_url(self, *args, **kwargs):
return self.get_absolute_url()

def get_http_url(self):
site_url = Config.get('site', 'site_domain', request.url_root)
return u"{0}{1}".format(site_url, self.get_absolute_url())
site_url = get_site_url()
absolute_url = self.get_absolute_url()
absolute_url = absolute_url[1:]
return u"{0}{1}".format(site_url, absolute_url)

def clean(self):
homepage = Channel.objects(is_homepage=True)
Expand Down
46 changes: 19 additions & 27 deletions quokka/core/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# coding: utf-8

import sys
import logging
import collections
import hashlib
import collections
import PyRSS2Gen as pyrss
import sys

from datetime import datetime, timedelta
from flask import request, redirect, url_for, abort, current_app
Expand All @@ -19,10 +19,8 @@
# python3 support
if sys.version_info.major == 3:
from urllib.parse import urljoin
# from io import StringIO
else:
from urlparse import urljoin
# import StringIO

logger = logging.getLogger()

Expand Down Expand Up @@ -568,31 +566,25 @@ def get_contents(self):
'published': True,
'available_at__lte': now,
}

contents = Content.objects().filter(**filters)
return contents

def sitemap_render(self, contents):
tmpl = """
<?xml version="1.0" encoding="UTF-8"?><!-- generator="quokka" -->
<!-- generated-on="12 de February de 2013 1:41 PM" -->
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
"""
for content in contents:
tmpl += """
<url>
<loc>{0}</loc>
<lastmod>{1}</lastmod>
<changefreq>daily</changefreq>
<priority>0.2</priority>
</url>
""".format(
self.make_external_url(content.get_absolute_url()),
content.created_at
)
tmpl += "</urlset>"
return tmpl
def get_channels(self):
now = datetime.now()
filters = {
'published': True,
'available_at__lte': now,
}
channels = Channel.objects().filter(**filters)
return channels

def get(self):
contents = self.get_contents()
return self.sitemap_render(contents)
"""
Fixme: Should include extra paths, fixed paths
config based paths, static paths
"""
return render_template(
'sitemap.xml',
contents=self.get_contents(),
channels=self.get_channels()
)
3 changes: 1 addition & 2 deletions quokka/ext/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# coding: utf-8
from flask.ext.mail import Mail

from flask_mail import Mail
from quokka.core.db import db
from quokka.core.cache import cache
from quokka.core.admin import configure_admin
Expand Down
18 changes: 18 additions & 0 deletions quokka/themes/pure/templates/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% macro render_item(item) %}
<url>
<loc>{{item.get_http_url()|safe}}</loc>
<lastmod>{{item.available_at.strftime("%Y-%m-%d %H:%M:%S")}}</lastmod>
<changefreq>daily</changefreq>
<priority>0.2</priority>
</url>
{% endmacro %}

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for item in channels %}
{{ render_item(item) }}
{% endfor %}
{% for item in contents %}
{{ render_item(item) }}
{% endfor %}
</urlset>

1 comment on commit e82340b

@avelino
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Please sign in to comment.