"""Forms for RTD donations"""

import logging

from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from readthedocs.payments.forms import StripeModelForm, StripeResourceMixin
from readthedocs.payments.utils import stripe

from .models import Supporter

log = logging.getLogger(__name__)


class SupporterForm(StripeResourceMixin, StripeModelForm):

    """Donation support sign up form

    This extends the basic payment form, giving fields for credit card number,
    expiry, and CVV. The proper Knockout data bindings are established on
    :py:class:`StripeModelForm`
    """

    class Meta:
        model = Supporter
        fields = (
            'last_4_digits',
            'name',
            'email',
            'dollars',
            'logo_url',
            'site_url',
            'public',
        )
        labels = {
            'public': _('Make this donation public'),
        }
        help_texts = {
            'public': _('Your name and image will be displayed on the donation page'),
            'email': _('Your email is used for Gravatar and so we can send you a receipt'),
            'logo_url': _("URL of your company's logo, images should be 300x300 pixels or less"),
            'dollars': _('Companies donating over $400 can specify a logo URL and site link'),
        }
        widgets = {
            'dollars': forms.HiddenInput(attrs={
                'data-bind': 'value: dollars'
            }),
            'logo_url': forms.TextInput(attrs={
                'data-bind': 'value: logo_url, enable: urls_enabled'
            }),
            'site_url': forms.TextInput(attrs={
                'data-bind': 'value: site_url, enable: urls_enabled'
            }),
            'last_4_digits': forms.TextInput(attrs={
                'data-bind': 'valueInit: card_digits, value: card_digits'
            }),
        }

    last_4_digits = forms.CharField(widget=forms.HiddenInput(), required=True)
    name = forms.CharField(required=True)
    email = forms.CharField(required=True)

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super(SupporterForm, self).__init__(*args, **kwargs)

    def validate_stripe(self):
        """Call stripe for payment (not ideal here) and clean up logo < $200"""
        dollars = self.cleaned_data['dollars']
        if dollars < 200:
            self.cleaned_data['logo_url'] = None
            self.cleaned_data['site_url'] = None
        stripe.Charge.create(
            amount=int(self.cleaned_data['dollars']) * 100,
            currency='usd',
            source=self.cleaned_data['stripe_token'],
            description='Read the Docs Sustained Engineering',
            receipt_email=self.cleaned_data['email']
        )

    def save(self, commit=True):
        supporter = super(SupporterForm, self).save(commit)
        if commit and self.user is not None and self.user.is_authenticated():
            supporter.user = self.user
            supporter.save()
        return supporter


class EthicalAdForm(StripeResourceMixin, StripeModelForm):

    """Payment form for ethical ads

    This extends the basic payment form, giving fields for credit card number,
    expiry, and CVV. The proper Knockout data bindings are established on
    :py:class:`StripeModelForm`
    """

    class Meta:
        model = Supporter
        fields = (
            'last_4_digits',
            'name',
            'email',
            'dollars',
        )
        help_texts = {
            'email': _('Your email is used so we can send you a receipt'),
        }
        widgets = {
            'dollars': forms.HiddenInput(attrs={
                'data-bind': 'value: dollars'
            }),
            'last_4_digits': forms.TextInput(attrs={
                'data-bind': 'valueInit: card_digits, value: card_digits'
            }),
        }

    last_4_digits = forms.CharField(widget=forms.HiddenInput(), required=True)
    name = forms.CharField(required=True)
    email = forms.CharField(required=True)

    def validate_stripe(self):
        stripe.Charge.create(
            amount=int(self.cleaned_data['dollars']) * 100,
            currency='usd',
            source=self.cleaned_data['stripe_token'],
            description='Read the Docs Sponsorship Payment',
            receipt_email=self.cleaned_data['email']
        )

[((14, 6, 14, 33), 'logging.getLogger', 'logging.getLogger', ({(14, 24, 14, 32): '__name__'}, {}), '(__name__)', False, 'import logging\n'), ((62, 11, 62, 41), 'django.forms.CharField', 'forms.CharField', (), '', False, 'from django import forms\n'), ((63, 12, 63, 42), 'django.forms.CharField', 'forms.CharField', (), '', False, 'from django import forms\n'), ((121, 11, 121, 41), 'django.forms.CharField', 'forms.CharField', (), '', False, 'from django import forms\n'), ((122, 12, 122, 42), 'django.forms.CharField', 'forms.CharField', (), '', False, 'from django import forms\n'), ((38, 22, 38, 52), 'django.utils.translation.ugettext_lazy', '_', ({(38, 24, 38, 51): '"""Make this donation public"""'}, {}), "('Make this donation public')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((41, 22, 41, 85), 'django.utils.translation.ugettext_lazy', '_', ({(41, 24, 41, 84): '"""Your name and image will be displayed on the donation page"""'}, {}), "('Your name and image will be displayed on the donation page')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((42, 21, 42, 90), 'django.utils.translation.ugettext_lazy', '_', ({(42, 23, 42, 89): '"""Your email is used for Gravatar and so we can send you a receipt"""'}, {}), "('Your email is used for Gravatar and so we can send you a receipt')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((43, 24, 43, 96), 'django.utils.translation.ugettext_lazy', '_', ({(43, 26, 43, 95): '"""URL of your company\'s logo, images should be 300x300 pixels or less"""'}, {}), '("URL of your company\'s logo, images should be 300x300 pixels or less")', True, 'from django.utils.translation import ugettext_lazy as _\n'), ((44, 23, 44, 93), 'django.utils.translation.ugettext_lazy', '_', ({(44, 25, 44, 92): '"""Companies donating over $400 can specify a logo URL and site link"""'}, {}), "('Companies donating over $400 can specify a logo URL and site link')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((47, 23, 49, 14), 'django.forms.HiddenInput', 'forms.HiddenInput', (), '', False, 'from django import forms\n'), ((50, 24, 52, 14), 'django.forms.TextInput', 'forms.TextInput', (), '', False, 'from django import forms\n'), ((53, 24, 55, 14), 'django.forms.TextInput', 'forms.TextInput', (), '', False, 'from django import forms\n'), ((56, 29, 58, 14), 'django.forms.TextInput', 'forms.TextInput', (), '', False, 'from django import forms\n'), ((61, 43, 61, 62), 'django.forms.HiddenInput', 'forms.HiddenInput', ({}, {}), '()', False, 'from django import forms\n'), ((109, 21, 109, 73), 'django.utils.translation.ugettext_lazy', '_', ({(109, 23, 109, 72): '"""Your email is used so we can send you a receipt"""'}, {}), "('Your email is used so we can send you a receipt')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((112, 23, 114, 14), 'django.forms.HiddenInput', 'forms.HiddenInput', (), '', False, 'from django import forms\n'), ((115, 29, 117, 14), 'django.forms.TextInput', 'forms.TextInput', (), '', False, 'from django import forms\n'), ((120, 43, 120, 62), 'django.forms.HiddenInput', 'forms.HiddenInput', ({}, {}), '()', False, 'from django import forms\n')]