summaryrefslogtreecommitdiff
path: root/app/syndication
diff options
context:
space:
mode:
Diffstat (limited to 'app/syndication')
-rw-r--r--app/syndication/medium.py14
-rw-r--r--app/syndication/models.py16
-rw-r--r--app/syndication/templatetags/__init__.py0
-rw-r--r--app/syndication/templatetags/facebook_processor.py23
4 files changed, 46 insertions, 7 deletions
diff --git a/app/syndication/medium.py b/app/syndication/medium.py
index 9787a1a..de157af 100644
--- a/app/syndication/medium.py
+++ b/app/syndication/medium.py
@@ -16,16 +16,16 @@ def post_to_medium(item):
client = Client(application_id=settings.MEDIUM_CLIENT_ID, application_secret=settings.MEDIUM_CLIENT_SECRET)
client.access_token = settings.MEDIUM_INT_TOKEN
user = client.get_current_user()
- head = '<p><i>This was originally posted <a href="https://luxagraf.net%s" rel="canonical">on my own site</a>.</i></p>' % item.content_object.get_absolute_url()
- body = "%s %s" % (head, absolute_urls_for_syndication(item.content_object.body_html))
+ head = '<p><i>This was originally posted <a href="https://luxagraf.net%s" rel="canonical">on my own site</a>.</i></p>' % item.get_absolute_url()
+ body = "%s %s" % (head, absolute_urls_for_syndication(item.body_html))
# Create a post.
post = client.create_post(
user_id=user["id"],
- title=item.content_object.title,
+ title=item.title,
content=body,
content_format="html",
- publish_status="draft"
+ publish_status="public",
+ canonicalUrl="https://luxagraf.net%s" % item.get_absolute_url(),
+ license="all-rights-reserved"
)
- item.rel_link = post["url"]
- item.status = 2
- item.save()
+ return post["url"]
diff --git a/app/syndication/models.py b/app/syndication/models.py
index 5856de1..a58da8d 100644
--- a/app/syndication/models.py
+++ b/app/syndication/models.py
@@ -1,6 +1,10 @@
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
+from django.db.models.signals import post_save
+from django.dispatch import receiver
+
+from .medium import post_to_medium
class Syndicate(models.Model):
@@ -25,3 +29,15 @@ class SyndicatedItem(models.Model):
def __str__(self):
return self.content_object.title
+
+
+@receiver(post_save, sender=SyndicatedItem)
+def post_save_events(sender, update_fields, created, instance, **kwargs):
+ if instance.status == 1:
+ for item in instance.syndicate.all():
+ if item.name == "Medium":
+ instance.rel_link = post_to_medium(instance.content_object)
+ instance.status = 2
+ post_save.disconnect(post_save_events, sender=SyndicatedItem)
+ instance.save()
+ post_save.connect(post_save_events, sender=SyndicatedItem)
diff --git a/app/syndication/templatetags/__init__.py b/app/syndication/templatetags/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/app/syndication/templatetags/__init__.py
diff --git a/app/syndication/templatetags/facebook_processor.py b/app/syndication/templatetags/facebook_processor.py
new file mode 100644
index 0000000..d940838
--- /dev/null
+++ b/app/syndication/templatetags/facebook_processor.py
@@ -0,0 +1,23 @@
+from django import template
+from bs4 import BeautifulSoup
+
+from .medium import absolute_urls_for_syndication
+
+register = template.Library()
+
+
+def wrap_image_tags(text):
+ soup = BeautifulSoup(text, 'lxml')
+ for img in soup.find_all('img'):
+ if img.parent.name != "figure":
+ new_tag = soup.new_tag('figure')
+ img.wrap(new_tag)
+ return soup
+
+
+def facebook_processor(text):
+ ia = absolute_urls_for_syndication(text)
+ ia = wrap_image_tags(ia)
+ return ia
+
+register.filter('facebook_processor', facebook_processor)