u
This commit is contained in:
parent
64ce1b2293
commit
c140533954
4 changed files with 52 additions and 6 deletions
|
|
@ -12,9 +12,14 @@ from rest_framework.response import Response
|
||||||
from .validation_error import CustomValidationError
|
from .validation_error import CustomValidationError
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
|
|
||||||
|
class UserSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = ['id', 'email', 'full_name']
|
||||||
|
|
||||||
|
|
||||||
class CustomLoginSerializer(LoginSerializer):
|
class CustomLoginSerializer(LoginSerializer):
|
||||||
|
|
||||||
email = serializers.EmailField(required=True)
|
email = serializers.EmailField(required=True)
|
||||||
|
|
|
||||||
|
|
@ -13,4 +13,5 @@ urlpatterns = [
|
||||||
name='password_reset_confirm',
|
name='password_reset_confirm',
|
||||||
),
|
),
|
||||||
path('change-email/', views.ChangeEmailView.as_view(), name='change_email'),
|
path('change-email/', views.ChangeEmailView.as_view(), name='change_email'),
|
||||||
|
path('user-info/', views.UserView.as_view()),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,43 @@ from allauth.account.models import EmailConfirmation, EmailConfirmationHMAC, Ema
|
||||||
from rest_framework.permissions import AllowAny, IsAuthenticated
|
from rest_framework.permissions import AllowAny, IsAuthenticated
|
||||||
from .serializers import ChangeEmailSerializer
|
from .serializers import ChangeEmailSerializer
|
||||||
from asgiref.sync import sync_to_async
|
from asgiref.sync import sync_to_async
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from .validation_error import CustomSuccessResponse, CustomValidationError
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
|
class UserView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def get(self, request):
|
||||||
|
user = request.user
|
||||||
|
image_url = request.build_absolute_uri(user.image.url) if user.image else None
|
||||||
|
return CustomSuccessResponse({
|
||||||
|
"name": user.full_name,
|
||||||
|
"image": image_url
|
||||||
|
})
|
||||||
|
|
||||||
|
def patch(self, request):
|
||||||
|
print(request.data)
|
||||||
|
user = request.user
|
||||||
|
full_name = request.data.get('full_name')
|
||||||
|
|
||||||
|
if full_name:
|
||||||
|
user.full_name = full_name
|
||||||
|
|
||||||
|
profile_image = request.FILES.get('profile_image')
|
||||||
|
if profile_image:
|
||||||
|
user.image = profile_image
|
||||||
|
print("Ok")
|
||||||
|
user.save()
|
||||||
|
|
||||||
|
return CustomSuccessResponse(
|
||||||
|
{"ok"},
|
||||||
|
code=status.HTTP_200_OK
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ChangeEmailView(APIView):
|
class ChangeEmailView(APIView):
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -299,19 +299,22 @@ class EnrollmentViewSet(ModelViewSet):
|
||||||
return CustomSuccessResponse(f"Student {student.full_name} has been added",
|
return CustomSuccessResponse(f"Student {student.full_name} has been added",
|
||||||
code=status.HTTP_201_CREATED)
|
code=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
raise CustomValidationError(serializer.errors, code=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
@action(detail=False, methods=['get'], url_path='get-my-students')
|
@action(detail=False, methods=['get'], url_path='get-my-students')
|
||||||
def get_my_students(self, request):
|
def get_my_students(self, request):
|
||||||
"""
|
"""
|
||||||
fetch detailed information about my students in my courses.
|
fetch detailed information about my students in my courses.
|
||||||
"""
|
"""
|
||||||
course = request.query_params('course')
|
course = request.query_params.get('course')
|
||||||
my_courses = Course.objects.filter(owner=request.user, id=course)
|
my_courses = Course.objects.filter(owner=request.user, id=course)
|
||||||
my_students = Enrollment.objects.filter(course__in=my_courses).values('student').distinct()
|
my_students = (
|
||||||
|
Enrollment.objects.filter(course__in=my_courses)
|
||||||
|
.values('student__full_name', 'student__email')
|
||||||
|
.distinct()
|
||||||
|
)
|
||||||
|
|
||||||
serializer = CourseSerializer(my_students, many=True)
|
return CustomSuccessResponse(list(my_students), code=200)
|
||||||
return Response(serializer.data)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue