Code

Opened 2 years ago

Closed 11 months ago

#18112 closed New feature (fixed)

contrib.syndication.views: Add hook for getting extra context in feed templates

Reported by: slinkp Owned by: zsiciarz
Component: contrib.syndication Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

This seems to me like a useful low-impact hook for authors of Feed subclasses...
I was writing one today and wished I had such a hook.

It would be intentionally similar to the get_context_data hook on class-based views.

Usage would look like:

class MyFeed(Feed):
    def get_context_data(**kwargs):
        context = super(MyFeed, self).get_context_data(**kwargs)
        context['foo'] = settings.FOO
        context['more_things'] = app.models.Things.objects.filter(...)
        return context

The description_template and title_template could then make use of the extra context variables.

Attaching a patch; will write tests if this seems worthwhile.

Attachments (1)

feedviews.diff (1.8 KB) - added by slinkp 2 years ago.

Download all attachments as: .zip

Change History (8)

Changed 2 years ago by slinkp

comment:1 Changed 2 years ago by slinkp

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Type changed from Uncategorized to New feature

comment:2 Changed 22 months ago by aaugustin

  • Easy pickings set
  • Needs documentation set
  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted

I don't really understand the obj vs. item dance; could you clarify why you chose this implementation?

Otherwise, this looks like a good idea.

comment:3 Changed 22 months ago by slinkp

I can see how this is confusing.
Those names were not chosen by me, they are in the current implementation, as the old part of the diff shows.

If you look in the local scope of where I'm invoking this method, the old implementation
has these :

obj - the object returned by get_object().

This was not being made available to the templates; I added it as the 'obj' kwarg
to get_context_data(), which by default ignores it; but I'm passing it so at least it's possible to use it
in your subclass if you want.

item - the current feed item from this iteration of obj.items().

This was already being made available to the template context as, very unfortunately, 'obj'.
That kind of sucks, as it's easy to confuse with the one returned by get_object().
But it *has* to be named 'obj' in the template context or we'll break existing templates.

Any thoughts on how to make this less confusing without breaking backward compatibility?

comment:4 Changed 22 months ago by aaugustin

This makes sense. Let's explain it in the documentation.

The patch just needs tests and docs before it can be committed.

comment:5 Changed 14 months ago by zsiciarz

  • Owner changed from nobody to zsiciarz
  • Status changed from new to assigned

comment:6 Changed 14 months ago by zsiciarz

  • Needs documentation unset
  • Needs tests unset

I added a test case and a few words of documentation to the initial patch. See https://github.com/django/django/pull/826

comment:7 Changed 11 months ago by zsiciarz

  • Resolution set to fixed
  • Status changed from assigned to closed

Hmm, I guess trac did not pick up closing commit from GitHub.

In 0a8402eb052a5c35085baa5408aaf4ee36ebc0a6:
Fixed #18112: Test case and docs for custom context data in feeds; Thanks Paul Winkler for the initial patch.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.