<commit_before>from decimal import Decimal
from django.db import transaction
from django.utils.module_loading import import_string
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers, exceptions
from oscar.core.loading import get_model, get_class
from oscarapi.serializers.checkout import (
    CheckoutSerializer as OscarCheckoutSerializer,
    OrderSerializer as OscarOrderSerializer,
)
from oscarapi.basket.operations import get_basket
from .settings import API_ENABLED_PAYMENT_METHODS, ORDER_STATUS_PAYMENT_DECLINED
from .signals import pre_calculate_total
from .states import PENDING
from . import utils

Basket = get_model('basket', 'Basket')
Order = get_model('order', 'Order')
BillingAddress = get_model('order', 'BillingAddress')
ShippingAddress = get_model('order', 'ShippingAddress')

OrderTotalCalculator = get_class('checkout.calculators', 'OrderTotalCalculator')


class PaymentMethodsSerializer(serializers.Serializer):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        request = self.context.get('request', None)
        assert request is not None, (
            "`%s` requires the request in the serializer"
            " context. Add `context={'request': request}` when instantiating "
            "the serializer." % self.__class__.__name__
        )

        self.methods = {}
        for m in API_ENABLED_PAYMENT_METHODS:
            PermissionClass = import_string(m['permission'])
            permission = PermissionClass()
            if permission.is_permitted(request=request, user=request.user):
                MethodClass = import_string(m['method'])
                method = MethodClass()
                self.fields[method.code] = method.serializer_class(required=False)
                self.methods[method.code] = method

        if not any(self.fields):
            raise RuntimeError('No payment methods were permitted for user %s' % request.user)


    def validate(self, data):
        # At least one method must be enabled
        enabled_methods = { k: v for k, v in data.items() if v['enabled'] }
        if len(enabled_methods) <= 0:
            raise serializers.ValidationError("At least one payment method must be enabled.")

        # Must set pay_balance flag on exactly one payment method<commit_msg>Add context to payment method serializers<commit_after>from decimal import Decimal
from django.db import transaction
from django.utils.module_loading import import_string
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers, exceptions
from oscar.core.loading import get_model, get_class
from oscarapi.serializers.checkout import (
    CheckoutSerializer as OscarCheckoutSerializer,
    OrderSerializer as OscarOrderSerializer,
)
from oscarapi.basket.operations import get_basket
from .settings import API_ENABLED_PAYMENT_METHODS, ORDER_STATUS_PAYMENT_DECLINED
from .signals import pre_calculate_total
from .states import PENDING
from . import utils

Basket = get_model('basket', 'Basket')
Order = get_model('order', 'Order')
BillingAddress = get_model('order', 'BillingAddress')
ShippingAddress = get_model('order', 'ShippingAddress')

OrderTotalCalculator = get_class('checkout.calculators', 'OrderTotalCalculator')


class PaymentMethodsSerializer(serializers.Serializer):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        request = self.context.get('request', None)
        assert request is not None, (
            "`%s` requires the request in the serializer"
            " context. Add `context={'request': request}` when instantiating "
            "the serializer." % self.__class__.__name__
        )

        self.methods = {}
        for m in API_ENABLED_PAYMENT_METHODS:
            PermissionClass = import_string(m['permission'])
            permission = PermissionClass()
            if permission.is_permitted(request=request, user=request.user):
                MethodClass = import_string(m['method'])
                method = MethodClass()
                self.fields[method.code] = method.serializer_class(required=False, context=self.context)
                self.methods[method.code] = method

        if not any(self.fields):
            raise RuntimeError('No payment methods were permitted for user %s' % request.user)


    def validate(self, data):
        # At least one method must be enabled
        enabled_methods = { k: v for k, v in data.items() if v['enabled'] }
        if len(enabled_methods) <= 0:
            raise serializers.ValidationError("At least one payment method must be enabled.")

        # Must set pay_balance flag on exactly one payment method
{'fraction_non_alphanumeric': 0.0884974533106961, 'fraction_numerical': 0.00042444821731748726, 'mean_word_length': 3.5230326295585415, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5502288', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1242, 'n_words': 359}