models.py
from django.db import models
class Post(models.Model):
text = models.TextField()
approved = models.BooleanField(default=False)
group = models.ForeignKey('bot_users.BotUserGroup', on_delete=models.SET_NULL, null=True, blank=True)
from django.conf import settings
from django.db import models
import os
class PostMedia(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='mediafiles')
media = models.FileField(upload_to='post_media/', blank=True)
absolute_media_path = models.CharField(max_length=255, blank=True)
def save(self, *args, **kwargs):
self.absolute_media_path = self.get_absolute_media_path()
return super().save(*args, **kwargs)
def get_absolute_media_path(self):
return os.path.join(settings.BASE_DIR, self.media.path)
#Post.objects.last().mediafiles.last().absolute_media_path
services.py (functions for aiogram bot and django)
async def get_post_path(post_id):
url = f'http://localhost:8000/api/get_media_urls/{post_id}/'
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status == 200:
data_list = await response.json()
return data_list
else:
return None
async def register_manager(CHAT_ID: str):
fake = Faker()
username = fake.user_name()
password = fake.password()
await bot.send_message(CHAT_ID, f'username: {username}\npassword: {password}\nadmin-panel: http://localhost:8000/admin', reply_markup=set_manager_kb())
# async def send_post(instance):
# admin_id = '679553167'
# await bot.send_message(admin_id, f'{instance}', reply_markup=set_post_kb())
async def send_post(post):
message = post.split('\n')
user_id = '679553167'
post_id = message[0]
media = []
media_paths = await get_post_path(post_id)
if media_paths:
for photo_path in media_paths:
if photo_path.lower().endswith(('.jpg', '.jpeg', '.png')):
media_type = types.InputMediaPhoto
elif photo_path.lower().endswith(('.mp4', '.avi', '.mkv')):
media_type = types.InputMediaVideo
else:
continue
photo_file = open(photo_path, 'rb')
input_media = media_type(media=types.InputFile(photo_file))
media.append(input_media)
media[0]["caption"] = message[1]
await bot.send_media_group(chat_id=user_id, media=media, reply_markup=set_post_kb())
photo_file.close()
else:
await bot.send_message(user_id, message[1], reply_markup=set_post_kb())
signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Post, PostMedia
from bot.django_services import send_post, get_post_path
from asgiref.sync import async_to_sync
@receiver(post_save, sender=PostMedia)
def post_created(sender, instance, created, **kwargs):
if created:
send_post_sync = async_to_sync(send_post)
if instance.post.group:
print(instance.absolute_media_path)
send_post_sync(f'{instance.post.id}\n{instance.post.text}\n{instance.post.group.id}')
else:
print(instance.absolute_media_path)
send_post_sync(f'{instance.post.id}\n{instance.post.text}')
The thing is I get an empty list of media_paths = await get_post_path(post_id), although Post have a few PostMedia instances with FK.
Also, as you can see, I have prints in my signals and that's what they show:
[13/Aug/2023 00:37:48] "GET /admin/posts/post/add/ HTTP/1.1" 200 18144
[13/Aug/2023 00:37:49] "GET /admin/jsi18n/ HTTP/1.1" 200 3343
C:\Users\hp\Desktop\job\media\depositphotos_59977559-stock-photo-hands-holding-word-about-us.jpg
[13/Aug/2023 00:38:01] "GET /api/get_media_urls/51/ HTTP/1.1" 200 2
C:\Users\hp\Desktop\job\media\Green_card_2_X2Wbqbu.jpg
[13/Aug/2023 00:38:04] "GET /api/get_media_urls/51/ HTTP/1.1" 200 2
[13/Aug/2023 00:38:04] "POST /admin/posts/post/add/ HTTP/1.1" 302 0
[13/Aug/2023 00:38:04] "GET /admin/posts/post/ HTTP/1.1" 200 16314
[13/Aug/2023 00:38:04] "GET /admin/jsi18n/ HTTP/1.1" 200 3343
So, they print what I nedd, but not in one list (firstly C:\Users\hp\Desktop\job\media\depositphotos_59977559-stock-photo-hands-holding-word-about-us.jpg, then C:\Users\hp\Desktop\job\media\Green_card_2_X2Wbqbu.jpg etc.)
I've tried make a sender=Post, but it didn't work at all.