示例#1
0
 def put(self, request, teacher_id):
     try:
         token = self._get_token(request)
         if not token.user.is_staff:
             return RF_Response({'status': 'error', 'code': 'InsufficientAuthorization'},
                                status=status.HTTP_403_FORBIDDEN)
         try:
             teacher = Teacher.objects.get(id=teacher_id)
             data = request.data
             data_keys = data.keys()
             if 'first_name' in data_keys:
                 teacher.first_name = data['first_name']
             if 'last_name' in data_keys:
                 teacher.last_name = data['last_name']
             if 'email' in data_keys:
                 try:
                     validate_email(data['email'])
                     teacher.email = data['email']
                 except ValidationError:
                     return RF_Response({'status': 'error', 'code': 'InvalidEmail'},
                                        status=status.HTTP_422_UNPROCESSABLE_ENTITY)
             if 'phone_number' in data_keys:
                 try:
                     phone_number_validator(data['phone_number'])
                     teacher.phone_number = data['phone_number']
                 except ValidationError:
                     return RF_Response({'status': 'error', 'code': 'InvalidPhoneNumber'},
                                        status=status.HTTP_422_UNPROCESSABLE_ENTITY)
             if 'rank' in data_keys:
                 if data['rank'] not in ranks:
                     return RF_Response({'status': 'error', 'code': 'InvalidRank'},
                                        status=status.HTTP_422_UNPROCESSABLE_ENTITY)
                 teacher.rank = data['rank']
             if 'password' in data_keys:
                 try:
                     validate_password(data['new_password'])
                     teacher.set_password(data['new_password'])
                 except ValidationError:
                     return RF_Response({'status': 'error', 'code': 'PasswordTooSimple'},
                                        status=status.HTTP_422_UNPROCESSABLE_ENTITY)
             teacher.save()
             return RF_Response({'status': 'success'})
         except Teacher.DoesNotExist:
             return RF_Response({'status': 'error', 'code': 'InvalidID'}, status=status.HTTP_404_NOT_FOUND)
     except Token.DoesNotExist:
         return RF_Response({'status': 'error', 'code': 'InvalidCredentials'},
                            status=status.HTTP_401_UNAUTHORIZED)
     except AttributeError:
         return RF_Response({'status': 'error', 'code': 'InvalidCredentials'},
                            status=status.HTTP_401_UNAUTHORIZED)
示例#2
0
    def get(self, request, student_id):
        try:
            token = self._get_token(request)
            if not token.user.is_staff:
                return RF_Response({'status': 'error', 'code': 'InsufficientAuthorization'},
                                   status=status.HTTP_403_FORBIDDEN)
            try:
                student = Student.objects.get(id=student_id)

                def get_subjects() -> list:
                    subject_list = []
                    student_subjects = StudentSubject.objects.filter(student=student)
                    # TODO check this shit
                    for s in student_subjects:
                        subject_list.append(
                            {
                                'name': s.subject.name,
                                'group': s.group_number,
                            }
                        )
                    return subject_list

                subjects = get_subjects()

                def count_hours() -> int:
                    total = 0
                    return total

                student = {
                    'first_name': student.first_name,
                    'last_name': student.last_name,
                    'username': student.username,
                    'total_hours': count_hours(),
                    'subjects': subjects,
                }
                return RF_Response({'status': 'success', 'student': student})
            except Student.DoesNotExist:
                return RF_Response({'status': 'error', 'code': 'InvalidID'}, status=status.HTTP_404_NOT_FOUND)
        except Token.DoesNotExist:
            return RF_Response({'status': 'error', 'code': 'InvalidCredentials'},
                               status=status.HTTP_401_UNAUTHORIZED)
        except AttributeError:
            return RF_Response({'status': 'error', 'code': 'InvalidCredentials'},
                               status=status.HTTP_401_UNAUTHORIZED)
