aboutsummaryrefslogtreecommitdiff
path: root/server/szurubooru/api/user_api.py
diff options
context:
space:
mode:
authorrr-2016-04-03 19:00:47 +0200
committerrr-2016-04-03 19:17:54 +0200
commit2578a297bfc95b165401436110d0d810df4ed4b2 (patch)
tree34d223082698c6a4369d94b38e72e6072abda4f5 /server/szurubooru/api/user_api.py
parent9ce67b64ed42c9946a51cdd78e25342e9ee92841 (diff)
server/general: improve pylint score
+ incorporate some in-house rules - no more useless doc strings...
Diffstat (limited to 'server/szurubooru/api/user_api.py')
-rw-r--r--server/szurubooru/api/user_api.py52
1 files changed, 25 insertions, 27 deletions
diff --git a/server/szurubooru/api/user_api.py b/server/szurubooru/api/user_api.py
index e4134c6..946c413 100644
--- a/server/szurubooru/api/user_api.py
+++ b/server/szurubooru/api/user_api.py
@@ -1,11 +1,9 @@
-''' Exports UserListApi and UserDetailApi. '''
-
import re
import sqlalchemy
+from szurubooru import errors
+from szurubooru import util
from szurubooru.api.base_api import BaseApi
-from szurubooru.errors import IntegrityError, ValidationError, NotFoundError, AuthError
-from szurubooru.services.search import UserSearchConfig, SearchExecutor
-from szurubooru.util import is_valid_email
+from szurubooru.services import search
def _serialize_user(authenticated_user, user):
ret = {
@@ -22,17 +20,18 @@ def _serialize_user(authenticated_user, user):
class UserListApi(BaseApi):
''' API for lists of users. '''
+
def __init__(self, auth_service, user_service):
super().__init__()
self._auth_service = auth_service
self._user_service = user_service
- self._search_executor = SearchExecutor(UserSearchConfig())
+ self._search_executor = search.SearchExecutor(search.UserSearchConfig())
- def get(self, request, context):
+ def get(self, context):
''' Retrieves a list of users. '''
self._auth_service.verify_privilege(context.user, 'users:list')
- query = request.get_param_as_string('query')
- page = request.get_param_as_int('page', 1)
+ query = context.get_param_as_string('query')
+ page = context.get_param_as_int('page', 1)
count, users = self._search_executor.execute(context.session, query, page)
return {
'query': query,
@@ -42,7 +41,7 @@ class UserListApi(BaseApi):
'users': [_serialize_user(context.user, user) for user in users],
}
- def post(self, request, context):
+ def post(self, context):
''' Creates a new user. '''
self._auth_service.verify_privilege(context.user, 'users:create')
@@ -51,18 +50,19 @@ class UserListApi(BaseApi):
password = context.request['password']
email = context.request['email'].strip()
except KeyError as ex:
- raise ValidationError('Field %r not found.' % ex.args[0])
+ raise errors.ValidationError('Field %r not found.' % ex.args[0])
user = self._user_service.create_user(
context.session, name, password, email)
try:
context.session.commit()
except sqlalchemy.exc.IntegrityError:
- raise IntegrityError('User %r already exists.' % name)
+ raise errors.IntegrityError('User %r already exists.' % name)
return {'user': _serialize_user(context.user, user)}
class UserDetailApi(BaseApi):
''' API for individual users. '''
+
def __init__(self, config, auth_service, password_service, user_service):
super().__init__()
self._available_access_ranks = config['service']['user_ranks']
@@ -72,19 +72,19 @@ class UserDetailApi(BaseApi):
self._auth_service = auth_service
self._user_service = user_service
- def get(self, request, context, user_name):
+ def get(self, context, user_name):
''' Retrieves an user. '''
self._auth_service.verify_privilege(context.user, 'users:view')
user = self._user_service.get_by_name(context.session, user_name)
if not user:
- raise NotFoundError('User %r not found.' % user_name)
+ raise errors.NotFoundError('User %r not found.' % user_name)
return {'user': _serialize_user(context.user, user)}
- def put(self, request, context, user_name):
+ def put(self, context, user_name):
''' Updates an existing user. '''
user = self._user_service.get_by_name(context.session, user_name)
if not user:
- raise NotFoundError('User %r not found.' % user_name)
+ raise errors.NotFoundError('User %r not found.' % user_name)
if context.user.user_id == user.user_id:
infix = 'self'
@@ -96,7 +96,7 @@ class UserDetailApi(BaseApi):
context.user, 'users:edit:%s:name' % infix)
name = context.request['name'].strip()
if not re.match(self._name_regex, name):
- raise ValidationError(
+ raise errors.ValidationError(
'Name must satisfy regex %r.' % self._name_regex)
user.name = name
@@ -105,7 +105,7 @@ class UserDetailApi(BaseApi):
self._auth_service.verify_privilege(
context.user, 'users:edit:%s:pass' % infix)
if not re.match(self._password_regex, password):
- raise ValidationError(
+ raise errors.ValidationError(
'Password must satisfy regex %r.' % self._password_regex)
user.password_salt = self._password_service.create_password()
user.password_hash = self._password_service.get_password_hash(
@@ -114,12 +114,10 @@ class UserDetailApi(BaseApi):
if 'email' in context.request:
self._auth_service.verify_privilege(
context.user, 'users:edit:%s:email' % infix)
- email = context.request['email'].strip()
- if not is_valid_email(email):
- raise ValidationError('%r is not a vaild email address.' % email)
- # prefer nulls to empty strings in the DB
- if not email:
- email = None
+ email = context.request['email'].strip() or None
+ if not util.is_valid_email(email):
+ raise errors.ValidationError(
+ '%r is not a vaild email address.' % email)
user.email = email
if 'accessRank' in context.request:
@@ -127,12 +125,12 @@ class UserDetailApi(BaseApi):
context.user, 'users:edit:%s:rank' % infix)
rank = context.request['accessRank'].strip()
if not rank in self._available_access_ranks:
- raise ValidationError(
+ raise errors.ValidationError(
'Bad access rank. Valid access ranks: %r' \
% self._available_access_ranks)
if self._available_access_ranks.index(context.user.access_rank) \
< self._available_access_ranks.index(rank):
- raise AuthError(
+ raise errors.AuthError(
'Trying to set higher access rank than one has')
user.access_rank = rank
@@ -141,6 +139,6 @@ class UserDetailApi(BaseApi):
try:
context.session.commit()
except sqlalchemy.exc.IntegrityError:
- raise IntegrityError('User %r already exists.' % name)
+ raise errors.IntegrityError('User %r already exists.' % name)
return {'user': _serialize_user(context.user, user)}

© 2015 - 2026 Jakob L. Kreuze