<commit_before>import transaction
from yafowil.base import (
    factory,
    ExtractionError,
    UNSET,
)
from yafowil.yaml import parse_from_YAML
from yafowil.plone.form import Form
from zExceptions import Redirect
from zope.interface import implementer
from zope.component import getMultiAdapter
from zope.i18nmessageid import MessageFactory
from bda.plone.payment import Payments
from ..interfaces import (
    IFieldsProvider,
    ICheckoutAdapter,
)

_ = MessageFactory('bda.plone.checkout')


class ProviderRegistry(object):
    
    def __init__(self):
        self.providers = list()
    
    def add(self, factory):
        self.providers.append(factory)
    
    def __iter__(self):
        return self.providers.__iter__()

provider_registry = ProviderRegistry()


CHECKOUT = 0
CONFIRM = 1

class FormContext(object):
    
    @property
    def form_context(self):
        return self.request.get('checkout_confirm') and CONFIRM or CHECKOUT
    
    @property
    def mode(self):
        return self.form_context is CONFIRM and 'display' or 'edit'
    
    def get_value(self, widget, data):
        return self.request.get(widget.dottedpath, UNSET)


@implementer(IFieldsProvider)
class FieldsProvider(FormContext):
    fields_template = None
    fields_name = ''
    message_factory = _
    
    def __init__(self, context, request):
        self.context = context
        self.request = request
    
    def extend(self, form):
        fields = parse_from_YAML(self.fields_template, 
                                 self, self.message_factory)
        form[self.fields_name] = fields


class CartSummary(FieldsProvider):
    fields_name = 'cart_summary'
    
    def extend(self, form):
        if not self.form_context == CONFIRM:
            return
        compound = form[self.fields_name] = factory('compound', props={
            'structural': True})
        compound['heading'] = factory('tag', props={
            'structural': True,
            'tag': 'h2',
            'text': _('heading_cart_summary', 'Cart')})
        compound['overview'] = factory('tag', props={
            'structural': True,
            'class': 'cart_overview',
            'tag': 'div',
            'text': self.context.restrictedTraverse('@@cart_overview')()})

provider_registry.add(CartSummary)


class PersonalData(FieldsProvider):
    fields_template = 'bda.plone.checkout.browser:forms/personal_data.yaml'
    fields_name = 'personal_data'
    
    @property
    def gender_vocabulary(self):
        return [('-', '-'),
                ('male', _('male', 'Male')),
                ('female', _('female', 'Female'))]

provider_registry.add(PersonalData)


class BillingAddress(FieldsProvider):
    fields_template = 'bda.plone.checkout.browser:forms/billing_address.yaml'
    fields_name = 'billing_address'

provider_registry.add(BillingAddress)


class DeliveryAddress(FieldsProvider):
    fields_template = 'bda.plone.checkout.browser:forms/delivery_address.yaml'
    fields_name = 'delivery_address'
    
    def conditional_required(self, widget, data):
        if data.parent['alternative_delivery'].extracted and not data.extracted:
            raise ExtractionError(widget.attrs['conditional_required'])
        return data.extracted
    
    def get_alternative_delivery(self, widget, data):
        return widget.dottedpath in self.request
    
    @property
    def alternative_delivery_vocab(self):
        return {
            True: _('yes', 'Yes'),
            False: _('no', 'No'),
            UNSET: _('not set', 'not set'),
        }

provider_registry.add(DeliveryAddress)


class PaymentSelection(FieldsProvider):
    fields_template = 'bda.plone.checkout.browser:forms/payment_selection.yaml'
    fields_name = 'payment_selection'
    
    @property
    def payments(self):
        return Payments(self.context)
    
    @property
    def payment_vocabulary(self):
        return self.payments.vocab
    
    def get_payment(self, widget, data):
        return self.request.get(widget.dottedpath, self.payments.default)

provider_registry.add(PaymentSelection)


class OrderComment(FieldsProvider):
    fields_template = 'bda.plone.checkout.browser:forms/order_comment.yaml'
    fields_name = 'order_comment'

provider_registry.add(OrderComment)