示例#3
0
 def get(self, request):
     try:
         token = self._get_token(request)
         if token.user.is_staff:
             return RF_Response(
                 {
                     'status': 'error',
                     'code': 'InsufficientAuthorization'
                 },
                 status=status.HTTP_403_FORBIDDEN)
         o = Occupancy.objects.filter(start_datetime__gte=datetime.now(
             tz=timezone(settings.TIME_ZONE))).order_by('start_datetime')[0]
         event = {
             'id': o.id,
             'group_name': f'Groupe {o.group_number}',
             'subject_name': o.subject.name,
             'teacher_name':
             f'{o.teacher.first_name} {o.teacher.last_name}',
             'start': o.start_datetime.timestamp(),
             'end': o.end_datetime.timestamp(),
             'occupancy_type': o.occupancy_type,
             'name': o.name,
         }
         if o.subject:
             event['class_name'] = o.subject._class.name
         if o.classroom:
             event['classroom_name'] = o.classroom.name
         return RF_Response({'status': 'success', 'occupancy': event})
     except Token.DoesNotExist:
         return RF_Response(
             {
                 'status': 'error',
                 'code': 'InvalidCredentials'
             },
             status=status.HTTP_401_UNAUTHORIZED)
     except AttributeError:
         return RF_Response(
             {
                 'status': 'error',
                 'code': 'InvalidCredentials'
             },
             status=status.HTTP_401_UNAUTHORIZED)
示例#4
0
    def get(self, request, classroom_id):
        try:
            token = self._get_token(request)
            if not token.user.is_staff:
                return RF_Response(
                    {
                        'status': 'error',
                        'code': 'InsufficientAuthorization'
                    },
                    status=status.HTTP_403_FORBIDDEN)
            try:
                classroom = Classroom.objects.get(id=classroom_id)

                classroom = {
                    'id': classroom.id,
                    'name': classroom.name,
                    'capacity': classroom.capacity,
                }
                return RF_Response({
                    'status': 'success',
                    'classroom': classroom
                })
            except Classroom.DoesNotExist:
                return RF_Response({
                    'status': 'error',
                    'code': 'InvalidID'
                },
                                   status=status.HTTP_404_NOT_FOUND)
        except Token.DoesNotExist:
            return RF_Response(
                {
                    'status': 'error',
                    'code': 'InvalidCredentials'
                },
                status=status.HTTP_401_UNAUTHORIZED)
        except AttributeError:
            return RF_Response(
                {
                    'status': 'error',
                    'code': 'InvalidCredentials'
                },
                status=status.HTTP_401_UNAUTHORIZED)
示例#5
0
 def delete(self, request, *args, **kwargs):
     try:
         token = self._get_token(request)
         if token.user:
             token.delete()
             logout(request)
             return RF_Response({'status': 'success'})
         return RF_Response(
             {
                 'status': 'error',
                 'code': 'InvalidCredentials'
             },
             status=status.HTTP_403_FORBIDDEN)
     except Token.DoesNotExist:
         return RF_Response(
             {
                 'status': 'error',
                 'code': 'InvalidCredentials'
             },
             status=status.HTTP_403_FORBIDDEN)
示例#6
0
    def post(self, request, *args, **kwargs):
        user = authenticate(request,
                            username=request.data['username'],
                            password=request.data['password'])
        if user is not None:

            def get_user_type():
                if user.is_staff:
                    return 'ADM'
                try:
                    Teacher.objects.get(id=user.id)
                    return 'TEA'
                except Teacher.DoesNotExist:
                    pass
                try:
                    Student.objects.get(id=user.id)
                    return 'STU'
                except Student.DoesNotExist:
                    pass

            token, created = Token.objects.get_or_create(user=user)
            response = {
                'status': 'success',
                'token': token.key,
                'user': {
                    'id': user.id,
                    'first_name': user.first_name,
                    'last_name': user.last_name,
                    'kind': get_user_type(),
                },
            }
            login(request, user)
            return RF_Response(response, status=status.HTTP_201_CREATED)
        else:
            return RF_Response(
                {
                    'status': 'error',
                    'code': 'InvalidCredentials'
                },
                status=status.HTTP_403_FORBIDDEN)
