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

feinheit/django-news-sitemaps

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django News Sitemaps

This is an implementation of sitemaps with news specific tags as defined by Google's News Sitemaps specification. It is just regular Django sitemaps with a couple other tags added in specifically for news publications.

Install

For starters, just place news_sitemaps into your INSTALLED_APPS and then you can load the default urlconf like so:

urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
    ...
    (r'^news-sitemaps/', include('news_sitemaps.urls')),
)

This will create a /news-sitemaps/index.xml which is a sitemap index of all available sitemaps which will appear by section at urls like /news-sitemaps/<section>.xml (more on this later)

Settings

Three new settings are defined to control content of your news sitemaps

PUBLICATION_NAME

The proper name of the news publication. It must exactly match the name as it appears on your articles in news.google.com, omitting any trailing parentheticals. By default it is "The Example Times" so please set this.

PUBLICATION_LANGUAGE

The language code of your publication. It should be an ISO 639 Language Code (either 2 or 3 letters). Defaults to Django's LANGUAGE_CODE setting.

PUBLICATION_TIME_ZONE

The timezone suffix for your news publication. Defaults to "-5:00" (America/Eastern)

Adding Sitemaps

To add your own news sitemaps you must register them with the app first. Here is a quick and dirty example of creating a news sitemap for Django's Comments:

from news_sitemaps import register, NewsSitemap
from django.contrib.comments.models import Comment

class CommentSitemap(NewsSitemap):
    limit = 5000
    def items(self):
        return Comment.objects.filter(is_public=True,is_removed=False)

    def lastmod(self, obj):
        return obj.submit_date

    def genres(self, obj):
        return 'UserGenerated, Opinion'

register(comments=CommentSitemap)

Then the comments sitemap will appear on your index sitemap and you will be able to access the actual sitemap at /news-sitemaps/comments.xml.

Notice the genres method. There are a few new methods in addition to the normal Sitemap methods which are news specific. They are: title, access, keywords, stock_tickers, and genres. Please refer to Google's News Sitemaps specification for more info on how to use them correctly.

Note

Make sure you add the lastmod method or Google will not accept the sitemap.

Multi-language Sitemaps

Use translation.override in your Sitemap methods:

def items(self):
    two_days_old = timezone.now() - timedelta(days=2)
    return Entry.objects.filter(date__gte=two_days_old).exclude(
                        **{'slug_%s__isnull' % self.language_code: True})

def title(self, obj):
    with translation.override(self.language_code):
        return obj.title

def location(self, obj):
    with translation.override(self.language_code):
        return obj.get_absolute_url()

def lastmod(self, obj):
    return obj.updated

Create a Sitemap for each language:

for language in settings.LANGUAGES:
    register(**{'news_%s' % language[0]: MyNewsSitemap(language[0])})

About

Generates sitemaps compatible with the Google News schema

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%