# Path: pynuts/rights.py
# class allow_if(object):  # pylint: disable=C0103
#     """Check that the global context matches a criteria."""
#     def __init__(self, auth_fun, exception=None):
#         self.auth_fun = auth_fun
#         self.exception = exception
# 
#     def __call__(self, function):
#         """Check the global context."""
#         @wraps(function)
#         def check_auth(*args, **kwargs):
#             """Function wrapper."""
#             if self.auth_fun():
#                 return function(*args, **kwargs)
#             else:
#                 if self.exception:
#                     raise self.exception
#                 else:
#                     abort(403)
# 
#         check_auth._auth_fun = self.auth_fun
#         return check_auth
# 
# Path: pynuts/directives.py
# class Editable(_Tag):
#     """ReST directive creating an HTML contenteditable tag.
# 
#     The directive's argument must look like this:
#     `document_type/document_id/version/part`
# 
#     You can put those optional attributes on your editable content:
# 
#     - `title`
#     - `id`
#     - `class`
# 
#     """
#     option_spec = {
#         'title': directives.unchanged,
#         'class': directives.class_option,
#         'id': directives.class_option,
#         'contenteditable': directives.class_option,
#     }
# 
#     def run(self):
#         try:
#             super(Editable, self).run()
#         except NotFoundError:
#             self.parsed_content = docutils.core.publish_parts(
#                 '\n'.join(self.content), writer_name='html')['body']
#         for key in self.option_spec:
#             if key == 'title':
#                 self.attributes[key] = self.options.get(key)
#             elif key == 'contenteditable':
#                 self.attributes[key] = ' '.join(self.options.get(key, ['true']))
#             else:
#                 self.attributes[key] = ' '.join(self.options.get(key, []))
#         content = '<div %s>%s</div>' % (
#             ' '.join(('%s="%s"' % a) for a in self.attributes.items()),
#             self.parsed_content or '')
#         return [docutils.nodes.raw('', content, format='html')]


from flask import flash, redirect, session, request, render_template, g
from application import app
from pynuts.rights import allow_if
from pynuts.directives import Editable
import view
import document
import rights as Is

@app.errorhandler(403)
@app.route('/login')
def login(error=None):
    return render_template('login.html')


@app.route('/login', methods=('POST', ))
def login_post():
    login = request.form.get('login')
    password = request.form.get('password')
    employee = view.EmployeeView.model.query.filter_by(
        login=login, password=password).first()
    if employee:
        session['id'] = employee.person_id
        session['login'] = login
        flash('You were logged in !', 'ok')
    else:
        flash('invalid credentials', 'error')
    return redirect('employees')


@app.route('/logout')
def logout():
    session.clear()
    return redirect('employees')


@app.route('/')
@app.route('/employees/')

# Path: pynuts/rights.py
# class allow_if(object):  # pylint: disable=C0103
#     """Check that the global context matches a criteria."""
#     def __init__(self, auth_fun, exception=None):
#         self.auth_fun = auth_fun
#         self.exception = exception
# 
#     def __call__(self, function):
#         """Check the global context."""
#         @wraps(function)
#         def check_auth(*args, **kwargs):
#             """Function wrapper."""
#             if self.auth_fun():
#                 return function(*args, **kwargs)
#             else:
#                 if self.exception:
#                     raise self.exception
#                 else:
#                     abort(403)
# 
#         check_auth._auth_fun = self.auth_fun
#         return check_auth
# 
# Path: pynuts/directives.py
# class Editable(_Tag):
#     """ReST directive creating an HTML contenteditable tag.
# 
#     The directive's argument must look like this:
#     `document_type/document_id/version/part`
# 
#     You can put those optional attributes on your editable content:
# 
#     - `title`
#     - `id`
#     - `class`
# 
#     """
#     option_spec = {
#         'title': directives.unchanged,
#         'class': directives.class_option,
#         'id': directives.class_option,
#         'contenteditable': directives.class_option,
#     }
# 
#     def run(self):
#         try:
#             super(Editable, self).run()
#         except NotFoundError:
#             self.parsed_content = docutils.core.publish_parts(
#                 '\n'.join(self.content), writer_name='html')['body']
#         for key in self.option_spec:
#             if key == 'title':
#                 self.attributes[key] = self.options.get(key)
#             elif key == 'contenteditable':
#                 self.attributes[key] = ' '.join(self.options.get(key, ['true']))
#             else:
#                 self.attributes[key] = ' '.join(self.options.get(key, []))
#         content = '<div %s>%s</div>' % (
#             ' '.join(('%s="%s"' % a) for a in self.attributes.items()),
#             self.parsed_content or '')
#         return [docutils.nodes.raw('', content, format='html')]


# Path: docs/example/advanced/executable.py
from flask import flash, redirect, session, request, render_template, g
from application import app
from pynuts.rights import allow_if
from pynuts.directives import Editable
import view
import document
import rights as Is


@app.errorhandler(403)
@app.route('/login')
def login(error=None):
    return render_template('login.html')


@app.route('/login', methods=('POST', ))
def login_post():
    login = request.form.get('login')
    password = request.form.get('password')
    employee = view.EmployeeView.model.query.filter_by(
        login=login, password=password).first()
    if employee:
        session['id'] = employee.person_id
        session['login'] = login
        flash('You were logged in !', 'ok')
    else:
        flash('invalid credentials', 'error')
    return redirect('employees')


@app.route('/logout')
def logout():
    session.clear()
    return redirect('employees')


@app.route('/')
@app.route('/employees/')

@allow_if(Is.connected)