# Path: papaye/schemas.py
# class LoginSchema(colander.MappingSchema):
#     username = colander.SchemaNode(String(), missing=None,
#                                    validator=username_validator)
#     password = colander.SchemaNode(
#         String(), missing=None, widget=PasswordWidget()
#     )
# 
#     def validator(self, node, appstruct):
#         request = node.bindings['request']
#         username = appstruct.get('username') or ''
#         password = appstruct.get('password') or ''
#         error = colander.Invalid(node, 'username and/or password invalid')
#         username_matching_users = [
#             user for user in request.root
#             if user.username == username
#         ]
#         if not len(username_matching_users):
#             raise error
#         user = username_matching_users[0]
#         if not user.password_verify(password):
#             raise error


import json
import logging
from deform import Form
from deform.exception import ValidationFailure
from pyramid.httpexceptions import HTTPMovedPermanently
from pyramid.response import Response
from pyramid.security import remember, NO_PERMISSION_REQUIRED
from pyramid.view import view_config
from papaye.schemas import LoginSchema
    from pyramid.security import forget


logger = logging.getLogger(__name__)


@view_config(route_name='home', renderer='index.jinja2', request_method='GET')
def index_view(context, request):
    username = request.session.get('username', '')
    app_context = {
        'username': username,
        'papaye': {
            'debug': False,
        },
        'urls':  {
            'login': request.route_url('login'),
            'logout': request.route_url('logout'),
            'package_resource': request.route_url('packages'),
            'api': request.route_url('api'),
            'simple': request.route_url('simple', traverse=()),
        }
    }
    return {'app_context': json.dumps(app_context)}


@view_config(route_name="login", renderer='login.jinja2',
             permission=NO_PERMISSION_REQUIRED)
class LoginView(object):

    def __init__(self, request):
        self.request = request

# Path: papaye/schemas.py
# class LoginSchema(colander.MappingSchema):
#     username = colander.SchemaNode(String(), missing=None,
#                                    validator=username_validator)
#     password = colander.SchemaNode(
#         String(), missing=None, widget=PasswordWidget()
#     )
# 
#     def validator(self, node, appstruct):
#         request = node.bindings['request']
#         username = appstruct.get('username') or ''
#         password = appstruct.get('password') or ''
#         error = colander.Invalid(node, 'username and/or password invalid')
#         username_matching_users = [
#             user for user in request.root
#             if user.username == username
#         ]
#         if not len(username_matching_users):
#             raise error
#         user = username_matching_users[0]
#         if not user.password_verify(password):
#             raise error


# Path: papaye/views/index.py
import json
import logging
from deform import Form
from deform.exception import ValidationFailure
from pyramid.httpexceptions import HTTPMovedPermanently
from pyramid.response import Response
from pyramid.security import remember, NO_PERMISSION_REQUIRED
from pyramid.view import view_config
from papaye.schemas import LoginSchema
    from pyramid.security import forget



logger = logging.getLogger(__name__)


@view_config(route_name='home', renderer='index.jinja2', request_method='GET')
def index_view(context, request):
    username = request.session.get('username', '')
    app_context = {
        'username': username,
        'papaye': {
            'debug': False,
        },
        'urls':  {
            'login': request.route_url('login'),
            'logout': request.route_url('logout'),
            'package_resource': request.route_url('packages'),
            'api': request.route_url('api'),
            'simple': request.route_url('simple', traverse=()),
        }
    }
    return {'app_context': json.dumps(app_context)}


@view_config(route_name="login", renderer='login.jinja2',
             permission=NO_PERMISSION_REQUIRED)
class LoginView(object):

    def __init__(self, request):
        self.request = request

        self.schema = LoginSchema().bind(request=self.request)