diff --git a/apps/user/forms.py b/apps/user/forms.py index 9a9e68d5..b0f6d51d 100644 --- a/apps/user/forms.py +++ b/apps/user/forms.py @@ -2,7 +2,8 @@ from django import forms from django.contrib.auth import get_user_model from phonenumber_field.formfields import PhoneNumberField -from .fields import CreditCardField +from apps.user.fields import CreditCardField +from apps.user.models import Child User = get_user_model() @@ -31,11 +32,6 @@ class UserEditForm(forms.ModelForm): site = forms.URLField(required=False) photo = forms.ImageField(required=False) - child_gender = forms.CharField(required=False) - child_birthday = forms.DateField(input_formats=['%d/%m/%Y'], required=False) - child_first_name = forms.CharField(required=False) - child_last_name = forms.CharField(required=False) - class Meta: model = User fields = ( @@ -61,10 +57,6 @@ class UserEditForm(forms.ModelForm): 'vkontakte', 'site', 'photo', - 'child_gender', - 'child_birthday', - 'child_first_name', - 'child_last_name', ) @@ -79,3 +71,11 @@ class AuthorRequesForm(forms.Form): email = forms.CharField() about = forms.CharField() facebook = forms.URLField(required=False) + + +class ChildForm(forms.ModelForm): + birthday = forms.DateField(input_formats=['%d/%m/%Y'], required=False) + + class Meta: + model = Child + fields = '__all__' diff --git a/apps/user/migrations/0032_child.py b/apps/user/migrations/0032_child.py new file mode 100644 index 00000000..95c2921c --- /dev/null +++ b/apps/user/migrations/0032_child.py @@ -0,0 +1,26 @@ +# Generated by Django 2.0.7 on 2019-06-06 21:05 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('user', '0031_user_review_url'), + ] + + operations = [ + migrations.CreateModel( + name='Child', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('first_name', models.CharField(max_length=30, verbose_name='Имя ребенка')), + ('last_name', models.CharField(blank=True, default='', max_length=150, verbose_name='Фамилия ребенка')), + ('gender', models.CharField(choices=[('n', 'не указан'), ('m', 'Мужчина'), ('f', 'Женщина')], default='n', max_length=1, verbose_name='Пол ребенка')), + ('birthday', models.DateField(blank=True, null=True, verbose_name='День рождения ребенка')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='childs', to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/apps/user/migrations/0033_add_childs.py b/apps/user/migrations/0033_add_childs.py new file mode 100644 index 00000000..a3cc360e --- /dev/null +++ b/apps/user/migrations/0033_add_childs.py @@ -0,0 +1,21 @@ +# Generated by Django 2.0.7 on 2019-06-07 20:48 + +from django.db import migrations + + +def add_childs(apps, schema_editor): + User = apps.get_model('user', 'User') + Child = apps.get_model('user', 'Child') + for user in User.objects.exclude(child_first_name=''): + Child.objects.get_or_create(user=user, first_name=user.child_first_name, last_name=user.child_last_name, + gender=user.child_gender, birthday=user.child_birthday) + +class Migration(migrations.Migration): + + dependencies = [ + ('user', '0032_child'), + ] + + operations = [ + migrations.RunPython(add_childs), + ] diff --git a/apps/user/models.py b/apps/user/models.py index aca57b23..854c03ea 100644 --- a/apps/user/models.py +++ b/apps/user/models.py @@ -88,12 +88,6 @@ class User(AbstractUser): 'content.Gallery', on_delete=models.CASCADE, verbose_name='Галерея', null=True, blank=True, ) - - child_first_name = models.CharField('Имя ребенка', max_length=30, blank=True) - child_last_name = models.CharField('Фамилия ребенка', max_length=150, blank=True) - child_gender = models.CharField( - 'Пол ребенка', max_length=1, default='n', choices=GENDER_CHOICES) - child_birthday = models.DateField('День рождения ребенка', null=True, blank=True) review_url = models.URLField('Ссылка на видеоотзыв', blank=True, default='') objects = UserManager() @@ -104,6 +98,14 @@ class User(AbstractUser): class Meta(AbstractUser.Meta): ordering = ('-date_joined',) + @cached_property + def has_child(self): + return self.childs.all().count() > 0 + + def child_filled(self): + child = self.childs.all().first() + return child and child.first_name and child.last_name and child.birthday + @property def url(self): return reverse('user', args=[self.slug or self.id]) @@ -338,3 +340,20 @@ class EmailLog(models.Model): email = models.EmailField(_('email address')) created_at = models.DateTimeField(auto_now_add=True) source = models.PositiveSmallIntegerField(choices=SOURCE_CHOICES) + + +class Child(models.Model): + NOT_DEFINED = 'n' + MALE = 'm' + FEMALE = 'f' + GENDER_CHOICES = ( + (NOT_DEFINED, 'не указан'), + (MALE, 'Мужчина'), + (FEMALE, 'Женщина'), + ) + user = models.ForeignKey(User, related_name='childs', on_delete=models.CASCADE) + first_name = models.CharField('Имя ребенка', max_length=30) + last_name = models.CharField('Фамилия ребенка', max_length=150, blank=True, default='') + gender = models.CharField( + 'Пол ребенка', max_length=1, default='n', choices=GENDER_CHOICES) + birthday = models.DateField('День рождения ребенка', null=True, blank=True) diff --git a/apps/user/templates/user/bonus-history.html b/apps/user/templates/user/bonus-history.html index 0170f356..2edff50a 100644 --- a/apps/user/templates/user/bonus-history.html +++ b/apps/user/templates/user/bonus-history.html @@ -83,7 +83,7 @@
{% endif %}