from datetime import datetime from django.core.management.base import BaseCommand from social_scheduler.web.posts.models import Post, Image from social_scheduler.workers.instagram import InstagramWorker from social_scheduler.workers.facebook import FacebookWorker class Command(BaseCommand): help = 'Post all scheduled posts.' def post(self, message, images): self.facebook = FacebookWorker('', '') self.instagram = InstagramWorker('', '') if len(images) > 1: self.facebook.post_multiple(message, images) self.instagram.post_multiple(message, images[0:9]) elif len(images) == 1: for image in images: self.facebook.post_single(message, image) self.instagram.post_single(message, image) def handle(self, *args, **options): posts = Post.objects.filter( publication_date__lte=datetime.now().astimezone(), posted=False) for post in posts: images = Image.objects.filter(post_id=post.id) imgs = {img.path.path: img.caption for img in images} self.post(post.message, imgs) self.stdout.write(self.style.SUCCESS(post.message)) self.stdout.write(self.style.SUCCESS(imgs))