2024-07-08 11:44:39 -04:00
|
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
from .serializers import ChangeEmailSerializer
|
|
|
|
from drf_yasg.utils import swagger_auto_schema
|
|
|
|
from drf_yasg import openapi
|
2024-08-16 12:21:43 -04:00
|
|
|
from django.conf import settings
|
2024-09-06 23:19:44 -04:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from .serializers import CustomUserDetailsSerializer as PublicUserSerializer
|
|
|
|
|
|
|
|
User = get_user_model()
|
2024-07-08 11:44:39 -04:00
|
|
|
|
|
|
|
class ChangeEmailView(APIView):
|
|
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
|
|
|
|
@swagger_auto_schema(
|
|
|
|
request_body=ChangeEmailSerializer,
|
|
|
|
responses={
|
|
|
|
200: openapi.Response('Email successfully changed'),
|
|
|
|
400: 'Bad Request'
|
|
|
|
},
|
|
|
|
operation_description="Change the email address for the authenticated user."
|
|
|
|
)
|
|
|
|
def post(self, request):
|
|
|
|
serializer = ChangeEmailSerializer(data=request.data, context={'request': request})
|
|
|
|
if serializer.is_valid():
|
|
|
|
user = request.user
|
|
|
|
new_email = serializer.validated_data['new_email']
|
|
|
|
user.email = new_email
|
2024-08-16 11:47:53 -04:00
|
|
|
# remove all other email addresses for the user
|
|
|
|
user.emailaddress_set.exclude(email=new_email).delete()
|
|
|
|
user.emailaddress_set.create(email=new_email, primary=True, verified=False)
|
2024-07-08 11:44:39 -04:00
|
|
|
user.save()
|
|
|
|
return Response({"detail": "Email successfully changed."}, status=status.HTTP_200_OK)
|
2024-08-16 12:21:43 -04:00
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
class IsRegistrationDisabled(APIView):
|
|
|
|
@swagger_auto_schema(
|
|
|
|
responses={
|
|
|
|
200: openapi.Response('Registration is disabled'),
|
|
|
|
400: 'Bad Request'
|
|
|
|
},
|
|
|
|
operation_description="Check if registration is disabled."
|
|
|
|
)
|
|
|
|
def get(self, request):
|
2024-08-24 23:20:50 -04:00
|
|
|
return Response({"is_disabled": settings.DISABLE_REGISTRATION, "message": settings.DISABLE_REGISTRATION_MESSAGE}, status=status.HTTP_200_OK)
|
2024-09-06 23:19:44 -04:00
|
|
|
|
|
|
|
class PublicUserListView(APIView):
|
|
|
|
# Allow the listing of all public users
|
|
|
|
permission_classes = []
|
|
|
|
|
|
|
|
@swagger_auto_schema(
|
|
|
|
responses={
|
|
|
|
200: openapi.Response('List of public users'),
|
|
|
|
400: 'Bad Request'
|
|
|
|
},
|
|
|
|
operation_description="List public users."
|
|
|
|
)
|
|
|
|
def get(self, request):
|
|
|
|
users = User.objects.filter(public_profile=True).exclude(id=request.user.id)
|
2024-09-10 09:59:59 -04:00
|
|
|
# remove the email addresses from the response
|
|
|
|
for user in users:
|
|
|
|
user.email = None
|
2024-09-06 23:19:44 -04:00
|
|
|
serializer = PublicUserSerializer(users, many=True)
|
|
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
class PublicUserDetailView(APIView):
|
|
|
|
# Allow the retrieval of a single public user
|
|
|
|
permission_classes = []
|
|
|
|
|
|
|
|
@swagger_auto_schema(
|
|
|
|
responses={
|
|
|
|
200: openapi.Response('Public user information'),
|
|
|
|
400: 'Bad Request'
|
|
|
|
},
|
|
|
|
operation_description="Get public user information."
|
|
|
|
)
|
|
|
|
def get(self, request, user_id):
|
|
|
|
user = get_object_or_404(User, uuid=user_id, public_profile=True)
|
2024-09-10 09:59:59 -04:00
|
|
|
# remove the email address from the response
|
|
|
|
user.email = None
|
2024-09-06 23:19:44 -04:00
|
|
|
serializer = PublicUserSerializer(user)
|
|
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
2024-11-29 14:41:13 -05:00
|
|
|
|
|
|
|
class UserMetadataView(APIView):
|
|
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
|
|
|
|
@swagger_auto_schema(
|
|
|
|
responses={
|
|
|
|
200: openapi.Response('User metadata'),
|
|
|
|
400: 'Bad Request'
|
|
|
|
},
|
|
|
|
operation_description="Get user metadata."
|
|
|
|
)
|
|
|
|
def get(self, request):
|
|
|
|
user = request.user
|
|
|
|
serializer = PublicUserSerializer(user)
|
|
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|