|
1 | 1 | from django.contrib.syndication.views import Feed
|
| 2 | +from django.utils.feedgenerator import Rss201rev2Feed |
2 | 3 | from django.urls import reverse
|
3 | 4 | from django.db.models import Q
|
| 5 | +from django.conf import settings |
| 6 | +from django.contrib.postgres.search import SearchQuery |
4 | 7 |
|
5 |
| -from .models import Media |
| 8 | +from .models import Media, Category |
| 9 | +from . import helpers |
| 10 | +from .stop_words import STOP_WORDS |
6 | 11 |
|
7 | 12 |
|
8 |
| -class RssMediaFeed(Feed): |
| 13 | +class MediaRSSFeed(Rss201rev2Feed): |
| 14 | + def rss_attributes(self): |
| 15 | + attrs = super(MediaRSSFeed, self).rss_attributes() |
| 16 | + attrs["xmlns:media"] = "http://search.yahoo.com/mrss/" |
| 17 | + attrs["xmlns:atom"] = "http://www.w3.org/2005/Atom" |
| 18 | + return attrs |
| 19 | + |
| 20 | + def add_item_elements(self, handler, item): |
| 21 | + """Callback to add elements to each item (item/entry) element.""" |
| 22 | + super(MediaRSSFeed, self).add_item_elements(handler, item) |
| 23 | + |
| 24 | + if "media:title" in item: |
| 25 | + handler.addQuickElement("media:title", item["title"]) |
| 26 | + if "media:description" in item: |
| 27 | + handler.addQuickElement("media:description", item["description"]) |
| 28 | + |
| 29 | + if "content_url" in item: |
| 30 | + content = dict(url=item["content_url"]) |
| 31 | + if "content_width" in item: |
| 32 | + content["width"] = str(item["content_width"]) |
| 33 | + if "content_height" in item: |
| 34 | + content["height"] = str(item["content_height"]) |
| 35 | + handler.addQuickElement("media:content", "", content) |
| 36 | + |
| 37 | + if "thumbnail_url" in item: |
| 38 | + thumbnail = dict(url=item["thumbnail_url"]) |
| 39 | + if "thumbnail_width" in item: |
| 40 | + thumbnail["width"] = str(item["thumbnail_width"]) |
| 41 | + if "thumbnail_height" in item: |
| 42 | + thumbnail["height"] = str(item["thumbnail_height"]) |
| 43 | + handler.addQuickElement("media:thumbnail", "", thumbnail) |
| 44 | + |
| 45 | + if "keywords" in item: |
| 46 | + handler.addQuickElement("media:keywords", item["keywords"]) |
| 47 | + |
| 48 | + def add_root_elements(self, handler): |
| 49 | + super().add_root_elements(handler) |
| 50 | + if self.feed["author_name"] is not None: |
| 51 | + handler.startElement("author", {}) |
| 52 | + handler.addQuickElement("name", self.feed["author_name"]) |
| 53 | + handler.endElement("author") |
| 54 | + if self.feed.get("published") is not None: |
| 55 | + handler.startElement("published", {}) |
| 56 | + handler.addQuickElement("name", self.feed["published"]) |
| 57 | + handler.endElement("published") |
| 58 | + |
| 59 | + |
| 60 | +class IndexRSSFeed(Feed): |
| 61 | + feed_type = MediaRSSFeed |
9 | 62 | title = "Latest Media"
|
10 |
| - link = "/media" |
| 63 | + link = "/rss" |
11 | 64 | description = "Latest Media RSS feed"
|
12 | 65 |
|
13 | 66 | def items(self):
|
14 |
| - basic_query = Q(listable=True) |
15 |
| - media = Media.objects.filter(basic_query).order_by("-add_date") |
| 67 | + media = Media.objects.filter(listable=True).order_by("-add_date") |
16 | 68 | media = media.prefetch_related("user")
|
17 |
| - return media[:40] |
| 69 | + return media[:20] |
18 | 70 |
|
19 | 71 | def item_title(self, item):
|
20 | 72 | return item.title
|
21 | 73 |
|
22 | 74 | def item_description(self, item):
|
23 | 75 | return item.description
|
24 | 76 |
|
| 77 | + def item_author_name(self, item): |
| 78 | + return item.user.username |
| 79 | + |
| 80 | + def item_pubdate(self, item): |
| 81 | + return item.add_date |
| 82 | + |
| 83 | + def item_updateddate(self, item): |
| 84 | + return item.edit_date |
| 85 | + |
25 | 86 | def item_link(self, item):
|
26 | 87 | return reverse("get_media") + "?m={0}".format(item.friendly_token)
|
| 88 | + |
| 89 | + def item_extra_kwargs(self, item): |
| 90 | + item = { |
| 91 | + "media:title": item.title, |
| 92 | + "media:description": item.description, |
| 93 | + "content_width": 720, |
| 94 | + "thumbnail_url": f"{settings.SSL_FRONTEND_HOST}/{item.poster_url}", |
| 95 | + "content_url": f"{settings.SSL_FRONTEND_HOST}/{item.get_absolute_url()}", |
| 96 | + "thumbnail_width": 720, |
| 97 | + } |
| 98 | + return item |
| 99 | + |
| 100 | + |
| 101 | +class SearchRSSFeed(Feed): |
| 102 | + feed_type = MediaRSSFeed |
| 103 | + description = "Latest Media RSS feed" |
| 104 | + |
| 105 | + def link(self, obj): |
| 106 | + return f"/rss/search" |
| 107 | + |
| 108 | + def get_object(self, request): |
| 109 | + category = request.GET.get("c", "") |
| 110 | + tag = request.GET.get("t", "") |
| 111 | + query = request.GET.get("q", "") |
| 112 | + |
| 113 | + media = Media.objects.filter(listable=True) |
| 114 | + |
| 115 | + if category: |
| 116 | + media = media.filter(category__title=category) |
| 117 | + elif tag: |
| 118 | + media = media.filter(tags__title=tag) |
| 119 | + elif query: |
| 120 | + # same as on files.views.MediaSearch: move this processing to a prepare_query function |
| 121 | + query = helpers.clean_query(query) |
| 122 | + q_parts = [ |
| 123 | + q_part.rstrip("y") |
| 124 | + for q_part in query.split() |
| 125 | + if q_part not in STOP_WORDS |
| 126 | + ] |
| 127 | + if q_parts: |
| 128 | + query = SearchQuery(q_parts[0] + ":*", search_type="raw") |
| 129 | + for part in q_parts[1:]: |
| 130 | + query &= SearchQuery(part + ":*", search_type="raw") |
| 131 | + else: |
| 132 | + query = None |
| 133 | + if query: |
| 134 | + media = media.filter(search=query) |
| 135 | + |
| 136 | + media = media.order_by("-add_date").prefetch_related("user") |
| 137 | + |
| 138 | + return media |
| 139 | + |
| 140 | + def items(self, objects): |
| 141 | + return objects[:20] |
| 142 | + |
| 143 | + def item_title(self, item): |
| 144 | + return item.title |
| 145 | + |
| 146 | + def item_description(self, item): |
| 147 | + return item.description |
| 148 | + |
| 149 | + def item_author_name(self, item): |
| 150 | + return item.user.username |
| 151 | + |
| 152 | + def item_pubdate(self, item): |
| 153 | + return item.add_date |
| 154 | + |
| 155 | + def item_updateddate(self, item): |
| 156 | + return item.edit_date |
| 157 | + |
| 158 | + def item_link(self, item): |
| 159 | + return reverse("get_media") + "?m={0}".format(item.friendly_token) |
| 160 | + |
| 161 | + def item_extra_kwargs(self, item): |
| 162 | + item = { |
| 163 | + "media:title": item.title, |
| 164 | + "media:description": item.description, |
| 165 | + "content_width": 720, |
| 166 | + "thumbnail_url": f"{settings.SSL_FRONTEND_HOST}/{item.poster_url}", |
| 167 | + "content_url": f"{settings.SSL_FRONTEND_HOST}/{item.get_absolute_url()}", |
| 168 | + "thumbnail_width": 720, |
| 169 | + } |
| 170 | + return item |
0 commit comments