<commit_before>import datetime
import difflib

from flask import Blueprint
from flask import flash, redirect, render_template, request, url_for
from flask.ext.login import current_user
from flask import abort

from viaduct import db
from viaduct.helpers import flash_form_errors
from viaduct.forms import EditPageForm, HistoryPageForm
from viaduct.models import Group
from viaduct.models.page import Page, PageRevision, PagePermission

blueprint = Blueprint('page', __name__)

def get_error_page(path=''):
	revisions = []

	class struct(object):
		pass

	data = struct()
	data.title = 'Oh no! It looks like you have found a dead Link!'
	data.content = '![alt text](/static/img/404.png "404")'
	data.filter_html = True
	data.path = ''

	revisions.append(data)

	return render_template('page/get_page.htm', revisions=revisions)

@blueprint.route('/favicon.ico', methods=['GET', 'POST'])
def favicon_route():
	return "None";

@blueprint.route('/', methods=['GET', 'POST'])
@blueprint.route('/<path:path>', methods=['GET', 'POST'])
def get_page(path=''):
	revisions = []

	print 

	is_main_page = False
	if path == '' or path == 'index':
		paths = ['laatste_bestuursblog', 'activities', 'twitter', 'contact']
		is_main_page = True
	else:
		paths = [path]

	print paths
	for path in paths:
		page = Page.query.filter(Page.path==path).first()

		if not page:
			page = Page('')

		if page.revisions.count() > 0:
			revision = page.revisions.order_by(PageRevision.id.desc()).first()

			if not revision.author.id == current_user.id:
				if not page.can_read(current_user):
					if not path == 'activities':
						abort(403)
		else:
			revision = PageRevision(page, current_user, 'Oh no! It looks like' +
				' you have found a dead Link!',
				'![alt text](/static/img/404.png "404")', True)


		class struct(object):
			pass

		data = struct()
		data.is_main_page = is_main_page
		data.title = revision.title
		data.content = revision.content
		data.filter_html = revision.filter_html
		data.path = path

		revisions.append(data)

	return render_template('page/get_page.htm', revisions=revisions)

@blueprint.route('/history/', methods=['GET', 'POST'])
@blueprint.route('/history/<path:path>', methods=['GET', 'POST'])
def get_page_history(path=''):
	#if not current_user.is_authenticated():
	#	return get_error_page()

	form = HistoryPageForm(request.form)

	page = Page.query.filter(Page.path==path).first()

	if not page.can_write(current_user):
		abort(403)

	if not page:
		page = Page('')

	if page.revisions.count() > 0:
		revisions = page.revisions.order_by(PageRevision.id.desc()).all()
	else:
		revisions = None

	form.previous.choices = [(revision.id, '') for revision in revisions]
	form.current.choices = [(revision.id, '') for revision in revisions]

	if form.validate_on_submit():
		previous = request.form['previous']
		current = request.form['current']

		previous_revision = page.revisions.filter(PageRevision.id==previous).first()
		current_revision = page.revisions.filter(PageRevision.id==current).first()

		diff = difflib.HtmlDiff().make_table(previous_revision.content.splitlines(),
			current_revision.content.splitlines())

		return render_template('page/compare_page_history.htm', diff=diff)

	return render_template('page/get_page_history.htm', form=form,
		revisions=zip(revisions, form.previous, form.current))

@blueprint.route('/edit/', methods=['GET', 'POST'])
@blueprint.route('/edit/<path:path>', methods=['GET', 'POST'])
def edit_page(path=''):
	class struct(object):
		pass

#	if not current_user.is_authenticated():
#		return get_error_page()

	page = Page.query.filter(Page.path==path).first()


	#UNCOMMEND AFTER MERGE!!!
	#if not ModulePermissionApi.can_write('page'):
		#abort(403)

	data = None

	if page:
		revision = page.revisions.order_by(PageRevision.id.desc()).first()

		if revision:
			if not revision.author.id == current_user.id:
				if not page.can_write(current_user):
					abort(403)

			data = struct()
			data.title = revision.title
			data.content = revision.content
			data.filter_html = not revision.filter_html
			data.path = path

	form = EditPageForm(request.form, obj=data)

	groups = Group.query.all()


	if form.validate_on_submit():
		title = request.form['title'].strip()
		content = request.form['content'].strip()<commit_msg>module write check page permissions<commit_after>import datetime
import difflib

from flask import Blueprint
from flask import flash, redirect, render_template, request, url_for
from flask.ext.login import current_user
from flask import abort

