Beispiel #1
0
def upload(name, version, category, grade, clss, course, username, path,
           testfile):
    uid = get_model_id(User, teachers, 'username', username)
    version_id = get_model_id(BookProvider, versions, 'name', version, True)
    category_id = get_model_id(CoursewareCategory, categories, 'name_ch',
                               category, True)
    class_id = get_class_id(grade, clss)
    course_id = get_model_id(Course, courses, 'course_name', course)

    filename = os.path.basename(path)
    desdir = os.path.join(settings.COURSEWARE_UPLOAD_DIR, str(uid),
                          str(course_id))
    if not os.path.exists(desdir):
        os.makedirs(desdir)
    despath = os.path.join(desdir, re.sub(r'\s+', '_', filename))
    shutil.copy(path, despath)

    cw = Courseware.objects.create(name=name,
                                   grade=grade,
                                   week=1,
                                   volume_id='1',
                                   path=despath,
                                   course_id=course_id,
                                   teacher_id=uid,
                                   book_provider_id=version_id,
                                   category_id=category_id)
    cw.classes.add(class_id)
    if 'test' not in sys.argv:
        course_ware.convert_courseware(cw, False, '8001')
    return cw.id
Beispiel #2
0
def _add_courseware(request, token, uid, back_level):
    course_id = request.POST['course_id']
    grade = int(request.POST['grade'])
    classes = db_util.get_teacher_classes(request.user, grade, course_id)
    if not classes:
        return _error_page(request, u'对不起,您无权管理该课程的课件')

    form = forms.CoursewareForm(request.POST, request.FILES)
    form.refresh_choices(classes)
    if form.is_valid():
        path = util.handle_upload(
            form.cleaned_data['file'],
            util.upload_dir(str(request.user.id), course_id))
        courseware = course_ware.create_courseware(course_id, grade,
                                                   request.user, path,
                                                   form.cleaned_data)
        log.log_addition(request.user, courseware, u'上传课件')
        course_ware.convert_courseware(courseware, False,
                                       request.META['SERVER_PORT'])
        #log.log_change(request.user, courseware, u'格式转换')
        ctx = RequestContext(request, {
            'level': -back_level,
        })
        return render_to_response('history_goto.html', ctx)
    else:
        course_name = request.POST['course_name']
        ctx = RequestContext(
            request, {
                'courseware': _dummy_courseware(grade, course_id, course_name),
                'form': form,
                'newware': True,
                'back_level': back_level,
                'upload_id': uuid.uuid1().get_hex(),
            })
        return render_to_response('courseware_detail.html', ctx)
Beispiel #3
0
def upload_yu(request):
    data = json.loads(request.REQUEST['data'])
    course = models.Course.objects.get(course_name=data['course'])
    user = User.objects.get(username='******')  # data['username'])
    try:
        category = models.CoursewareCategory.objects.get(
            name_ch=data['category'])
        category = category.id
    except:
        category = 3
    classes = models.TeacherClassCourse.objects.filter(teacher=user,
                                                       course=course)
    filename = os.path.basename(data['path'])
    (
        title,
        _,
    ) = os.path.splitext(filename)
    path = os.path.join(util.upload_dir(str(user.id), str(course.id)),
                        filename)
    others = {
        'title': title,
        'provider': 1,
        'category': category,
        'share': 6,
        'week': data['lesson_count'],
        'description': '',
        'image': '',
        'classes': [c.myclass_id for c in classes],
        'volume_id': data['volume_id']
    }
    shutil.copy(data['path'], path)
    courseware = course_ware.create_courseware(course.id, data['grade'], user,
                                               path, others)
    course_ware.convert_courseware(courseware, False,
                                   request.META['SERVER_PORT'])
    params = [('coursename', course.course_name),
              ('coursename_en', course.english_name),
              ('courseid', str(course.id)), ('category', data['category']),
              ('category_id', str(category)),
              ('category_name', data['category']), ('category_name_en', ''),
              ('bookname', courseware.name), ('bookid', str(courseware.id)),
              ('password', ''), ('type', 'mrf')]
    _set_backurl(str(courseware.id), data['backurl'])
    url = 'https://k12.telecomedu.com/dl/getfile/?' + '&'.join(
        ['='.join([k, v]) for (
            k,
            v,
        ) in params])
    s = json.dumps({
        'url': url,
        'bookid': str(courseware.id)
    },
                   ensure_ascii=False)
    return HttpResponse(s)
Beispiel #4
0
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)}
Beispiel #5
0
def _edit_courseware(courseware, request, classes, back_level):
    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 = 0
            courseware.modified_time = datetime.now()
            course_ware.delete_delivered(courseware)
            course_ware.convert_courseware(courseware, False,
                                           request.META['SERVER_PORT'])
        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