Skip to content

Commit

Permalink
Refactored TokenCreateSerializer added validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemKAF committed Nov 9, 2023
1 parent 1337e43 commit e59af37
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
8 changes: 4 additions & 4 deletions backend/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ def filter_queryset(self, queryset):

if user.is_organizer:
status_filter = self.data.get("draft") and self.filter_draft or \
self.data.get("active") and self.filter_active or \
self.data.get("completed") and self.filter_completed or \
self.data.get("archive") and self.filter_archive
self.data.get("active") and self.filter_active or \
self.data.get("completed") and self.filter_completed or \
self.data.get("archive") and self.filter_archive
elif user.is_volunteer:
status_filter = self.data.get("active") and self.filter_active or \
self.data.get("completed") and self.filter_completed
self.data.get("completed") and self.filter_completed

if status_filter:
queryset = status_filter(queryset)
Expand Down
4 changes: 1 addition & 3 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
'http://*.127.0.0.1',
]

# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
Expand All @@ -42,7 +40,6 @@
'djoser',
'rest_framework',
'rest_framework.authtoken',
# 'rest_framework_swagger', # убираем
'drf_yasg',
'taggit',
'gmailapi_backend',
Expand Down Expand Up @@ -199,6 +196,7 @@
'SEND_CONFIRMATION_EMAIL': True,
'SERIALIZERS': {
'current_user': 'api.serializers.CurrentUserSerializer',
'token_create': 'core.auth.serialisers.CustomTokenCreateSerializer',
},
}

Expand Down
Empty file added backend/core/auth/__init__.py
Empty file.
47 changes: 47 additions & 0 deletions backend/core/auth/serialisers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.contrib.auth import authenticate, get_user_model
from djoser.conf import settings
from djoser.serializers import TokenCreateSerializer
from rest_framework.validators import ValidationError

User = get_user_model()


class CustomTokenCreateSerializer(TokenCreateSerializer):
default_error_messages = {
'invalid_credentials': (
settings.CONSTANTS.messages.INVALID_CREDENTIALS_ERROR
),
'inactive_account': settings.CONSTANTS.messages.INACTIVE_ACCOUNT_ERROR,
'missing_account': settings.CONSTANTS.messages.EMAIL_NOT_FOUND,
'wrong_password': settings.CONSTANTS.messages.INVALID_PASSWORD_ERROR,
}

def validate(self, attrs):
password = attrs.get('password')
params = {settings.LOGIN_FIELD: attrs.get(settings.LOGIN_FIELD)}
self.user = User.objects.filter(**params).first()
if not self.user:
raise ValidationError(
{
settings.LOGIN_FIELD:
self.default_error_messages.get('missing_account')
},
)
elif not self.user.check_password(password):
raise ValidationError(
{
'password':
self.default_error_messages.get('wrong_password')
},
)
self.user = authenticate(
request=self.context.get('request'), **params, password=password
)
if self.user and self.user.is_active:
return attrs
raise ValidationError(
{
'not_active':
self.default_error_messages.get('inactive_account')
},
)
2 changes: 1 addition & 1 deletion infra_bt/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ server {
server_tokens off;
client_max_body_size 20M;

root /usr/share/nginx/html;
root /usr/share/nginx/html/;

location /api/ {
proxy_set_header Host $http_host;
Expand Down

0 comments on commit e59af37

Please sign in to comment.