2020-12-24 23:49:38 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.postgres.search import SearchQuery
|
2021-05-26 17:35:21 +02:00
|
|
|
from django.contrib.syndication.views import Feed
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.utils.feedgenerator import Rss201rev2Feed
|
2020-12-15 22:33:43 +01:00
|
|
|
|
2020-12-24 23:49:38 +01:00
|
|
|
from . import helpers
|
2021-06-03 17:26:53 +02:00
|
|
|
from .models import Media
|
2020-12-24 23:49:38 +01:00
|
|
|
from .stop_words import STOP_WORDS
|
2020-12-15 22:33:43 +01:00
|
|
|
|
|
|
|
|
2020-12-24 23:49:38 +01:00
|
|
|
class MediaRSSFeed(Rss201rev2Feed):
|
|
|
|
def rss_attributes(self):
|
|
|
|
attrs = super(MediaRSSFeed, self).rss_attributes()
|
|
|
|
attrs["xmlns:media"] = "http://search.yahoo.com/mrss/"
|
|
|
|
attrs["xmlns:atom"] = "http://www.w3.org/2005/Atom"
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
def add_item_elements(self, handler, item):
|
|
|
|
"""Callback to add elements to each item (item/entry) element."""
|
|
|
|
super(MediaRSSFeed, self).add_item_elements(handler, item)
|
|
|
|
|
|
|
|
if "media:title" in item:
|
|
|
|
handler.addQuickElement("media:title", item["title"])
|
|
|
|
if "media:description" in item:
|
|
|
|
handler.addQuickElement("media:description", item["description"])
|
|
|
|
|
|
|
|
if "content_url" in item:
|
|
|
|
content = dict(url=item["content_url"])
|
|
|
|
if "content_width" in item:
|
|
|
|
content["width"] = str(item["content_width"])
|
|
|
|
if "content_height" in item:
|
|
|
|
content["height"] = str(item["content_height"])
|
|
|
|
handler.addQuickElement("media:content", "", content)
|
|
|
|
|
|
|
|
if "thumbnail_url" in item:
|
|
|
|
thumbnail = dict(url=item["thumbnail_url"])
|
|
|
|
if "thumbnail_width" in item:
|
|
|
|
thumbnail["width"] = str(item["thumbnail_width"])
|
|
|
|
if "thumbnail_height" in item:
|
|
|
|
thumbnail["height"] = str(item["thumbnail_height"])
|
|
|
|
handler.addQuickElement("media:thumbnail", "", thumbnail)
|
|
|
|
|
|
|
|
if "keywords" in item:
|
|
|
|
handler.addQuickElement("media:keywords", item["keywords"])
|
|
|
|
|
|
|
|
def add_root_elements(self, handler):
|
|
|
|
super().add_root_elements(handler)
|
|
|
|
if self.feed["author_name"] is not None:
|
|
|
|
handler.startElement("author", {})
|
|
|
|
handler.addQuickElement("name", self.feed["author_name"])
|
|
|
|
handler.endElement("author")
|
|
|
|
if self.feed.get("published") is not None:
|
|
|
|
handler.startElement("published", {})
|
|
|
|
handler.addQuickElement("name", self.feed["published"])
|
|
|
|
handler.endElement("published")
|
|
|
|
|
|
|
|
|
|
|
|
class IndexRSSFeed(Feed):
|
|
|
|
feed_type = MediaRSSFeed
|
2020-12-15 22:33:43 +01:00
|
|
|
title = "Latest Media"
|
2020-12-24 23:49:38 +01:00
|
|
|
link = "/rss"
|
2020-12-15 22:33:43 +01:00
|
|
|
description = "Latest Media RSS feed"
|
|
|
|
|
|
|
|
def items(self):
|
2020-12-24 23:49:38 +01:00
|
|
|
media = Media.objects.filter(listable=True).order_by("-add_date")
|
2020-12-15 22:33:43 +01:00
|
|
|
media = media.prefetch_related("user")
|
2020-12-24 23:49:38 +01:00
|
|
|
return media[:20]
|
|
|
|
|
|
|
|
def item_title(self, item):
|
|
|
|
return item.title
|
|
|
|
|
|
|
|
def item_description(self, item):
|
2020-12-25 14:25:53 +01:00
|
|
|
return item.description
|
2020-12-24 23:49:38 +01:00
|
|
|
|
|
|
|
def item_author_name(self, item):
|
|
|
|
return item.user.username
|
|
|
|
|
|
|
|
def item_pubdate(self, item):
|
|
|
|
return item.add_date
|
|
|
|
|
|
|
|
def item_updateddate(self, item):
|
|
|
|
return item.edit_date
|
|
|
|
|
|
|
|
def item_link(self, item):
|
|
|
|
return reverse("get_media") + "?m={0}".format(item.friendly_token)
|
|
|
|
|
|
|
|
def item_extra_kwargs(self, item):
|
|
|
|
item = {
|
|
|
|
"media:title": item.title,
|
2020-12-25 14:25:53 +01:00
|
|
|
"media:description": item.description,
|
2020-12-24 23:49:38 +01:00
|
|
|
"content_width": 720,
|
|
|
|
"thumbnail_url": f"{settings.SSL_FRONTEND_HOST}/{item.poster_url}",
|
|
|
|
"content_url": f"{settings.SSL_FRONTEND_HOST}/{item.get_absolute_url()}",
|
|
|
|
"thumbnail_width": 720,
|
|
|
|
}
|
|
|
|
return item
|
|
|
|
|
|
|
|
|
|
|
|
class SearchRSSFeed(Feed):
|
|
|
|
feed_type = MediaRSSFeed
|
|
|
|
description = "Latest Media RSS feed"
|
|
|
|
|
|
|
|
def link(self, obj):
|
2023-03-14 13:09:52 +01:00
|
|
|
return "/rss/search"
|
2020-12-24 23:49:38 +01:00
|
|
|
|
|
|
|
def get_object(self, request):
|
|
|
|
category = request.GET.get("c", "")
|
|
|
|
tag = request.GET.get("t", "")
|
|
|
|
query = request.GET.get("q", "")
|
|
|
|
|
|
|
|
media = Media.objects.filter(listable=True)
|
|
|
|
|
|
|
|
if category:
|
|
|
|
media = media.filter(category__title=category)
|
|
|
|
elif tag:
|
|
|
|
media = media.filter(tags__title=tag)
|
|
|
|
elif query:
|
2020-12-25 14:07:50 +01:00
|
|
|
# same as on files.views.MediaSearch: move this processing to a prepare_query function
|
2020-12-24 23:49:38 +01:00
|
|
|
query = helpers.clean_query(query)
|
2021-05-26 17:35:21 +02:00
|
|
|
q_parts = [q_part.rstrip("y") for q_part in query.split() if q_part not in STOP_WORDS]
|
2020-12-24 23:49:38 +01:00
|
|
|
if q_parts:
|
|
|
|
query = SearchQuery(q_parts[0] + ":*", search_type="raw")
|
|
|
|
for part in q_parts[1:]:
|
|
|
|
query &= SearchQuery(part + ":*", search_type="raw")
|
|
|
|
else:
|
|
|
|
query = None
|
|
|
|
if query:
|
|
|
|
media = media.filter(search=query)
|
|
|
|
|
|
|
|
media = media.order_by("-add_date").prefetch_related("user")
|
|
|
|
|
|
|
|
return media
|
|
|
|
|
|
|
|
def items(self, objects):
|
|
|
|
return objects[:20]
|
2020-12-15 22:33:43 +01:00
|
|
|
|
|
|
|
def item_title(self, item):
|
|
|
|
return item.title
|
|
|
|
|
|
|
|
def item_description(self, item):
|
2020-12-25 14:25:53 +01:00
|
|
|
return item.description
|
2020-12-24 23:49:38 +01:00
|
|
|
|
|
|
|
def item_author_name(self, item):
|
|
|
|
return item.user.username
|
|
|
|
|
|
|
|
def item_pubdate(self, item):
|
|
|
|
return item.add_date
|
|
|
|
|
|
|
|
def item_updateddate(self, item):
|
|
|
|
return item.edit_date
|
2020-12-15 22:33:43 +01:00
|
|
|
|
|
|
|
def item_link(self, item):
|
|
|
|
return reverse("get_media") + "?m={0}".format(item.friendly_token)
|
2020-12-24 23:49:38 +01:00
|
|
|
|
|
|
|
def item_extra_kwargs(self, item):
|
|
|
|
item = {
|
|
|
|
"media:title": item.title,
|
2020-12-25 14:25:53 +01:00
|
|
|
"media:description": item.description,
|
2020-12-24 23:49:38 +01:00
|
|
|
"content_width": 720,
|
|
|
|
"thumbnail_url": f"{settings.SSL_FRONTEND_HOST}/{item.poster_url}",
|
|
|
|
"content_url": f"{settings.SSL_FRONTEND_HOST}/{item.get_absolute_url()}",
|
|
|
|
"thumbnail_width": 720,
|
|
|
|
}
|
|
|
|
return item
|