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 process_request(ctx: rest.Context) -> None: ''' Bind the user to request. Update last login time if needed. ''' auth_user = _get_user(ctx) if auth_user: ctx.user = auth_user if ctx.get_param_as_bool('bump-login', default=False) and ctx.user.user_id: users.bump_user_login_time(ctx.user) ctx.session.commit()
def merge_posts(ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: source_post_id = ctx.get_param_as_int('remove') target_post_id = ctx.get_param_as_int('mergeTo') source_post = posts.get_post_by_id(source_post_id) target_post = posts.get_post_by_id(target_post_id) replace_content = ctx.get_param_as_bool('replaceContent') versions.verify_version(source_post, ctx, 'removeVersion') versions.verify_version(target_post, ctx, 'mergeToVersion') versions.bump_version(target_post) auth.verify_privilege(ctx.user, 'posts:merge') posts.merge_posts(source_post, target_post, replace_content) snapshots.merge(source_post, target_post, ctx.user) ctx.session.commit() return _serialize_post(ctx, target_post)
def merge_posts( ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: source_post_id = ctx.get_param_as_int('remove') target_post_id = ctx.get_param_as_int('mergeTo') source_post = posts.get_post_by_id(source_post_id) target_post = posts.get_post_by_id(target_post_id) replace_content = ctx.get_param_as_bool('replaceContent') versions.verify_version(source_post, ctx, 'removeVersion') versions.verify_version(target_post, ctx, 'mergeToVersion') versions.bump_version(target_post) auth.verify_privilege(ctx.user, 'posts:merge') posts.merge_posts(source_post, target_post, replace_content) snapshots.merge(source_post, target_post, ctx.user) ctx.session.commit() return _serialize_post(ctx, target_post)
def create_user_token(ctx: rest.Context, params: Dict[str, str] = {}) -> rest.Response: user = users.get_user_by_name(params["user_name"]) infix = "self" if ctx.user.user_id == user.user_id else "any" auth.verify_privilege(ctx.user, "user_tokens:create:%s" % infix) enabled = ctx.get_param_as_bool("enabled", True) user_token = user_tokens.create_user_token(user, enabled) if ctx.has_param("note"): note = ctx.get_param_as_string("note") user_tokens.update_user_token_note(user_token, note) if ctx.has_param("expirationTime"): expiration_time = ctx.get_param_as_string("expirationTime") user_tokens.update_user_token_expiration_time(user_token, expiration_time) ctx.session.add(user_token) ctx.session.commit() return _serialize(ctx, user_token)
def create_user_token( ctx: rest.Context, params: Dict[str, str] = {}) -> rest.Response: user = users.get_user_by_name(params['user_name']) infix = 'self' if ctx.user.user_id == user.user_id else 'any' auth.verify_privilege(ctx.user, 'user_tokens:create:%s' % infix) enabled = ctx.get_param_as_bool('enabled', True) user_token = user_tokens.create_user_token(user, enabled) if ctx.has_param('note'): note = ctx.get_param_as_string('note') user_tokens.update_user_token_note(user_token, note) if ctx.has_param('expirationTime'): expiration_time = ctx.get_param_as_string('expirationTime') user_tokens.update_user_token_expiration_time( user_token, expiration_time) ctx.session.add(user_token) ctx.session.commit() return _serialize(ctx, user_token)
def create_user_token(ctx: rest.Context, params: Dict[str, str] = {}) -> rest.Response: user = users.get_user_by_name(params['user_name']) infix = 'self' if ctx.user.user_id == user.user_id else 'any' auth.verify_privilege(ctx.user, 'user_tokens:create:%s' % infix) enabled = ctx.get_param_as_bool('enabled', True) user_token = user_tokens.create_user_token(user, enabled) if ctx.has_param('note'): note = ctx.get_param_as_string('note') user_tokens.update_user_token_note(user_token, note) if ctx.has_param('expirationTime'): expiration_time = ctx.get_param_as_string('expirationTime') user_tokens.update_user_token_expiration_time(user_token, expiration_time) ctx.session.add(user_token) ctx.session.commit() return _serialize(ctx, user_token)
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 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 update_user_token(ctx: rest.Context, params: Dict[str, str] = {}) -> rest.Response: user = users.get_user_by_name(params["user_name"]) infix = "self" if ctx.user.user_id == user.user_id else "any" auth.verify_privilege(ctx.user, "user_tokens:edit:%s" % infix) user_token = user_tokens.get_by_user_and_token(user, params["user_token"]) versions.verify_version(user_token, ctx) versions.bump_version(user_token) if ctx.has_param("enabled"): auth.verify_privilege(ctx.user, "user_tokens:edit:%s" % infix) user_tokens.update_user_token_enabled(user_token, ctx.get_param_as_bool("enabled")) if ctx.has_param("note"): auth.verify_privilege(ctx.user, "user_tokens:edit:%s" % infix) note = ctx.get_param_as_string("note") user_tokens.update_user_token_note(user_token, note) if ctx.has_param("expirationTime"): auth.verify_privilege(ctx.user, "user_tokens:edit:%s" % infix) expiration_time = ctx.get_param_as_string("expirationTime") user_tokens.update_user_token_expiration_time(user_token, expiration_time) user_tokens.update_user_token_edit_time(user_token) ctx.session.commit() return _serialize(ctx, user_token)
def update_user_token( ctx: rest.Context, params: Dict[str, str] = {}) -> rest.Response: user = users.get_user_by_name(params['user_name']) infix = 'self' if ctx.user.user_id == user.user_id else 'any' auth.verify_privilege(ctx.user, 'user_tokens:edit:%s' % infix) user_token = user_tokens.get_by_user_and_token(user, params['user_token']) versions.verify_version(user_token, ctx) versions.bump_version(user_token) if ctx.has_param('enabled'): auth.verify_privilege(ctx.user, 'user_tokens:edit:%s' % infix) user_tokens.update_user_token_enabled( user_token, ctx.get_param_as_bool('enabled')) if ctx.has_param('note'): auth.verify_privilege(ctx.user, 'user_tokens:edit:%s' % infix) note = ctx.get_param_as_string('note') user_tokens.update_user_token_note(user_token, note) if ctx.has_param('expirationTime'): auth.verify_privilege(ctx.user, 'user_tokens:edit:%s' % infix) expiration_time = ctx.get_param_as_string('expirationTime') user_tokens.update_user_token_expiration_time( user_token, expiration_time) user_tokens.update_user_token_edit_time(user_token) ctx.session.commit() return _serialize(ctx, user_token)
def update_user_token(ctx: rest.Context, params: Dict[str, str] = {}) -> rest.Response: user = users.get_user_by_name(params['user_name']) infix = 'self' if ctx.user.user_id == user.user_id else 'any' auth.verify_privilege(ctx.user, 'user_tokens:edit:%s' % infix) user_token = user_tokens.get_by_user_and_token(user, params['user_token']) versions.verify_version(user_token, ctx) versions.bump_version(user_token) if ctx.has_param('enabled'): auth.verify_privilege(ctx.user, 'user_tokens:edit:%s' % infix) user_tokens.update_user_token_enabled(user_token, ctx.get_param_as_bool('enabled')) if ctx.has_param('note'): auth.verify_privilege(ctx.user, 'user_tokens:edit:%s' % infix) note = ctx.get_param_as_string('note') user_tokens.update_user_token_note(user_token, note) if ctx.has_param('expirationTime'): auth.verify_privilege(ctx.user, 'user_tokens:edit:%s' % infix) expiration_time = ctx.get_param_as_string('expirationTime') user_tokens.update_user_token_expiration_time(user_token, expiration_time) user_tokens.update_user_token_edit_time(user_token) ctx.session.commit() return _serialize(ctx, user_token)
def process_request(ctx: rest.Context) -> None: ''' Bind the user to request. Update last login time if needed. ''' bump_login = ctx.get_param_as_bool('bump-login', default=False) auth_user = _get_user(ctx, bump_login) if auth_user: ctx.user = auth_user
def process_request(ctx: rest.Context) -> None: ''' Bind the user to request. Update last login time if needed. ''' bump_login = ctx.get_param_as_bool('bump-login', default=False) auth_user = _get_user(ctx, bump_login) if auth_user: ctx.user = auth_user