Пример #1
0
 def put(self, request, pk, format=None):
     person = self.get_object(pk)
     serializer = PersonSerializer(person, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #2
0
class StudentParentSerializer(serializers.ModelSerializer):
    person = PersonSerializer()
    classroom = ClassroomSerializer()

    class Meta:
        model = Student
        fields = '__all__'
Пример #3
0
class TeacherProfileSerializer(serializers.ModelSerializer):
    account_type = serializers.CharField(default='teacher')
    person = PersonSerializer()
    class Meta:
        model = Teacher
        fields = ['id', 'person', 'account_type']

    def update(self, instance, validated_data):
        try:
            update_person(instance.person, validated_data.pop('person'))
        except KeyError:
            pass

        instance.save()
        return instance
Пример #4
0
class TeacherSerializer(serializers.ModelSerializer):
    account = AccountSerializer()
    person = PersonSerializer()
    achievements = AchievementSerializer(required=False,
                                         allow_null=True,
                                         many=True)

    class Meta:
        model = Teacher
        fields = ['id', 'account', 'person', 'achievements', 'home_class']
        extra_kwargs = {'home_class': {'required': False}}

    def create(self, valiated_data):
        person_model = create_person(valiated_data.pop('person'))
        account_model = create_account(valiated_data.pop('account'))

        try:
            teacher = Teacher.objects.create(account=account_model,
                                             person=person_model,
                                             **valiated_data)
        except Exception:
            raise serializers.ValidationError(
                'Something wrong with your teacher information')

        return teacher

    def update(self, instance, valiated_data):
        try:
            update_person(instance.person, valiated_data.pop('person'))
        except KeyError:
            pass

        try:
            update_account(instance.account, valiated_data.pop('account'))
        except KeyError:
            pass

        instance.save()
        return instance
Пример #5
0
class ParentSerializer(serializers.ModelSerializer):
    person = PersonSerializer()
    student_id = serializers.PrimaryKeyRelatedField(
        source='students',
        many=True,
        queryset=Student.objects.all(),
        write_only=True)
    students = StudentParentSerializer(read_only=True, many=True)

    class Meta:
        model = Parent
        fields = ['id', 'person', 'student_id', 'avacation', 'students']

    def create(self, validated_data):
        logger.error(validated_data)
        person = create_person(validated_data.pop('person'))
        parent = Parent.objects.create(
            person=person, avacation=validated_data.pop('avacation'))
        for student in validated_data.pop('students'):
            parent.students.add(student)

        parent.save()
        return parent

    def update(self, instance, validated_data):
        try:
            update_person(instance.person, validated_data.pop('person'))
        except KeyError:
            pass

        instance.students.clear()
        for student in validated_data.pop('students'):
            instance.students.add(student)

        instance.save()
        return instance
Пример #6
0
class StudentSerializer(serializers.ModelSerializer):
    account = AccountSerializer()
    person = PersonSerializer()
    health = HealthSerializer(required=False, allow_null=True)
    classroom_id = serializers.IntegerField(write_only=True)
    classroom = ClassroomSerializer(read_only=True)
    parents = ParentSerializer(many=True, read_only=True)

    class Meta:
        model = Student
        fields = [
            'id', 'account', 'person', 'classroom_id', 'admission_year',
            'health', 'status', 'parents', 'classroom'
        ]

    def create(self, validated_data):
        person_model = create_person(validated_data.pop('person'))
        account_model = create_account(validated_data.pop('account'))
        try:
            student = Student.objects.create(account=account_model,
                                             person=person_model,
                                             **validated_data)
        except Exception:
            raise serializers.ValidationError(
                'Something wrong with your student information')

        try:
            create_health(validated_data.pop('health'))
        except KeyError:
            pass

        student.save()
        return student

    def update(self, instance, validated_data):
        try:
            update_account(instance.account, validated_data.pop('account'))
        except KeyError:
            pass

        try:
            update_person(instance.person, validated_data.pop('person'))
        except KeyError:
            pass

        try:
            if instance.health is None:
                health = create_health(validated_data.pop('health'))
                assign_health(instance, health)
            else:
                update_health(instance, validated_data.pop('health'))
        except:
            pass

        instance.classroom_id = validated_data.get('classroom_id',
                                                   instance.classroom_id)
        instance.status = validated_data.get('status', instance.status)
        instance.admission_year = validated_data.get('admission_year',
                                                     instance.admission_year)
        instance.save()
        return instance
Пример #7
0
 def get(self, request, format=None):
     persons = Person.objects.all()
     serializer = PersonSerializer(persons, many=True)
     return Response(serializer.data)
Пример #8
0
 def get(self, request, pk, format=None):
     person = self.get_object(pk)
     serializer = PersonSerializer(person)
     return Response(serializer.data)