def create_lesson(request, course_slug, page_slug): ''' Creates a new lesson and shows the user the edit page but does not save the lesson to the database @author Matthew Tytel ''' #enforcing permissions try: e = Enrollment.objects.get(course__slug = course_slug, user = request.user) except Enrollment.DoesNotExist: return custom_403(request, "User cannot create a lesson in the course because the user is not enrolled in this course") if not e.edit: return custom_403(request, "User cannot create a lesson in the course because the user does not have edit permissions on the course") if request.method == "POST" and "Save" in request.POST: name = request.POST["lessonname"].strip() #create a lesson (don't save) this will allow the len < 1 case to #save the content that the user entered, and display course name lesson = CreateLesson(name) lesson.workingCopy = lesson.content = request.POST["content"] lesson.course = Course.objects.get(slug=course_slug) if len(name) < 1: return master_rtr(request, 'page/lesson/edit_lesson.html', \ {'course_slug':course_slug, 'page_slug': page_slug, 'course':course_slug, 'message':'Lesson names must be non-empty', 'lesson':lesson, 'new':True}) if saveNewLesson(request.POST['lessonname'], request.POST['content'], course_slug, page_slug) == 0: return HttpResponseRedirect(reverse('pages.views.show_page', args=[course_slug, lesson.slug])) else: return master_rtr(request, 'page/lesson/edit_lesson.html', \ {'course_slug':course_slug, 'page_slug': page_slug, 'course':course_slug, 'message':'A lesson with that name already exists', 'lesson':lesson, 'new':True}) lesson = CreateLesson('') return master_rtr(request, 'page/lesson/edit_lesson.html', \ {'course_slug':course_slug, \ 'course':course_slug, \ 'page_slug':page_slug, \ 'pid':lesson.name, 'new':True})
def create_quiz(request, course_slug, page_slug): ''' create_Quiz View This view will create a quiz and take the user to the quiz editor screen. @author John Hartquist ''' #enforcing permissions try: e = Enrollment.objects.get(course__slug = course_slug, user = request.user) except Enrollment.DoesNotExist: return custom_403(request, "User cannot create a quiz in the course because the user is not enrolled in this course") if not e.edit: return custom_403(request, "User cannot create a quiz in the course because the user does not have edit permissions on the course") page_slug = safeSlug(page_slug) if request.method == "POST" and "Create Quiz" in request.POST: course = Course.objects.get(slug=course_slug) name = request.POST['name'] if name == "": return master_rtr(request, 'page/quiz/create-quiz.html', {'message': "You may not enter a blank quiz name"}) #exists = Quiz.objects.get(slug=slugify(name)) try: exists = Quiz.objects.get(slug=slugify(name), course=course) return master_rtr(request, 'page/quiz/create-quiz.html', {'message': "Quiz with this name already exists"}) except: newQuiz = Quiz(course=course, name=name, slug=slugify(name), text=name, upToDate=True) insertLastChildPage(newQuiz, Page.objects.get(slug=page_slug, course=course)) newQuiz = Quiz.objects.get(slug=newQuiz.slug, course=course) workingCopy = Quiz(course=newQuiz.course, name=newQuiz.name, slug=(newQuiz.slug + "_workingCopy"), text=newQuiz.name, left=0, right=0) workingCopy.save() return HttpResponseRedirect(reverse('pages.views.edit_page', args=[course_slug, newQuiz.slug])) else: return master_rtr(request, 'page/quiz/create-quiz.html')
def move_page(request, course_slug, page_slug): ''' This view allows instructors to move pages around in a course. @pre request is a Request object, other two args are strings, request.user.is_authenticated() @author Russell Mezzetta ''' #check if the course is a real course in the database data = {} data['course_slug'] = course_slug data['page_slug'] = page_slug try: data['course'] = Course.objects.get(slug=course_slug) except Course.DoesNotExist: return custom_404(request, "BAD URL: The course: %s does not exist" % (course_slug)) #check that user has edit permissions on the course try: e = request.user.enrollments.get(course = data['course']) if not e.edit: return custom_403(request, "User does not have edit permissions on the course") except ObjectDoesNotExist: return custom_403(request, "User is not enrolled in the course") #check if the page is a real page in the database try: data['page'] = Page.objects.get(slug=page_slug) except Page.DoesNotExist: return custom_404(request, "ERROR: BAD URL: The course: %s does not contain the page: %s." % (course_slug, page_slug)) #check to make sure that we are not trying to move a course-page if data['page'].left == 1: return custom_404(request, "You may not move the home page of a course") #save a list of all pages in the course EXCEPT the given page and exclude #ignored page values (left<=0 or right<=0) data['pagelist'] = data['course'].pages.all().exclude(slug=page_slug).exclude(left__lte=0).exclude(right__lte=0).order_by('left') if request.method == "POST": if "referencePageID" in request.POST and "siblingOrChild" in request.POST: #get the page specified by refPageID from the data['pagelist'] refPage = None for p in data['pagelist']: if p.slug == request.POST['referencePageID']: refPage = p break #refPage should never be none.... #if refPage == None: # return HttpResponse("error, the previously selected page somehow is no longer in the list of pages in this course") #movePage should be passed lessons or quizzes, #cast refPage and data['page'] appropriately p1 = data['page'] try: #to cast to a lesson p1 = p1.lesson except Lesson.DoesNotExist: #try: #to cast to a quiz p1 = p1.quiz #except Quiz.DoesNotExist: # print "warning -- move_page view, page neither quiz nor lesson" p2 = refPage try: #to cast to a lesson p2 = p2.lesson except Lesson.DoesNotExist: #try: #to cast to a quiz p2 = p2.quiz #except Quiz.DoesNotExist: #print "warning -- move_page view, page neither quiz nor lesson" if request.POST['siblingOrChild'] == "sibling": #move page to be the first sibling of refPage movePage(p1, p2) else: #move the page to be the first child of refPage movePageToParent(p1, p2) #after moving the page, redirect them to the edit view of the page return HttpResponseRedirect(reverse('pages.views.edit_page', args=[p1.course.slug, p1.slug])) return master_rtr(request, 'page/move_page.html', data)