summaryrefslogtreecommitdiff
path: root/app/syndication/models.py
blob: a58da8d9eaaa1c4399f4822a9f0924fb76b466d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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):
    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name


class SyndicatedItem(models.Model):
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    syndicate = models.ManyToManyField(Syndicate)
    publish_date = models.DateTimeField()
    STATUS = (
        ('1', "Unsent"),
        ('2', "Sent"),
    )
    status = models.CharField(max_length=1, choices=STATUS, null=True, blank=True)
    rel_link = models.CharField(max_length=300, null=True, blank=True)

    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)