Skip to content
This repository has been archived by the owner on Feb 9, 2018. It is now read-only.

Commit

Permalink
Implemented sitemap Fix #19
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Jan 9, 2018
1 parent 5b625b1 commit ea90a4d
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 40 deletions.
31 changes: 30 additions & 1 deletion quokka/core/content/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from flask import url_for
from flask import current_app as app
from quokka.utils.text import (
slugify, slugify_category, make_social_link, make_social_name
slugify, slugify_category, make_social_link,
make_social_name, make_external_url
)
from quokka.utils.dateformat import pretty_date
from quokka.utils.custom_vars import custom_var_dict
Expand All @@ -16,6 +17,9 @@

@functools.total_ordering
class Orderable:

is_content = False # to use in templates

def __eq__(self, other):
if isinstance(other, self.__class__):
return self.slug == other.slug
Expand Down Expand Up @@ -46,6 +50,10 @@ def _normalize_key(self, key):
def __html__(self):
return str(self)

@property
def external_url(self):
return make_external_url(self.url)


class Series(Orderable):
def __init__(self, name, index=1):
Expand Down Expand Up @@ -94,6 +102,20 @@ def __str__(self):
return self.category


class Fixed(Orderable):
"""Fixed pages like /authors/ and /tags/ and /categories/"""
def __init__(self, name):
self.name = name
self.slug = slugify_category(name)

@property
def url(self):
return self.slug

def __str__(self):
return self.name


class Author(Orderable):
def __init__(self, authors):
self.authors = authors
Expand Down Expand Up @@ -162,6 +184,9 @@ def __getitem__(self, item):


class Content:

is_content = True # to use in templates

def __init__(self, data):
self.data = data
self.format = get_format(data)
Expand All @@ -170,6 +195,10 @@ def __init__(self, data):
def url(self):
return url_for_content(self)

@property
def external_url(self):
return make_external_url(self.url)

@property
def locale_date(self):
if app.theme_context.get('SHOW_PRETTY_DATES') is True:
Expand Down
46 changes: 7 additions & 39 deletions quokka/core/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from pathlib import Path
from flask import current_app, redirect, request, send_from_directory, url_for
from .sitemap import SiteMapView


def template_files(filename):
Expand Down Expand Up @@ -41,51 +42,18 @@ def favicon():
# TODO: Dynamize
return redirect(url_for('static', filename='favicon.ico'), code=301)

# app.add_quokka_url_rule('/sitemap.xml',
# view_func=SiteMap.as_view('sitemap'))
app.add_quokka_url_rule('/sitemap.xml',
view_func=SiteMapView.as_view('sitemap'))

app.add_quokka_url_rule('/mediafiles/<path:filename>', view_func=media)

app.add_quokka_url_rule('/template_files/<path:filename>',
view_func=template_files)

app.add_quokka_url_rule(
'/theme_template_files/<identifier>/<path:filename>',
view_func=theme_template_files
)

for filepath in app.config.get('MAP_STATIC_ROOT', []):
app.add_quokka_url_rule(filepath, view_func=static_from_root)

# content_extension = app.config.get("CONTENT_EXTENSION", "html")
# app.add_quokka_url_rule('/<path:slug>.{0}'.format(content_extension),
# view_func=DetailView.as_view('detail'))

# Draft preview
# app.add_quokka_url_rule('/<path:long_slug>.preview',
# view_func=ContentDetailPreview.as_view('preview'))

# Atom Feed
# app.add_quokka_url_rule(
# '/<path:long_slug>.atom',
# view_func=FeedAtom.as_view('atom_list')
# )
# app.add_quokka_url_rule(
# '/tag/<tag>.atom', view_func=TagAtom.as_view('atom_tag')
# )

# RSS Feed
# app.add_quokka_url_rule(
# '/<path:long_slug>.xml', view_func=FeedRss.as_view('rss_list')
# )
# app.add_quokka_url_rule('/tag/<tag>.xml',
# view_func=TagRss.as_view('rss_tag'))

# Tag list
# app.add_quokka_url_rule('/tag/<tag>/', view_func=TagList.as_view('tag'))

# Match channels by its long_slug mpath
# app.add_quokka_url_rule('/<path:long_slug>/',
# view_func=ContentList.as_view('list'))
# Home page
# app.add_quokka_url_rule(
# '/',
# view_func=ContentList.as_view('home'),
# defaults={"long_slug": Channel.get_homepage('long_slug') or "home"}
# )
66 changes: 66 additions & 0 deletions quokka/core/views/sitemap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from flask import render_template
from flask import current_app as app
from flask.views import MethodView
from quokka.core.content.models import make_model


class SiteMapView(MethodView):

def get_contents(self):
"""
TODO: Should include extra paths, fixed paths
config based paths, static paths
"""
return (
self.get_index() +
self.get_categories() +
self.get_tags() +
self.get_authors() +
self.get_articles_and_pages()
)

def get_index(self):
return [
make_model(app.theme_context.get('INDEX_CATEGORY'),
content_type='category')
] + [
make_model(item, content_type='category')
for item in app.theme_context.get('LIST_CATEGORIES', [])
]

def get_articles_and_pages(self):
return [
make_model(item)
for item in app.db.content_set(
{'published': True,
'content_type': {'$in': ['article', 'page']}},
sort=app.theme_context.get('ARTICLE_ORDER_BY', [('date', -1)])
)
]

def get_categories(self):
return [
make_model('categories', content_type='fixed')
] + [
make_model(item, content_type='category')
for item in app.db.category_set()
]

def get_tags(self):
return [
make_model('tags', content_type='fixed')
] + [
make_model(item, content_type='tag')
for item in app.db.tag_set()
]

def get_authors(self):
return [
make_model('authors', content_type='fixed')
] + [
make_model(item, content_type='author')
for item in app.db.author_set()
]

def get(self):
return render_template('sitemap.xml', contents=self.get_contents())
16 changes: 16 additions & 0 deletions quokka/project_template/themes/malt/templates/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{%- macro render_item(item) -%}
<url>
<loc>{{item.external_url|safe}}</loc>
{% if item.is_content %}
<lastmod>{{item.modified.strftime('%Y-%m-%d')}}</lastmod>
{% endif %}
<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 contents -%}
{{ render_item(item) }}
{%- endfor -%}
</urlset>

0 comments on commit ea90a4d

Please sign in to comment.