def search(): # TODO: add filter by user keyword = request.args.get('keyword') start_date = request.args.get('start') end_date = request.args.get('end') kwargs = dict() form = SearchForm(search=keyword, start_date=start_date, end_date=end_date) if keyword and not keyword.isspace(): filters = dict() if start_date: start_date = datetime.strptime(start_date, '%m/%d/%Y') filters['modified_on__gte'] = temp if end_date: end_date = datetime.strptime(end_date, '%m/%d/%Y') + timedelta(days=1) filters['modified_on__lte'] = temp query_set = (WikiPage.objects(**filters).search_text(keyword).only( 'title', 'modified_on', 'modified_by').order_by('$text_score', '-modified_on')) kwargs = paginate(query_set) if form.validate_on_submit(): return redirect( url_for('.search', keyword=form.search.data, start=form.start_date.data, end=form.end_date.data)) return render_template('wiki/search.html', form=form, **kwargs)
def page(wiki_page_id): form = CommentForm() wiki_page = (WikiPage.objects.exclude('versions', 'refs').get_or_404(id=wiki_page_id)) if form.validate_on_submit(): _, comment_html = markdown(wiki_page, form.textArea.data) new_comment = WikiComment(id='{}-{}'.format( datetime.utcnow().strftime('%s.%f'), current_user.id), author=current_user.name, html=comment_html, md=form.textArea.data) (WikiPage.objects(id=wiki_page_id).update_one( push__comments=new_comment)) user_emails = [u.email for u in g.users_to_email] msg = '{0} ({1}) mentioned you at <a href="{2}">{3}</a>'\ .format( current_user.name, current_user.email, request.base_url, wiki_page.title) send_email(user_emails, 'You are mentioned', msg) return redirect(url_for('.page', wiki_page_id=wiki_page_id)) return render_template('wiki/page.html', wiki_page=wiki_page, form=form)
def reference(wiki_page_id): wiki_page = WikiPage.objects.only('title').get_or_404(id=wiki_page_id) wiki_referencing_pages = (WikiPage.objects( refs__contains=wiki_page_id).only('title').all()) return render_template('wiki/reference.html', wiki_page=wiki_page, wiki_referencing_pages=wiki_referencing_pages)
def keypage_edit(): wiki_keypages = (WikiPage.objects( keypage__exists=True).only('title').order_by('+keypage')) keypage_titles = [wiki_keypage.title for wiki_keypage in wiki_keypages] form = KeyPageEditForm(textArea='\n'.join(keypage_titles)) if form.validate_on_submit(): (WikiPage.objects(keypage__exists=True).update(unset__keypage=1)) new_titles = form.textArea.data.splitlines() for i, new_title in enumerate(new_titles): (WikiPage.objects(title=new_title).update_one(set__keypage=i + 1)) return redirect(url_for('wiki.home')) else: flash_errors(form) return render_template('admin/keypage_edit.html', form=form)
def rename(wiki_page_id): wiki_page = WikiPage.objects.only('title').get_or_404(id=wiki_page_id) if wiki_page.title == 'Home': return redirect(url_for('.home')) form = RenameForm(new_title=wiki_page.title) if form.validate_on_submit(): new_title = form.new_title.data if wiki_page.title == new_title: flash('The page name is not changed.', 'warning') elif WikiPage.objects(title=new_title).count() > 0: flash('The new page title has already been taken.', 'danger') else: old_md = '[[{}]]'.format(wiki_page.title) new_md = '[[{}]]'.format(new_title) old_html = render_wiki_page(wiki_page.id, wiki_page.title) new_html = render_wiki_page(wiki_page.id, new_title) # update the markdown of referencing wiki page wiki_referencing_pages = (WikiPage.objects( refs__contains=wiki_page_id).only('md', 'html', 'comments').all()) for ref in wiki_referencing_pages: new_md_content = ref.md.replace(old_md, new_md) new_html_content = ref.html.replace(old_html, new_html) (WikiPage.objects(id=ref.id).update_one( set__md=new_md_content, set__html=new_html_content)) # update renamed page title in comments for wp in WikiPage.objects( comments__md__contains=old_md).only('comments'): for comment in wp.comments: comment.md = comment.md.replace(old_md, new_md) comment.html = comment.html.replace(old_html, new_html) WikiPage.objects(id=wp.id).update_one(comments=wp.comments) # update the diff of related wiki page versions for pv in WikiPageVersion.objects.search_text(old_md).all(): pv.diff = pv.diff.replace(old_md, new_md) pv.save() (WikiPage.objects(id=wiki_page.id).update_one( set__title=new_title)) return redirect(url_for('.page', wiki_page_id=wiki_page.id)) else: flash_errors(form) return render_template('wiki/rename.html', wiki_page=wiki_page, form=form)
def home(): """Manage wiki groups.""" all_wiki_groups = WikiGroup.objects.all() form = AddWikiGroupForm() # Create a new wiki group with its own database and static file directory if form.validate_on_submit(): new_wiki_group_name = form.wiki_group_name.data new_db_name = new_wiki_group_name.replace(' ', '') # Save the name of the new wiki group in database `_admin` # Remove whitespaces in the wiki group name. # Then use it to name the database which is about to be initialized. new_group = WikiGroup(name=new_wiki_group_name, db_name=new_db_name, active=True) # Initialize a new database for the just-created group # Make sure the new group name is not occupied. if new_group.db_name in db.connection.database_names(): flash('Wiki group already exists', 'danger') else: try: os.mkdir( os.path.join(current_app.config['UPLOAD_PATH'], new_group.db_name)) new_group.save() db.register_connection( alias=new_group.db_name, name=new_group.db_name, host=current_app.config['MONGODB_SETTINGS']['host'], port=current_app.config['MONGODB_SETTINGS']['port']) new_user = WikiUser(name=form.username.data, email=form.email.data, is_admin=True) new_user.set_password(form.password.data) new_user.switch_db(new_group.db_name).save() WikiPage(title='Home').switch_db(new_group.db_name).save() flash('New wiki group added', 'success') return redirect(url_for('.home')) except FileExistsError: flash('Upload directory already exists', 'danger') else: flash_errors(form) return render_template('super_admin/home.html', form=form, all_wiki_groups=all_wiki_groups)
def wiki_page(self, title): wiki_page_title = title wiki_page = (WikiPage .objects(title=wiki_page_title) .only('title') .first()) if not wiki_page: wiki_page = WikiPage( title=wiki_page_title, modified_by=current_user.name ).save() g.wiki_page.refs.append(wiki_page) return render_wiki_page(wiki_page.id, wiki_page_title)
def replace_file(): form = request.form file = request.files['wiki_file'] wiki_file_id = form.get('wiki_file_id', None) wiki_file_markdown = '[file:{0}]'.format(wiki_file_id) # save uploaded file with WikiFile.id as filename file.save( os.path.join(current_app.config['UPLOAD_PATH'], g.wiki_group, wiki_file_id)) wiki_file = WikiFile.objects(id=int(wiki_file_id)).first() (WikiFile.objects(id=int(wiki_file_id)).update_one( set__name=file.filename, set__mime_type=file.mimetype, # `size` is reserved set__size__=file.tell())) if wiki_file and wiki_file.name != file.filename: # re-render the wiki page markdown with replace file for wiki_page in WikiPage.objects( md__contains=wiki_file_markdown).only('md'): toc, html = markdown(wiki_page, wiki_page.md) (WikiPage.objects(id=wiki_page.id).update_one(set__toc=toc, set__html=html)) # re-render the wiki comment markdown with replace file for wiki_page in WikiPage.objects( comments__md__contains=wiki_file_markdown).only('comments'): for comment in wiki_page.comments: _, comment.html = markdown(wiki_page, comment.md) WikiPage.objects(id=wiki_page.id).update_one( comments=wiki_page.comments) return ''
def changes(): selected_wiki_user = request.args.get('user') wiki_users = WikiUser.objects.distinct('name') filters = dict() try: selected_index = wiki_users.index(selected_wiki_user) + 1 filters['modified_by'] = selected_wiki_user except ValueError: selected_index = 0 query_set = (WikiPage.objects(**filters).only( 'title', 'modified_on', 'modified_by').order_by('-modified_on')) kwargs = paginate(query_set) return render_template('wiki/changes.html', wiki_users=wiki_users, selected_index=selected_index, **kwargs)
def delete_comment(wiki_page_id): wiki_comment_id = request.args.get('comment') WikiPage.objects(id=wiki_page_id).update_one( pull__comments__id=wiki_comment_id) return redirect(url_for('.page', wiki_page_id=wiki_page_id))
def home(): """Home page.""" wiki_page = WikiPage.objects(title='Home').only('id').first() return redirect(url_for('.page', wiki_page_id=wiki_page.id))