def create_photos_from_files(cls, tweet, files): photos = [] for index, file in enumerate(files): photo = TweetPhoto(tweet=tweet, user=tweet.user, file=file, order=index) photos.append(photo) TweetPhoto.objects.bulk_create(photos)
def create_tweet_photos(self, tweet, files): photos = [] for order, file in enumerate(files): photo = TweetPhoto( user=tweet.user, tweet=tweet, file=file, order=order, ) photos.append(photo) TweetPhoto.objects.bulk_create(photos)
def test_tweetphoto(self): self.assertEqual(TweetPhoto.objects.count(), 0) tweetphoto1 = TweetPhoto.objects.create( tweet=self.tweet, user=self.user1, ) self.assertEqual(TweetPhoto.objects.count(), 1) self.assertEqual(tweetphoto1.status, TweetPhotoStatus.PENDING) self.assertEqual(tweetphoto1.has_deleted, False) tweetphoto2 = TweetPhoto.objects.create( tweet=self.tweet, user=self.user2, ) self.assertEqual(TweetPhoto.objects.count(), 2) self.assertEqual(tweetphoto2.status, TweetPhotoStatus.PENDING) tweetphoto2.delete() self.assertEqual(TweetPhoto.objects.count(), 1) # bulk_create files = [ SimpleUploadedFile( name='dummy image {}.jpg'.format(i), content=str.encode('dummy image'), content_type='image/jpeg', ) for i in range(5) ] photos = [ TweetPhoto( user=self.user1, tweet=self.tweet, file=file, order=i, ) for i, file in enumerate(files) ] TweetPhoto.objects.bulk_create(photos) self.assertEqual(TweetPhoto.objects.count(), 6)