from django import forms
from django.core.exceptions import ValidationError

from eventex.subscriptions.model import Subscription


class SubscriptionForm(forms.ModelForm):
    class Meta:
        model = Subscription
        fields = ['name', 'cpf', 'email', 'phone']

    def clean_name(self):
        name = self.cleaned_data.get('name')
        name_capitalized = [w.capitalize() for w in name.split()]
        return ' '.join(name_capitalized)

    def clean(self):
        self.cleaned_data = super().clean()
        if not self.cleaned_data.get('email') and not self.cleaned_data.get('phone'):
            raise ValidationError('Informe seu email ou telefone.')

        return self.cleaned_data
