diff options
| author | rr- | 2016-08-14 12:35:14 +0200 |
|---|---|---|
| committer | rr- | 2016-08-14 16:43:35 +0200 |
| commit | af62f8c45ac8986342a56d41f176435ada1a1a9b (patch) | |
| tree | 5bb920cff28c4ac74f2ca1c292ac007d2fcc5c76 /server/szurubooru/api/user_api.py | |
| parent | d102c9bdbafe503952c306abb55d430db2c75718 (diff) | |
server/general: ditch falcon for in-house WSGI app
For quite some time, I hated Falcon's class maps approach that caused
more chaos than good for Szurubooru. I've taken a look at the other
frameworks (hug, flask, etc) again, but they all looked too
bloated/over-engineered. I decided to just talk to WSGI myself.
Regex-based routing may not be the fastest in the world, but I'm fine
with response time of 10 ms for cached /posts.
Diffstat (limited to 'server/szurubooru/api/user_api.py')
| -rw-r--r-- | server/szurubooru/api/user_api.py | 138 |
1 files changed, 69 insertions, 69 deletions
diff --git a/server/szurubooru/api/user_api.py b/server/szurubooru/api/user_api.py index 706e5d7..94b82f3 100644 --- a/server/szurubooru/api/user_api.py +++ b/server/szurubooru/api/user_api.py @@ -1,6 +1,8 @@ from szurubooru import search -from szurubooru.api.base_api import BaseApi from szurubooru.func import auth, users, util +from szurubooru.rest import routes + +_search_executor = search.Executor(search.configs.UserSearchConfig()) def _serialize(ctx, user, **kwargs): return users.serialize_user( @@ -9,75 +11,73 @@ def _serialize(ctx, user, **kwargs): options=util.get_serialization_options(ctx), **kwargs) -class UserListApi(BaseApi): - def __init__(self): - super().__init__() - self._search_executor = search.Executor( - search.configs.UserSearchConfig()) - - def get(self, ctx): - auth.verify_privilege(ctx.user, 'users:list') - return self._search_executor.execute_and_serialize( - ctx, lambda user: _serialize(ctx, user)) +@routes.get('/users/?') +def get_users(ctx, _params=None): + auth.verify_privilege(ctx.user, 'users:list') + return _search_executor.execute_and_serialize( + ctx, lambda user: _serialize(ctx, user)) - def post(self, ctx): - auth.verify_privilege(ctx.user, 'users:create') - name = ctx.get_param_as_string('name', required=True) - password = ctx.get_param_as_string('password', required=True) - email = ctx.get_param_as_string('email', required=False, default='') - user = users.create_user(name, password, email) - if ctx.has_param('rank'): - users.update_user_rank( - user, ctx.get_param_as_string('rank'), ctx.user) - if ctx.has_param('avatarStyle'): - users.update_user_avatar( - user, - ctx.get_param_as_string('avatarStyle'), - ctx.get_file('avatar')) - ctx.session.add(user) - ctx.session.commit() - return _serialize(ctx, user, force_show_email=True) +@routes.post('/users/?') +def create_user(ctx, _params=None): + auth.verify_privilege(ctx.user, 'users:create') + name = ctx.get_param_as_string('name', required=True) + password = ctx.get_param_as_string('password', required=True) + email = ctx.get_param_as_string('email', required=False, default='') + user = users.create_user(name, password, email) + if ctx.has_param('rank'): + users.update_user_rank( + user, ctx.get_param_as_string('rank'), ctx.user) + if ctx.has_param('avatarStyle'): + users.update_user_avatar( + user, + ctx.get_param_as_string('avatarStyle'), + ctx.get_file('avatar')) + ctx.session.add(user) + ctx.session.commit() + return _serialize(ctx, user, force_show_email=True) -class UserDetailApi(BaseApi): - def get(self, ctx, user_name): - user = users.get_user_by_name(user_name) - if ctx.user.user_id != user.user_id: - auth.verify_privilege(ctx.user, 'users:view') - return _serialize(ctx, user) +@routes.get('/user/(?P<user_name>[^/]+)/?') +def get_user(ctx, params): + user = users.get_user_by_name(params['user_name']) + if ctx.user.user_id != user.user_id: + auth.verify_privilege(ctx.user, 'users:view') + return _serialize(ctx, user) - def put(self, ctx, user_name): - user = users.get_user_by_name(user_name) - util.verify_version(user, ctx) - infix = 'self' if ctx.user.user_id == user.user_id else 'any' - if ctx.has_param('name'): - auth.verify_privilege(ctx.user, 'users:edit:%s:name' % infix) - users.update_user_name(user, ctx.get_param_as_string('name')) - if ctx.has_param('password'): - auth.verify_privilege(ctx.user, 'users:edit:%s:pass' % infix) - users.update_user_password( - user, ctx.get_param_as_string('password')) - if ctx.has_param('email'): - auth.verify_privilege(ctx.user, 'users:edit:%s:email' % infix) - users.update_user_email(user, ctx.get_param_as_string('email')) - if ctx.has_param('rank'): - auth.verify_privilege(ctx.user, 'users:edit:%s:rank' % infix) - users.update_user_rank( - user, ctx.get_param_as_string('rank'), ctx.user) - if ctx.has_param('avatarStyle'): - auth.verify_privilege(ctx.user, 'users:edit:%s:avatar' % infix) - users.update_user_avatar( - user, - ctx.get_param_as_string('avatarStyle'), - ctx.get_file('avatar')) - util.bump_version(user) - ctx.session.commit() - return _serialize(ctx, user) +@routes.put('/user/(?P<user_name>[^/]+)/?') +def update_user(ctx, params): + user = users.get_user_by_name(params['user_name']) + util.verify_version(user, ctx) + infix = 'self' if ctx.user.user_id == user.user_id else 'any' + if ctx.has_param('name'): + auth.verify_privilege(ctx.user, 'users:edit:%s:name' % infix) + users.update_user_name(user, ctx.get_param_as_string('name')) + if ctx.has_param('password'): + auth.verify_privilege(ctx.user, 'users:edit:%s:pass' % infix) + users.update_user_password( + user, ctx.get_param_as_string('password')) + if ctx.has_param('email'): + auth.verify_privilege(ctx.user, 'users:edit:%s:email' % infix) + users.update_user_email(user, ctx.get_param_as_string('email')) + if ctx.has_param('rank'): + auth.verify_privilege(ctx.user, 'users:edit:%s:rank' % infix) + users.update_user_rank( + user, ctx.get_param_as_string('rank'), ctx.user) + if ctx.has_param('avatarStyle'): + auth.verify_privilege(ctx.user, 'users:edit:%s:avatar' % infix) + users.update_user_avatar( + user, + ctx.get_param_as_string('avatarStyle'), + ctx.get_file('avatar')) + util.bump_version(user) + ctx.session.commit() + return _serialize(ctx, user) - def delete(self, ctx, user_name): - user = users.get_user_by_name(user_name) - util.verify_version(user, ctx) - infix = 'self' if ctx.user.user_id == user.user_id else 'any' - auth.verify_privilege(ctx.user, 'users:delete:%s' % infix) - ctx.session.delete(user) - ctx.session.commit() - return {} +@routes.delete('/user/(?P<user_name>[^/]+)/?') +def delete_user(ctx, params): + user = users.get_user_by_name(params['user_name']) + util.verify_version(user, ctx) + infix = 'self' if ctx.user.user_id == user.user_id else 'any' + auth.verify_privilege(ctx.user, 'users:delete:%s' % infix) + ctx.session.delete(user) + ctx.session.commit() + return {} |