示例#7
0
    def delete(self, request, *args, **kwargs):
        try:
            token = self._get_token(request)
            if not token.user.is_staff:
                return RF_Response(
                    {
                        'status': 'error',
                        'code': 'InsufficientAuthorization'
                    },
                    status=status.HTTP_401_UNAUTHORIZED)

            def delete_classroom(classroom_id: int):
                classroom = Classroom.objects.get(id=classroom_id)
                classroom.delete()

            for post_id in request.data:
                try:
                    delete_classroom(post_id)
                except Classroom.DoesNotExist:
                    return RF_Response({
                        'status': 'error',
                        'code': 'InvalidID'
                    },
                                       status=status.HTTP_404_NOT_FOUND)

            return RF_Response({'status': 'success'})
        except Token.DoesNotExist:
            return RF_Response(
                {
                    'status': 'error',
                    'code': 'InvalidCredentials'
                },
                status=status.HTTP_401_UNAUTHORIZED)
        except AttributeError:
            return RF_Response(
                {
                    'status': 'error',
                    'code': 'InvalidCredentials'
                },
                status=status.HTTP_401_UNAUTHORIZED)
示例#8
0
 def put(self, request, *args, **kwargs):
     try:
         token = self._get_token(request)
         correct_old_password = token.user.check_password(
             request.data['old_password'])
         if not correct_old_password:
             return RF_Response(
                 {
                     'status': 'error',
                     'code': 'InvalidOldPassword'
                 },
                 status=status.HTTP_403_FORBIDDEN)
         user = User.objects.get(id=token.user.id)
         try:
             validate_password(request.data['new_password'])
             user.set_password(request.data['new_password'])
         except ValidationError:
             return RF_Response(
                 {
                     'status': 'error',
                     'code': 'PasswordTooSimple'
                 },
                 status=status.HTTP_422_UNPROCESSABLE_ENTITY)
     except Token.DoesNotExist:
         return RF_Response(
             {
                 'status': 'error',
                 'code': 'InvalidCredentials'
             },
             status=status.HTTP_401_UNAUTHORIZED)
     except AttributeError:
         return RF_Response(
             {
                 'status': 'error',
                 'code': 'InvalidCredentials'
             },
             status=status.HTTP_401_UNAUTHORIZED)
    def delete(self, request, occupancy_id):
        try:
            token = self._get_token(request)
            if not token.user.is_staff:
                return RF_Response(
                    {
                        'status': 'error',
                        'code': 'InsufficientAuthorization'
                    },
                    status=status.HTTP_401_UNAUTHORIZED)

            try:
                occupancy = Occupancy.objects.get(id=occupancy_id)
                occupancy.delete()

                return RF_Response({'status': 'success'})
            except Occupancy.DoesNotExist:
                return RF_Response({
                    'status': 'error',
                    'code': 'InvalidID'
                },
                                   status=status.HTTP_404_NOT_FOUND)
        except Token.DoesNotExist:
            return RF_Response(
                {
                    'status': 'error',
                    'code': 'InvalidCredentials'
                },
                status=status.HTTP_401_UNAUTHORIZED)
        except AttributeError:
            return RF_Response(
                {
                    'status': 'error',
                    'code': 'InvalidCredentials'
                },
                status=status.HTTP_401_UNAUTHORIZED)
示例#10
0
 def put(self, request, student_id):
     try:
         token = self._get_token(request)
         if not token.user.is_staff:
             return RF_Response(
                 {
                     'status': 'error',
                     'code': 'InsufficientAuthorization'
                 },
                 status=status.HTTP_403_FORBIDDEN)
         try:
             occupancy = Occupancy.objects.get(id=student_id)
             data = request.data
             data_keys = data.keys()
             if 'classroom_id' in data_keys:
                 try:
                     classroom = Classroom.objects.get(
                         id=data['classroom_id'])
                     occupancy._class = classroom
                 except Class.DoesNotExist:
                     return RF_Response(
                         {
                             'status': 'error',
                             'code': 'InvalidID'
                         },
                         status=status.HTTP_404_NOT_FOUND)
             if 'start' in data_keys:
                 occupancy.last_name = data['start']
             if 'end' in data_keys:
                 occupancy.duration = data['end'] - occupancy.start_datetime
             if 'name' in data_keys:
                 occupancy.name = data['name']
             occupancy.save()
             return RF_Response({'status': 'success'})
         except Occupancy.DoesNotExist:
             return RF_Response({
                 'status': 'error',
                 'code': 'InvalidID'
             },
                                status=status.HTTP_404_NOT_FOUND)
     except Token.DoesNotExist:
         return RF_Response(
             {
                 'status': 'error',
                 'code': 'InvalidCredentials'
             },
             status=status.HTTP_401_UNAUTHORIZED)
     except AttributeError:
         return RF_Response(
             {
                 'status': 'error',
                 'code': 'InvalidCredentials'
             },
             status=status.HTTP_401_UNAUTHORIZED)
