Skip to content

Publications feed2 #120

Merged
merged 3 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from mpicms.personal.views import ContactListView, RawContactListView
from mpicms.events.views import ics_view
from mpicms.publications.views import PublicationsFeedView
from mpicms.publications.views import PublicationsRssFeedView


urlpatterns = [
Expand Down Expand Up @@ -48,7 +49,8 @@
path('contactsraw/', RawContactListView.as_view(), name='contactsraw'),

path('events/ics', ics_view, name='ics'),
path('feed/last_publications', PublicationsFeedView.as_view()),
path('feed/last_publications_xml', PublicationsFeedView.as_view()),
path('feed/last_publications', PublicationsRssFeedView.as_view()),
] + static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT
)
Expand Down
3 changes: 3 additions & 0 deletions mpicms/publications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

from modelcluster.models import ClusterableModel


class LatestFirstOrderable(Orderable):
def save(self, *args, **kwargs):
if self.pk is None:
self.sort_order = self.__class__.objects.aggregate(models.Min('sort_order'))['sort_order__min'] - 1
super(Orderable, self).save(*args, **kwargs)

class Meta:
abstract = True


@register_snippet
class Publication(index.Indexed, ClusterableModel, LatestFirstOrderable):
title = RichTextField(_('title'), features=['bold', 'italic', 'link'])
Expand Down
61 changes: 53 additions & 8 deletions mpicms/publications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,72 @@

from .models import Publication


class PublicationsFeedView(View):

# IMPORTANT: Quick hack to get experiments going, This assumes, that the
# editor of the publications is trusted to put only expected values into the
# fields.
#
# 2DO: check for legal XML characters everywhere https://www.w3.org/TR/xml/#NT-Char
# 2DO: Put HTML content in <![CDATA[...]]> ?
# If we don't make DOI into element:
# 2DO: escape invalid characterds in attribute ( &quot; &amp; &lt; )
# If we don't convert the rich text fields to plain text)
# 2DO: Remove editor artefacts from ricch text fields ("<p>blabla</p>") ?
# 2DO: escape invalid characterds in attribute ( &quot; &amp; &lt; &gt; )
# If we don't convert the rich text fields to plain text maybe:
# 2DO: Remove <p> editor artefacts from rich text fields ("<p>blabla</p>") ?
# 2DO: Remove <i> from source fields ("<p><i>Mol Cell.</i></p>") ?
# maybe use xml.dom for that?
# 2DO: ... ?

def get(self, request, *args, **kwargs):
response = HttpResponse(content_type="application/rss+xml; charset=utf-8")
response.write('<?xml version="1.0" encoding="utf-8" ?>\n')
response.write('<publication-list>\n')
for item in Publication.objects.order_by('sort_order')[:3]:
response.write(f' <publication doi="{item.doi}">\n')
response.write(f' <authors>{item.authors}</authors>\n');
response.write(f' <title>{item.title}</title>\n');
response.write(f' <source>{item.source}</source>\n');
response.write(f' <groups>{item.groups}</groups>\n');
response.write(f' <authors>{item.authors}</authors>\n')
response.write(f' <title>{item.title}</title>\n')
response.write(f' <source>{item.source}</source>\n')
response.write(f' <groups>{item.groups}</groups>\n')
response.write(' </publication>\n')
response.write('</publication-list>\n')
return response;
return response


class PublicationsRssFeedView(View):

# IMPORTANT: Quick hack to get experiments going, This assumes, that the
# editor of the publications is trusted to put only expected values into the
# fields.
#
# 2DO: check for legal XML characters everywhere https://www.w3.org/TR/xml/#NT-Char
# 2DO: Put HTML content in <![CDATA[...]]> ?
# If we don't make DOI into element:
# 2DO: escape invalid characterds in attribute ( &quot; &amp; &lt; &gt; )
# If we don't convert the rich text fields to plain text maybe:
# 2DO: Remove <p> editor artefacts from rich text fields ("<p>blabla</p>") ?
# 2DO: Remove <i> from source fields ("<p><i>Mol Cell.</i></p>") ?
# maybe use xml.dom for that?
# 2DO: ... ?

def get(self, request, *args, **kwargs):
response = HttpResponse(content_type="application/rss+xml; charset=utf-8")
response.write('<?xml version="1.0" encoding="utf-8" ?>\n')
response.write('<rss version="2.0">\n')
response.write(' <channel xmlns:pl="http://www.molgen.mpg.de/xml/publication_feed">\n')
# the next three elements are required https://www.rssboard.org/rss-specification
response.write(' <title>Last Three Publications</title>\n')
response.write(' <link>https://intranet.molgen.mpg.de/</link>\n')
response.write(' <description>The last three publications published on the Intranet site</description>\n')
for item in Publication.objects.order_by('sort_order')[:3]:
# all elements optional, but either title or description allowed
response.write(f' <item pl:doi="{item.doi}">\n')
response.write(f' <title>{item.title}</title>\n')
response.write(f' <pl:authors>{item.authors}</pl:authors>\n')
response.write(f' <pl:title>{item.title}</pl:title>\n')
response.write(f' <pl:source>{item.source}</pl:source>\n')
response.write(f' <pl:groups>{item.groups}</pl:groups>\n')
response.write(' </item>\n')
response.write(' </channel>\n')
response.write('</rss>\n')
return response