2022-05-02 11:20:00 +08:00
|
|
|
from functools import wraps
|
|
|
|
|
2011-04-02 16:44:47 +08:00
|
|
|
from django.contrib.syndication import views
|
2013-09-08 16:43:33 +08:00
|
|
|
from django.utils import feedgenerator
|
|
|
|
from django.utils.timezone import get_fixed_timezone
|
2011-10-14 05:34:56 +08:00
|
|
|
|
|
|
|
from .models import Article, Entry
|
2008-08-12 04:49:19 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2022-05-02 11:20:00 +08:00
|
|
|
def wraps_decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
value = f(*args, **kwargs)
|
|
|
|
return f"{value} -- decorated by @wraps."
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def common_decorator(f):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
value = f(*args, **kwargs)
|
|
|
|
return f"{value} -- common decorated."
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
class TestRss2Feed(views.Feed):
|
2008-08-12 04:49:19 +08:00
|
|
|
title = "My blog"
|
2010-01-28 21:46:18 +08:00
|
|
|
description = "A more thorough description of my blog."
|
|
|
|
link = "/blog/"
|
|
|
|
feed_guid = "/foo/bar/1234"
|
|
|
|
author_name = "Sally Smith"
|
|
|
|
author_email = "test@example.com"
|
|
|
|
author_link = "http://www.example.com/"
|
|
|
|
categories = ("python", "django")
|
|
|
|
feed_copyright = "Copyright (c) 2007, Sally Smith"
|
|
|
|
ttl = 600
|
|
|
|
|
2008-08-12 04:49:19 +08:00
|
|
|
def items(self):
|
|
|
|
return Entry.objects.all()
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def item_description(self, item):
|
|
|
|
return "Overridden description: %s" % item
|
|
|
|
|
|
|
|
def item_pubdate(self, item):
|
2013-07-18 03:20:20 +08:00
|
|
|
return item.published
|
|
|
|
|
|
|
|
def item_updateddate(self, item):
|
|
|
|
return item.updated
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2020-07-23 22:42:25 +08:00
|
|
|
def item_comments(self, item):
|
|
|
|
return "%scomments" % item.get_absolute_url()
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
item_author_name = "Sally Smith"
|
|
|
|
item_author_email = "test@example.com"
|
|
|
|
item_author_link = "http://www.example.com/"
|
|
|
|
item_categories = ("python", "testing")
|
|
|
|
item_copyright = "Copyright (c) 2007, Sally Smith"
|
|
|
|
|
|
|
|
|
2022-05-23 05:15:11 +08:00
|
|
|
class TestRss2FeedWithCallableObject(TestRss2Feed):
|
|
|
|
class TimeToLive:
|
|
|
|
def __call__(self):
|
|
|
|
return 700
|
|
|
|
|
|
|
|
ttl = TimeToLive()
|
|
|
|
|
|
|
|
|
2022-05-02 11:20:00 +08:00
|
|
|
class TestRss2FeedWithDecoratedMethod(TestRss2Feed):
|
|
|
|
class TimeToLive:
|
|
|
|
@wraps_decorator
|
|
|
|
def __call__(self):
|
|
|
|
return 800
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@wraps_decorator
|
|
|
|
def feed_copyright():
|
|
|
|
return "Copyright (c) 2022, John Doe"
|
|
|
|
|
|
|
|
ttl = TimeToLive()
|
|
|
|
|
2022-05-26 06:45:46 +08:00
|
|
|
@staticmethod
|
|
|
|
def categories():
|
|
|
|
return ("javascript", "vue")
|
|
|
|
|
2022-05-02 11:20:00 +08:00
|
|
|
@wraps_decorator
|
|
|
|
def title(self):
|
|
|
|
return "Overridden title"
|
|
|
|
|
|
|
|
@wraps_decorator
|
|
|
|
def item_title(self, item):
|
|
|
|
return f"Overridden item title: {item.title}"
|
|
|
|
|
|
|
|
@wraps_decorator
|
|
|
|
def description(self, obj):
|
|
|
|
return "Overridden description"
|
|
|
|
|
|
|
|
@wraps_decorator
|
|
|
|
def item_description(self):
|
|
|
|
return "Overridden item description"
|
|
|
|
|
|
|
|
|
|
|
|
class TestRss2FeedWithWrongDecoratedMethod(TestRss2Feed):
|
|
|
|
@common_decorator
|
|
|
|
def item_description(self, item):
|
|
|
|
return f"Overridden item description: {item.title}"
|
|
|
|
|
2022-05-26 06:45:46 +08:00
|
|
|
|
2013-02-06 18:25:35 +08:00
|
|
|
class TestRss2FeedWithGuidIsPermaLinkTrue(TestRss2Feed):
|
|
|
|
def item_guid_is_permalink(self, item):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class TestRss2FeedWithGuidIsPermaLinkFalse(TestRss2Feed):
|
|
|
|
def item_guid(self, item):
|
|
|
|
return str(item.pk)
|
|
|
|
|
|
|
|
def item_guid_is_permalink(self, item):
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
class TestRss091Feed(TestRss2Feed):
|
|
|
|
feed_type = feedgenerator.RssUserland091Feed
|
|
|
|
|
|
|
|
|
2012-10-01 02:44:27 +08:00
|
|
|
class TestNoPubdateFeed(views.Feed):
|
|
|
|
title = "Test feed"
|
|
|
|
link = "/feed/"
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return Entry.objects.all()
|
|
|
|
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
class TestAtomFeed(TestRss2Feed):
|
|
|
|
feed_type = feedgenerator.Atom1Feed
|
|
|
|
subtitle = TestRss2Feed.description
|
|
|
|
|
|
|
|
|
2013-07-18 03:20:20 +08:00
|
|
|
class TestLatestFeed(TestRss2Feed):
|
|
|
|
"""
|
|
|
|
A feed where the latest entry date is an `updated` element.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2013-07-18 03:20:20 +08:00
|
|
|
feed_type = feedgenerator.Atom1Feed
|
|
|
|
subtitle = TestRss2Feed.description
|
|
|
|
|
|
|
|
def items(self):
|
2020-10-21 11:33:23 +08:00
|
|
|
return Entry.objects.exclude(title="My last entry")
|
2013-07-18 03:20:20 +08:00
|
|
|
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
class ArticlesFeed(TestRss2Feed):
|
|
|
|
"""
|
|
|
|
A feed to test no link being defined. Articles have no get_absolute_url()
|
|
|
|
method, and item_link() is not defined.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def items(self):
|
|
|
|
return Article.objects.all()
|
|
|
|
|
|
|
|
|
2015-08-21 17:50:43 +08:00
|
|
|
class TestSingleEnclosureRSSFeed(TestRss2Feed):
|
|
|
|
"""
|
|
|
|
A feed to test that RSS feeds work with a single enclosure.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-08-21 17:50:43 +08:00
|
|
|
def item_enclosure_url(self, item):
|
|
|
|
return "http://example.com"
|
|
|
|
|
|
|
|
def item_enclosure_size(self, item):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def item_mime_type(self, item):
|
|
|
|
return "image/png"
|
|
|
|
|
|
|
|
|
|
|
|
class TestMultipleEnclosureRSSFeed(TestRss2Feed):
|
|
|
|
"""
|
|
|
|
A feed to test that RSS feeds raise an exception with multiple enclosures.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-08-21 17:50:43 +08:00
|
|
|
def item_enclosures(self, item):
|
|
|
|
return [
|
|
|
|
feedgenerator.Enclosure("http://example.com/hello.png", 0, "image/png"),
|
|
|
|
feedgenerator.Enclosure("http://example.com/goodbye.png", 0, "image/png"),
|
|
|
|
]
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TemplateFeed(TestRss2Feed):
|
|
|
|
"""
|
|
|
|
A feed to test defining item titles and descriptions with templates.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
title_template = "syndication/title.html"
|
|
|
|
description_template = "syndication/description.html"
|
|
|
|
|
|
|
|
# Defining a template overrides any item_title definition
|
|
|
|
def item_title(self):
|
|
|
|
return "Not in a template"
|
|
|
|
|
|
|
|
|
2013-02-24 22:00:34 +08:00
|
|
|
class TemplateContextFeed(TestRss2Feed):
|
|
|
|
"""
|
|
|
|
A feed to test custom context data in templates for title or description.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2013-02-24 22:00:34 +08:00
|
|
|
title_template = "syndication/title_context.html"
|
|
|
|
description_template = "syndication/description_context.html"
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
context = super().get_context_data(**kwargs)
|
2013-02-24 22:00:34 +08:00
|
|
|
context["foo"] = "bar"
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2019-04-11 05:48:58 +08:00
|
|
|
class TestLanguageFeed(TestRss2Feed):
|
|
|
|
language = "de"
|
|
|
|
|
|
|
|
|
2020-10-13 09:53:49 +08:00
|
|
|
class TestGetObjectFeed(TestRss2Feed):
|
|
|
|
def get_object(self, request, entry_id):
|
|
|
|
return Entry.objects.get(pk=entry_id)
|
|
|
|
|
|
|
|
def items(self, obj):
|
|
|
|
return Article.objects.filter(entry=obj)
|
|
|
|
|
|
|
|
def item_link(self, item):
|
|
|
|
return "%sarticle/%s/" % (item.entry.get_absolute_url(), item.pk)
|
|
|
|
|
|
|
|
def item_comments(self, item):
|
|
|
|
return "%scomments" % self.item_link(item)
|
|
|
|
|
|
|
|
def item_description(self, item):
|
|
|
|
return "Article description: %s" % item.title
|
|
|
|
|
|
|
|
def item_title(self, item):
|
|
|
|
return "Title: %s" % item.title
|
|
|
|
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
class NaiveDatesFeed(TestAtomFeed):
|
|
|
|
"""
|
|
|
|
A feed with naive (non-timezone-aware) dates.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def item_pubdate(self, item):
|
2013-07-18 03:20:20 +08:00
|
|
|
return item.published
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TZAwareDatesFeed(TestAtomFeed):
|
|
|
|
"""
|
|
|
|
A feed with timezone-aware dates.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def item_pubdate(self, item):
|
|
|
|
# Provide a weird offset so that the test can know it's getting this
|
|
|
|
# specific offset and not accidentally getting on from
|
|
|
|
# settings.TIME_ZONE.
|
2013-09-08 16:43:33 +08:00
|
|
|
return item.published.replace(tzinfo=get_fixed_timezone(42))
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestFeedUrlFeed(TestAtomFeed):
|
|
|
|
feed_url = "http://example.com/customfeedurl/"
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
class MyCustomAtom1Feed(feedgenerator.Atom1Feed):
|
2008-08-12 06:22:26 +08:00
|
|
|
"""
|
|
|
|
Test of a custom feed generator class.
|
2010-01-28 21:46:18 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def root_attributes(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
attrs = super().root_attributes()
|
2012-06-08 00:08:47 +08:00
|
|
|
attrs["django"] = "rocks"
|
2008-08-12 06:22:26 +08:00
|
|
|
return attrs
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_root_elements(self, handler):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().add_root_elements(handler)
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("spam", "eggs")
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def item_attributes(self, item):
|
2017-01-21 21:13:44 +08:00
|
|
|
attrs = super().item_attributes(item)
|
2012-06-08 00:08:47 +08:00
|
|
|
attrs["bacon"] = "yum"
|
2008-08-12 06:22:26 +08:00
|
|
|
return attrs
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_item_elements(self, handler, item):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().add_item_elements(handler, item)
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("ministry", "silly walks")
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
class TestCustomFeed(TestAtomFeed):
|
|
|
|
feed_type = MyCustomAtom1Feed
|
2015-08-21 17:50:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestSingleEnclosureAtomFeed(TestAtomFeed):
|
|
|
|
"""
|
|
|
|
A feed to test that Atom feeds work with a single enclosure.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-08-21 17:50:43 +08:00
|
|
|
def item_enclosure_url(self, item):
|
|
|
|
return "http://example.com"
|
|
|
|
|
|
|
|
def item_enclosure_size(self, item):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def item_mime_type(self, item):
|
|
|
|
return "image/png"
|
|
|
|
|
|
|
|
|
|
|
|
class TestMultipleEnclosureAtomFeed(TestAtomFeed):
|
|
|
|
"""
|
|
|
|
A feed to test that Atom feeds work with multiple enclosures.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-08-21 17:50:43 +08:00
|
|
|
def item_enclosures(self, item):
|
|
|
|
return [
|
2015-09-19 13:59:18 +08:00
|
|
|
feedgenerator.Enclosure("http://example.com/hello.png", "0", "image/png"),
|
|
|
|
feedgenerator.Enclosure("http://example.com/goodbye.png", "0", "image/png"),
|
2015-08-21 17:50:43 +08:00
|
|
|
]
|