Skip to content

Commit

Permalink
UPdate new allauth settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kovacspe committed Nov 11, 2023
1 parent 1c7c0f9 commit eaeec47
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 39 deletions.
41 changes: 11 additions & 30 deletions user/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from allauth.account.adapter import get_adapter
from allauth.account.models import EmailAddress
from allauth.account.utils import setup_user_email
from allauth.utils import email_address_exists
from django.contrib.auth import authenticate, get_user_model
from django.core.mail import send_mail
from django.template.loader import render_to_string
Expand Down Expand Up @@ -106,10 +106,12 @@ def update(self, instance, validated_data):

# Update Profile
# Nie všetky polia v modeloch User a Profile sú editovateľné cez API.
instance.profile.nickname = profile_data.get(
'nickname', instance.profile.nickname)
instance.profile.phone = profile_data.get(
'phone', instance.profile.phone)
instance.profile.fisrt_name = profile_data.get(
'first_name', instance.profile.first_name)
instance.profile.last_name = profile_data.get(
'last_name', instance.profile.last_name)
instance.profile.parent_phone = profile_data.get(
'parent_phone', instance.profile.parent_phone)
instance.profile.gdpr = profile_data.get(
Expand All @@ -121,13 +123,17 @@ def update(self, instance, validated_data):

# User sa nikdy neupdatuje preto nie je potrebné volať instance.save()
instance.profile.save()
self.handle_other_school(validated_data.pop('new_school_description'))
instance.save()
self.handle_other_school(validated_data.pop(
'new_school_description', None))
return instance

def handle_other_school(self, school):
'''
Ak je zadana skola "ina skola" tak posle o tom mail.
'''
if school is None:
return
if school.code == self.OTHER_SCHOOL_CODE:
email = self.validated_data['email']
first_name = self.validated_data['profile']['first_name']
Expand Down Expand Up @@ -169,7 +175,7 @@ class Meta:

def validate_email(self, email):
email = get_adapter().clean_email(email)
if email and email_address_exists(email):
if email and EmailAddress.objects.filter(email__iexact=email).exists():
raise serializers.ValidationError(
"Používateľ s danou emailovou adresou už existuje.")
return email
Expand Down Expand Up @@ -215,7 +221,6 @@ def save(self, request):
Profile.objects.create(user=user,
first_name=profile_data['first_name'],
last_name=profile_data['last_name'],
nickname=profile_data['nickname'],
school=profile_data['school'],
year_of_graduation=grade.get_year_of_graduation_by_date(),
phone=profile_data['phone'],
Expand All @@ -226,27 +231,3 @@ def save(self, request):
setup_user_email(request, user, [])

return user

def handle_other_school(self, school):
'''
Ak je zadana skola "ina skola" tak posle o tom mail.
'''
if school.code == self.OTHER_SCHOOL_CODE:
email = self.validated_data['email']
first_name = self.validated_data['profile']['first_name']
last_name = self.validated_data['profile']['last_name']
school_info = self.validated_data['new_school_description']
send_mail(
'Žiadosť o pridanie novej školy',
render_to_string(
'user/emails/new_school_request.txt',
{
'email': email,
'first_name': first_name,
'last_name': last_name,
'school_info': school_info
},
),
EMAIL_NO_REPLY,
[EMAIL_ALERT]
)
7 changes: 3 additions & 4 deletions user/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

app_name = 'user'

urlpatterns = [
path('', include('dj_rest_auth.urls')),
path('registration/', include('dj_rest_auth.registration.urls')),
]
urlpatterns = [path('', include('dj_rest_auth.urls')),
path('registration/', include('dj_rest_auth.registration.urls')),
]
8 changes: 3 additions & 5 deletions webstrom/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,10 @@

REST_SESSION_LOGIN = True

REST_AUTH_SERIALIZERS = {
'LOGIN_SERIALIZER': 'user.serializers.LoginSerializer',
'USER_DETAILS_SERIALIZER': 'user.serializers.UserDetailsSerializer'
}

REST_AUTH_REGISTER_SERIALIZERS = {
REST_AUTH = {
'LOGIN_SERIALIZER': 'user.serializers.LoginSerializer',
'USER_DETAILS_SERIALIZER': 'user.serializers.UserDetailsSerializer',
'REGISTER_SERIALIZER': 'user.serializers.RegisterSerializer'
}

Expand Down

0 comments on commit eaeec47

Please sign in to comment.