def view(title=None): _ = current_app.gettext if title is None: title = current_app.front_page p = page.get_page(request, title) try: content = p.view_content() except error.NotFoundErr: if current_app.fallback_url: url = current_app.fallback_url if '%s' in url: url = url % werkzeug.url_quote(title) else: url = "%s/%s" % (url, werkzeug.url_quote(title)) return response.redirect(url, code=303) if current_app.read_only: raise error.NotFoundErr(_("Page not found.")) url = current_app.get_url(title, 'edit', external=True) return response.redirect(url, code=303) html = p.template("page.html", content=content) dependencies = p.dependencies() etag = '/(%s)' % ','.join(dependencies) return response.response(request, title, html, etag=etag)
def atom(): _ = current_app.gettext feed = werkzeug.contrib.atom.AtomFeed(current_app.site_name, feed_url=request.url, url=request.adapter.build('view', force_external=True), subtitle=_('Track the most recent changes to the wiki ' 'in this feed.')) history = itertools.islice(current_app.storage.history(), None, 10, None) unique_titles = set() for title, rev, date, author, comment in history: if title in unique_titles: continue unique_titles.add(title) if rev > 0: url = request.adapter.build('diff', { 'title': title, 'from_rev': rev - 1, 'to_rev': rev, }, force_external=True) else: url = request.adapter.build('view', { 'title': title, }, force_external=True) feed.add(title, comment, content_type="text", author=author, url=url, updated=date) rev = current_app.storage.repo_revision() resp = response.response(request, 'atom', feed.generate(), '/+feed', 'application/xml', rev) resp.make_conditional(request) return resp
def history(title): """Display history of changes of a page.""" max_rev = -1 history = [] if title not in current_app.storage: raise error.NotFoundErr("Page not found.") p = page.get_page(request, title) make_url = current_app.get_url for rev, date, author, comment in current_app.storage.page_history(title): if max_rev < rev: max_rev = rev if rev > 0: date_url = make_url(view='diff', title=title, from_rev=rev - 1, to_rev=rev, ) else: date_url = make_url(view='revision', title=title, rev=rev, ) history.append((date, date_url, rev, author, comment)) html = p.template('history.html', history=history, date_html=page.date_html, parent=max_rev) resp = response.response(request, title, html, '/history') return resp
def render(title): """Serve a thumbnail or otherwise rendered content.""" p = page.get_page(request, title) try: cache_filename, cache_mime = p.render_mime() render_func = p.render_cache if not current_app.cache: raise NotImplementedError() except (AttributeError, NotImplementedError): return download(title) cache_key = '/render/%s_%s' % (werkzeug.url_quote(title, safe=''), cache_filename) rev, dt, author, comment = current_app.storage.page_meta(title) cache_file = current_app.cache.open(cache_key, 'r') if (cache_file and dt > cache_file.modified) or not cache_file: cache_file = current_app.cache.open(cache_key, 'w') try: result_file = render_func(cache_file) except error.UnsupportedMediaTypeErr: return download(title) else: cache_file = current_app.cache.open(cache_key, 'r') resp = response.response(request, title, werkzeug.wrap_file(request.environ, cache_file), '/render', cache_mime, size=cache_file.length) resp.direct_passthrough = True return resp
def download(title): """Serve the raw content of a page directly from disk.""" mime = page.page_mime(title) if mime == 'text/x-wiki': mime = 'text/plain' data = current_app.storage.page_data(title) resp = response.response(request, title, data, '/download', mime, size=len(data)) resp.direct_passthrough = True return resp
def edit(title, preview=None, captcha_error=None): page.check_lock(current_app, title) exists = title in current_app.storage p = page.get_page(request, title, wiki=current_app) html = p.render_editor(preview, captcha_error) if not exists: resp = response.WikiResponse(html, content_type="text/html", status=404) elif preview: resp = response.WikiResponse(html, content_type="text/html") else: resp = response.response(request, title, html, '/edit') resp.headers['Cache-Control'] = 'no-cache' return resp
def revision(title, rev): _ = current_app.gettext text = current_app.storage.revision_text(title, rev) link = werkzeug.html.a(werkzeug.html(title), href=current_app.get_url(title)) content = [ werkzeug.html.p( werkzeug.html( _('Content of revision %(rev)d of page %(title)s:')) % {'rev': rev, 'title': link}), werkzeug.html.pre(werkzeug.html(text)), ] special_title = _('Revision of "%(title)s"') % {'title': title} p = page.get_page(request, title) html = p.template('page_special.html', content=content, special_title=special_title) resp = response.response(request, title, html, rev=rev, etag='/old') return resp