<commit_before># Copyright (c) 2014 by Ecreall under licence AGPL terms 
# avalaible on http://www.gnu.org/licenses/agpl.html 

# licence: AGPL
# author: <NAME>

import colander
import deform
from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound

from substanced.interfaces import IUserLocator, IPasswordReset
from substanced.principal import DefaultUserLocator

from dace.processinstance.core import  Behavior
from pontus.form import FormView
from pontus.schema import Schema

from novaideo import _
from novaideo.content.novaideo_application import NovaIdeoApplication


class Send(Behavior):

    behavior_id = "send"
    title = _("Send")
    description = ""

    def start(self, context, request, appstruct, **kw):
        login = appstruct['email']
        adapter = request.registry.queryMultiAdapter(
                    (context, request),
                    IUserLocator
                    )
        if adapter is None:
            adapter = DefaultUserLocator(context, request)

        user = adapter.get_user_by_email(login)
        if user is not None:
            user.email_password_reset(request)

        return True

    def redirect(self, context, request, **kw):
        return HTTPFound(request.resource_url(context, ""))


@colander.deferred
def login_validator(node, kw):
    context = kw['context']
    request = kw['request']
    def _login_validator(node, value):
        adapter = request.registry.queryMultiAdapter(
                    (context, request),
                    IUserLocator
                    )
        if adapter is None:
            adapter = DefaultUserLocator(context, request)

        user = adapter.get_user_by_email(value)
        if user is None:
            raise colander.Invalid(node, 'No such user %s' % value)

    return _login_validator


class ResetRequestSchema(Schema):
    """ The schema for validating password reset requests."""
    email = colander.SchemaNode(
        colander.String(),
        validator = login_validator,
        )


@view_config(
    name='resetpassword',
    context=NovaIdeoApplication,
    renderer='pontus:templates/views_templates/grid.pt',
    )
class ResetRequestView(FormView):
    title = _('Request Password Reset')
    schema = ResetRequestSchema()
    behaviors = [Send]



class Reset(Behavior):
    behavior_id = "send"
    title = _("Send")
    description = ""

    def start(self, context, request, appstruct, **kw):
        context.reset_password(appstruct['new_password'])
        return True

    def redirect(self, context, request, **kw):
        return HTTPFound(request.resource_url(request.virtual_root, "@@login"))


class ResetSchema(Schema):
    """ The schema for validating password reset requests."""
    new_password = colander.SchemaNode(
        colander.String(),
        validator = colander.Length(min=3, max=100),
        widget = deform.widget.CheckedPasswordWidget(),
        )<commit_msg>Execution error management<commit_after># Copyright (c) 2014 by Ecreall under licence AGPL terms 
# avalaible on http://www.gnu.org/licenses/agpl.html 

# licence: AGPL
# author: <NAME>

import colander
import deform
from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound

from substanced.interfaces import IUserLocator, IPasswordReset
from substanced.principal import DefaultUserLocator

from dace.processinstance.core import  Behavior
from pontus.form import FormView
from pontus.schema import Schema

from novaideo import _
from novaideo.content.novaideo_application import NovaIdeoApplication


class Send(Behavior):

    behavior_id = "send"
    title = _("Send")
    description = ""

    def start(self, context, request, appstruct, **kw):
        login = appstruct['email']
        adapter = request.registry.queryMultiAdapter(
                    (context, request),
                    IUserLocator
                    )
        if adapter is None:
            adapter = DefaultUserLocator(context, request)

        user = adapter.get_user_by_email(login)
        if user is not None:
            user.email_password_reset(request)

        return {}

    def redirect(self, context, request, **kw):
        return HTTPFound(request.resource_url(context, ""))


@colander.deferred
def login_validator(node, kw):
    context = kw['context']
    request = kw['request']
    def _login_validator(node, value):
        adapter = request.registry.queryMultiAdapter(
                    (context, request),
                    IUserLocator
                    )
        if adapter is None:
            adapter = DefaultUserLocator(context, request)

        user = adapter.get_user_by_email(value)
        if user is None:
            raise colander.Invalid(node, 'No such user %s' % value)

    return _login_validator


class ResetRequestSchema(Schema):
    """ The schema for validating password reset requests."""
    email = colander.SchemaNode(
        colander.String(),
        validator = login_validator,
        )


@view_config(
    name='resetpassword',
    context=NovaIdeoApplication,
    renderer='pontus:templates/views_templates/grid.pt',
    )
class ResetRequestView(FormView):
    title = _('Request Password Reset')
    schema = ResetRequestSchema()
    behaviors = [Send]



class Reset(Behavior):
    behavior_id = "send"
    title = _("Send")
    description = ""

    def start(self, context, request, appstruct, **kw):
        context.reset_password(appstruct['new_password'])
        return {}

    def redirect(self, context, request, **kw):
        return HTTPFound(request.resource_url(request.virtual_root, "@@login"))


class ResetSchema(Schema):
    """ The schema for validating password reset requests."""
    new_password = colander.SchemaNode(
        colander.String(),
        validator = colander.Length(min=3, max=100),
        widget = deform.widget.CheckedPasswordWidget(),
        )
{'fraction_non_alphanumeric': 0.09446419502285425, 'fraction_numerical': 0.002708650753343491, 'mean_word_length': 2.915175612988734, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5929121', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1599, 'n_words': 416}