def class_edit(request, class_id): if request.POST.has_key('edit'): cid = request.POST['class_id'] cls = models.SchoolClass.objects.get(pk=cid) form = forms.ClassForm(request.POST, instance=cls) if form.is_valid(): grade = cls.get_grade() form.save() log.log_change(request.user, cls, u'修改班级') cache.clear_grade_classes(request.session['school'], grade) return HttpResponseRedirect('/class/list/%d/' % grade) elif request.POST.has_key('delete'): cid = request.POST['class_id'] cls = models.SchoolClass.objects.get(pk=cid) for u in cls.userprofile_set.all(): u.myclass_id = None u.save() cls.delete() log.log_deletion(request.user, cls, u'删除班级') cache.clear_grade_classes(request.session['school'], cls.get_grade()) return HttpResponseRedirect('/class/list/%d/' % cls.get_grade()) else: cls = models.SchoolClass.objects.get(pk=class_id) form = forms.ClassForm(instance=cls) ctx = RequestContext(request, { 'form': form, 'class': cls, }) return render_to_response('change_class.html', ctx)
def semester_list(request): level = 0 if request.POST.has_key('add'): form = forms.SemesterForm(request.POST) level = int(request.POST['level']) + 1 if form.is_valid(): semester = models.Semester.objects.create( name=form.cleaned_data['name'], start_date=form.cleaned_data['start_date'], end_date=form.cleaned_data['end_date'], region_id=request.session['region'], school_id=request.session.get('school')) log.log_addition(request.user, semester, u'添加学期') ctx = RequestContext(request, { 'level': -level, }) return render_to_response('history_goto.html', ctx) elif request.POST.has_key('delete'): ids = request.POST.getlist('selected') models.Semester.objects.filter(id__in=ids).delete() log.log_deletion(request.user, None, u'删除学期') form = forms.SemesterForm() else: form = forms.SemesterForm() semesters = db_util.get_semesters(request.session['region'], request.session.get('school')) ctx = RequestContext(request, { 'form': form, 'semesters': semesters, 'level': level }) return render_to_response('semester_list.html', ctx)
def class_list(request, grade): grades = common_def.get_all_grades(request.session['school_type'], True) #grade = int(request.GET.get('grade', '0')) grade = int(grade) form = None if request.POST.has_key('grade'): form = forms.GradeForm(request.POST) form.fields['grade'].choices = grades form.full_clean() if form.is_valid(): grade = int(form.cleaned_data['grade']) elif request.POST.has_key('delete'): ids = request.POST.getlist('selected') for id in ids: clas = models.SchoolClass.objects.get(id=id) clas.delete() log.log_deletion(request.user, clas, u'删除班级') cache.clear_grade_classes(request.session['school'], grade) # if not grade: # grade = grades[0][0] if not form: form = forms.GradeForm(initial={'grade': grade}) form.fields['grade'].choices = grades year = common_util.get_grade_year(grade) classes = models.SchoolClass.objects.filter( school=request.session['school'], year=year, is_active=True) ctx = RequestContext(request, { 'classes': classes, 'form': form, 'grade': grade, }) return render_to_response('class_list.html', ctx)
def region_list(request): if request.POST.has_key('search'): form = forms.SearchRegionForm(request.POST) if form.is_valid(): s = form.cleaned_data['name'] t = form.cleaned_data['kind'] else: s = request.POST.get('name', '') t = request.POST.get('kind', 'name') page = 1 elif request.POST.has_key('delete'): ids = request.POST.getlist('selected') regions = models.Region.objects.select_for_update().filter(id__in=ids) for region in regions: log.log_deletion(request.user, region, u'删除区域') regions.update(is_active=False) s = request.POST['find'] t = request.POST['kind'] page = request.POST['page'] else: s = '' t = 'name' page = '1' form = forms.SearchRegionForm(initial={'name': s, 'kind': t}) regions = db_util.search_region(s, t) ctx = RequestContext( request, { 'regions': regions, 'form': form, 'search': s, 'search_type': t, 'page': page, }) return render_to_response('region_list.html', ctx)
def teacher_edit(request): if request.POST.has_key('edit'): form = forms.TeacherForm(request.POST) form.fields['password'].required = False form.fields['password1'].required = False uid = request.POST['uid'] user = User.objects.get(pk=uid) if form.is_valid(): password = form.cleaned_data['password'] if password: user.set_password(password) user.username = form.cleaned_data['username'] user.first_name = form.cleaned_data['name'] user.email = form.cleaned_data['email'] profile = user.userprofile profile.gender = form.cleaned_data['gender'] profile.telphone = form.cleaned_data['telphone'] profile.contact = json.dumps( { 'tel': profile.telphone, 'email': user.email }, ensure_ascii=False) with transaction.commit_on_success(): user.save() profile.save() log.log_change(request.user, user, u'修改教师') return HttpResponseRedirect('/teacher/list/') elif request.POST.has_key('delete'): uid = request.POST['uid'] user = User.objects.get(pk=uid) user.delete() log.log_deletion(request.user, user, u'删除教师') return HttpResponseRedirect('/teacher/list/') else: uid = request.GET['uid'] profile = models.UserProfile.objects.select_related( 'user', 'myclass').get(user__id=uid) data = { 'username': profile.user.username, 'name': profile.user.first_name, #'en_name': user.last_name, 'gender': profile.gender, 'email': profile.user.email, 'telphone': profile.telphone, } form = forms.TeacherForm(data) form.fields['password'].required = False form.fields['password1'].required = False ctx = RequestContext(request, { 'form': form, 'uid': uid, }) return render_to_response('change_teacher.html', ctx)
def courseware_delete(request, token, uid, courseware_id): try: courseware = models.Courseware.objects.get(pk=courseware_id) if not db_util.is_course_managable_by( courseware.grade, courseware.course_id, request.user): ret = {'result': 'fail', 'message': u'对不起,您无权管理该课程的课件'} else: course_ware.delete_courseware(courseware) log.log_deletion(request.user, courseware, u'删除课件') ret = {'result': 'ok'} except Exception, e: ret = {'result': 'error', 'message': str(e)}
def courseware_detail(request, courseware_id): try: if request.method == 'GET' and courseware_id == '0': grade = int(request.GET['grade']) course_id = request.GET['course_id'] classes = db_util.get_teacher_classes(request.user, grade, course_id) course_name = request.GET['course_name'] if not classes: return _error_page(request, u'对不起,您无权管理 %s 课程的课件' % course_name) courseware = _dummy_courseware(grade, course_id, course_name) form = forms.CoursewareForm(initial={'classes': [c[0] for c in classes], 'share': common_def.SHARE_LEVEL_SCHOOL}) form.refresh_choices(classes) newware = True elif request.POST.has_key('add'): return _add_courseware(request) else: courseware = models.Courseware.objects.select_related('book_provider', 'category', 'course').get(id=courseware_id) newware = False classes = db_util.get_teacher_classes(request.user, courseware.grade, courseware.course_id) if not classes: return _error_page(request, u'对不起,您无权管理 %s 课程的课件' % courseware.course_name) if request.POST.has_key('edit'): form = _edit_courseware(courseware, request, classes) elif request.POST.has_key('delete'): log.log_deletion(request.user, courseware, u'删除课件') course_ware.delete_courseware(courseware) return HttpResponseRedirect('/coursewares/%d/%d/' % (courseware.course_id, courseware.grade)) else: data = { 'title': courseware.name, #'grade': courseware.grade, 'provider': courseware.book_provider_id, 'category': courseware.category_id, 'description': courseware.description, 'share': courseware.share_level, 'week': courseware.week, 'classes': db_util.get_courseware_classes(courseware), } form = forms.CoursewareForm(data, {'file': os.path.basename(courseware.path),}) form.refresh_choices(classes) form.files['file'] = os.path.basename(courseware.path) if hasattr(courseware.image, 'path'): form.files['image'] = os.path.basename(courseware.image.path) except palmtao.LicenseException, e: return e.response
def admin_list(request): if request.POST.has_key('delink'): ids = request.POST.getlist('selected') for id in ids: user = User.objects.get(id=id) db_util.delink_device(user) #course_ware.delete_coursewares(user) log.log_change(request.user, user, u'解除设备绑定') s = request.POST['find'] page = request.POST['page'] elif request.POST.has_key('delete'): for id in request.POST.getlist('selected'): user = User.objects.get(id=id) user.delete() log.log_deletion(request.user, user, u'删除管理员') s = request.POST['find'] page = request.POST['page'] elif request.POST.has_key('search'): form = forms.NameForm(request.POST) if form.is_valid(): s = form.cleaned_data['name'] else: s = request.POST.get('find', '') page = 1 else: s = request.GET.get('username', '') page = request.GET.get('page', '1') form = forms.NameForm({'name': s}) usertype = request.session['user_type'] upper_ut = usertype + 1 if usertype == common_def.USERTYPE_SYSTEM_ADMIN else usertype profiles = models.UserProfile.objects.select_related().filter( usertype__gt=common_def.USERTYPE_TEACHER, usertype__lt=upper_ut, user__is_active=True) if s: profiles = profiles.filter(user__username__icontains=s) ctx = RequestContext( request, { 'profiles': profiles.order_by('user__username'), 'form': form, 'username': s, 'page': page }) return render_to_response('admin_list.html', ctx)
def region_edit(request, region_id): region = models.Region.objects.get(pk=region_id) if request.POST.has_key('edit'): form = forms.RegionForm(request.POST) if form.is_valid(): region.name = form.cleaned_data['name'] region.code = form.cleaned_data['code'] region.save() log.log_change(request.user, region, u'修改区域') return HttpResponseRedirect('/region/list/') elif request.POST.has_key('delete'): region.is_active = False region.save() log.log_deletion(request.user, region, u'删除区域') return HttpResponseRedirect('/region/list/') else: form = forms.RegionForm(instance=region) ctx = RequestContext(request, {'form': form, 'region': region}) return render_to_response('change_region.html', ctx)
def coursewares(request, course_id, grade): grade = int(grade) course = models.Course.objects.get(pk=course_id) #course.course_name = common_def.get_grade_display(grade)+course.course_name if not db_util.is_course_managable_by(grade, course_id, request.user): return _error_page(request, u'对不起,您无权管理 %s%s 课程' % (common_def.get_grade_display(grade), course.course_name)) if request.POST.has_key('delete'): for id in request.POST.getlist('selected_courseware'): courseware = models.Courseware.objects.get(id=id) course_ware.delete_courseware(courseware) log.log_deletion(request.user, courseware, u'删除课件') coursewares = course_ware.coursewares_of_course(course_id, grade, user=request.user).order_by('-id') ctx = RequestContext(request, { 'course': course, 'grade': grade, 'coursewares': coursewares, 'selected': 'selected_courseware' }) return render_to_response('courseware_page.html', ctx)
def edit_course(request): if request.POST.has_key('edit'): cid = request.POST['course_id'] course = models.Course.objects.get(pk=cid) form = forms.CourseForm(request.POST, request.FILES, instance=course) if form.is_valid(): form.save() log.log_change(request.user, course, u'修改课程') return HttpResponseRedirect('/course/list/') elif request.POST.has_key('delete'): cid = request.POST['course_id'] course = models.Course.objects.get(pk=cid) course.delete() log.log_deletion(request.user, course, u'删除课程') return HttpResponseRedirect('/course/list/') else: cid = request.GET['course_id'] course = models.Course.objects.get(pk=cid) form = forms.CourseForm(instance=course) ctx = RequestContext(request, {'form': form, 'course': course}) return render_to_response('change_course.html', ctx)
def teacher_list(request): if request.POST: s = request.POST['username'] t = request.POST['kind'] page = request.POST.get('page', 1) if request.POST.has_key('delink'): ids = request.POST.getlist('selected') for id in ids: user = User.objects.get(id=id) db_util.delink_device(user) #course_ware.delete_coursewares(user) log.log_change(request.user, user, u'解除设备绑定') elif request.POST.has_key('delete'): for id in request.POST.getlist('selected'): user = User.objects.get(id=id) user.delete() log.log_deletion(request.user, user, u'删除教师') else: s = request.GET.get('username', '') t = request.GET.get('kind', 'fullname') page = request.GET.get('page', '1') form = forms.UsernameForm({'username': s, 'kind': t}) profiles = models.UserProfile.objects.select_related('user').filter( usertype=common_def.USERTYPE_TEACHER, school=request.session['school'], user__is_active=True) if s: if t == 'fullname': profiles = profiles.filter(user__first_name__icontains=s) else: profiles = profiles.filter(user__username__icontains=s) ctx = RequestContext( request, { 'profiles': profiles.order_by('user__username'), 'form': form, 'username': s, 'kind': t, 'page': page }) return render_to_response('teacher_list.html', ctx)
def school_list(request, region): if request.POST.has_key('search'): form = forms.SearchSchoolForm(request.POST) if form.is_valid(): s = form.cleaned_data['name'] t = form.cleaned_data['kind'] else: s = request.POST.get('name', '') t = request.POST.get('kind', 'name') page = 1 schools = db_util.search_school(region, s, t) elif request.POST.has_key('delete'): ids = request.POST.getlist('selected') #models.Course.objects.filter(id__in=ids).delete() for id in ids: school = models.School.objects.get(id=id) log.log_deletion(request.user, school, u'删除学校') school.delete() s = request.POST['find'] t = request.POST['kind'] page = request.POST['page'] schools = db_util.search_school(region, s, t) else: s = request.GET.get('search', '') t = request.GET.get('kind', 'name') page = request.GET.get('page', '1') schools = db_util.search_school(region, s, t) form = forms.SearchSchoolForm({'name': s, 'kind': t}) ctx = RequestContext( request, { 'schools': schools, 'region': region, 'form': form, 'page': page, 'search': s, 'search_type': t, }) return render_to_response('school_list.html', ctx)
def school_edit(request, school_id): school = models.School.objects.get(pk=school_id) if request.POST.has_key('edit'): #cid = request.POST['class_id'] form = forms.SchoolForm(request.POST, instance=school) if form.is_valid(): form.save() log.log_change(request.user, school, u'修改学校') return HttpResponseRedirect('/school/list/%d/' % school.region_id) elif request.POST.has_key('delete'): #cid = request.POST['class_id'] school.delete() log.log_deletion(request.user, school, u'删除学校') return HttpResponseRedirect('/school/list/%d/' % school.region_id) else: form = forms.SchoolForm(instance=school) form.fields['type'].choices = common_def.get_all_schooltypes() ctx = RequestContext(request, { 'form': form, 'school': school, 'region': school.region_id, }) return render_to_response('change_school.html', ctx)
def course_list(request): if request.POST.has_key('search'): form = forms.SearchCourseForm(request.POST) if form.is_valid(): s = form.cleaned_data['course'] t = form.cleaned_data['type'] else: s = request.POST.get('course', '') t = request.POST.get('type', 'name') page = 1 elif request.POST.has_key('delete'): ids = request.POST.getlist('selected') #models.Course.objects.filter(id__in=ids).delete() courses = models.Course.objects.filter(id__in=ids) for course in courses: log.log_deletion(request.user, course, u'删除课程') courses.delete() s = request.POST['find'] t = request.POST['type'] page = request.POST['page'] else: s = request.GET.get('search', '') t = request.GET.get('type', 'name') page = request.GET.get('page', '1') form = forms.SearchCourseForm({'course': s, 'type': t}) courses = db_util.search_course(s, t) ctx = RequestContext( request, { 'courses': courses, #.order_by('id'), 'form': form, 'course': s, 'type': t, 'page': page }) return render_to_response('course_list.html', ctx)
def student_edit(request): if request.POST.has_key('edit'): form = forms.UserForm(request.POST) form.fields['password'].required = False form.fields['password1'].required = False form.fields['grade'].choices = common_def.get_all_grades( request.session['school_type'], prompt=True) form.fields['myclass'].choices = cache.get_grade_classes( request.session['school'], int(request.POST['grade'])) uid = request.POST['uid'] user = User.objects.get(pk=uid) if form.is_valid(): password = form.cleaned_data['password'] if password: user.set_password(password) #utype = int(form.cleaned_data['type']) user.username = form.cleaned_data['username'] user.first_name = form.cleaned_data['name'] user.email = form.cleaned_data['email'] profile = user.userprofile #profile.usertype = utype profile.gender = form.cleaned_data['gender'] profile.myclass_id = form.cleaned_data[ 'myclass'] #int(request.POST['myclass']) profile.birthday = form.cleaned_data['birthday'] profile.telphone = form.cleaned_data['tel'] profile.contact = json.dumps( { 'tel': profile.telphone, 'email': user.email }, ensure_ascii=False) with transaction.commit_on_success(): user.save() profile.save() log.log_change(request.user, user, u'修改用户') return HttpResponseRedirect('/student/list/') elif request.POST.has_key('delete'): uid = request.POST['uid'] user = User.objects.get(pk=uid) user.delete() log.log_deletion(request.user, user, u'删除用户') return HttpResponseRedirect('/student/list/') elif request.POST: # change grade uid = request.POST['uid'] grade = int(request.POST['grade']) cache.set_selected_grade(request.user, 'change_student', grade, 300) form = forms.UserForm(request.POST) form.fields['grade'].choices = common_def.get_all_grades( request.session['school_type'], prompt=True) form.fields['myclass'].choices = cache.get_grade_classes( request.session['school'], grade) form.errors.clear() else: uid = request.GET['uid'] profile = models.UserProfile.objects.select_related( 'user', 'myclass').get(user__id=uid) grade = profile.myclass.get_grade() data = { 'username': profile.user.username, 'name': profile.user.first_name, #'en_name': user.last_name, 'gender': profile.gender, 'grade': grade, 'myclass': profile.myclass_id, 'birthday': profile.birthday, #'type': profile.usertype, } form = forms.UserForm(initial=data) form.fields['password'].required = False form.fields['password1'].required = False form.fields['grade'].choices = common_def.get_all_grades( request.session['school_type'], prompt=True) form.fields['myclass'].choices = cache.get_grade_classes( request.session['school'], grade) ctx = RequestContext(request, { 'form': form, 'uid': uid, }) return render_to_response('change_user.html', ctx)
def admin_edit(request): if request.POST.has_key('edit'): uid = request.POST['uid'] profile = models.UserProfile.objects.select_related('user').get( user=uid) user = profile.user form = forms.adminForm1(request.user, profile, request.POST, request.session.get('region'), True) if form.is_valid(): password = form.cleaned_data['password'] if password: user.set_password(password) utype = form.cleaned_data['type'] user.username = form.cleaned_data['username'] user.first_name = form.cleaned_data['name'] user.email = form.cleaned_data['email'] type_change = profile.usertype != utype profile.school_id = form.cleaned_data.get('school') region = form.cleaned_data.get('region') profile.region_id = region if region else request.session['region'] with transaction.commit_on_success(): user.save() if type_change: user.groups.clear() user.groups.add(db_util.group_of_usertype(utype)) profile.usertype = utype profile.save() log.log_change(request.user, user, u'修改管理员') return HttpResponseRedirect('/admin/list/') elif request.POST.has_key('delete'): uid = request.POST['uid'] user = User.objects.get(pk=uid) user.delete() log.log_deletion(request.user, user, u'删除管理员') return HttpResponseRedirect('/admin/list/') else: data = request.POST or request.GET uid = data['uid'] profile = models.UserProfile.objects.select_related('user').get( user=uid) user = profile.user usertype = request.POST and int( request.POST['type']) or profile.usertype data = { 'username': user.username, 'name': user.first_name, #'en_name': user.last_name, 'email': user.email, 'type': usertype, } if usertype == common_def.USERTYPE_REGION_ADMIN: region = profile.region_id data['region'] = region if region else 0 elif usertype == common_def.USERTYPE_SCHOOL_ADMIN: school = profile.school_id data['school'] = school if school else 0 form = forms.adminForm1(request.user, profile, data, profile.region_id) ctx = RequestContext(request, { 'form': form, 'uid': uid, }) return render_to_response('change_admin.html', ctx)