def upload(request): if request.method == 'POST': form = UploadCourseForm(request.POST,request.FILES) if form.is_valid(): # All validation rules pass extract_path = settings.COURSE_UPLOAD_DIR + 'temp/' + str(request.user.id) + '/' course = handle_uploaded_file(request.FILES['course_file'], extract_path, request) if course: shutil.rmtree(extract_path) #add the tags tags = form.cleaned_data.get("tags").strip().split(",") if len(tags) > 0: for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = request.user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course,tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save() return HttpResponseRedirect('success/') # Redirect after POST else: shutil.rmtree(extract_path,ignore_errors=True) os.remove(settings.COURSE_UPLOAD_DIR + request.FILES['course_file'].name) else: form = UploadCourseForm() # An unbound form return render(request, 'oppia/upload.html', {'form': form,})
def upload_step2(request, course_id, editing=False): if editing and not can_edit_course(request, course_id): return HttpResponse('Unauthorized', status=401) elif not request.user.userprofile.get_can_upload(): return HttpResponse('Unauthorized', status=401) course = Course.objects.get(pk=course_id) if request.method == 'POST': form = UploadCourseStep2Form(request.POST, request.FILES) if form.is_valid(): # All validation rules pass if course: #add the tags tags = form.cleaned_data.get("tags").strip().split(",") is_draft = form.cleaned_data.get("is_draft") if len(tags) > 0: course.is_draft = is_draft course.save() for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = request.user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course, tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save() redirect = 'oppia_course' if editing else 'oppia_upload_success' return HttpResponseRedirect( reverse(redirect)) # Redirect after POST else: form = UploadCourseStep2Form(initial={ 'tags': course.get_tags(), 'is_draft': course.is_draft, }) # An unbound form page_title = _(u'Upload Course - step 2') if not editing else _( u'Edit course') print course.title return render_to_response('oppia/upload.html', { 'form': form, 'course_title': course.title, 'editing': editing, 'title': page_title }, context_instance=RequestContext(request))
def upload_step2(request, course_id, editing=False): if editing and not can_edit_course(request, course_id): return HttpResponse("Unauthorized", status=401) elif not request.user.userprofile.get_can_upload(): return HttpResponse("Unauthorized", status=401) course = Course.objects.get(pk=course_id) if request.method == "POST": form = UploadCourseStep2Form(request.POST, request.FILES) if form.is_valid(): # All validation rules pass is_draft = form.cleaned_data.get("is_draft") if course: # add the tags tags = form.cleaned_data.get("tags").strip().split(",") is_draft = form.cleaned_data.get("is_draft") if len(tags) > 0: course.is_draft = is_draft course.save() for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = request.user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course, tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save() redirect = "oppia_course" if editing else "oppia_upload_success" return HttpResponseRedirect(reverse(redirect)) # Redirect after POST else: form = UploadCourseStep2Form( initial={"tags": course.get_tags(), "is_draft": course.is_draft} ) # An unbound form page_title = _(u"Upload Course - step 2") if not editing else _(u"Edit course") return render_to_response( "oppia/upload.html", {"form": form, "course_title": course.title, "editing": editing, "title": page_title}, context_instance=RequestContext(request), )
def add_course_tags(user, course, tags): for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course, tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save()
def add_course_categories(request, course, categories): for t in categories: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = request.user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course, tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save()
def update_course_tags(self, form, course, user): tags = form.cleaned_data.get("tags", "").strip().split(",") is_draft = form.cleaned_data.get("is_draft") course.is_draft = is_draft course.save() # remove any existing tags CourseTag.objects.filter(course=course).delete() # now add the new ones for t in tags: tag, created = Tag.objects.get_or_create(name=t.strip()) if created: tag.created_by = user tag.save() # add tag to course if not CourseTag.objects.filter(course=course, tag=tag).exists(): ct = CourseTag() ct.course = course ct.tag = tag ct.save()
def upload_step2(request, course_id): if not request.user.userprofile.get_can_upload(): return HttpResponse('Unauthorized', status=401) course = Course.objects.get(pk=course_id) if request.method == 'POST': form = UploadCourseStep2Form(request.POST,request.FILES) if form.is_valid(): # All validation rules pass is_draft = form.cleaned_data.get("is_draft") if course: #add the tags tags = form.cleaned_data.get("tags").strip().split(",") is_draft = form.cleaned_data.get("is_draft") if len(tags) > 0: course.is_draft = is_draft course.save() for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = request.user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course,tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save() return HttpResponseRedirect('success/') # Redirect after POST else: form = UploadCourseStep2Form(initial={'tags':course.get_tags(), 'is_draft':course.is_draft,}) # An unbound form return render_to_response('oppia/upload.html', {'form': form, 'title':_(u'Upload Course - step 2')}, context_instance=RequestContext(request))
def upload(request): if request.method == 'POST': form = UploadCourseForm(request.POST, request.FILES) if form.is_valid(): # All validation rules pass extract_path = settings.COURSE_UPLOAD_DIR + 'temp/' + str( request.user.id) + '/' course = handle_uploaded_file(request.FILES['course_file'], extract_path, request) if course: shutil.rmtree(extract_path) #add the tags tags = form.cleaned_data.get("tags").strip().split(",") if len(tags) > 0: for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = request.user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course, tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save() return HttpResponseRedirect('success/') # Redirect after POST else: shutil.rmtree(extract_path, ignore_errors=True) os.remove(settings.COURSE_UPLOAD_DIR + request.FILES['course_file'].name) else: form = UploadCourseForm() # An unbound form return render(request, 'oppia/upload.html', { 'form': form, })
def upload_step2(request, course_id): if not can_upload(request): return render_to_response('oppia/upload-staff-only.html', {'settings': settings}, context_instance=RequestContext(request)) course = Course.objects.get(pk=course_id) if request.method == 'POST': form = UploadCourseStep2Form(request.POST,request.FILES) if form.is_valid(): # All validation rules pass is_draft = form.cleaned_data.get("is_draft") if course: #add the tags tags = form.cleaned_data.get("tags").strip().split(",") is_draft = form.cleaned_data.get("is_draft") if len(tags) > 0: course.is_draft = is_draft course.save() for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = request.user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course,tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save() return HttpResponseRedirect('success/') # Redirect after POST else: form = UploadCourseStep2Form(initial={'tags':course.get_tags(), 'is_draft':course.is_draft,}) # An unbound form return render(request, 'oppia/upload.html', {'form': form,'title':_(u'Upload Course - step 2')})
def add_course_tags(form, course): tags = form.cleaned_data.get("tags").strip().split(",") is_draft = form.cleaned_data.get("is_draft") if len(tags) > 0: course.is_draft = is_draft course.save() for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = request.user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course, tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save()
def add_course_tags(form, course, user): tags = form.cleaned_data.get("tags", "").strip().split(",") is_draft = form.cleaned_data.get("is_draft") if len(tags) > 0: course.is_draft = is_draft course.save() for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course, tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save()
def update_course_tags(form, course, user): tags = form.cleaned_data.get("tags", "").strip().split(",") is_draft = form.cleaned_data.get("is_draft") course.is_draft = is_draft course.save() # remove any existing tags CourseTag.objects.filter(course=course).delete() # now add the new ones for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course, tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save()
def publish_view(request): if request.method != 'POST': return HttpResponse(status=405) required = ['username', 'password', 'tags', 'is_draft'] for r in required: if r not in request.POST: print r + " not found" return HttpResponse(status=201) if 'course_file' not in request.FILES: print "Course file not found" return HttpResponse(status=400) # authenticate user username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is None or not user.is_active: return HttpResponse(status=200) # check user has permissions to publish course if settings.OPPIA_STAFF_ONLY_UPLOAD is True and not user.is_staff and user.userprofile.can_upload is False: return HttpResponse(status=401) extract_path = os.path.join(settings.COURSE_UPLOAD_DIR, 'temp', str(user.id)) course = handle_uploaded_file(request.FILES['course_file'], extract_path, request, user) if course is False: return HttpResponse(status=500) else: if request.POST['is_draft'] == "False": course.is_draft = False else: course.is_draft = True course.save() # remove any existing tags CourseTag.objects.filter(course=course).delete() # add tags tags = request.POST['tags'].strip().split(",") for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course, tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save() return HttpResponse(status=201)
def publish_view(request): if request.method != 'POST': return HttpResponse(status=405) required = ['username','password','tags','is_draft'] for r in required: if r not in request.POST: print r + " not found" return HttpResponse(status=201) if 'course_file' not in request.FILES: print "Course file not found" return HttpResponse(status=400) # authenticate user username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is None or not user.is_active: return HttpResponse(status=200) # check user has permissions to publish course if settings.OPPIA_STAFF_ONLY_UPLOAD is True and not user.is_staff and user.userprofile.can_upload is False: return HttpResponse(status=401) extract_path = os.path.join(settings.COURSE_UPLOAD_DIR,'temp',str(user.id)) course = handle_uploaded_file(request.FILES['course_file'], extract_path, request, user) if course is False: return HttpResponse(status=500) else: if request.POST['is_draft'] == "False": course.is_draft = False else: course.is_draft = True course.save() # remove any existing tags CourseTag.objects.filter(course=course).delete() # add tags tags = request.POST['tags'].strip().split(",") for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course,tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save() return HttpResponse(status=201)
def publish_view(request): # get the messages to clear possible previous unprocessed messages get_messages_array(request) if request.method != 'POST': return HttpResponse(status=405) required = ['username', 'password', 'tags', 'is_draft'] validationErrors = [] for field in required: if field not in request.POST: print field + " not found" validationErrors.append("field '{0}' missing".format(field)) if COURSE_FILE_FIELD not in request.FILES: print "Course file not found" validationErrors.append("file '{0}' missing".format(COURSE_FILE_FIELD)) else: # check the file size of the course doesnt exceed the max file = request.FILES[COURSE_FILE_FIELD] if file is not None and file._size > settings.OPPIA_MAX_UPLOAD_SIZE: size = int(math.floor(settings.OPPIA_MAX_UPLOAD_SIZE / 1024 / 1024)) validationErrors.append((_( "Your file is larger than the maximum allowed (%(size)d Mb). You may want to check your course for large includes, such as images etc." ) % { 'size': size, })) if file is not None and file.content_type != 'application/zip' and file.content_type != 'application/x-zip-compressed': validationErrors.append(_("You may only upload a zip file")) if validationErrors: return JsonResponse( {'errors': validationErrors}, status=400, ) # authenticate user username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is None or not user.is_active: messages.error(request, "Invalid username/password") response_data = { 'message': _('Authentication errors'), 'messages': get_messages_array(request) } return JsonResponse(response_data, status=401) # check user has permissions to publish course if settings.OPPIA_STAFF_ONLY_UPLOAD is True \ and not user.is_staff \ and user.userprofile.can_upload is False: return HttpResponse(status=401) extract_path = os.path.join(settings.COURSE_UPLOAD_DIR, 'temp', str(user.id)) course, response = handle_uploaded_file(request.FILES['course_file'], extract_path, request, user) if course is False: resp_code = response if response is not None else 500 response_data = {'messages': get_messages_array(request)} return JsonResponse(response_data, status=resp_code) else: course.is_draft = (request.POST['is_draft'] == "True" or request.POST['is_draft'] == "true") course.save() # remove any existing tags CourseTag.objects.filter(course=course).delete() # add tags tags = request.POST['tags'].strip().split(",") for t in tags: try: tag = Tag.objects.get(name__iexact=t.strip()) except Tag.DoesNotExist: tag = Tag() tag.name = t.strip() tag.created_by = user tag.save() # add tag to course try: ct = CourseTag.objects.get(course=course, tag=tag) except CourseTag.DoesNotExist: ct = CourseTag() ct.course = course ct.tag = tag ct.save() msgs = get_messages_array(request) if len(msgs) > 0: return JsonResponse({'messages': msgs}, status=201) else: return HttpResponse(status=201)