示例#11
0
 def put(self, request, student_id):
     try:
         token = self._get_token(request)
         if not token.user.is_staff:
             return RF_Response({'status': 'error', 'code': 'InsufficientAuthorization'},
                                status=status.HTTP_403_FORBIDDEN)
         try:
             student = Student.objects.get(id=student_id)
             data = request.data
             data_keys = data.keys()
             if 'first_name' in data_keys:
                 student.first_name = data['first_name']
             if 'last_name' in data_keys:
                 student.last_name = data['last_name']
             if 'class_id' in data_keys:
                 try:
                     _class = Class.objects.get(id=data['class_id'])
                     student._class = _class
                 except Class.DoesNotExist:
                     return RF_Response({'status': 'error', 'code': 'InvalidID'}, status=status.HTTP_404_NOT_FOUND)
             if 'password' in data_keys:
                 try:
                     validate_password(data['new_password'])
                     student.set_password(data['new_password'])
                 except ValidationError:
                     return RF_Response({'status': 'error', 'code': 'PasswordTooSimple'},
                                        status=status.HTTP_422_UNPROCESSABLE_ENTITY)
             student.save()
             return RF_Response({'status': 'success'})
         except Student.DoesNotExist:
             return RF_Response({'status': 'error', 'code': 'InvalidID'}, status=status.HTTP_404_NOT_FOUND)
     except Token.DoesNotExist:
         return RF_Response({'status': 'error', 'code': 'InvalidCredentials'},
                            status=status.HTTP_401_UNAUTHORIZED)
     except AttributeError:
         return RF_Response({'status': 'error', 'code': 'InvalidCredentials'},
                            status=status.HTTP_401_UNAUTHORIZED)
示例#12
0
    def get(self, request, teacher_id):
        try:
            token = self._get_token(request)
            if not token.user.is_staff:
                return RF_Response({'status': 'error', 'code': 'InsufficientAuthorization'},
                                   status=status.HTTP_403_FORBIDDEN)
            try:
                teacher = Teacher.objects.get(id=teacher_id)

                def get_subjects() -> list:
                    subject_list = []
                    student_subjects = TeacherSubject.objects.filter(teacher=teacher)
                    # TODO check this shit
                    for ss in student_subjects:
                        def get_subject_teachers() -> list:
                            teachers = []
                            teacher_subjects = TeacherSubject.objects.filter(subject=ss.subject)
                            for t in teacher_subjects:
                                teachers.append(
                                    {
                                        'first_name': t.teacher.first_name,
                                        'last_name': t.teacher.last_name,
                                        'in_charge': t.in_charge,
                                        'email': t.teacher.email,
                                        'phone_number': t.teacher.phone_number,
                                    }
                                )
                            return teachers

                        def get_subject_groups() -> list:
                            groups = []
                            for i in range(1, ss.subject.group_count + 1):
                                groups.append(
                                    {
                                        'number': i,
                                        'name': f'Groupe {i}',
                                        'count': group_size(i),
                                    }
                                )
                            return groups

                        subject_list.append(
                            {
                                'id': ss.subject.id,
                                'name': ss.subject.name,
                                'class_name': ss.subject._class.name,
                                'teachers': get_subject_teachers(),
                                'groups': get_subject_groups(),
                            }
                        )
                    return subject_list

                response = {
                    'status': 'success',
                    'subjects': get_subjects(),
                }
                return RF_Response(response)
            except Teacher.DoesNotExist:
                return RF_Response({'status': 'error', 'code': 'InvalidID'}, status=status.HTTP_404_NOT_FOUND)
        except Token.DoesNotExist:
            return RF_Response({'status': 'error', 'code': 'InvalidCredentials'},
                               status=status.HTTP_401_UNAUTHORIZED)
        except AttributeError:
            return RF_Response({'status': 'error', 'code': 'InvalidCredentials'},
                               status=status.HTTP_401_UNAUTHORIZED)
