def initialize_studyrecord(self, request, queryset): '''初始化学习记录''' print('self,request,queryset', self, request, queryset) if len(queryset) > 1: return HttpResponse('只能选择一个') # enrollment关联着外键,_set反向查找拿到 new_obj_list = [] for enrollment_obj in queryset[0].from_class.enrollment_set.all(): print(enrollment_obj) # models.StudyRecord.objects.get_or_create( # student=enrollment_obj, # course_record=queryset[0], # attendance=0, # score=0, # ) new_obj_list.append( models.StudyRecord( student=enrollment_obj, course_record=queryset[0], attendance=0, score=0, )) #之前用get_or_create避免的重复修改的问题,但是使用bulk_create避免不了,只能接受错误 try: models.StudyRecord.objects.bulk_create(new_obj_list) #具有事务性,批量创建 except Exception as e: return HttpResponse('批量修改失败,有学生已经有对应的学习记录') # ?course_record__id__exact =带上过滤的的值,跳转过去只显示指定的值 return redirect( '/admin/app01/studyrecord/?course_record__id__exact=%s' % queryset[0].id)
def multi_init(self, request): """ 自定义批量初始化方法 :param request: :return: """ #上课记录ID列表 pk_list = request.POST.getlist("choice") #上课记录对象 record_list = models.CourseRecord.objects.filter(id__in=pk_list) for record in record_list: #上课记录对象 #第一天,第二天 exists = models.StudyRecord.objects.filter( course_record=record).exists() if exists: continue student_list = models.Student.objects.filter( class_list=record.class_obj) bulk_list = [] for student in student_list: #为每一个学生创建每一天的学习记录 bulk_list.append( models.StudyRecord(student=student, course_record=record)) models.StudyRecord.objects.bulk_create(bulk_list) return HttpResponse("添加成功")
def mutil_init(self,request): """自定义批量初始化方法""" # 上课记录id列表 pk_list = request.POST.getlist('pk') print(pk_list) # 上课记录对象列表 record_list = models.CourseRecord.objects.filter(id__in=pk_list) print(record_list) # print(record_list) # # 这种是,遍历每一个学生,查看是否存在记录。 # for record in record_list: # student_list = models.Student.objects.filter(class_list=record.class_obj) # bulk_list = [] # for student in student_list: # exists = models.StudyRecord.objects.filter(student=student,course_record=record).exists() # if exists: # continue # bulk_list.append(models.StudyRecord(student=student,course_record=record)) # models.StudyRecord.objects.bulk_create(bulk_list) # 下面这种是,只要有当天的学习记录,后面不管还有没有学生来,都不能添加 for record in record_list: if models.StudyRecord.objects.filter(course_record=record).exists(): continue student_list = models.Student.objects.filter(class_list=record.class_obj) # 为每一个学生创建dayn的学习记录 bulk_list = [] for student in student_list: bulk_list.append(models.StudyRecord(student=student,course_record=record)) models.StudyRecord.objects.bulk_create(bulk_list) # return redirect('/stark/app01/courserecord/') return HttpResponse('初始化成功!')
def courserecord_add(request): if request.method == 'POST': form = forms.CourseRecordModelForm(request.POST) print(form.is_valid()) print(request.POST) if form.is_valid(): #创建上课记录 new_cousrerecord = form.save() #批量生成该课程中的学员上课记录 course_id = request.POST['course'] course_day_num = request.POST['day_num'] #找出课程下的所有学员 customer_objs = models.Course.objects.filter( id=course_id)[0].customer_set.select_related() #创建上课记录 if customer_objs: new_studyrecord_objs = [] for customer in customer_objs: new_studyrecord_objs.append( models.StudyRecord(course_record=new_cousrerecord, student=customer)) models.StudyRecord.objects.bulk_create(new_studyrecord_objs) base_url = '/'.join(request.path.split('/')[:-2]) return redirect(base_url) else: form = forms.CourseRecordModelForm() return render(request, 'app01/courserecord_add.html', {'data_form': form})
def bulk_create_studyrecord(self, requset, recode_lst): """ 批量生成对应班级的学习记录(对应学习中的客户) @param requset: @param recode_lst: @return: """ msg = "" try: with transaction.atomic(): study = list() for course_id in recode_lst: course_obj = models.CourseRecord.objects.filter( id=course_id).first() students_objs = course_obj.re_class.customer_set.filter( status='studying') if not students_objs: raise TypeError("待添加对象不存在") for student_obj in students_objs: study_obj = models.StudyRecord( course_record_id=course_id, student=student_obj) study.append(study_obj) models.StudyRecord.objects.bulk_create(study) msg = 'success' return self.get(requset, msg) except IntegrityError: msg = 'filed' return self.get(requset, msg) except TypeError: msg = 'null' return self.get(requset, msg) except Exception as e: msg = 'error' return self.get(requset, msg)
def multu_init(self,request): id_list=request.POST.getlist('pk') CourseRecord_list=models.CourseRecord.objects.filter(id__in=id_list).all() for courseRecord in CourseRecord_list: exists=models.StudyRecord.objects.filter(course_record=courseRecord).exists() if exists: continue class_obj=courseRecord.class_obj student_list=models.Student.objects.filter(class_list=class_obj) studyrecord_list=[] for student in student_list: studyrecord= models.StudyRecord(course_record=courseRecord,student=student) studyrecord_list.append(studyrecord) models.StudyRecord.objects.bulk_create(studyrecord_list)
def post(self, request): print(request.POST) customer_ids = request.POST.getlist('customer_ids') student_obj = models.Student.objects.filter( customer_id__in=customer_ids) study_record_obj = [] for student in student_obj: study = models.StudyRecord( student=student, course_record=models.CourseRecord.objects.get( name_id=student.customer_id), ) study_record_obj.append(study) models.StudyRecord.objects.bulk_create(study_record_obj) return redirect(request.path)
def bluk_create_staudy_records(self, request, pk_data): with transaction.atomic(): course_recode_list = models.CourseRecord.objects.filter( id__in=pk_data) for course_record in course_recode_list: student_objs = course_record.re_class.customer_set.all( ).exclude(status="unregistered", ) obj_list = [] for student in student_objs: obj_list.append( models.StudyRecord(course_record=course_record, student=student)) models.StudyRecord.objects.bulk_create(obj_list) return JsonResponse({ "status": 1, "url": reverse("app01:course_record") })
def initialize_studyrecords(self, request, queryset): print('--->initialize_studyrecords', self, request, queryset) if len(queryset) > 1: return HttpResponse("只能选择一个班级") #queryset[0] 为上课记录 也就是课时 # 一次正向操作,课时表取班级 一次反向操作 班级表取报名对象 最终取到所有的报名学员 print(queryset[0].from_class.enrollment_set.all()) #get_or_create 有就不进行创建,没有才创建 create在有的情况下,是会唯一性 错误的 # for enroll_obj in queryset[0].from_class.enrollment_set.all(): # models.StudyRecord.objects.get_or_create( # student = enroll_obj, # course_record = queryset[0], # attendance = 0, # score = 0, # ) #不过上面方式存在问题,也就是get_or_create执行过程包括了commit,即是每次循环都操作了一次数据库,一旦数据量大了,就影响速度 new_obj_list = [] for enroll_obj in queryset[0].from_class.enrollment_set.all(): new_obj_list.append( models.StudyRecord( student=enroll_obj, course_record=queryset[0], attendance=0, score=0, )) try: models.StudyRecord.objects.bulk_create( new_obj_list) #批量创建,事物操作(原子性) except Exception as e: return HttpResponse('批量初始化学习记录失败,请检查该节课是否已经有对应的学习记录') return redirect( '/admin/app01/studyrecord/?course_record__id__exact=%s' % queryset[0].id)
def add(request): if request.method == 'POST': print("this is post") if request.POST['add_action'] == 'add_userprofile': print(request.POST) user_obj = User.objects.get(pk=int(request.POST['user'])) print(user_obj, type(user_obj)) form_data = { 'user': request.POST['user'], 'name': request.POST['name'] } request_form = myforms.UserProfileForm(form_data) if request_form.is_valid(): add_data = {'user': user_obj, 'name': request.POST['name']} new_profile = models.UserProfile(**add_data) new_profile.save() return HttpResponseRedirect('/index') else: print(request_form.errors) return render( request, 'error.html', { 'errors': request_form.errors, 'group': request.user.groups.all()[0].name }) elif request.POST['add_action'] == 'add_course': print(request.POST) form_data = { 'name': request.POST['name'], 'price': request.POST['price'], 'online_price': request.POST['online_price'], 'brief': request.POST['brief'], } request_form = myforms.CourseForm(form_data) if request_form.is_valid(): new_profile = models.Course(**form_data) new_profile.save() return HttpResponseRedirect('/index') else: print(request_form.errors) return render( request, 'error.html', { 'errors': request_form.errors, 'group': request.user.groups.all()[0].name }) elif request.POST['add_action'] == 'add_classlist': # teach=request.POST.getlist('teachers') # print(teach,type(teach)) form_data = { 'course': request.POST['course'], 'course_type': request.POST['course_type'], 'semester': request.POST['semester'], 'start_date_month': request.POST['start_date_month'], 'start_date_day': request.POST['start_date_day'], 'start_date_year': request.POST['start_date_year'], 'graduate_date_year': request.POST['graduate_date_year'], 'graduate_date_month': request.POST['graduate_date_month'], 'graduate_date_day': request.POST['graduate_date_day'], 'teachers': request.POST.getlist('teachers'), } request_form = myforms.ClassListForm(form_data) if request_form.is_valid(): course_obj = models.Course.objects.get( pk=int(request.POST['course'])) teachers_obj = [] for item in request.POST.getlist('teachers'): teacher_obj = models.UserProfile.objects.get(pk=int(item)) teachers_obj.append(teacher_obj) if int(request.POST['graduate_date_year']) == 0 or int( request.POST['graduate_date_month']) == 0 or int( request.POST['graduate_date_day']) == 0: graduate_date_value = None else: graduate_date_value = "%s-%s-%s" % ( int(request.POST['graduate_date_year']), int(request.POST['graduate_date_month']), int(request.POST['graduate_date_day'])) add_data = { 'course': course_obj, 'course_type': request.POST['course_type'], 'semester': int(request.POST['semester']), 'start_date': "%s-%s-%s" % (int(request.POST['start_date_year']), int(request.POST['start_date_month']), int(request.POST['start_date_day'])), 'graduate_date': graduate_date_value, } new_classlist = models.ClassList(**add_data) new_classlist.save() classlist_obj = models.ClassList.objects.filter( course=add_data['course'], course_type=add_data['course_type'], semester=add_data['semester']).first() for teacher_obj in teachers_obj: classlist_obj.teachers.add(teacher_obj) classlist_obj.save() return HttpResponseRedirect('/index') else: print(request_form.errors) return render( request, 'error.html', { 'errors': request_form.errors, 'group': request.user.groups.all()[0].name }) elif request.POST['add_action'] == 'add_customer': print("customer_data", request.POST) form_data = { 'qq': request.POST['qq'], 'name': request.POST['name'], 'phone': int(request.POST['phone']), 'stu_id': request.POST['stu_id'], 'source': request.POST['source'], 'referral_from': request.POST['referral_from'], # 可能需要修改 'course': request.POST['course'], 'class_type': request.POST['class_type'], 'customer_note': request.POST['customer_note'], 'status': request.POST['status'], 'consultant': request.POST['consultant'], #'date':request.POST['date'], auto_add 属性后前台页面不显示这个字段 'class_list': request.POST.getlist('class_list'), } request_form = myforms.CustomerForm(form_data) if request_form.is_valid(): if request.POST['referral_from'] == '': referral_num = 0 else: referral_num = int(request.POST['referral_from']) referral_obj = models.Customer.objects.filter( pk=referral_num).first() course_obj = models.Course.objects.get( pk=int(request.POST['course'])) consultant_obj = models.UserProfile.objects.get( pk=int(request.POST['consultant'])) class_list_obj = [] for item in request.POST.getlist('class_list'): clist_obj = models.ClassList.objects.get(pk=int(item)) class_list_obj.append(clist_obj) add_data = { 'qq': request.POST['qq'], 'name': request.POST['name'], 'phone': int(request.POST['phone']), 'stu_id': request.POST['stu_id'], 'source': request.POST['source'], 'referral_from': referral_obj, # 可能需要修改 'course': course_obj, 'class_type': request.POST['class_type'], 'customer_note': request.POST['customer_note'], 'status': request.POST['status'], 'consultant': consultant_obj, } new_customer = models.Customer(**add_data) new_customer.save() customer_obj = models.Customer.objects.filter( qq=add_data['qq']).first() for clist_obj in class_list_obj: customer_obj.class_list.add(clist_obj) customer_obj.save() return HttpResponseRedirect('/index') else: print(request_form.errors) return render( request, 'error.html', { 'errors': request_form.errors, 'group': request.user.groups.all()[0].name }) elif request.POST['add_action'] == 'add_consultrecord': print("consultant_data", request.POST) form_data = { 'customer': request.POST['customer'], 'note': request.POST['note'], 'status': request.POST['status'], 'consultant': request.POST['consultant'], #'date':request.POST['date'], auto_add 属性后前台页面不显示这个字段 } request_form = myforms.ConsultRecordForm(form_data) if request_form.is_valid(): customer_obj = models.Customer.objects.filter( pk=int(request.POST['customer'])).first() consultant_obj = models.UserProfile.objects.get( pk=int(request.POST['consultant'])) add_data = { 'customer': customer_obj, 'note': request.POST['note'], 'status': request.POST['status'], 'consultant': consultant_obj, } new_consultrecord = models.ConsultRecord(**add_data) new_consultrecord.save() return HttpResponseRedirect('/index') else: print(request_form.errors) return render( request, 'error.html', { 'errors': request_form.errors, 'group': request.user.groups.all()[0].name }) elif request.POST['add_action'] == 'add_courserecord': print("courserecord_data", request.POST) form_data = { 'course': request.POST['course'], 'day_num': int(request.POST['day_num']), 'teacher': request.POST['teacher'] } request_form = myforms.CourseRecordForm(form_data) if request_form.is_valid(): course_obj = models.ClassList.objects.filter( pk=int(request.POST['course'])).first() teacher_obj = models.UserProfile.objects.get( pk=int(request.POST['teacher'])) add_data = { 'course': course_obj, 'day_num': int(request.POST['day_num']), 'teacher': teacher_obj } new_courserecord = models.CourseRecord(**add_data) new_courserecord.save() return HttpResponseRedirect('/index') else: print(request_form.errors) return render( request, 'error.html', { 'errors': request_form.errors, 'group': request.user.groups.all()[0].name }) elif request.POST['add_action'] == 'add_studyrecord': print("studyrecord_data", request.POST) form_data = { 'course_record': request.POST['course_record'], 'student': request.POST['student'], 'record': request.POST['record'], 'score': int(request.POST['score']), 'note': request.POST['note'], } request_form = myforms.StudyRecordForm(form_data) if request_form.is_valid(): course_record_obj = models.CourseRecord.objects.filter( pk=int(request.POST['course_record'])).first() student_obj = models.Customer.objects.get( pk=int(request.POST['student'])) add_data = { 'course_record': course_record_obj, 'student': student_obj, 'record': request.POST['record'], 'score': int(request.POST['score']), 'note': request.POST['note'], } new_studyrecord = models.StudyRecord(**add_data) new_studyrecord.save() return HttpResponseRedirect('/index') else: print(request_form.errors) return render( request, 'error.html', { 'errors': request_form.errors, 'group': request.user.groups.all()[0].name }) else: print("this is get") if request.GET['action'] == 'add_userprofile': profiles = myforms.UserProfileForm() return render( request, "add.html", { 'addprofiles': profiles, 't_user_profile': 'true', 'group': request.user.groups.all()[0].name }) elif request.GET['action'] == 'add_course': course = myforms.CourseForm() return render( request, "add.html", { 'addcourses': course, 't_course': 'true', 'group': request.user.groups.all()[0].name }) elif request.GET['action'] == 'add_classlist': classlists = myforms.ClassListForm() return render( request, "add.html", { 'addclasslist': classlists, 't_classlist': 'true', 'group': request.user.groups.all()[0].name }) elif request.GET['action'] == 'add_customer': customers = myforms.CustomerForm() return render( request, "add.html", { 'addcustomer': customers, 't_customer': 'true', 'group': request.user.groups.all()[0].name }) elif request.GET['action'] == 'add_consultrecord': consultrecords = myforms.ConsultRecordForm() return render( request, "add.html", { 'addconsultrecord': consultrecords, 't_consultrecord': 'true', 'group': request.user.groups.all()[0].name }) elif request.GET['action'] == 'add_courserecord': courserecords = myforms.CourseRecordForm() return render( request, "add.html", { 'addcourserecord': courserecords, 't_courserecord': 'true', 'group': request.user.groups.all()[0].name }) elif request.GET['action'] == 'add_studyrecord': studyrecords = myforms.StudyRecordForm() return render( request, "add.html", { 'addstudyrecord': studyrecords, 't_studyrecord': 'true', 'group': request.user.groups.all()[0].name })