class ScheduleChangeSerializer(serializers.ModelSerializer): send_email_general = serializers.BooleanField(write_only=True, required=False) send_email_substitute = serializers.BooleanField(write_only=True, required=False) classes = FlatArrayField() teachers_replaced = ResponsibleSerializer(read_only=True, many=True) teachers_replaced_id = serializers.SlugRelatedField( queryset=ResponsibleModel.objects.all(), source='teachers_replaced', required=False, allow_null=True, many=True, slug_field='matricule') teachers_substitute = ResponsibleSerializer(read_only=True, many=True) teachers_substitute_id = serializers.SlugRelatedField( queryset=ResponsibleModel.objects.all(), source='teachers_substitute', required=False, allow_null=True, many=True, slug_field='matricule') class Meta: model = ScheduleChangeModel fields = '__all__' read_only_fields = ( 'datetime_created', 'datetime_modified', 'user', 'created_by', )
def serialize_people(person): if not person: return {} if type(person) == StudentModel: return StudentSerializer(person).data elif type(person) == ResponsibleModel: return ResponsibleSerializer(person).data
class AppelSerializer(serializers.ModelSerializer): # In order to write with the id and read the entire object, it uses two fields field + field_id. matricule = StudentSerializer(read_only=True) matricule_id = serializers.PrimaryKeyRelatedField( queryset=StudentModel.objects.all(), source='matricule', required=False, allow_null=True) responsible = ResponsibleSerializer(read_only=True) responsible_pk = serializers.PrimaryKeyRelatedField( queryset=ResponsibleModel.objects.all(), source='responsible', required=False, allow_null=True) motive = MotiveSerializer(read_only=True) motive_id = serializers.PrimaryKeyRelatedField( queryset=MotiveModel.objects.all(), source='motive', write_only=True) object = ObjectSerializer(read_only=True) object_id = serializers.PrimaryKeyRelatedField( queryset=ObjectModel.objects.all(), source='object', write_only=True) class Meta: model = Appel exclude = ( 'datetime_encodage', 'motif', 'objet', ) read_only_fields = ('user', )
class ActivitySerializer(serializers.ModelSerializer): responsibles = ResponsibleSerializer(read_only=True, many=True) responsibles_id = serializers.PrimaryKeyRelatedField( queryset=ResponsibleModel.objects.all(), source='responsibles', required=False, allow_null=True, many=True) competence = CompetenceSerializer(read_only=True, many=True) competence_id = serializers.PrimaryKeyRelatedField( queryset=models.CompetenceModel.objects.all(), source='competence', required=False, allow_null=True, many=True) class Meta: model = models.ActivityModel fields = "__all__"
class AbsenceProfSerializer(serializers.ModelSerializer): responsible = ResponsibleSerializer(read_only=True) responsible_id = serializers.PrimaryKeyRelatedField( queryset=ResponsibleModel.objects.all(), source='matricule', required=False, allow_null=True) status = serializers.ReadOnlyField() def validate(self, data): """Ensure date_asbence_start is before date_absence_end.""" print(data) if data["date_absence_start"] > data["date_absence_end"]: raise serializers.ValidationError( "La date de fin doit se trouver après la date de début.") return data class Meta: model = Absence exclude = ('user', )
def search_people(query, people_type, teachings, check_access, user, tenure_class_only=True, educ_by_years=True, active=True): if len(query) < 1: return [] truncate_limit = 50 people = [] if people_type == 'all': classe_years = get_classes(teachings, check_access=True, user=user) if check_access else None result = People().get_people_by_name(query, teachings, classes=classe_years, active=active) # Check quality. if result['student'][1] > result['responsible'][1]: people = StudentSerializer(result['student'][0][:truncate_limit], many=True).data elif result['responsible'][1] > result['student'][1]: people = ResponsibleSerializer( result['responsible'][0][:truncate_limit], many=True).data else: people = StudentSerializer( result['student'][0][:truncate_limit / 2], many=True).data + ResponsibleSerializer( result['responsible'][0][:truncate_limit / 2], many=True).data if people_type == 'student': classe_years = get_classes( teachings, check_access=True, user=user, tenure_class_only=tenure_class_only, educ_by_years=educ_by_years) if check_access else None if query == 'everybody': students = StudentModel.objects.all() if active: students = students.filter(inactive_from__isnull=True, classe__isnull=False) if classe_years: students = students.filter(classe__in=classe_years) if teachings and 'all' not in teachings: if type(teachings[0]) == TeachingModel: students = students.filter(teaching__in=teachings) else: students = students.filter(teaching__name__in=teachings) truncate_limit = len(students) else: students = People().get_students_by_name( query, teachings, classes=classe_years, active=active)[:truncate_limit] people = StudentSerializer(students, many=True).data if people_type == 'responsible': people = ResponsibleSerializer(People().get_responsibles_by_name( query, teachings, active=active)[:truncate_limit], many=True).data if people_type == 'teacher': people = ResponsibleSerializer(People().get_teachers_by_name( query, teachings, active=active)[:truncate_limit], many=True).data if people_type == 'educator': people = ResponsibleSerializer(People().get_educators_by_name( query, teachings, active=active)[:truncate_limit], many=True).data return people[:truncate_limit]