示例#13
0
    def get(self, request, teacher_id):
        try:
            token = self._get_token(request)
            if not token.user.is_staff:
                return RF_Response({'status': 'error', 'code': 'InsufficientAuthorization'},
                                   status=status.HTTP_403_FORBIDDEN)
            try:
                teacher = Teacher.objects.get(id=teacher_id)

                def get_days() -> list:
                    start_timestamp = request.query_params.get('start', None)
                    end_timestamp = request.query_params.get('end', None)
                    nb_per_day = int(request.query_params.get('occupancies_per_day', 0))

                    days = []
                    occ = Occupancy.objects.filter(
                        teacher=teacher,
                        deleted=False
                    ).order_by('start_datetime').annotate(day=Trunc('start_datetime', 'day'))
                    if start_timestamp:
                        occ = occ.filter(
                            start_datetime__gte=datetime.fromtimestamp(
                                int(start_timestamp),
                                tz=timezone(settings.TIME_ZONE)
                            )
                        )
                    if end_timestamp:
                        occ = occ.filter(
                            end_datetime__lte=datetime.fromtimestamp(
                                int(end_timestamp),
                                tz=timezone(settings.TIME_ZONE)
                            )
                        )
                    for day in (occ[0].day + timedelta(n) for n in
                                range((occ[len(occ) - 1].day - occ[0].day).days + 2)):
                        day_occupancies = occ.filter(start_datetime__day=day.day)
                        if len(day_occupancies) == 0:
                            continue
                        if nb_per_day != 0:
                            day_occupancies = day_occupancies[:nb_per_day]
                        occ_list = []
                        for o in day_occupancies:
                            event = {
                                'id': o.id,
                                'group_name': f'Groupe {o.group_number}',
                                'subject_name': o.subject.name,
                                'teacher_name': f'{o.teacher.first_name} {o.teacher.last_name}',
                                'start': o.start_datetime.timestamp(),
                                'end': o.end_datetime.timestamp(),
                                'occupancy_type': o.occupancy_type,
                                'name': o.name,
                            }
                            if o.subject:
                                event['class_name'] = o.subject._class.name
                            if o.classroom:
                                event['classroom_name'] = o.classroom.name
                            occ_list.append(event)
                        days.append({'date': day.strftime("%d-%m-%Y"), 'occupancies': occ_list})
                    return days

                return RF_Response({'status': 'success', 'days': get_days()})
            except Teacher.DoesNotExist:
                return RF_Response({'status': 'error', 'code': 'InvalidID'}, status=status.HTTP_404_NOT_FOUND)
        except Token.DoesNotExist:
            return RF_Response({'status': 'error', 'code': 'InvalidCredentials'},
                               status=status.HTTP_401_UNAUTHORIZED)
        except AttributeError:
            return RF_Response({'status': 'error', 'code': 'InvalidCredentials'},
                               status=status.HTTP_401_UNAUTHORIZED)