from viaduct import db
from viaduct.helpers import flash_form_errors
from viaduct.forms import EditPageForm, HistoryPageForm
from viaduct.models import Group
from viaduct.models.page import Page, PageRevision, PagePermission
from viaduct.api.group import GroupPermissionAPI

blueprint = Blueprint('page', __name__)

def get_error_page(path=''):
	revisions = []

	class struct(object):
		pass

	data = struct()
	data.title = 'Oh no! It looks like you have found a dead Link!'
	data.content = '![alt text](/static/img/404.png "404")'
	data.filter_html = True
	data.path = ''

	revisions.append(data)

	return render_template('page/get_page.htm', revisions=revisions)

@blueprint.route('/favicon.ico', methods=['GET', 'POST'])
def favicon_route():
	return "None";

@blueprint.route('/', methods=['GET', 'POST'])
@blueprint.route('/<path:path>', methods=['GET', 'POST'])
def get_page(path=''):
	revisions = []

	print 

	is_main_page = False
	if path == '' or path == 'index':
		paths = ['laatste_bestuursblog', 'activities', 'twitter', 'contact']
		is_main_page = True
	else:
		paths = [path]

	print paths
	for path in paths:
		page = Page.query.filter(Page.path==path).first()

		if not page:
			page = Page('')

		if page.revisions.count() > 0:
			revision = page.revisions.order_by(PageRevision.id.desc()).first()

			if not revision.author.id == current_user.id:
				if not page.can_read(current_user):
					if not path == 'activities':
						abort(403)
		else:
			revision = PageRevision(page, current_user, 'Oh no! It looks like' +
				' you have found a dead Link!',
				'![alt text](/static/img/404.png "404")', True)


		class struct(object):
			pass

		data = struct()
		data.is_main_page = is_main_page
		data.title = revision.title
		data.content = revision.content
		data.filter_html = revision.filter_html
		data.path = path

		revisions.append(data)

	return render_template('page/get_page.htm', revisions=revisions)

@blueprint.route('/history/', methods=['GET', 'POST'])
@blueprint.route('/history/<path:path>', methods=['GET', 'POST'])
def get_page_history(path=''):
	#if not current_user.is_authenticated():
	#	return get_error_page()

	form = HistoryPageForm(request.form)

	page = Page.query.filter(Page.path==path).first()

	if not page.can_write(current_user):
		abort(403)

	if not page:
		page = Page('')

	if page.revisions.count() > 0:
		revisions = page.revisions.order_by(PageRevision.id.desc()).all()
	else:
		revisions = None

	form.previous.choices = [(revision.id, '') for revision in revisions]
	form.current.choices = [(revision.id, '') for revision in revisions]

	if form.validate_on_submit():
		previous = request.form['previous']
		current = request.form['current']

		previous_revision = page.revisions.filter(PageRevision.id==previous).first()
		current_revision = page.revisions.filter(PageRevision.id==current).first()

		diff = difflib.HtmlDiff().make_table(previous_revision.content.splitlines(),
			current_revision.content.splitlines())

		return render_template('page/compare_page_history.htm', diff=diff)

	return render_template('page/get_page_history.htm', form=form,
		revisions=zip(revisions, form.previous, form.current))

@blueprint.route('/edit/', methods=['GET', 'POST'])
@blueprint.route('/edit/<path:path>', methods=['GET', 'POST'])
def edit_page(path=''):
	class struct(object):
		pass

#	if not current_user.is_authenticated():
#		return get_error_page()

	page = Page.query.filter(Page.path==path).first()

	data = None

	if page:
		revision = page.revisions.order_by(PageRevision.id.desc()).first()

		if revision:
			if not revision.author.id == current_user.id:
				if not page.can_write(current_user):
					abort(403)

			data = struct()
			data.title = revision.title
			data.content = revision.content
			data.filter_html = not revision.filter_html
			data.path = path
	else:
		if not(GroupPermissionAPI.can_write('page')):
			return abort(403);


	form = EditPageForm(request.form, obj=data)

	groups = Group.query.all()


	if form.validate_on_submit():
		title = request.form['title'].strip()
		content = request.form['content'].strip()
{'fraction_non_alphanumeric': 0.1467932391757351, 'fraction_numerical': 0.006019912016670526, 'mean_word_length': 4.813593539703903, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '772892', 'n_tokens_mistral': 3192, 'n_tokens_neox': 2839, 'n_words': 633}