def tutorial(request, enc, tutorial_id): tutorial = Tutorial.objects.get(id=tutorial_id) video_path = '' if tutorial.video_path: video_path = tutorialhelpers.get_tutorial_video_path(tutorial) return helpers.req_render_to_response( request, "tutorials/view_tutorial.html", { 'tutorial': tutorial, 'video_path': video_path, })
def admin_tutorials(request, enc, tutorial_id): if tutorial_id: # edit existing tutorial tutorial = Tutorial.objects.get(id=tutorial_id) video_path = '' if request.method == 'POST': form = TutorialForm(request.POST) if form.is_valid(): cleaned = form.cleaned_data tutorial.title = cleaned['title'] tutorial.position = cleaned['position'] if cleaned['content']: tutorial.content = cleaned['content'] else: tutorial.content = '' tutorial.active = cleaned['active'] tutorial.save() tutorialhelpers.save_uploaded_video(request, tutorial, form) helpers.add_good_message( request, "You modified a tutorial. Congratulations.") else: print 'Some kind of problem with your form entry, try again.' if tutorial.video_path: video_path = tutorialhelpers.get_tutorial_video_path(tutorial) return helpers.req_render_to_response( request, "admin/admin_tutorials.html", { 'form': form, 'tutorial_id': tutorial_id, 'tutorial': tutorial, 'video_path': video_path, }) else: # request.method == 'GET' form = TutorialForm(initial={ 'position': tutorial.position, 'title': tutorial.title, 'content': tutorial.content, 'active': tutorial.active, }) if tutorial.video_path: video_path = tutorialhelpers.get_tutorial_video_path(tutorial) return helpers.req_render_to_response( request, "admin/admin_tutorials.html", { 'form': form, 'tutorial_id': tutorial_id, 'tutorial': tutorial, 'video_path': video_path, }) else: # create new tutorial / list existing tutorials if request.method == 'POST': # create new tutorial form = TutorialForm(request.POST) if form.is_valid(): cleaned = form.cleaned_data if cleaned['content']: newcontent = cleaned['content'] else: newcontent = '' current_tutorial = Tutorial.objects.create(position=cleaned['position'], title=cleaned['title'], content=newcontent, active=cleaned['active']) tutorialhelpers.save_uploaded_video(request, current_tutorial, form) helpers.add_good_message( request, "You created a new tutorial. Good for you, cheeto-fingers.") else: print 'Some kind of problem with your form entry, try again.' form = TutorialForm() tutorials = Tutorial.objects.filter().order_by('position') return helpers.req_render_to_response( request, "admin/admin_tutorials.html", { 'form': form, 'tutorials': tutorials, }) else: # request.method == 'GET' form = TutorialForm() tutorials = Tutorial.objects.filter().order_by('position') return helpers.req_render_to_response( request, "admin/admin_tutorials.html", { 'form': form, 'tutorials': tutorials, })