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 _edit_courseware(courseware, request, classes): form = forms.CoursewareForm(request.POST, request.FILES) form.fields['file'].required = False #classes = db_util.get_teacher_classes(request.user, courseware.grade, courseware.course_id) form.refresh_choices(classes) form.full_clean() if form.is_valid(): f = form.cleaned_data['file'] if f: path = util.handle_upload(f, util.upload_dir(str(request.user.id), str(courseware.course_id))) courseware.path = path courseware.state = common_def.COURSEWARE_STATE_WAITING courseware.modified_time = datetime.now() course_ware.delete_delivered(courseware) courseware.name = form.cleaned_data['title'] courseware.book_provider_id = form.cleaned_data['provider'] courseware.category_id = form.cleaned_data['category'] courseware.share_level = form.cleaned_data['share'] courseware.week = form.cleaned_data['week'] courseware.description = form.cleaned_data['description'] courseware.password = form.cleaned_data['password'] courseware.classes = form.cleaned_data['classes'] upload_img = form.cleaned_data['image'] if upload_img: #image = courseware.image courseware.image = upload_img # if image: # try: # os.remove(image.path) # except Exception, e: # print >>sys.stderr, e courseware.save() log.log_change(request.user, courseware, u'修改课件') return form
def courseware_undeliver(request, token, uid, courseware_id): try: courseware = models.Courseware.objects.get(pk=courseware_id) course_ware.undeliver_courseware(courseware) log.log_change(request.user, courseware, u'取消分发') ret = {'result': 'ok'} except Exception, e: ret = {'result': 'error', 'message': str(e)}
def courseware_undeliver(request, courseware_id): courseware = models.Courseware.objects.get(pk=courseware_id) course_ware.undeliver_courseware(courseware) log.log_change(request.user, courseware, u'取消分发') next_url = request.META.get('HTTP_REFERER', '') if not next_url: next_url = '/coursewares/%d/%d/' % (courseware.course_id, courseware.grade) return HttpResponseRedirect(next_url)
def courseware_deliver(request, courseware_id): courseware = models.Courseware.objects.get(pk=courseware_id) if courseware.state == common_def.COURSEWARE_STATE_CONVERTED: course_ware.deliver_courseware(courseware) log.log_change(request.user, courseware, u'分发课件') next_url = request.META.get('HTTP_REFERER', '') if not next_url: next_url = '/coursewares/%d/%d/' % (courseware.course_id, courseware.grade) return HttpResponseRedirect(next_url)
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_convert(request, token, uid, courseware_id): try: _set_backurl(courseware_id) courseware = models.Courseware.objects.get(pk=courseware_id) if 'recovert' in request.GET: courseware.modified_time = datetime.now() reconvert = True else: reconvert = False course_ware.convert_courseware(courseware, reconvert, request.META['SERVER_PORT']) log.log_change(request.user, courseware, u'格式转换') ret = {'result': 'ok'} except Exception, e: ret = {'result': 'error', 'message': str(e)}
def courseware_convert(request, courseware_id): courseware = models.Courseware.objects.get(pk=courseware_id) if request.GET.has_key('reconvert'): courseware.modified_time = datetime.now() reconvert = True else: reconvert = False course_ware.convert_courseware(courseware, reconvert, request.META['SERVER_PORT']) log.log_change(request.user, courseware, u'格式转换') if request.GET.has_key('ajax'): return HttpResponse('') else: next_url = request.META.get('HTTP_REFERER', '') if not next_url: next_url = '/coursewares/%d/%d/' % (courseware.course_id, courseware.grade) return HttpResponseRedirect(next_url)
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 teacher_class_course(request, uid): form = None if request.POST.has_key('add'): grade = int(request.POST['grade']) form = forms.ClassCourseForm(request.POST) form.set_choices(request.session, grade) if form.is_valid(): for clas in request.POST.getlist('myclass'): for course in request.POST.getlist('course'): try: models.TeacherClassCourse.objects.create( teacher_id=uid, myclass_id=clas, course_id=course) except IntegrityError: pass teacher = User.objects.get(pk=uid) log.log_change(request.user, teacher, u'改变课程设置') elif request.POST.has_key('delete'): ids = request.POST.getlist('selected') models.TeacherClassCourse.objects.filter(id__in=ids).delete() teacher = User.objects.get(pk=uid) log.log_change(request.user, teacher, u'改变课程设置') grade = int(request.POST['grade1']) elif request.POST: # change grade grade = int(request.POST['grade']) form = forms.ClassCourseForm(request.POST) form.set_choices(request.session, grade) form.errors.clear() else: grade = 0 #int(request.GET.get('grade', '0')) if not form: form = forms.ClassCourseForm({'grade': grade}) form.set_choices(request.session, grade) form.errors.clear() qs = models.TeacherClassCourse.objects.select_related( 'myclass', 'course').filter(teacher=uid, myclass__is_active=True).order_by( '-myclass__year', 'myclass__id', 'course__id') ctx = RequestContext(request, { 'form': form, 'queryset': qs, 'grade': grade, }) return render_to_response('teacher_class_course.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 misc_config(request): offline_timeout = None if request.POST.get('edit_offline_timeout'): form = forms.OfflineTimeoutForm(request.POST) if form.is_valid(): s = form.cleaned_data['offline_timeout'] r = models.MiscConfig.objects.get(pk='offline_timeout') r.conf_val = s r.save() log.log_change(request.user, r, u'修改离线时限') else: offline_timeout, _ = models.MiscConfig.objects.get_or_create( pk='offline_timeout') form = forms.OfflineTimeoutForm( {'offline_timeout': offline_timeout.conf_val}) ctx = RequestContext(request, { 'form': form, }) return render_to_response('misc_config.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_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 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)
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)