diff options
| -rw-r--r-- | blog/feed.py | 52 | ||||
| -rw-r--r-- | blog/urls.py | 3 | ||||
| -rw-r--r-- | static/js/globals.js | 14 | ||||
| -rw-r--r-- | static/js/tl.js | 1 | ||||
| -rw-r--r-- | templates/blog/post.html | 11 |
5 files changed, 79 insertions, 2 deletions
diff --git a/blog/feed.py b/blog/feed.py new file mode 100644 index 00000000..85788710 --- /dev/null +++ b/blog/feed.py @@ -0,0 +1,52 @@ +from django.contrib.syndication.views import Feed +from django.utils import feedgenerator +from django.utils.feedgenerator import Enclosure +import requests +from .context_processors import add_excerpt +from .models import Post +from django.conf import settings + +request_domain = settings.DEBUG and 'https://preview.thatcomputerscientist.com' or 'https://thatcomputerscientist.com' + +class RSSFeed(Feed): + title = 'That Computer Scientist - RSS Feed' + link = '/weblog/' + description = 'RSS Feed for That Computer Scientist Weblog' + feed_type = feedgenerator.Rss201rev2Feed + + def items(self): + unique_items = set() + items = [] + + for post in Post.objects.filter(is_public=True).order_by('-date'): + if post.id not in unique_items: + unique_items.add(post.id) + items.append(post) + + return items + + def item_title(self, item): + return item.title + + def item_description(self, item): + post_excerpt = add_excerpt(item) + return post_excerpt + + def item_link(self, item): + return f'{request_domain}/weblog/{item.slug}' + + def item_pubdate(self, item): + return item.date + + def get_cl(self, url): + r = requests.head(url) + return str(r.headers['Content-Length']) + + def item_enclosures(self, item): + return [ + Enclosure( + url=f'{request_domain}/ignis/post_image/1200/{item.id}.gif', + length=self.get_cl(f'{request_domain}/ignis/post_image/1200/{item.id}.gif'), + mime_type='image/gif', + ) + ] diff --git a/blog/urls.py b/blog/urls.py index e21ed5de..f1fad34d 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -1,5 +1,5 @@ from django.urls import path -from django.views.generic import RedirectView +from .feed import RSSFeed from . import views @@ -26,4 +26,5 @@ urlpatterns = [ path('~<str:username>', views.user_activity, name='user_activity'), path('policy', views.policy, name='policy'), path('socialify', views.socialify, name='socialify'), + path('rss/', RSSFeed(), name='rss_feed'), ] diff --git a/static/js/globals.js b/static/js/globals.js index 9100251b..61b3fcce 100644 --- a/static/js/globals.js +++ b/static/js/globals.js @@ -14,6 +14,20 @@ function changeLang(lang) { } } +function copyToClipboard(text) { + $('body').append('<input type="text" value="' + text + '" id="copyToClipboard">'); + const copyText = $('#copyToClipboard'); + copyText.select(); + copyText[0].setSelectionRange(0, 99999); + navigator.clipboard.writeText(copyText.val()).then(function () { + alert("Copied to clipboard!"); + }, function (err) { + alert("Failed to copy to clipboard!"); + }); + copyText.remove(); +} + + // Smooth scroll to anchor $(document).ready(function () { $('a[href^="#"]').on("click", function (e) { diff --git a/static/js/tl.js b/static/js/tl.js index 4cfdb7ff..599f64bd 100644 --- a/static/js/tl.js +++ b/static/js/tl.js @@ -19,7 +19,6 @@ function restoreLang() { } else { translateContainers.each(function (index, element) { if (element.contentWindow.document.getElementById(":1.restore")) { - console.log(element.contentWindow.document.getElementById(":1.restore")); element.contentWindow.document.getElementById(":1.restore").click(); } }); diff --git a/templates/blog/post.html b/templates/blog/post.html index 8769c253..888032b4 100644 --- a/templates/blog/post.html +++ b/templates/blog/post.html @@ -55,6 +55,17 @@ </div> </div> +<div id="rss_subscribe" class="mtsbitem" style=" +padding: 20px; +border: white 1px dashed; +border-radius: 4px; +background: #ffffff0d; +"> + <h1 style="margin-top: 0">Liked this post? Wanna stay updated?</h1> + <p>Subscribe to our RSS feed to get the latest updates from this weblog. Copy and paste the following link into your favorite RSS reader:</p> + <pre style="display: inline-block; margin-right: 20px;">https://thatcomputerscientist.com/rss/</pre><button onclick="copyToClipboard('https://thatcomputerscientist.com/rss/')" class="button button-special">Copy URL</button> +</div> + {% if read_next %} <h2 class="mtsbitem" style="clear: both;">Read Next</h2> <div id="read-next"> |
