Exemplo n.º 1
0
    def post(self, request, format=None):
        '''To add students in a classroom by the faculty.
		Takes the classroom_id, students(list)
		return the Classroomobject with list of student objects in that classroom'''
        classroom_id = request.data.get('id')
        try:
            classroom = Classroom.objects.get(id=classroom_id)
            if request.user.username == classroom.creator.username:
                student_ids = request.data.get('students')
                student_users = [
                    User.objects.get(username=student_id)
                    for student_id in student_ids
                ]
                classroom.students.add(*student_users)
                classroom.save()
                serialized_classroom = ClassroomSerializer(classroom,
                                                           many=False).data
                serialized_students = UserSerializer(classroom.students.all(),
                                                     many=True).data
                serialized_classroom['students'] = serialized_students
                return Response(serialized_classroom)
            else:
                return Response(
                    {
                        "error":
                        "You are not authorized to add students to this classroom."
                    },
                    status=status.HTTP_403_FORBIDDEN)
        except Exception as e:
            return Response({"error": "Query does not exists."},
                            status=status.HTTP_403_FORBIDDEN)
class NotificationSerializer(ModelSerializer):
    actor = UserSerializer(many=False, required=False)
    receiver = UserSerializer(many=False, required=False)

    class Meta:
        model = Notification
        fields = [
            'id',
            'actor',
            'receiver',
            'notification_type',
            'text',
            'read',
            'created_at',
        ]
        read_only_fields = ['id', 'created_at']
Exemplo n.º 3
0
class StorageSerializer(ModelSerializer):
    user = UserSerializer(required=False)

    class Meta:
        model = Storage
        fields = ['id', 'user', 'limit', 'created_date']
        read_only_fields = ['id', 'created_date', 'limit']
