def create_pdf(request, slug): """Returns article as PDF, with the help of RML markup.""" from rml import rml2pdf # We only allow PDF creation of existing articles article = get_object_or_404(Article, slug__iexact=slug) # Create XML of article, with content unescaped unescaped_article = render_to_string('article-to-xml.xsl', article) # Generate RML of article format = "%a, %d %b %Y %H:%M:%S" timestamp = datetime.datetime.today().strftime(format) params = {'timestamp' : xslt_param_builder(timestamp)} rml = StringIO.StringIO(render_xml_to_string('rml.xsl', unescaped_article, params)) # Create the PDF buf = cStringIO.StringIO() rml2pdf.go(rml, outputFileName=buf) buf.seek(0) pdf = buf.read() buf.close() # Set up response object response = HttpResponse(mimetype='application/pdf') response.write(pdf) response['Content-Disposition'] = ('attachment; filename=%s.pdf' % article.title) return response
def edit_article(request, slug): article = None params = {} try: # Enables case-insentivity article = Article.objects.get(slug__iexact=slug) if article.slug != slug: return HttpResponseRedirect(reverse('edit_article', args=[article.slug])) except Article.DoesNotExist: title = slug.replace('_', ' ') article = Article(title=title, creator=request.user) if request.method == 'GET': # Check mutual exclusion lock = cache.get(article.title, None) if lock is None: lock = EditLock(article.title, request) elif not lock.is_mine(request): print "Possible editing conflict. Another user started editing", lock.created_at params['locked'] = xslt_param_builder("True") format = "%d %b %Y %H:%M:%S" params['lock_created'] = xslt_param_builder( lock.created_at.strftime(format) ) elif request.method == 'POST': article.title = request.POST['title'] article.content = request.POST['content'] article.save() cache.delete(article.title) view_url = reverse('view_article', args=[article.slug]) return HttpResponseRedirect(view_url) view_url = reverse('view_article', args=[slug]) params['viewurl'] = xslt_param_builder(view_url) return render_to_response('edit_article.xsl', article, params)
def search(request): if request.method == 'POST': result = query(request.POST['query']) # Redirect straight to single result if len(result) == 1: view_url = reverse('view_article', args=[result[0].slug]) return HttpResponseRedirect(view_url) params = {'query' : xslt_param_builder(request.POST['query'])} return render_to_response('search.xsl', result, params)
def view_article(request, slug): edit_url = reverse('edit_article', args=[slug]) try: # Enables case-insentivity article = Article.objects.get(slug__iexact=slug) if article.slug != slug: return HttpResponseRedirect(reverse('view_article', args=[article.slug])) params = {'editurl' : xslt_param_builder(edit_url)} return render_to_response('article.xsl', article, params) # If the article does not exist, we go to edit mode except Article.DoesNotExist: return HttpResponseRedirect(edit_url)