def test_user_can_create_two_channels_with_same_name_if_one_is_deleted( self, user, simple_uploaded_img_file_factory): name = 'My Channel' services.create_channel( name=name, user=user, description='x' * 5000, sync_videos_interested=True, primary_language='en', avatar_image=simple_uploaded_img_file_factory(), banner_image=simple_uploaded_img_file_factory( path=EXAMPLE_BANNER_IMG), deleted_on=timezone.now(), ) channel = services.create_channel( name=name, user=user, description='x' * 5000, sync_videos_interested=True, primary_language='en', avatar_image=simple_uploaded_img_file_factory(), banner_image=simple_uploaded_img_file_factory( path=EXAMPLE_BANNER_IMG), ) assert channel
def test_user_cannot_create_two_channels_with_the_same_name( self, user, simple_uploaded_img_file_factory): name = 'My Channel' services.create_channel( name=name, user=user, description='x' * 5000, sync_videos_interested=True, primary_language='en', avatar_image=simple_uploaded_img_file_factory(), banner_image=simple_uploaded_img_file_factory( path=EXAMPLE_BANNER_IMG), ) with pytest.raises(DRFValidationError): services.create_channel( name=name, user=user, description='x' * 5000, sync_videos_interested=True, primary_language='en', avatar_image=simple_uploaded_img_file_factory(), banner_image=simple_uploaded_img_file_factory( path=EXAMPLE_BANNER_IMG), )
def test_without_images(self, user): channel = services.create_channel( name='My Channel', user=user, description='x' * 5000, sync_videos_interested=True, primary_language='en', avatar_image=None, banner_image=None, ) assert isinstance(channel, services.models.Channel) assert channel.name == 'My Channel' assert channel.user == user assert channel.description == 'x' * 5000 assert channel.sync_videos_interested is True # Original avatar (default) assert not channel.avatar_image # Small avatar (default) assert not channel.avatar_image_small assert channel.avatar_image_small_url # Large avatar (default) assert not channel.avatar_image_large assert channel.avatar_image_large_url # Large banner_image (default) assert not channel.banner_image_large assert channel.banner_image_large_url
def test(self, user, simple_uploaded_img_file_factory): channel = services.create_channel( name='My Channel', user=user, description='x' * 5000, sync_videos_interested=True, primary_language='en', avatar_image=simple_uploaded_img_file_factory(), banner_image=simple_uploaded_img_file_factory( path=EXAMPLE_BANNER_IMG), ) assert isinstance(channel, services.models.Channel) assert channel.is_selected is True assert channel.name == 'My Channel' assert channel.user == user assert channel.description == 'x' * 5000 assert channel.sync_videos_interested is True # Original avatar assert channel.avatar_image.url assert channel.avatar_image.width == 3113 assert channel.avatar_image.height == 2106 # Small avatar try: channel.avatar_image_small.open() except ValueError: pass assert channel.avatar_image_small.url == channel.avatar_image_small_url assert channel.avatar_image_small.width == 44 assert channel.avatar_image_small.height == 44 # Large avatar try: channel.avatar_image_large.open() except ValueError: pass assert channel.avatar_image_large.url == channel.avatar_image_large_url assert channel.avatar_image_large.width == 98 assert channel.avatar_image_large.height == 98 # Original banner_image assert channel.banner_image.url assert channel.banner_image.width == 2048 assert channel.banner_image.height == 338 # Small banner_image try: channel.banner_image_small.open() except ValueError: pass assert channel.banner_image_small.url == channel.banner_image_small_url assert channel.banner_image_small.width == 800 assert channel.banner_image_small.height == 132 # Large banner_image try: channel.banner_image_large.open() except ValueError: pass assert channel.banner_image_large.url == channel.banner_image_large_url assert channel.banner_image_large.width == 1138 assert channel.banner_image_large.height == 188
def make( *, user, description=None, name=None, avatar_image=None, banner_image=None, is_selected=True, ): return channel_services.create_channel( name=name or 'My Channel', user=user, description=description or 'x' * 5000, sync_videos_interested=True, language='en', avatar_image=avatar_image, banner_image=banner_image, is_selected=is_selected, )
def _run(): seed_data = Path(settings.BASE_DIR / 'seed_data.json') with seed_data.open('r') as file_: data = json.load(file_) for row in data: user = get_user_model().objects.create( username=row['user']['username'], email=row['user']['email'], is_staff=True, is_superuser=True, ) user.set_password(row['user']['password']) user.save() for channel in row['channels']: channel_record = channel_services.create_channel( name=channel['name'], description=channel['description'], sync_videos_interested=channel['sync_videos_interested'], language='en', user=user, ) print('Created Channel', channel_record.id) for video in channel['videos']: path = Path(video['path']) upload, video_record = upload_manager.prepare( user=user, filename=path.name, channel_id=channel_record.id) video_record.title = video['title'] video_record.description = video['description'] video_record.save() print('Created Video', video_record.id) with path.open('rb') as file_: # Upload the file completely outside of Django resp = requests.put(upload.presigned_upload_url, io.BytesIO(file_.read())) resp.raise_for_status() upload.file = File(file_) upload.save() upload_manager.complete(upload_id=upload.id)
def _run(): seed_data = Path(settings.BASE_DIR / 'seed_data.json') with seed_data.open('r') as file_: data = json.load(file_) for row in data: user = get_user_model()( username=row['user']['username'], email=row['user']['email'], is_staff=True, is_superuser=True, ) user.set_password(row['user']['password']) user.save() for channel in row['channels']: channel_record = channel_services.create_channel( name=channel['name'], description=channel['description'], sync_videos_interested=channel['sync_videos_interested'], primary_language='en', user=user, ) if channel['avatar_image_url']: file_ = tempfile.NamedTemporaryFile('wb+', suffix='.jpg') file_.write(requests.get(channel['avatar_image_url']).content) file_.flush() channel_services.set_channel_avatar_image( channel=channel_record, avatar_image=File(file_)) channel_record.refresh_from_db() assert channel_record.avatar_image print('Created Channel', channel_record.id) for video in channel['videos']: path = Path(video['path']) file_size = path.stat().st_size num_parts = math.ceil(file_size / UPLOAD_CHUNK_SIZE) upload, video_record = upload_manager.prepare( user=user, filename=path.name, channel_id=channel_record.id, num_parts=num_parts, ) video_record.title = video['title'] video_record.description = video['description'] video_record.save(update_fields=('title', 'description')) print('Created Video', video_record.id) parts = [] with path.open('rb') as file_: for part_idx in range(num_parts): chunk = file_.read(UPLOAD_CHUNK_SIZE) # Upload the file completely outside of Django resp = requests.put( upload.presigned_upload_urls[part_idx], io.BytesIO(chunk), ) parts.append({ 'etag': resp.headers['ETag'], 'part_number': part_idx + 1, }) resp.raise_for_status() print('Uploaded file chunk') upload.file = File(file_) upload.save(update_fields=('file', )) upload_manager.complete(upload_id=upload.id, parts=parts) for comment_body in video.get('comments', ()): print('Creating video comment', comment_body) comment_services.create_video_comment( video_id=video_record.id, commenter_channel_id=channel_record.id, body=comment_body, )