class CheckoutForm(Form, FormContext):
    
    action_resource = '@@checkout'
    
    def prepare(self):
        self.form = factory('#form', name='checkout', props={
            'action': self.form_action})
        for fields_factory in provider_registry:
            fields_factory(self.context, self.request).extend(self.form)
        # checkout data input
        if not self.request.get('checkout_confirm') \
          and not self.request.get('action.checkout.finish'):
            self.form['checkout_back'] = factory('submit', props={
                'label': _('back', 'Back'),
                'action': 'checkout_back',
                'handler': None,
                'next': self.checkout_back,
                'skip': True})
            self.form['next'] = factory('submit', props={
                'label': _('next', 'Next'),
                'action': 'next',
                'handler': None,
                'next': self.checkout_summary})
        # checkout confirmation
        else:
            self.form['confirm_back'] = factory('submit', props={
                'label': _('back', 'Back'),
                'action': 'confirm_back',
                'handler': None,
                'next': self.confirm_back})
            self.form['finish'] = factory('submit', props={
                'label': _('finish', 'Finish'),
                'action': 'finish',
                'handler': self.finish,<commit_msg>add mode css class on form<commit_after>import transaction
from yafowil.base import (
    factory,
    ExtractionError,
    UNSET,
)
from yafowil.yaml import parse_from_YAML
from yafowil.plone.form import Form
from zExceptions import Redirect
from zope.interface import implementer
from zope.component import getMultiAdapter
from zope.i18nmessageid import MessageFactory
from bda.plone.payment import Payments
from ..interfaces import (
    IFieldsProvider,
    ICheckoutAdapter,
)

_ = MessageFactory('bda.plone.checkout')


class ProviderRegistry(object):
    
    def __init__(self):
        self.providers = list()
    
    def add(self, factory):
        self.providers.append(factory)
    
    def __iter__(self):
        return self.providers.__iter__()

provider_registry = ProviderRegistry()


CHECKOUT = 0
CONFIRM = 1

class FormContext(object):
    
    @property
    def form_context(self):
        return self.request.get('checkout_confirm') and CONFIRM or CHECKOUT
    
    @property
    def mode(self):
        return self.form_context is CONFIRM and 'display' or 'edit'
    
    def get_value(self, widget, data):
        return self.request.get(widget.dottedpath, UNSET)


@implementer(IFieldsProvider)
class FieldsProvider(FormContext):
    fields_template = None
    fields_name = ''
    message_factory = _
    
    def __init__(self, context, request):
        self.context = context
        self.request = request
    
    def extend(self, form):
        fields = parse_from_YAML(self.fields_template, 
                                 self, self.message_factory)
        form[self.fields_name] = fields


class CartSummary(FieldsProvider):
    fields_name = 'cart_summary'
    
    def extend(self, form):
        if not self.form_context == CONFIRM:
            return
        compound = form[self.fields_name] = factory('compound', props={
            'structural': True})
        compound['heading'] = factory('tag', props={
            'structural': True,
            'tag': 'h2',
            'text': _('heading_cart_summary', 'Cart')})
        compound['overview'] = factory('tag', props={
            'structural': True,
            'class': 'cart_overview',
            'tag': 'div',
            'text': self.context.restrictedTraverse('@@cart_overview')()})

provider_registry.add(CartSummary)


class PersonalData(FieldsProvider):
    fields_template = 'bda.plone.checkout.browser:forms/personal_data.yaml'
    fields_name = 'personal_data'
    
    @property
    def gender_vocabulary(self):
        return [('-', '-'),
                ('male', _('male', 'Male')),
                ('female', _('female', 'Female'))]

provider_registry.add(PersonalData)


class BillingAddress(FieldsProvider):
    fields_template = 'bda.plone.checkout.browser:forms/billing_address.yaml'
    fields_name = 'billing_address'

provider_registry.add(BillingAddress)


class DeliveryAddress(FieldsProvider):
    fields_template = 'bda.plone.checkout.browser:forms/delivery_address.yaml'
    fields_name = 'delivery_address'
    
    def conditional_required(self, widget, data):
        if data.parent['alternative_delivery'].extracted and not data.extracted:
            raise ExtractionError(widget.attrs['conditional_required'])
        return data.extracted
    
    def get_alternative_delivery(self, widget, data):
        return widget.dottedpath in self.request
    
    @property
    def alternative_delivery_vocab(self):
        return {
            True: _('yes', 'Yes'),
            False: _('no', 'No'),
            UNSET: _('not set', 'not set'),
        }

provider_registry.add(DeliveryAddress)


class PaymentSelection(FieldsProvider):
    fields_template = 'bda.plone.checkout.browser:forms/payment_selection.yaml'
    fields_name = 'payment_selection'
    
    @property
    def payments(self):
        return Payments(self.context)
    
    @property
    def payment_vocabulary(self):
        return self.payments.vocab
    
    def get_payment(self, widget, data):
        return self.request.get(widget.dottedpath, self.payments.default)

provider_registry.add(PaymentSelection)


class OrderComment(FieldsProvider):
    fields_template = 'bda.plone.checkout.browser:forms/order_comment.yaml'
    fields_name = 'order_comment'

provider_registry.add(OrderComment)


class CheckoutForm(Form, FormContext):
    
    action_resource = '@@checkout'
    
    def prepare(self):
        request = self.request
        checkout = not request.get('checkout_confirm') and \
                   not request.get('action.checkout.finish')
        form_class = checkout and 'mode_edit' or 'mode_display'
        self.form = factory('#form', name='checkout', props={
            'action': self.form_action,
            'class_add': form_class})
        for fields_factory in provider_registry:
            fields_factory(self.context, self.request).extend(self.form)
        # checkout data input
        if checkout:
            self.form['checkout_back'] = factory('submit', props={
                'label': _('back', 'Back'),
                'action': 'checkout_back',
                'handler': None,
                'next': self.checkout_back,
                'skip': True})
            self.form['next'] = factory('submit', props={
                'label': _('next', 'Next'),
                'action': 'next',
                'handler': None,
                'next': self.checkout_summary})
        # checkout confirmation
        else:
            self.form['confirm_back'] = factory('submit', props={
                'label': _('back', 'Back'),
                'action': 'confirm_back',
                'handler': None,
                'next': self.confirm_back})
            self.form['finish'] = factory('submit', props={
                'label': _('finish', 'Finish'),
                'action': 'finish',
                'handler': self.finish,
{'fraction_non_alphanumeric': 0.11644782308223911, 'fraction_numerical': 0.0008638562543192813, 'mean_word_length': 2.625743814594425, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6186986', 'n_tokens_mistral': 3395, 'n_tokens_neox': 3297, 'n_words': 680}