Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User field notifications #32

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions backend/api/serializers/admin_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class AdminSerializer(serializers.ModelSerializer):

faculties = serializers.HyperlinkedRelatedField(
many=True,
read_only=True,
view_name='faculty-detail'
)

class Meta:
model = Admin
Expand Down
6 changes: 6 additions & 0 deletions backend/api/serializers/assistant_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class AssistantSerializer(serializers.ModelSerializer):
read_only=True,
)

faculties = serializers.HyperlinkedRelatedField(
many=True,
read_only=True,
view_name='faculty-detail'
)

class Meta:
model = Assistant
fields = [
Expand Down
6 changes: 6 additions & 0 deletions backend/api/serializers/student_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class StudentSerializer(serializers.ModelSerializer):
read_only=True,
)

faculties = serializers.HyperlinkedRelatedField(
many=True,
read_only=True,
view_name='faculty-detail'
)

class Meta:
model = Student
fields = [
Expand Down
6 changes: 6 additions & 0 deletions backend/api/serializers/teacher_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class TeacherSerializer(serializers.ModelSerializer):
read_only=True,
)

faculties = serializers.HyperlinkedRelatedField(
many=True,
read_only=True,
view_name='faculty-detail'
)

class Meta:
model = Teacher
fields = [
Expand Down
28 changes: 22 additions & 6 deletions backend/authentication/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.contrib.auth.models import update_last_login
from rest_framework.serializers import CharField, EmailField, ModelSerializer, ValidationError, Serializer
from rest_framework.serializers import (
CharField, EmailField, ModelSerializer, ValidationError,
Serializer, HyperlinkedIdentityField, HyperlinkedRelatedField
)
from rest_framework_simplejwt.tokens import RefreshToken, AccessToken
from rest_framework_simplejwt.settings import api_settings
from authentication.signals import user_created, user_login
Expand Down Expand Up @@ -65,23 +68,36 @@ def validate(self, data):
'refresh': str(RefreshToken.for_user(user))
}


class UserSerializer(ModelSerializer):
"""Serializer for the user model
This serializer validates the user fields for creation and updating.
"""
id = CharField()
username = CharField()
email = EmailField()

email = EmailField()

faculties = HyperlinkedRelatedField(
many=True,
read_only=True,
view_name='faculty-detail'
)

notifications = HyperlinkedIdentityField(
view_name='notification-detail',
read_only=True,
)

class Meta:
model = User
fields = [
'id', 'username', 'email',
'first_name', 'last_name',
'faculty',
'last_enrolled', 'last_login', 'create_time'
'faculties',
'last_enrolled', 'last_login', 'create_time',
'notifications'
]

def get_or_create(self, validated_data: dict) -> User:
"""Create or fetch the user based on the validated data."""
return User.objects.get_or_create(**validated_data)
return User.objects.get_or_create(**validated_data)