def test_update_post_thumbnail_to_default( tmpdir, config_injector, read_asset, post_factory, is_existing): config_injector({ 'data_dir': str(tmpdir.mkdir('data')), 'thumbnails': { 'post_width': 300, 'post_height': 300, }, }) post = post_factory() db.session.add(post) if is_existing: db.session.flush() assert post.post_id else: assert not post.post_id generated_path = str(tmpdir) + '/data/generated-thumbnails/1.jpg' source_path = str(tmpdir) + '/data/posts/custom-thumbnails/1.dat' assert not os.path.exists(generated_path) assert not os.path.exists(source_path) posts.update_post_content(post, read_asset('png.png')) posts.update_post_thumbnail(post, read_asset('jpeg.jpg')) posts.update_post_thumbnail(post, None) assert not os.path.exists(generated_path) assert not os.path.exists(source_path) db.session.flush() assert os.path.exists(generated_path) assert not os.path.exists(source_path)
def test_update_post_thumbnail_to_default(tmpdir, config_injector, read_asset, post_factory, is_existing): config_injector({ 'data_dir': str(tmpdir.mkdir('data')), 'thumbnails': { 'post_width': 300, 'post_height': 300, }, 'secret': 'test', }) post = post_factory(id=1) db.session.add(post) if is_existing: db.session.flush() assert post.post_id generated_path = ( '{}/data/generated-thumbnails/1_244c8840887984c4.jpg'.format(tmpdir)) source_path = ('{}/data/posts/custom-thumbnails/1_244c8840887984c4.dat'. format(tmpdir)) assert not os.path.exists(generated_path) assert not os.path.exists(source_path) posts.update_post_content(post, read_asset('png.png')) posts.update_post_thumbnail(post, read_asset('jpeg.jpg')) posts.update_post_thumbnail(post, None) assert not os.path.exists(generated_path) assert not os.path.exists(source_path) db.session.flush() assert os.path.exists(generated_path) assert not os.path.exists(source_path)
def test_update_post_thumbnail_with_broken_thumbnail( tmpdir, config_injector, read_asset, post_factory, is_existing): config_injector({ 'data_dir': str(tmpdir.mkdir('data')), 'thumbnails': { 'post_width': 300, 'post_height': 300, }, }) post = post_factory() db.session.add(post) if is_existing: db.session.flush() assert post.post_id else: assert not post.post_id generated_path = str(tmpdir) + '/data/generated-thumbnails/1.jpg' source_path = str(tmpdir) + '/data/posts/custom-thumbnails/1.dat' assert not os.path.exists(generated_path) assert not os.path.exists(source_path) posts.update_post_content(post, read_asset('png.png')) posts.update_post_thumbnail(post, read_asset('png-broken.png')) assert not os.path.exists(generated_path) assert not os.path.exists(source_path) db.session.flush() assert os.path.exists(generated_path) assert os.path.exists(source_path) with open(source_path, 'rb') as handle: assert handle.read() == read_asset('png-broken.png') with open(generated_path, 'rb') as handle: image = images.Image(handle.read()) assert image.width == 1 assert image.height == 1
def test_update_post_content_convert_heif_to_png_when_processing( tmpdir, config_injector, read_asset, post_factory, filename ): config_injector( { "data_dir": str(tmpdir.mkdir("data")), "thumbnails": { "post_width": 300, "post_height": 300, }, "secret": "test", "allow_broken_uploads": False, } ) post = post_factory(id=1) db.session.add(post) posts.update_post_content(post, read_asset(filename)) posts.update_post_thumbnail(post, read_asset(filename)) db.session.flush() generated_path = ( "{}/data/generated-thumbnails/".format(tmpdir) + "1_244c8840887984c4.jpg" ) source_path = ( "{}/data/posts/custom-thumbnails/".format(tmpdir) + "1_244c8840887984c4.dat" ) assert os.path.exists(source_path) assert os.path.exists(generated_path)
def post(self, ctx): auth.verify_privilege(ctx.user, 'posts:create') content = ctx.get_file('content', required=True) tag_names = ctx.get_param_as_list('tags', required=True) safety = ctx.get_param_as_string('safety', required=True) source = ctx.get_param_as_string('source', required=False, default=None) if ctx.has_param('contentUrl') and not source: source = ctx.get_param_as_string('contentUrl') relations = ctx.get_param_as_list('relations', required=False) or [] notes = ctx.get_param_as_list('notes', required=False) or [] flags = ctx.get_param_as_list('flags', required=False) or [] post = posts.create_post(content, tag_names, ctx.user) posts.update_post_safety(post, safety) posts.update_post_source(post, source) posts.update_post_relations(post, relations) posts.update_post_notes(post, notes) posts.update_post_flags(post, flags) if ctx.has_file('thumbnail'): posts.update_post_thumbnail(post, ctx.get_file('thumbnail')) ctx.session.add(post) snapshots.save_entity_creation(post, ctx.user) ctx.session.commit() tags.export_to_json() return posts.serialize_post_with_details(post, ctx.user)
def test_update_post_thumbnail_to_default(tmpdir, config_injector, read_asset, post_factory, is_existing): config_injector({ "data_dir": str(tmpdir.mkdir("data")), "thumbnails": { "post_width": 300, "post_height": 300, }, "secret": "test", "allow_broken_uploads": False, }) post = post_factory(id=1) db.session.add(post) if is_existing: db.session.flush() assert post.post_id generated_path = ("{}/data/generated-thumbnails/".format(tmpdir) + "1_244c8840887984c4.jpg") source_path = ("{}/data/posts/custom-thumbnails/".format(tmpdir) + "1_244c8840887984c4.dat") assert not os.path.exists(generated_path) assert not os.path.exists(source_path) posts.update_post_content(post, read_asset("png.png")) posts.update_post_thumbnail(post, read_asset("jpeg.jpg")) posts.update_post_thumbnail(post, None) assert not os.path.exists(generated_path) assert not os.path.exists(source_path) db.session.flush() assert os.path.exists(generated_path) assert not os.path.exists(source_path)
def test_update_post_thumbnail_to_new_one(tmpdir, config_injector, read_asset, post_factory, is_existing): config_injector({ 'data_dir': str(tmpdir.mkdir('data')), 'thumbnails': { 'post_width': 300, 'post_height': 300, }, }) post = post_factory(id=1) db.session.add(post) if is_existing: db.session.flush() assert post.post_id generated_path = '{}/data/generated-thumbnails/1.jpg'.format(tmpdir) source_path = '{}/data/posts/custom-thumbnails/1.dat'.format(tmpdir) assert not os.path.exists(generated_path) assert not os.path.exists(source_path) posts.update_post_content(post, read_asset('png.png')) posts.update_post_thumbnail(post, read_asset('jpeg.jpg')) assert not os.path.exists(generated_path) assert not os.path.exists(source_path) db.session.flush() assert os.path.exists(generated_path) assert os.path.exists(source_path) with open(source_path, 'rb') as handle: assert handle.read() == read_asset('jpeg.jpg')
def create_post(ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: anonymous = ctx.get_param_as_bool('anonymous', default=False) if anonymous: auth.verify_privilege(ctx.user, 'posts:create:anonymous') else: auth.verify_privilege(ctx.user, 'posts:create:identified') content = ctx.get_file('content') tag_names = ctx.get_param_as_string_list('tags', default=[]) safety = ctx.get_param_as_string('safety') source = ctx.get_param_as_string('source', default='') if ctx.has_param('contentUrl') and not source: source = ctx.get_param_as_string('contentUrl', default='') relations = ctx.get_param_as_int_list('relations', default=[]) notes = ctx.get_param_as_list('notes', default=[]) flags = ctx.get_param_as_string_list('flags', default=[]) post, new_tags = posts.create_post(content, tag_names, None if anonymous else ctx.user) if len(new_tags): auth.verify_privilege(ctx.user, 'tags:create') posts.update_post_safety(post, safety) posts.update_post_source(post, source) posts.update_post_relations(post, relations) posts.update_post_notes(post, notes) posts.update_post_flags(post, flags) if ctx.has_file('thumbnail'): posts.update_post_thumbnail(post, ctx.get_file('thumbnail')) ctx.session.add(post) ctx.session.flush() snapshots.create(post, None if anonymous else ctx.user) for tag in new_tags: snapshots.create(tag, None if anonymous else ctx.user) ctx.session.commit() return _serialize_post(ctx, post)
def put(self, ctx, post_id): post = posts.get_post_by_id(post_id) if ctx.has_file('content'): auth.verify_privilege(ctx.user, 'posts:edit:content') posts.update_post_content(post, ctx.get_file('content')) if ctx.has_param('tags'): auth.verify_privilege(ctx.user, 'posts:edit:tags') posts.update_post_tags(post, ctx.get_param_as_list('tags')) if ctx.has_param('safety'): auth.verify_privilege(ctx.user, 'posts:edit:safety') posts.update_post_safety(post, ctx.get_param_as_string('safety')) if ctx.has_param('source'): auth.verify_privilege(ctx.user, 'posts:edit:source') posts.update_post_source(post, ctx.get_param_as_string('source')) elif ctx.has_param('contentUrl'): posts.update_post_source(post, ctx.get_param_as_string('contentUrl')) if ctx.has_param('relations'): auth.verify_privilege(ctx.user, 'posts:edit:relations') posts.update_post_relations(post, ctx.get_param_as_list('relations')) if ctx.has_param('notes'): auth.verify_privilege(ctx.user, 'posts:edit:notes') posts.update_post_notes(post, ctx.get_param_as_list('notes')) if ctx.has_param('flags'): auth.verify_privilege(ctx.user, 'posts:edit:flags') posts.update_post_flags(post, ctx.get_param_as_list('flags')) if ctx.has_file('thumbnail'): auth.verify_privilege(ctx.user, 'posts:edit:thumbnail') posts.update_post_thumbnail(post, ctx.get_file('thumbnail')) post.last_edit_time = datetime.datetime.now() ctx.session.flush() snapshots.save_entity_modification(post, ctx.user) ctx.session.commit() tags.export_to_json() return posts.serialize_post_with_details(post, ctx.user)
def create_post(ctx, _params=None): anonymous = ctx.get_param_as_bool('anonymous', default=False) if anonymous: auth.verify_privilege(ctx.user, 'posts:create:anonymous') else: auth.verify_privilege(ctx.user, 'posts:create:identified') content = ctx.get_file('content', required=True) tag_names = ctx.get_param_as_list('tags', required=False, default=[]) safety = ctx.get_param_as_string('safety', required=True) source = ctx.get_param_as_string('source', required=False, default=None) if ctx.has_param('contentUrl') and not source: source = ctx.get_param_as_string('contentUrl') relations = ctx.get_param_as_list('relations', required=False) or [] notes = ctx.get_param_as_list('notes', required=False) or [] flags = ctx.get_param_as_list('flags', required=False) or [] post, new_tags = posts.create_post( content, tag_names, None if anonymous else ctx.user) if len(new_tags): auth.verify_privilege(ctx.user, 'tags:create') posts.update_post_safety(post, safety) posts.update_post_source(post, source) posts.update_post_relations(post, relations) posts.update_post_notes(post, notes) posts.update_post_flags(post, flags) if ctx.has_file('thumbnail'): posts.update_post_thumbnail(post, ctx.get_file('thumbnail')) ctx.session.add(post) ctx.session.flush() snapshots.create(post, None if anonymous else ctx.user) for tag in new_tags: snapshots.create(tag, None if anonymous else ctx.user) ctx.session.commit() tags.export_to_json() return _serialize_post(ctx, post)
def update_post(ctx: rest.Context, params: Dict[str, str]) -> rest.Response: post = _get_post(params) versions.verify_version(post, ctx) versions.bump_version(post) if ctx.has_file("content"): auth.verify_privilege(ctx.user, "posts:edit:content") posts.update_post_content( post, ctx.get_file( "content", use_video_downloader=auth.has_privilege( ctx.user, "uploads:use_downloader"), ), ) if ctx.has_param("tags"): auth.verify_privilege(ctx.user, "posts:edit:tags") new_tags = posts.update_post_tags(post, ctx.get_param_as_string_list("tags")) if len(new_tags): auth.verify_privilege(ctx.user, "tags:create") db.session.flush() for tag in new_tags: snapshots.create(tag, ctx.user) if ctx.has_param("safety"): auth.verify_privilege(ctx.user, "posts:edit:safety") posts.update_post_safety(post, ctx.get_param_as_string("safety")) if ctx.has_param("source"): auth.verify_privilege(ctx.user, "posts:edit:source") posts.update_post_source(post, ctx.get_param_as_string("source")) elif ctx.has_param("contentUrl"): posts.update_post_source(post, ctx.get_param_as_string("contentUrl")) if ctx.has_param("relations"): auth.verify_privilege(ctx.user, "posts:edit:relations") posts.update_post_relations(post, ctx.get_param_as_int_list("relations")) if ctx.has_param("notes"): auth.verify_privilege(ctx.user, "posts:edit:notes") posts.update_post_notes(post, ctx.get_param_as_list("notes")) if ctx.has_param("flags"): auth.verify_privilege(ctx.user, "posts:edit:flags") posts.update_post_flags(post, ctx.get_param_as_string_list("flags")) if ctx.has_file("thumbnail"): auth.verify_privilege(ctx.user, "posts:edit:thumbnail") posts.update_post_thumbnail(post, ctx.get_file("thumbnail")) post.last_edit_time = datetime.utcnow() ctx.session.flush() snapshots.modify(post, ctx.user) ctx.session.commit() return _serialize_post(ctx, post)
def test_update_post_content_leaving_custom_thumbnail( tmpdir, config_injector, read_asset, post_factory): config_injector({ 'data_dir': str(tmpdir.mkdir('data')), 'thumbnails': { 'post_width': 300, 'post_height': 300, }, }) post = post_factory(id=1) db.session.add(post) posts.update_post_content(post, read_asset('png.png')) posts.update_post_thumbnail(post, read_asset('jpeg.jpg')) posts.update_post_content(post, read_asset('png.png')) db.session.flush() assert os.path.exists(str(tmpdir) + '/data/posts/custom-thumbnails/1.dat') assert os.path.exists(str(tmpdir) + '/data/generated-thumbnails/1.jpg')
def test_update_post_content_leaves_custom_thumbnail(tmpdir, config_injector, read_asset, post_factory): config_injector({ 'data_dir': str(tmpdir.mkdir('data')), 'thumbnails': { 'post_width': 300, 'post_height': 300, }, }) post = post_factory(id=1) db.session.add(post) db.session.flush() posts.update_post_content(post, read_asset('png.png')) posts.update_post_thumbnail(post, read_asset('jpeg.jpg')) posts.update_post_content(post, read_asset('png.png')) assert os.path.exists(str(tmpdir) + '/data/posts/custom-thumbnails/1.dat') assert os.path.exists(str(tmpdir) + '/data/generated-thumbnails/1.jpg')
def create_post(ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: anonymous = ctx.get_param_as_bool("anonymous", default=False) if anonymous: auth.verify_privilege(ctx.user, "posts:create:anonymous") else: auth.verify_privilege(ctx.user, "posts:create:identified") content = ctx.get_file( "content", use_video_downloader=auth.has_privilege(ctx.user, "uploads:use_downloader"), ) tag_names = ctx.get_param_as_string_list("tags", default=[]) safety = ctx.get_param_as_string("safety") source = ctx.get_param_as_string("source", default="") if ctx.has_param("contentUrl") and not source: source = ctx.get_param_as_string("contentUrl", default="") relations = ctx.get_param_as_int_list("relations", default=[]) notes = ctx.get_param_as_list("notes", default=[]) flags = ctx.get_param_as_string_list( "flags", default=posts.get_default_flags(content)) post, new_tags = posts.create_post(content, tag_names, None if anonymous else ctx.user) if len(new_tags): auth.verify_privilege(ctx.user, "tags:create") posts.update_post_safety(post, safety) posts.update_post_source(post, source) posts.update_post_relations(post, relations) posts.update_post_notes(post, notes) posts.update_post_flags(post, flags) if ctx.has_file("thumbnail"): posts.update_post_thumbnail(post, ctx.get_file("thumbnail")) ctx.session.add(post) ctx.session.flush() create_snapshots_for_post(post, new_tags, None if anonymous else ctx.user) alternate_format_posts = posts.generate_alternate_formats(post, content) for alternate_post, alternate_post_new_tags in alternate_format_posts: create_snapshots_for_post( alternate_post, alternate_post_new_tags, None if anonymous else ctx.user, ) ctx.session.commit() return _serialize_post(ctx, post)
def update_post(ctx: rest.Context, params: Dict[str, str]) -> rest.Response: post = _get_post(params) versions.verify_version(post, ctx) versions.bump_version(post) if ctx.has_file('content'): auth.verify_privilege(ctx.user, 'posts:edit:content') posts.update_post_content(post, ctx.get_file('content')) if ctx.has_param('tags'): auth.verify_privilege(ctx.user, 'posts:edit:tags') new_tags = posts.update_post_tags( post, ctx.get_param_as_string_list('tags')) if len(new_tags): auth.verify_privilege(ctx.user, 'tags:create') db.session.flush() for tag in new_tags: snapshots.create(tag, ctx.user) if ctx.has_param('safety'): auth.verify_privilege(ctx.user, 'posts:edit:safety') posts.update_post_safety(post, ctx.get_param_as_string('safety')) if ctx.has_param('source'): auth.verify_privilege(ctx.user, 'posts:edit:source') posts.update_post_source(post, ctx.get_param_as_string('source')) elif ctx.has_param('contentUrl'): posts.update_post_source(post, ctx.get_param_as_string('contentUrl')) if ctx.has_param('relations'): auth.verify_privilege(ctx.user, 'posts:edit:relations') posts.update_post_relations( post, ctx.get_param_as_int_list('relations')) if ctx.has_param('notes'): auth.verify_privilege(ctx.user, 'posts:edit:notes') posts.update_post_notes(post, ctx.get_param_as_list('notes')) if ctx.has_param('flags'): auth.verify_privilege(ctx.user, 'posts:edit:flags') posts.update_post_flags(post, ctx.get_param_as_string_list('flags')) if ctx.has_file('thumbnail'): auth.verify_privilege(ctx.user, 'posts:edit:thumbnail') posts.update_post_thumbnail(post, ctx.get_file('thumbnail')) post.last_edit_time = datetime.utcnow() ctx.session.flush() snapshots.modify(post, ctx.user) ctx.session.commit() tags.export_to_json() return _serialize_post(ctx, post)
def test_update_post_thumbnail_with_broken_thumbnail( tmpdir, config_injector, read_asset, post_factory, is_existing ): config_injector( { "data_dir": str(tmpdir.mkdir("data")), "thumbnails": { "post_width": 300, "post_height": 300, }, "secret": "test", "allow_broken_uploads": False, } ) post = post_factory(id=1) db.session.add(post) if is_existing: db.session.flush() assert post.post_id generated_path = ( "{}/data/generated-thumbnails/".format(tmpdir) + "1_244c8840887984c4.jpg" ) source_path = ( "{}/data/posts/custom-thumbnails/".format(tmpdir) + "1_244c8840887984c4.dat" ) assert not os.path.exists(generated_path) assert not os.path.exists(source_path) posts.update_post_content(post, read_asset("png.png")) posts.update_post_thumbnail(post, read_asset("png-broken.png")) assert not os.path.exists(generated_path) assert not os.path.exists(source_path) db.session.flush() assert os.path.exists(generated_path) assert os.path.exists(source_path) with open(source_path, "rb") as handle: assert handle.read() == read_asset("png-broken.png") with open(generated_path, "rb") as handle: image = images.Image(handle.read()) assert image.width == 1 assert image.height == 1
def update_post(ctx: rest.Context, params: Dict[str, str]) -> rest.Response: post = _get_post(params) versions.verify_version(post, ctx) versions.bump_version(post) if ctx.has_file('content'): auth.verify_privilege(ctx.user, 'posts:edit:content') posts.update_post_content(post, ctx.get_file('content')) if ctx.has_param('tags'): auth.verify_privilege(ctx.user, 'posts:edit:tags') new_tags = posts.update_post_tags( post, ctx.get_param_as_string_list('tags')) if len(new_tags): auth.verify_privilege(ctx.user, 'tags:create') db.session.flush() for tag in new_tags: snapshots.create(tag, ctx.user) if ctx.has_param('safety'): auth.verify_privilege(ctx.user, 'posts:edit:safety') posts.update_post_safety(post, ctx.get_param_as_string('safety')) if ctx.has_param('source'): auth.verify_privilege(ctx.user, 'posts:edit:source') posts.update_post_source(post, ctx.get_param_as_string('source')) elif ctx.has_param('contentUrl'): posts.update_post_source(post, ctx.get_param_as_string('contentUrl')) if ctx.has_param('relations'): auth.verify_privilege(ctx.user, 'posts:edit:relations') posts.update_post_relations( post, ctx.get_param_as_int_list('relations')) if ctx.has_param('notes'): auth.verify_privilege(ctx.user, 'posts:edit:notes') posts.update_post_notes(post, ctx.get_param_as_list('notes')) if ctx.has_param('flags'): auth.verify_privilege(ctx.user, 'posts:edit:flags') posts.update_post_flags(post, ctx.get_param_as_string_list('flags')) if ctx.has_file('thumbnail'): auth.verify_privilege(ctx.user, 'posts:edit:thumbnail') posts.update_post_thumbnail(post, ctx.get_file('thumbnail')) post.last_edit_time = datetime.utcnow() ctx.session.flush() snapshots.modify(post, ctx.user) ctx.session.commit() return _serialize_post(ctx, post)
def create_post( ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: anonymous = ctx.get_param_as_bool('anonymous', default=False) if anonymous: auth.verify_privilege(ctx.user, 'posts:create:anonymous') else: auth.verify_privilege(ctx.user, 'posts:create:identified') content = ctx.get_file('content') tag_names = ctx.get_param_as_string_list('tags', default=[]) safety = ctx.get_param_as_string('safety') source = ctx.get_param_as_string('source', default='') if ctx.has_param('contentUrl') and not source: source = ctx.get_param_as_string('contentUrl', default='') relations = ctx.get_param_as_int_list('relations', default=[]) notes = ctx.get_param_as_list('notes', default=[]) flags = ctx.get_param_as_string_list('flags', default=[]) post, new_tags = posts.create_post( content, tag_names, None if anonymous else ctx.user) if len(new_tags): auth.verify_privilege(ctx.user, 'tags:create') posts.update_post_safety(post, safety) posts.update_post_source(post, source) posts.update_post_relations(post, relations) posts.update_post_notes(post, notes) posts.update_post_flags(post, flags) posts.test_sound(post, content) if ctx.has_file('thumbnail'): posts.update_post_thumbnail(post, ctx.get_file('thumbnail')) ctx.session.add(post) ctx.session.flush() create_snapshots_for_post(post, new_tags, None if anonymous else ctx.user) alternate_format_posts = posts.generate_alternate_formats(post, content) for alternate_post, alternate_post_new_tags in alternate_format_posts: create_snapshots_for_post( alternate_post, alternate_post_new_tags, None if anonymous else ctx.user) ctx.session.commit() return _serialize_post(ctx, post)
def test_update_post_thumbnail_broken_thumbnail(tmpdir, config_injector, read_asset, post_factory): config_injector({ 'data_dir': str(tmpdir.mkdir('data')), 'thumbnails': { 'post_width': 300, 'post_height': 300, }, }) post = post_factory(id=1) db.session.add(post) db.session.flush() posts.update_post_content(post, read_asset('png.png')) posts.update_post_thumbnail(post, read_asset('png-broken.png')) assert os.path.exists(str(tmpdir) + '/data/posts/custom-thumbnails/1.dat') assert os.path.exists(str(tmpdir) + '/data/generated-thumbnails/1.jpg') with open(str(tmpdir) + '/data/posts/custom-thumbnails/1.dat', 'rb') as handle: assert handle.read() == read_asset('png-broken.png') with open(str(tmpdir) + '/data/generated-thumbnails/1.jpg', 'rb') as handle: image = images.Image(handle.read()) assert image.width == 1 assert image.height == 1