<commit_before>from flask_login import current_user, login_required
from flask_wtf import Form
import wtforms as wtf

from uwiki.utils import sluggify_name

from uwiki.auth import ACL
from . import *


class PageForm(Form):
    title = wtf.TextField(validators=[wtf.validators.Required()])
    content = wtf.TextAreaField(validators=[wtf.validators.Required()])


def _common_fields():
    group = wtf.SelectField()
    group_perms = wtf.SelectField('Group Permissions', choices=[
        ('write', 'Read/Write'),
        ('read', 'Read'),
        ('list', 'None'),
        ('none', 'None & Hidden'),
    ], default='write')
    other_perms = wtf.SelectField('Other User Permissions', choices=[
        ('write', 'Read/Write'),
        ('read', 'Read'),
        ('list', 'None'),
        ('none', 'None & Hidden'),
    ], default='write')
    anon_perms = wtf.SelectField('Anonymous Permissions', choices=[
        ('read', 'Read'),
        ('list', 'None'),
        ('none', 'None & Hidden'),
    ], default='read')
    return group, group_perms, other_perms, anon_perms

class OwnerPageForm(PageForm):
    group, group_perms, other_perms, anon_perms = _common_fields()

class AdminPageForm(PageForm):

    # I don't like setting the ID field, but it should be okay to do this.
    # As long as you don't user the "owner" field directly until it is
    # comitted, this is okay.
    owner_id = wtf.SelectField('Owner', coerce=int, description='Page owners can modify non-custom permissions')

    group, group_perms, other_perms, anon_perms = _common_fields()

    custom_acl = wtf.TextAreaField('Custom ACL', description='For admin use only.')


@app.route('/wiki/')
@login_required
def page_index():
    pages = Page.query.all()
    pages.sort(key=lambda p:p.title)
    return render_template('page/index.haml', pages=pages)


@app.route('/wiki/<path:name>', methods=['GET', 'POST'])
def page(name='Index'):

    slug = sluggify_name(name)
    page = Page.query.filter(Page.name.like(slug)).first()

    # Make sure private pages stay that way.
    if page and not authz.can('page.read', page):
        if authz.can('page.list', page):
            abort(403)
        else:
            abort(404)

    # If it doesn't exist, don't let non-users see the page.
    if not page and not authz.can('page.create', ACL('ALLOW AUTHENTICATED ALL')):
        print 'HERE'
        abort(404)

    # Assert we are on the normalized page.
    if page and page.name != slug:
        return redirect(url_for('page', name=page.name))

    if request.args.get('action') == 'history':
        # TODO: Add a page.history.read perm.
        return render_template('page/history.haml', name=slug, page=page)

    if request.args.get('action') == 'edit':

        if page and not authz.can('page.write', page):
            return app.login_manager.unauthorized()

        is_admin = authz.can('page.admin', page) if page else authz.can('page.admin', ACL('ALLOW WHEEL ALL'))
        is_owner = not page or page.owner == current_user # current_user is a proxy, so "is" would fail

        if is_admin:
            form = AdminPageForm(request.form, page)
            # Manually coerce to 0 for the select field.
            form.owner_id.data = form.owner_id.data or 0
        elif is_owner:
            form = OwnerPageForm(request.form, page)
        else:
            form = PageForm(request.form, page)


        # Setup the owner/group fields.
        if is_admin or is_owner:
            users = User.query.all()
            if is_admin:
                form['owner_id'].choices = [
                    (u.id, '%s (%s)' % (u.display_name, u.name))
                    for u in User.query.all()
                ]
                form['owner_id'].choices.append((0, '<nobody>'))<commit_msg>Flask-WTF API changed a little.<commit_after>from flask_login import current_user, login_required
from flask_wtf import FlaskForm as Form
import wtforms as wtf

from uwiki.utils import sluggify_name

from uwiki.auth import ACL
from . import *


class PageForm(Form):
    title = wtf.TextField(validators=[wtf.validators.Required()])
    content = wtf.TextAreaField(validators=[wtf.validators.Required()])


def _common_fields():
    group = wtf.SelectField()
    group_perms = wtf.SelectField('Group Permissions', choices=[
        ('write', 'Read/Write'),
        ('read', 'Read'),
        ('list', 'None'),
        ('none', 'None & Hidden'),
    ], default='write')
    other_perms = wtf.SelectField('Other User Permissions', choices=[
        ('write', 'Read/Write'),
        ('read', 'Read'),
        ('list', 'None'),
        ('none', 'None & Hidden'),
    ], default='write')
    anon_perms = wtf.SelectField('Anonymous Permissions', choices=[
        ('read', 'Read'),
        ('list', 'None'),
        ('none', 'None & Hidden'),
    ], default='read')
    return group, group_perms, other_perms, anon_perms

class OwnerPageForm(PageForm):
    group, group_perms, other_perms, anon_perms = _common_fields()

class AdminPageForm(PageForm):

    # I don't like setting the ID field, but it should be okay to do this.
    # As long as you don't user the "owner" field directly until it is
    # comitted, this is okay.
    owner_id = wtf.SelectField('Owner', coerce=int, description='Page owners can modify non-custom permissions')

    group, group_perms, other_perms, anon_perms = _common_fields()

    custom_acl = wtf.TextAreaField('Custom ACL', description='For admin use only.')


@app.route('/wiki/')
@login_required
def page_index():
    pages = Page.query.all()
    pages.sort(key=lambda p:p.title)
    return render_template('page/index.haml', pages=pages)


@app.route('/wiki/<path:name>', methods=['GET', 'POST'])
def page(name='Index'):

    slug = sluggify_name(name)
    page = Page.query.filter(Page.name.like(slug)).first()

    # Make sure private pages stay that way.
    if page and not authz.can('page.read', page):
        if authz.can('page.list', page):
            abort(403)
        else:
            abort(404)

    # If it doesn't exist, don't let non-users see the page.
    if not page and not authz.can('page.create', ACL('ALLOW AUTHENTICATED ALL')):
        print 'HERE'
        abort(404)

    # Assert we are on the normalized page.
    if page and page.name != slug:
        return redirect(url_for('page', name=page.name))

    if request.args.get('action') == 'history':
        # TODO: Add a page.history.read perm.
        return render_template('page/history.haml', name=slug, page=page)

    if request.args.get('action') == 'edit':

        if page and not authz.can('page.write', page):
            return app.login_manager.unauthorized()

        is_admin = authz.can('page.admin', page) if page else authz.can('page.admin', ACL('ALLOW WHEEL ALL'))
        is_owner = not page or page.owner == current_user # current_user is a proxy, so "is" would fail

        if is_admin:
            form = AdminPageForm(request.form, obj=page)
            # Manually coerce to 0 for the select field.
            form.owner_id.data = form.owner_id.data or 0
        elif is_owner:
            form = OwnerPageForm(request.form, obj=page)
        else:
            form = PageForm(request.form, obj=page)


        # Setup the owner/group fields.
        if is_admin or is_owner:
            users = User.query.all()
            if is_admin:
                form['owner_id'].choices = [
                    (u.id, '%s (%s)' % (u.display_name, u.name))
                    for u in User.query.all()
                ]
                form['owner_id'].choices.append((0, '<nobody>'))
{'fraction_non_alphanumeric': 0.1369447739554501, 'fraction_numerical': 0.0031633056544088573, 'mean_word_length': 2.7434632461766157, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4408680', 'n_tokens_mistral': 2417, 'n_tokens_neox': 2326, 'n_words': 633}