示例#14
0
    def get(self, request, teacher_id):
        try:
            token = self._get_token(request)
            if not token.user.is_staff:
                return RF_Response({'status': 'error', 'code': 'InsufficientAuthorization'},
                                   status=status.HTTP_403_FORBIDDEN)
            try:
                teacher = Teacher.objects.get(id=teacher_id)

                def get_services() -> list:
                    services = []
                    # TODO check this shit
                    teacher_subjects = TeacherSubject.objects.filter(teacher=teacher)
                    for ts in teacher_subjects:
                        service_entry = {'class': teacher_subjects.subject._class.name}
                        for s_type in service_list:
                            try:
                                occupancies = Occupancy.objects.get(teacher=teacher, occupancy_type=s_type,
                                                                    subject=ts.subject)
                            except Occupancy.DoesNotExist:
                                service_entry[occupancy_types[s_type]] = 0
                                continue
                            total = 0
                            for occ in occupancies:
                                total += occ.duration / 3600.
                            service_entry[occupancy_types[s_type]] = total
                    return services

                service_list = get_services()

                def count_service_value() -> int:
                    coefficients = get_service_coefficients()
                    total = 0
                    for entry in service_list:
                        for k, v in coefficients:
                            total += entry[occupancy_types[k]] * float(v)
                    return total

                try:
                    teacher = {
                        'first_name': teacher.first_name,
                        'last_name': teacher.last_name,
                        'username': teacher.username,
                        'email': teacher.email,
                        'phone_number': teacher.phone_number,
                        'rank': teacher.rank,
                        'total_services': count_service_value(),
                        'services': service_list,
                    }
                except TeacherSubject.DoesNotExist:
                    teacher = {
                        'first_name': teacher.first_name,
                        'last_name': teacher.last_name,
                        'username': teacher.username,
                        'email': teacher.email,
                        'phone_number': teacher.phone_number,
                        'rank': teacher.rank,
                        'total_services': 0,
                        'services': [],
                    }
                return RF_Response({'status': 'success', 'teacher': teacher})
            except Teacher.DoesNotExist:
                return RF_Response({'status': 'error', 'code': 'InvalidID'}, status=status.HTTP_404_NOT_FOUND)
        except Token.DoesNotExist:
            return RF_Response({'status': 'error', 'code': 'InvalidCredentials'},
                               status=status.HTTP_401_UNAUTHORIZED)
        except AttributeError:
            return RF_Response({'status': 'error', 'code': 'InvalidCredentials'},
                               status=status.HTTP_401_UNAUTHORIZED)
示例#15
0
 def get(self, request):
     try:
         token = self._get_token(request)
         if token.user.is_staff:
             return RF_Response(
                 {
                     'status': 'error',
                     'code': 'InsufficientAuthorization'
                 },
                 status=status.HTTP_403_FORBIDDEN)
         try:
             student = Student.objects.get(id=token.user.id)
             occ_modifications = OccupancyModification.objects.filter(
                 occupancy__subject__studentsubject__student=student
             ).order_by('-modification_date')[:25]
             modifications = []
             for occ in occ_modifications:
                 occupancy = {
                     'subject_name':
                     occ.occupancy.subject.name
                     if occ.occupancy.occupancy_type != 'EXT' else None,
                     'class_name':
                     occ.occupancy.subject._class.name
                     if occ.occupancy.occupancy_type != 'EXT' else None,
                     'occupancy_type':
                     occ.occupancy.occupancy_type,
                     'occupancy_start':
                     occ.new_start_datetime.timestamp(),
                     'occupancy_end': (occ.new_start_datetime +
                                       occ.new_duration).timestamp(),
                 }
                 if occ.modification_type == 'EDITION':
                     occupancy[
                         'previous_occupancy_start'] = occ.previous_start_datetime.timestamp(
                         )
                     occupancy['previous_occupancy_end'] = (
                         occ.previous_start_datetime +
                         occ.previous_duration).timestamp()
                 modifications.append({
                     'modification_type':
                     occ.modification_type,
                     'modification_timestamp':
                     occ.modification_date.timestamp(),
                     'occupancy':
                     occupancy
                 })
             return RF_Response({
                 'status': 'success',
                 'modification': modifications
             })
         except Student.DoesNotExist:
             pass
     except Token.DoesNotExist:
         return RF_Response(
             {
                 'status': 'error',
                 'code': 'InvalidCredentials'
             },
             status=status.HTTP_401_UNAUTHORIZED)
     except AttributeError:
         return RF_Response(
             {
                 'status': 'error',
                 'code': 'InvalidCredentials'
             },
             status=status.HTTP_401_UNAUTHORIZED)