Exemplo n.º 1
0
 def action_multi_init(self, request, *args, **kwargs):
     class_id = kwargs.get("class_id")
     course_record_id_list = request.POST.getlist("pk")
     class_obj = models.ClassList.objects.filter(id=class_id).first()
     if not class_obj:
         return HttpResponse("没有找到班级")
     student_object_list = class_obj.student_set.all()
     for course_record_id in course_record_id_list:
         # 判断上课记录,是否合法。 是否有这个上课记录,班级记录是否正确。 如果为空的话,说明 课程记录的id 和 班级id 没有对上.
         # 就说明,这条记录是,不合法的
         courserecord_obj = models.CourseRecord.objects.filter(
             id=course_record_id, class_object_id=class_id).first()
         if not courserecord_obj:
             continue
         # 判断此上课记录的,学生考勤记录是否已经存在。 如果已存在就跳过。
         study_record_exists = models.StudyRecord.objects.filter(
             course_record=courserecord_obj).first()
         if study_record_exists:
             continue
         # 为每个学员,在这一天创建上课纪律
         # for stu in student_object_list:
         #     models.StudyRecord.objects.create(course_record_id=course_record_id, student_id=stu.id)
         study_record_object_list = [
             models.StudyRecord(course_record_id=course_record_id,
                                student_id=stu.id)
             for stu in student_object_list
         ]  # 此种方式时创建了,很多的上课纪律的对象。
         models.StudyRecord.objects.bulk_create(study_record_object_list,
                                                batch_size=50)
Exemplo n.º 2
0
    def action_multi_init(self, request, *args, **kwargs):
        course_record_id_list = request.POST.getlist('pk')  # 选择的考勤记录id
        class_id = kwargs.get('class_id')
        class_obj = models.ClassList.objects.filter(id=class_id).first()

        if not class_obj:
            return HttpResponse('班级不存在!')
        student_obj_list = class_obj.student_set.all()  # 反向关联,查询班级的学生

        for course_record in course_record_id_list:

            # 判断对应班级的上课记录是否存在
            course_record_obj = models.CourseRecord.objects.filter(
                id=course_record, class_object_id=class_id).first()

            if not course_record_obj:
                continue

            # 判断对应的考勤记录是否存在
            student_record_obj = models.StudyRecord.objects.filter(
                course_record=course_record_obj).exists()
            if student_record_obj:
                continue

            # 创建考勤记录
            student_record_obj_list = [
                models.StudyRecord(student_id=stu.id,
                                   course_record_id=course_record)
                for stu in student_obj_list
            ]

            models.StudyRecord.objects.bulk_create(student_record_obj_list,
                                                   batch_size=50)

        return
Exemplo n.º 3
0
    def select_multi_delete(self, request, *args, **kwargs):
        #  获取考勤记录班级的id
        class_id = kwargs.get('class_id')
        #  获取批量POST提交的每个考勤id
        course_record_id_list = request.POST.getlist('pk')
        #  获取每个班级的对象
        class_list_obj = models.ClassList.objects.filter(id=class_id).first()
        if not class_list_obj:
            return HttpResponse('班级不存在')
        #  获取班级里的每个学生, (班级反向关联)
        student_object_list = class_list_obj.student_set.all()

        for course_record_id in course_record_id_list:
            #  判断上课记录是否合法, 上课记录id 与班级id 需相同
            course_record_obj = models.CourseRecord.objects.filter(id=course_record_id,
                                                                   class_object_id=class_id).first()
            if not class_list_obj:
                continue
            #  判断考勤记录是否存在
            study_record_exists = models.StudyRecord.objects.filter(course_record=course_record_obj).exists()
            if study_record_exists:
                continue
            # 生成考勤记录
            study_record_object_list = [models.StudyRecord(student_id=stu.id, course_record_id=course_record_id) for stu
                                        in student_object_list]

            models.StudyRecord.objects.bulk_create(study_record_object_list, batch_size=50)
Exemplo n.º 4
0
    def action_multi_init(self, request, *args, **kwargs):
        course_record_id_list = request.POST.getlist('pk')
        class_id = kwargs.get('class_id')
        class_object = models.ClassList.objects.filter(id=class_id).first()
        if not class_object:
            return HttpResponse('班级不存在')
        student_object_list = class_object.student_set.all()

        for course_record_id in course_record_id_list:
            # 判断上课记录是否合法
            course_record_object = models.CourseRecord.objects.filter(
                id=course_record_id, class_object_id=class_id).first()
            if not course_record_object:
                continue

            # 判断此上课记录的考勤记录是否已经存在
            study_record_exists = models.StudyRecord.objects.filter(
                course_record=course_record_object).exists()
            if study_record_exists:
                continue

            # 为每个学生在该天创建考勤记录
            study_record_object_list = [
                models.StudyRecord(student_id=stu.id,
                                   course_record_id=course_record_id)
                for stu in student_object_list
            ]

            models.StudyRecord.objects.bulk_create(study_record_object_list,
                                                   batch_size=50)
