Search code examples
djangodjango-syndication

Django syndication framework: prevent appending SITE_ID to the links


According to the documentation here: https://djangobook.com/syndication-feed-framework/

If link doesn’t return the domain, the syndication framework will insert the domain of the current site, according to your SITE_ID setting

However, I'm trying to generate a feed of magnet: links. The framework doesn't recognize this and attempts to append the SITE_ID, such that the links end up like this (on localhost):

<link>http://localhost:8000magnet:?xt=...</link>

Is there a way to bypass this?


Solution

  • I did end up digging through the syndication source code and finding no easy way to override it and did some hacky monkey patching. (Unfortunately I did it before I saw the answers posted here, all of which I assume will work about as well as this one)

    Here's how I did it:

    def item_link(self, item):
        # adding http:// means the internal get_feed won't modify it
        return "http://"+item.magnet_link
    
    def get_feed(self, obj, request):
        # hacky way to bypass the domain handling
        feed = super().get_feed(obj, request)
        for item in feed.items:
            # strip that http:// we added above
            item['link'] = item['link'][7:]
        return feed
    

    For future readers, this was as of Django 2.0.1. Hopefully in a future patch they allow support for protocols like magnet.