1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
from functools import wraps
from django.contrib.syndication import views
from django.utils import feedgenerator
from django.utils.timezone import get_fixed_timezone
from .models import Article, Entry
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
class TestRss2Feed(views.Feed):
title = "My blog"
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
def items(self):
return Entry.objects.all()
def item_description(self, item):
return "Overridden description: %s" % item
def item_pubdate(self, item):
return item.published
def item_updateddate(self, item):
return item.updated
def item_comments(self, item):
return "%scomments" % item.get_absolute_url()
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"
class TestRss2FeedWithCallableObject(TestRss2Feed):
class TimeToLive:
def __call__(self):
return 700
ttl = TimeToLive()
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()
@staticmethod
def categories():
return ("javascript", "vue")
@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}"
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
class TestRss091Feed(TestRss2Feed):
feed_type = feedgenerator.RssUserland091Feed
class TestNoPubdateFeed(views.Feed):
title = "Test feed"
link = "/feed/"
def items(self):
return Entry.objects.all()
class TestAtomFeed(TestRss2Feed):
feed_type = feedgenerator.Atom1Feed
subtitle = TestRss2Feed.description
class TestLatestFeed(TestRss2Feed):
"""
A feed where the latest entry date is an `updated` element.
"""
feed_type = feedgenerator.Atom1Feed
subtitle = TestRss2Feed.description
def items(self):
return Entry.objects.exclude(title="My last entry")
class ArticlesFeed(TestRss2Feed):
"""
A feed to test no link being defined. Articles have no get_absolute_url()
method, and item_link() is not defined.
"""
def items(self):
return Article.objects.all()
class TestSingleEnclosureRSSFeed(TestRss2Feed):
"""
A feed to test that RSS feeds work with a single enclosure.
"""
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.
"""
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"),
]
class TemplateFeed(TestRss2Feed):
"""
A feed to test defining item titles and descriptions with templates.
"""
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"
class TemplateContextFeed(TestRss2Feed):
"""
A feed to test custom context data in templates for title or description.
"""
title_template = "syndication/title_context.html"
description_template = "syndication/description_context.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["foo"] = "bar"
return context
class TestLanguageFeed(TestRss2Feed):
language = "de"
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
class TestFeedWithStylesheets(TestRss2Feed):
stylesheets = [
"/stylesheet1.xsl",
feedgenerator.Stylesheet("/stylesheet2.xsl"),
]
class NaiveDatesFeed(TestAtomFeed):
"""
A feed with naive (non-timezone-aware) dates.
"""
def item_pubdate(self, item):
return item.published
class TZAwareDatesFeed(TestAtomFeed):
"""
A feed with timezone-aware dates.
"""
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.
return item.published.replace(tzinfo=get_fixed_timezone(42))
class TestFeedUrlFeed(TestAtomFeed):
feed_url = "http://example.com/customfeedurl/"
class MyCustomAtom1Feed(feedgenerator.Atom1Feed):
"""
Test of a custom feed generator class.
"""
def root_attributes(self):
attrs = super().root_attributes()
attrs["django"] = "rocks"
return attrs
def add_root_elements(self, handler):
super().add_root_elements(handler)
handler.addQuickElement("spam", "eggs")
def item_attributes(self, item):
attrs = super().item_attributes(item)
attrs["bacon"] = "yum"
return attrs
def add_item_elements(self, handler, item):
super().add_item_elements(handler, item)
handler.addQuickElement("ministry", "silly walks")
class TestCustomFeed(TestAtomFeed):
feed_type = MyCustomAtom1Feed
class TestSingleEnclosureAtomFeed(TestAtomFeed):
"""
A feed to test that Atom feeds work with a single enclosure.
"""
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.
"""
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"),
]
|