def slide_create(request): """ If the request is a GET, renders a form to create a new slide from a slide bundle. If the request is a POST, it should contain a bundle that is used to create a new slide. The slide's metadata is populated from the bundle's manifest file. """ if request.method == 'POST': f = CreateSlideForm(request.POST, request.FILES) if f.is_valid(): tf = tarfile.open(fileobj=request.FILES['bundle']) s = Slide(user=request.user, title='Uploaded %s' % (tf.__hash__())) s.populate_from_bundle(request.FILES['bundle'], tf) return redirect('orwell-slide-index') else: f = CreateSlideForm() return render_to_response('orwell/create-slide.html', {'form':f}, context_instance=RequestContext(request))
def pdf_slide_create(request): """ If the request is a GET, renders a form to create a new slide from a PDF. If the request is a POST, uses the information in the POST data to create a new slide from a PDF included in that data. """ if request.method == 'POST': f = CreatePDFSlideForm(request.POST, request.FILES) if f.is_valid(): def in_cur_dir(path): return os.path.join(os.path.dirname(__file__),path) fpath = handle_uploaded_file(request.FILES['pdf']) convert_pdf(fpath, in_cur_dir("PDFslide/pdf.png"), (1920,1080)) convert_pdf(fpath, in_cur_dir("PDFslide/_thumb.png"), (200, 113)) t = RenderTemplate(open(in_cur_dir('PDFslide/manifest.js.tmp')).read()) manifest = open(in_cur_dir('PDFslide/manifest.js'), 'w+') manifest.write(t.render(Context({'title':f.cleaned_data['title'], 'duration':f.cleaned_data['duration'], 'priority':f.cleaned_data['priority']}))) manifest.close() bundle_loc = in_cur_dir('bundle.tar.gz') PDFslide_loc = in_cur_dir('PDFslide') os.chdir(PDFslide_loc) os.system('tar -zcf %s *' % bundle_loc) s = Slide(user=request.user, title=f.cleaned_data['title'], duration=f.cleaned_data['duration'], priority=f.cleaned_data['priority']) s.populate_from_bundle(File(open(bundle_loc)), tarfile.open(bundle_loc)) # remove these so as not to cause problems for the next pdf slide os.remove(in_cur_dir("PDFslide/pdf.png")) os.remove(in_cur_dir("PDFslide/_thumb.png")) os.remove(in_cur_dir("PDFslide/manifest.js")) os.remove(bundle_loc) os.remove(fpath) return redirect('orwell-slide-index') else: f = CreatePDFSlideForm() return render_to_response('orwell/create-pdf-slide.html', {'form':f}, context_instance=RequestContext(request))
def cli_manage_slide(request): """ Accepts POSTS from authenticated clients, and allows those clients to either create or modify existing slides. """ if request.method == 'POST': f = CLICCreateSlideForm(request.POST, request.FILES) if f.is_valid(): tf = tarfile.open(fileobj=request.FILES['bundle']) id = f.cleaned_data['id'] create = f.cleaned_data['mode'] == 'create' if create and not id: s = Slide(user=request.user, title='cli uploaded %s' % (tf.__hash__())) elif not create and id: s = Slide.objects.get(id=id) if not s.allowed(request.user): return HttpResponse('Not allowed to modify slide') else: return HttpResponse('invalid: %s' % str(f.data)) s.populate_from_bundle(request.FILES['bundle'], tf) return HttpResponse('Slide %s %sd' % (s.id, f.cleaned_data['mode'])) return HttpResponseNotAllowed(['POST'])