Exemplo n.º 4
0
    def put(self, request, format=None):
        '''To remove moderators in a classroom by the faculty.
		Takes the classroom_id, moderators(list)
		return the Classroomobject with list of student objects in that classroom'''
        classroom_id = request.data.get('classroom_id')
        try:
            classroom = Classroom.objects.get(id=classroom_id)
            if request.user.username == classroom.creator.username:
                moderator_ids = request.data.get('moderators_to_remove')
                moderator_users = [
                    User.objects.get(username=moderator_id)
                    for moderator_id in moderator_ids
                ]
                classroom.moderators.remove(*moderator_users)
                classroom.save()
                serialized_classroom = ClassroomSerializer(classroom,
                                                           many=False).data
                serialized_students = UserSerializer(
                    classroom.moderators.all(), many=True).data
                serialized_classroom['moderators'] = serialized_students
                return Response(serialized_classroom)
            return Response(
                {"error": "You are not authorized to remove moderators."},
                status=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            return Response({"error": "Classroom query doesn't exists."},
                            status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 5
0
class UploadDocumentSerializer(ModelSerializer):
    uploader = UserSerializer(required=False)

    class Meta:
        model = UploadDocument
        fields = ['id', 'fileName', 'document', 'uploader', 'uploaded_on']
        read_only_fields = ['id', 'fileName', 'uploaded_on']
Exemplo n.º 6
0
class CommentSerializer(ModelSerializer):
	commenter = UserSerializer(many=False, required=False)
	upvoters = UserSerializer(many=True, required=False)
	downvoters = UserSerializer(many=True, required=False)

	class Meta:
		model = Comment
		fields = [
			'id',
			'comment_text',
			'commenter',
			'created_at',
			'upvoters',
			'downvoters',
			'parent'
			]
		read_only_fields = ['id', 'created_at', 'commenter']
class ClassroomStudentSerializer(ModelSerializer):
	students = UserSerializer(required=True)

	class Meta:
		model = Classroom
		fields = [
			'students'
			]
Exemplo n.º 8
0
class PollSerializer(ModelSerializer):
    creater = UserSerializer(many=False, required=True)
    classroom = ClassroomSerializer(many=False, required=False)

    class Meta:
        model = Poll
        fields = ['id', 'creater', 'classroom', 'poll_text', 'created_at']
        read_only_fields = ['id', 'created_at']
Exemplo n.º 9
0
class AssignmentSerializer(ModelSerializer):
    uploader = UserSerializer(required=False)
    classroom = ClassroomSerializer(required=False, write_only=True)

    class Meta:
        model = Assignment
        fields = [
            'id', 'title', 'uploader', 'classroom', 'attachment', 'max_score',
            'deadline', 'created_on'
        ]
        read_only_fields = ['id', 'created_on']
class TransactionSerializer(ModelSerializer):
    buyer = UserSerializer()
    item = ItemSerializer()

    class Meta:
        model = Transaction
        fields = [
            'id', 'buyer', 'item', 'status', 'paymentOption', 'created_on'
        ]

    read_only_fields = ['id', 'created_on']
Exemplo n.º 11
0
class SubmissionSerializer(ModelSerializer):
    submitter = UserSerializer(required=False)
    assignment_id = serializers.CharField(source='assignment.id')

    class Meta:
        model = Submission
        fields = [
            'id', 'submitter', 'assignment_id', 'attachment', 'score',
            'submitted_on'
        ]

        read_only_fields = ['id', 'assignment_id', 'submitted_on']
class FarmSerializer(ModelSerializer):

    farmer = UserSerializer()

    class Meta:
        model = Farm
        fields = [
            'id', 'farmer', 'farm_name', 'about', 'farmImage', 'longitude',
            'latitude', 'created_on'
        ]

    read_only_fields = ['id', 'created_on']
Exemplo n.º 13
0
class ResourceSerializer(ModelSerializer):
    uploader = UserSerializer(required=False)
    classroom = ClassroomSerializer(required=False, write_only=True)
    classroom_id = serializers.CharField(source='classroom.id')

    class Meta:
        model = Resource
        fields = [
            'id', 'uploader', 'classroom_id', 'classroom', 'attachment',
            'description', 'is_lecture', 'uploaded_on'
        ]
        read_only_fields = ['id', 'uploaded_on', 'classroom_id']
class ClassroomModeratorSerializer(ModelSerializer):
	moderators = UserSerializer(required=True)

	class Meta:
		model = Classroom
		fields = [
			'moderators'
			]



			
Exemplo n.º 15
0
class PollResponseSerializer(ModelSerializer):
    user = UserSerializer(many=False, required=False)
    poll_option = PollOptionSerializer(many=False, required=False)

    # poll = PollSerializer(many=False, required=False)
    class Meta:
        model = PollResponse
        fields = [
            'id',
            'user',
            # 'poll',
            'poll_option',
            'created_at',
        ]
        read_only_fields = ['id', 'created_at']
    def get(self, request, format=None):
        '''To get comments with upvoters, downvoters and its child.
		Takes the comment_id.
		return the comment object with list of upvoters, list of downvoters and list of its child'''
        try:
            comment = Comment.objects.get(id=request.GET.get('id'))
            print(comment)
            child_comments = comment.child_comment.all()
            upvoters = comment.upvoters.all()
            downvoters = comment.downvoters.all()
            serialized_comments = CommentSerializer(child_comments,
                                                    many=True).data
            serializer = CommentSerializer(comment, many=False).data
            serialized_upvotes = UserSerializer(upvoters, many=True).data
            serialized_downvoters = UserSerializer(downvoters, many=True).data

            serializer['child'] = serialized_comments
            serializer['upvoters'] = serialized_upvotes
            serializer['downvoters'] = serialized_downvoters
            print(serializer)
            return Response(serializer)
        except Exception as e:
            return Response({"error": "Comment query doesn't exists."},
                            status=status.HTTP_400_BAD_REQUEST)
class AnnouncementSerializer(ModelSerializer):
	announcer = UserSerializer(many=False, required=False)
	classroom = ClassroomSerializer(many=False, required=False, write_only=True)

	class Meta:
		model = Announcement
		fields = [
			'id',
			'content',
			'announcer',
			'classroom',
			'commentCount',
			'created_on',
			]
		read_only_fields = ['id', 'created_on', 'commentCount']
class ClassroomSerializer(ModelSerializer):
	# username = serializers.CharField(source='creator.username')
	creator = UserSerializer(required=False,many=False)
	class Meta:
		model = Classroom
		fields = [
			'id',
			'name',
			'code',
			'creator',
			'description',
			'image',
			'created_at',
			'is_active'
			]
		read_only_fields = ['id', 'created_at', 'image']
Exemplo n.º 19
0
    def get(self, request, format=None):
        '''To get moderators in a classroom.
		Takes the classroom_id
		return the list of moderator objects'''
        classroom_id = request.GET.get('id')
        try:
            classroom = Classroom.objects.get(id=classroom_id)
            if request.user.username == classroom.creator.username or request.user in classroom.moderators.all(
            ) or request.user in classroom.students.all():
                moderators = classroom.moderators.all()
                serialized_moderators = UserSerializer(moderators, many=True)
                return Response(serialized_moderators.data)
            else:
                return Response(
                    {"error": "You are not authorized to access this data."},
                    status=status.HTTP_403_FORBIDDEN)
        except Exception as e:
            return Response({"error": "Classroom query does not exists."},
                            status=status.HTTP_403_FORBIDDEN)
class ItemSerializer(ModelSerializer):

    seller = UserSerializer()

    class Meta:
        model = Item
        fields = [
            'id',
            'seller',
            'product_title',
            'product_description',
            'product_price',
            'product_quantity',
            'itemImage',
            'longitude',
            'latitude',
            'sold',
            'created_on',
        ]

    read_only_fields = ['id', 'created_on']