Exemplo n.º 5
0
    def action_multi_init(self, request, *args, **kwargs):
        """添加批量操作action"""
        course_record_id_list = request.POST.getlist('pk')  # 班级所有课程
        class_id = kwargs.get('class_id')
        class_obj = models.ClassList.objects.filter(id=class_id).first()
        if not class_obj:
            return HttpResponse('班级不存在')
        student_obj_list = class_obj.student_set.all()  # 取班级的所有学生
        for course_record_id in course_record_id_list:
            course_record_obj = models.CourseRecord.objects.filter(
                id=course_record_id, class_object_id=class_id).first()
            if not course_record_obj:
                # 上课记录不存在
                continue

            study_record_exists = models.StudyRecord.objects.filter(
                course_record=course_record_obj).exists()
            if study_record_exists:
                # 课记录考勤记录已经存在
                continue

            # 为每个学生在该天创建考勤记录
            '''创建对象到内存'''
            study_record_object_list = [
                models.StudyRecord(student_id=stu.id,
                                   course_record_id=course_record_id)
                for stu in student_obj_list
            ]
            '''提交到数据库'''
            models.StudyRecord.objects.bulk_create(study_record_object_list,
                                                   batch_size=50)  # 一次提交50个
Exemplo n.º 6
0
 def multi_add_record(self, request, *args, **kwargs):
     """批量添加学生考勤记录"""
     # 1、获取所有的上课记录id
     class_record_pk_list = request.POST.getlist('pk')
     # 获取班级id
     class_id = kwargs.get("class_id")
     # 获取班级对象及判断是否存在
     class_obj = models.ClassList.objects.filter(id=class_id).first()
     if not class_obj:
         return HttpResponse("班级不存在!")
     # 获取此班级下的所有学生
     students_queryset = class_obj.student_set.all()
     # 判断当前记录对应的记录id及班级id是否存在
     for class_record_pk in class_record_pk_list:
         record_obj = models.CourseRecord.objects.filter(
             id=class_record_pk, class_object__id=class_id).first()
         if not record_obj:
             continue
         # 判断当前天数的考勤记录是否存在,存在则不添加
         status = models.StudyRecord.objects.filter(
             course_record__id=class_record_pk).exists()
         if status:
             continue
         # 单条添加记录
         # for stu in students_queryset:
         # 	models.StudyRecord.objects.create(course_record_id=class_record_pk, student_id=stu.id)
         # 一次性向数据库写入多条记录
         study_obj_list = [
             models.StudyRecord(course_record_id=class_record_pk,
                                student_id=stu.id)
             for stu in students_queryset
         ]
         models.StudyRecord.objects.bulk_create(study_obj_list)
Exemplo n.º 7
0
    def action_multi_course_init(self, request, *args, **kwargs):
        pk_list = request.POST.getlist('pk')
        class_id = kwargs.get('class_id')

        class_object = models.ClassList.objects.filter(id=class_id).first()
        if not class_object:
            return HttpResponse('班级不存在')
        student_obj_list = class_object.student_set.all()

        for course_id in pk_list:
            course_obj = models.CourseRecord.objects.filter(
                id=course_id, class_object_id=class_object.id).first()
            if not course_obj:
                continue
            study_record_exists = models.StudyRecord.objects.filter(
                course_record=course_obj).exists()
            if study_record_exists:
                continue
            study_record_obj_list = [
                models.StudyRecord(student_id=stu.id,
                                   course_record_id=course_id)
                for stu in student_obj_list
            ]

            models.StudyRecord.objects.bulk_create(study_record_obj_list,
                                                   batch_size=100)
Exemplo n.º 8
0
    def action_multi_init(self, request, *args, **kwargs):
        course_record_id_list = request.POST.getlist('pk')
        class_id = kwargs.get('class_id')

        class_obj = models.ClassList.objects.filter(id=class_id).first()
        if not class_obj:
            HttpResponse('班级不存在')
        # 班级里所有学生列表
        student_object_list = class_obj.student_set.all()

        for course_record_id in course_record_id_list:
            # 判断上课记录是否合法,防止不法分子篡改上课记录的id
            course_record_object = models.CourseRecord.objects.filter(
                id=course_record_id, class_object_id=class_id).first()
            if not course_record_object:
                continue

            # 判断此上课记录的考勤记录是否已经存在
            study_record_exists = models.StudyRecord.objects.filter(
                course_record=course_record_object).exists()
            if study_record_exists:
                continue

            # # 为每个学生在该天创建考勤记录
            # for stu in student_object_list:
            #     models.StudyRecord.objects.create(student_id=stu.id, course_record_id=course_record_id)

            # 只创建对象,没有在数据库提交
            study_record_object_list = [
                models.StudyRecord(student_id=stu.id,
                                   course_record_id=course_record_id)
                for stu in student_object_list
            ]
            # 在数据库创建
            models.StudyRecord.objects.bulk_create(study_record_object_list,
                                                   batch_size=50)