api: pep8ness (#28962)

This commit is contained in:
Benjamin Dauvergne 2018-12-13 22:58:31 +01:00
parent 3f5821aa5f
commit dabfc180a0
1 changed files with 26 additions and 8 deletions

View File

@ -1,4 +1,19 @@
'''Views for Authentic2 API'''
# authentic2 - versatile identity manager
# Copyright (C) 2010-2018 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import smtplib
@ -121,8 +136,8 @@ class RegistrationSerializer(serializers.Serializer):
User.objects.filter(ou=ou, email__iexact=data['email']).exists():
raise serializers.ValidationError(
_('You already have an account'))
if ou.username_is_unique and not \
'username' in data:
if (ou.username_is_unique and
'username' not in data):
raise serializers.ValidationError(
_('Username is required in this ou'))
if ou.username_is_unique and User.objects.filter(
@ -200,7 +215,7 @@ class Register(BaseRpcView):
ou=validated_data['ou'],
context=ctx,
**registration_data)
except smtplib.SMTPException, e:
except smtplib.SMTPException as e:
response = {
'result': 0,
'errors': {
@ -387,7 +402,7 @@ class BaseUserSerializer(serializers.ModelSerializer):
context={
'data': original_data,
})
except smtplib.SMTPException, e:
except smtplib.SMTPException as e:
logging.getLogger(__name__).error(u'registration mail could not be sent to user %s '
'created through API: %s', instance, e)
return instance
@ -565,7 +580,10 @@ class UsersAPI(HookMixin, ExceptionHandlerMixin, ModelViewSet):
if 'service-slug' in self.request.GET and 'service-ou' in self.request.GET:
service_slug = self.request.GET['service-slug']
service_ou = self.request.GET['service-ou']
service = Service.objects.filter(slug=service_slug, ou__slug=service_ou).prefetch_related('authorized_roles').first()
service = Service.objects.filter(
slug=service_slug,
ou__slug=service_ou
).prefetch_related('authorized_roles').first()
if service and service.authorized_roles.all():
qs = qs.filter(roles__in=service.authorized_roles.children())
qs = qs.distinct()
@ -616,7 +634,8 @@ class UsersAPI(HookMixin, ExceptionHandlerMixin, ModelViewSet):
hooks.call_hooks('api_modify_response', self, 'synchronization', data)
return Response(data)
@detail_route(methods=['post'], url_path='password-reset', permission_classes=(DjangoPermission('custom_user.reset_password_user'),))
@detail_route(methods=['post'], url_path='password-reset',
permission_classes=(DjangoPermission('custom_user.reset_password_user'),))
def password_reset(self, request, uuid):
user = self.get_object()
# An user without email cannot receive the token
@ -742,7 +761,6 @@ class ValidatePasswordSerializer(serializers.Serializer):
password = serializers.CharField(required=True, allow_blank=True)
class ValidatePasswordAPI(BaseRpcView):
permission_classes = ()
authentication_classes = (CsrfExemptSessionAuthentication,)