2024-07-08 11:44:39 -04:00
|
|
|
from rest_framework import serializers
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
2024-11-29 14:41:13 -05:00
|
|
|
from adventures.models import Collection
|
2024-08-04 17:30:43 -04:00
|
|
|
from dj_rest_auth.serializers import PasswordResetSerializer
|
2024-07-08 11:44:39 -04:00
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
try:
|
|
|
|
from allauth.account import app_settings as allauth_account_settings
|
|
|
|
from allauth.account.adapter import get_adapter
|
|
|
|
from allauth.account.utils import setup_user_email
|
|
|
|
from allauth.socialaccount.models import EmailAddress
|
|
|
|
from allauth.utils import get_username_max_length
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError('allauth needs to be added to INSTALLED_APPS.')
|
|
|
|
|
|
|
|
class ChangeEmailSerializer(serializers.Serializer):
|
|
|
|
new_email = serializers.EmailField(required=True)
|
|
|
|
|
|
|
|
def validate_new_email(self, value):
|
|
|
|
user = self.context['request'].user
|
|
|
|
if User.objects.filter(email=value).exclude(pk=user.pk).exists():
|
|
|
|
raise serializers.ValidationError("This email is already in use.")
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2024-11-22 17:03:02 -05:00
|
|
|
|
2024-07-08 11:44:39 -04:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
UserModel = get_user_model()
|
|
|
|
from dj_rest_auth.serializers import UserDetailsSerializer
|
|
|
|
from .models import CustomUser
|
|
|
|
|
|
|
|
from rest_framework import serializers
|
|
|
|
from django.conf import settings
|
|
|
|
import os
|
|
|
|
|
|
|
|
class UserDetailsSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
User model w/o password
|
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def validate_username(username):
|
|
|
|
if 'allauth.account' not in settings.INSTALLED_APPS:
|
|
|
|
return username
|
|
|
|
|
|
|
|
from allauth.account.adapter import get_adapter
|
2024-09-11 09:42:53 -04:00
|
|
|
username = get_adapter().clean_username(username.lower()) # Convert username to lowercase
|
2024-07-08 11:44:39 -04:00
|
|
|
return username
|
|
|
|
|
|
|
|
class Meta:
|
2024-09-08 13:53:50 -04:00
|
|
|
extra_fields = ['profile_pic', 'uuid', 'public_profile']
|
2024-07-08 11:44:39 -04:00
|
|
|
profile_pic = serializers.ImageField(required=False)
|
2024-09-08 14:29:27 -04:00
|
|
|
|
2024-07-08 11:44:39 -04:00
|
|
|
if hasattr(UserModel, 'USERNAME_FIELD'):
|
|
|
|
extra_fields.append(UserModel.USERNAME_FIELD)
|
|
|
|
if hasattr(UserModel, 'EMAIL_FIELD'):
|
|
|
|
extra_fields.append(UserModel.EMAIL_FIELD)
|
|
|
|
if hasattr(UserModel, 'first_name'):
|
|
|
|
extra_fields.append('first_name')
|
|
|
|
if hasattr(UserModel, 'last_name'):
|
|
|
|
extra_fields.append('last_name')
|
|
|
|
if hasattr(UserModel, 'date_joined'):
|
|
|
|
extra_fields.append('date_joined')
|
|
|
|
if hasattr(UserModel, 'is_staff'):
|
|
|
|
extra_fields.append('is_staff')
|
2024-09-08 13:53:50 -04:00
|
|
|
if hasattr(UserModel, 'public_profile'):
|
|
|
|
extra_fields.append('public_profile')
|
2024-07-08 11:44:39 -04:00
|
|
|
|
|
|
|
class Meta(UserDetailsSerializer.Meta):
|
|
|
|
model = CustomUser
|
2024-09-08 13:53:50 -04:00
|
|
|
fields = UserDetailsSerializer.Meta.fields + ('profile_pic', 'uuid', 'public_profile')
|
2024-09-08 14:29:27 -04:00
|
|
|
|
2024-07-08 11:44:39 -04:00
|
|
|
model = UserModel
|
|
|
|
fields = ('pk', *extra_fields)
|
2024-08-06 09:35:18 -04:00
|
|
|
read_only_fields = ('email', 'date_joined', 'is_staff', 'is_superuser', 'is_active', 'pk')
|
2024-07-08 11:44:39 -04:00
|
|
|
|
2024-09-08 14:29:27 -04:00
|
|
|
def handle_public_profile_change(self, instance, validated_data):
|
|
|
|
"""Remove user from `shared_with` if public profile is set to False."""
|
|
|
|
if 'public_profile' in validated_data and not validated_data['public_profile']:
|
|
|
|
for collection in Collection.objects.filter(shared_with=instance):
|
|
|
|
collection.shared_with.remove(instance)
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
self.handle_public_profile_change(instance, validated_data)
|
|
|
|
return super().update(instance, validated_data)
|
|
|
|
|
|
|
|
def partial_update(self, instance, validated_data):
|
|
|
|
self.handle_public_profile_change(instance, validated_data)
|
|
|
|
return super().partial_update(instance, validated_data)
|
|
|
|
|
|
|
|
|
2024-07-08 11:44:39 -04:00
|
|
|
class CustomUserDetailsSerializer(UserDetailsSerializer):
|
|
|
|
|
|
|
|
|
|
|
|
class Meta(UserDetailsSerializer.Meta):
|
|
|
|
model = CustomUser
|
2024-09-06 23:19:44 -04:00
|
|
|
fields = UserDetailsSerializer.Meta.fields + ('profile_pic', 'uuid', 'public_profile')
|
2024-07-08 11:44:39 -04:00
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
representation = super().to_representation(instance)
|
|
|
|
if instance.profile_pic:
|
|
|
|
public_url = os.environ.get('PUBLIC_URL', 'http://127.0.0.1:8000').rstrip('/')
|
2024-08-13 11:09:49 -04:00
|
|
|
#print(public_url)
|
2024-07-08 11:44:39 -04:00
|
|
|
# remove any ' from the url
|
|
|
|
public_url = public_url.replace("'", "")
|
|
|
|
representation['profile_pic'] = f"{public_url}/media/{instance.profile_pic.name}"
|
2024-11-17 16:34:46 -05:00
|
|
|
del representation['pk'] # remove the pk field from the response
|
2024-07-08 11:44:39 -04:00
|
|
|
return representation
|