def test_initial(database): assert Document(slug='awerty').initial == 'a' assert Document(title='QWERTY').initial == 'q' assert Document(slug='awerty', title='QWERTY').initial == 'q' assert Document.get('a').initial == 'a' assert Document.get('b').initial == 'b'
def edit(slug): if request.method == 'GET': return render_template('edit.j2', page=Document.get(slug)) current_revision = Document.get(slug=slug) if not request.form['revision'] == str(current_revision.id): return render_template('error.j2', error=_('This page has been ' 'modified while you were working on it.')), 400 if not current_revision.id: current_revision.body = '' # For byte delta computation edition_policy = param('edition_policy', '') if edition_policy == 'users': if not current_user.is_authenticated: return render_template('error.j2', error=_('Edition is limited to ' 'registered users only')), 403 if edition_policy == 'admins': if not current_user.admin: return render_template('error.j2', error=_('Edition is limited to ' 'administrators only')), 403 Metadata.deactivate(slug) Document.deactivate(slug) parsed = parse(request.form['body']) for key, values in parsed['meta'].items(): for value in values: Metadata(slug=slug, key=key, value=value) \ .save() title = parsed['title'] or slug body = request.form['body'] comment = request.form['comment'] or None bytes_delta = len(body) - len(current_revision.body) page = Document(slug=slug, title=title, body=body, comment=comment, bytes_delta=bytes_delta) if current_user.is_authenticated: page.user_id = current_user.id page.save() if not current_user.is_anonymous: Notification.add(slug, current_user, 'automatic') db.session.commit() Notification.send(page, current_user) flash(_('Thank you for your changes. Your attention to detail ' 'is appreciated.')) return redirect(url_for('read', slug=slug))
def search(path=None): query = request.args.get('query', '') filters = [item.split('=', 1) for item in path.split('/')] \ if path else [] ignores = [item[0] for item in filters] hits = Document.search(query, filters) facets = Document.facets(hits, ignores=ignores) return render_template('search.j2', query=query, path=path, hits=hits, facets=facets, total=Document.count())
def diff(slug, a, b): doc_a = Document.get(slug, int(a)) doc_b = Document.get(slug, int(b)) body_a = (doc_a.body or '').splitlines() body_b = (doc_b.body or '').splitlines() name_a = '%s v%s' % (slug, a) name_b = '%s v%s' % (slug, b) diff = unified_diff(body_a, body_b, name_a, name_b) return render_template('diff.j2', page=Document.get(slug), diff=diff)
def deactivate(slug): if request.method == 'GET': return render_template('deactivate.j2') if not current_user.admin: return render_template('error.j2', error=_('You are not allowed to ' 'deactivate this page')), 403 Document.deactivate(slug) Metadata.deactivate(slug) db.session.commit() return redirect(url_for('read', slug=slug))
def test_search(database): docs = Document.search('') assert len(docs) == 3 docs = Document.search('111') assert len(docs) == 2 docs = Document.search('222') assert len(docs) == 1 docs = Document.search('333') assert len(docs) == 0 docs = Document.search('', filters=[('x', '1')]) assert len(docs) == 2 docs = Document.search('', filters=[('x', '1'), ('x', '2')]) assert len(docs) == 1 docs = Document.search('', filters=[('x', '1'), ('y', '2')]) assert len(docs) == 1 docs = Document.search('111', filters=[('x', '1')]) assert len(docs) == 1
def recent_changes(): page_arg = get_int_arg('page', 1) changes = Document.changes() \ .paginate(page_arg, 100) return render_template('recent-changes.j2', changes=changes)
def run(self, parent, blocks): block = blocks.pop(0) (directive, rest) = block.split(' ', 1) config = json.loads(rest) raw = config.get('raw', False) filters = config.get('filters') if filters: filters = filters.items() items = Document.search(filters=filters) if items: if raw: items = ['- [[' + item.slug + ']]' for item in items] else: items = ['- [' + item.title + '](' + item.slug + ')' for item in items] else: items = ['- *No results found*'] blocks.insert(0, '\n'.join(items))
def activate(slug): version = get_int_arg('version') if not version: return render_template('error.j2', error=_('Missing version ' 'parameter')) if request.method == 'GET': return render_template('activate.j2') if not current_user.admin: return render_template('error.j2', error=_('You are not allowed to ' 'activate this page')), 403 page = Document.get(slug, version) parsed = parse(page.body) for key, values in parsed['meta'].items(): for value in values: Metadata(slug=slug, key=key, value=value) \ .save() page.save() db.session.commit() return redirect(url_for('read', slug=slug))
def history(slug): page_arg = get_int_arg('page', 1) page = Document.get(slug) history = page.history() \ .paginate(page_arg, 100) return render_template('history.j2', page=page, history=history)
def recent_changes_atom(): page_arg = get_int_arg('page', 1) changes = Document.changes() \ .paginate(page_arg, 100) return render_template('recent-changes_atom.j2', changes=changes, now=datetime.datetime.utcnow())
def history_atom(slug): page_arg = get_int_arg('page', 1) page = Document.get(slug) history = page.history() \ .paginate(page_arg, 100) return render_template('history_atom.j2', page=page, history=history, now=datetime.datetime.utcnow())
def test_history(database): history = Document.get('a') \ .history() \ .all() assert len(history) == 2 history = Document.get('b') \ .history() \ .all() assert len(history) == 1 history = Document.get('c') \ .history() \ .all() assert len(history) == 1
def test_get(database): assert Document.get('a').id is not None assert Document.get('b').id is not None assert Document.get('c').id is not None assert Document.get('d').id is None assert '222' in Document.get('a').body assert '111' in Document.get('b').body assert '111' in Document.get('c').body assert 'Describe' in Document.get('d').body doc = Document.get('a', '1') assert '111' in doc.body assert not doc.active
def load_fixtures(): '''Load fixtures into the database.''' cwd = getcwd() dir = path.join(cwd, FIXTURES_DIR) for filename in listdir(dir): file = path.join(dir, filename) if not path.isfile(file) or not file.endswith('.md'): continue print(' * Loading %s' % filename) slug = filename[:-3] page = Document.get(slug=slug) if page.id: print(' * Document exists, skipping') continue body = codecs.open(file, 'r', 'utf-8') \ .read() parsed = parse(body) title = parsed['title'] or slug Metadata.deactivate(slug) Document.deactivate(slug) for key, values in parsed['meta'].items(): for value in values: Metadata(slug=slug, key=key, value=value) \ .save() Document(slug=slug, title=title, body=body) \ .save() db.session.commit()
def test_timestamp(database): assert Document().ymd is None assert Document().hm is None assert Document().ymd_hm is None assert Document.get('a').ymd is not None assert Document.get('a').hm is not None assert Document.get('a').ymd_hm is not None
def test_facets(database): docs = Document.search('') facets = Document.facets(docs) assert 'x' in facets assert 'y' in facets docs = Document.search('', filters=[('x', '3')]) facets = Document.facets(docs, ignores=['x']) assert not facets docs = Document.search('', filters=[('x', '1')]) facets = Document.facets(docs, ignores=['x']) assert 'y' in facets
def read(slug=None): version = get_int_arg('version') page = Document.get(slug or param('frontpage', 'front-page'), version=version) if version: if not page: return render_template('error.j2', error=_('This version does not ' 'exist')), 404 flash(_('You are viewing version %(version)s of this page', version=version)) return render_template('read.j2', page=page) parsed = parse(page.body) if 'page' in parsed['redirect']: page_name = page.title or page.slug flash(_('Redirected from %(page_name)s', page_name=page_name)) return redirect(url_for('read', slug=parsed['redirect']['page'])) return render_template('read.j2', page=page), 200 if page.id else 404
def database(): db.drop_all() db.create_all() Document(slug='a', title='A', body='111').save() db.session.commit() Document.deactivate('a') db.session.commit() Document(slug='a', title='A', body='222').save() Document(slug='b', title='B', body='111').save() Document(slug='c', title='C', body='111').save() Metadata(slug='a', key='x', value='1').save() Metadata(slug='a', key='y', value='2').save() Metadata(slug='b', key='x', value='1').save() Metadata(slug='b', key='x', value='2').save() db.session.commit()
def test_titles(database): titles = Document.titles().all() assert len(titles) == 3
def read_md(slug): page = Document.get(slug) return page.body, 200 if page.id else 404, { 'Content-Type': 'text/markdown; charset=utf-8' }
def test_count(database): assert Document.count() == 3
def test_meta(database): assert Document.get('a').meta() != [] assert Document.get('b').meta() != [] assert Document.get('c').meta() == [] assert Document.get('d').meta() == []
def title_index(): return render_template('title-index.j2', titles=Document.titles())
def page(self): return Document.get(self.slug)
def test_load_fixtures(): db.create_all() load_fixtures() assert Document.count() == 3
def test_changes(database): changes = Document.changes().all() assert len(changes) == 4