Skip to content

Commit

Permalink
Simplifica com modelform #102
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuspdf committed May 30, 2024
1 parent b55a8f5 commit a274b68
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
28 changes: 21 additions & 7 deletions eventex/subscriptions/forms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from django import forms
from django.core.exceptions import ValidationError

from eventex.subscriptions.models import Subscription
from eventex.subscriptions.validators import validate_cpf

def validate_cpf(value):
if not value.isdigit():
raise ValidationError('CPF deve conter apenas números.', 'digits')
if len(value) != 11:
raise ValidationError('CPF deve ter 11 números.', 'length')


class SubscriptionForm(forms.Form):
class SubscriptionFormOld(forms.Form):
name = forms.CharField(label='Nome')
cpf = forms.CharField(label='CPF', validators=[validate_cpf])
email = forms.EmailField(label='E-mail', required=False)
Expand All @@ -25,3 +21,21 @@ def clean(self):
raise ValidationError('Informe seu e-mail ou telefone')

return self.cleaned_data


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

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

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 e-mail ou telefone')

return self.cleaned_data
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 5.0.3 on 2024-05-30 18:36

import eventex.subscriptions.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("subscriptions", "0003_alter_subscription_paid"),
]

operations = [
migrations.AlterField(
model_name="subscription",
name="cpf",
field=models.CharField(
max_length=11,
validators=[eventex.subscriptions.validators.validate_cpf],
verbose_name="CPF",
),
),
migrations.AlterField(
model_name="subscription",
name="email",
field=models.EmailField(blank=True, max_length=254, verbose_name="e-mail"),
),
migrations.AlterField(
model_name="subscription",
name="phone",
field=models.CharField(blank=True, max_length=20, verbose_name="telefone"),
),
]
8 changes: 5 additions & 3 deletions eventex/subscriptions/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from django.db import models

from eventex.subscriptions.validators import validate_cpf


class Subscription(models.Model):
name = models.CharField('nome', max_length=100)
cpf = models.CharField('CPF', max_length=11)
email = models.EmailField('e-mail')
phone = models.CharField('telefone', max_length=20)
cpf = models.CharField('CPF', max_length=11, validators=[validate_cpf])
email = models.EmailField('e-mail', blank=True)
phone = models.CharField('telefone', max_length=20, blank=True)
created_at = models.DateTimeField('criado em', auto_now_add=True)
paid = models.BooleanField('pago', default=False)

Expand Down
2 changes: 1 addition & 1 deletion eventex/subscriptions/tests/test_form_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_phone_is_optional(self):
form = self.make_validated_form(phone='')
self.assertFalse(form.errors)

def test_must_onform_email_or_phone(self):
def test_must_inform_email_or_phone(self):
"""Email and phone are optional, but one must be informed"""
form = self.make_validated_form(email='', phone='')
self.assertListEqual(['__all__'], list(form.errors))
Expand Down
8 changes: 8 additions & 0 deletions eventex/subscriptions/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.core.exceptions import ValidationError


def validate_cpf(value):
if not value.isdigit():
raise ValidationError('CPF deve conter apenas números.', 'digits')
if len(value) != 11:
raise ValidationError('CPF deve ter 11 números.', 'length')
3 changes: 1 addition & 2 deletions eventex/subscriptions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def create(request):
if not form.is_valid():
return render(request, 'subscriptions/subscription_form.html',
{'form': form})

subscription = Subscription.objects.create(**form.cleaned_data)
subscription = form.save()

_send_email('Confirmação de inscrição',
settings.DEFAULT_FROM_EMAIL,
Expand Down

0 comments on commit a274b68

Please sign in to comment.