Commit 88de06d4 authored by Давид Ли's avatar Давид Ли

web 29

parent b81f7faa
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from django import forms
class RegisterForm(UserCreationForm):
......@@ -10,3 +11,20 @@ class RegisterForm(UserCreationForm):
'password2', 'first_name',
'last_name', 'email',
)
class PrimaryRegisterForm(forms.ModelForm):
class Meta:
model = get_user_model()
fields = (
'username', 'first_name',
'last_name', 'email',
)
class SecondaryRegisterForm(UserCreationForm):
id = forms.IntegerField(widget=forms.HiddenInput())
class Meta(UserCreationForm.Meta):
model = get_user_model()
fields = ('id', 'password1', 'password2')
# Generated by Django 3.2.19 on 2024-02-28 13:36
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='user',
name='is_draft',
field=models.BooleanField(default=True),
),
]
# Generated by Django 3.2.19 on 2024-02-28 13:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0002_user_is_draft'),
]
operations = [
migrations.AlterField(
model_name='user',
name='password',
field=models.CharField(max_length=128, null=True, verbose_name='password'),
),
]
# Generated by Django 3.2.19 on 2024-02-28 14:05
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0003_alter_user_password'),
]
operations = [
migrations.AlterField(
model_name='user',
name='is_active',
field=models.BooleanField(default=False, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active'),
),
]
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
pass
is_draft = models.BooleanField(default=True)
# is_draft=True is_active=False => only first page completed
# is_draft=False is_active=True => all pages has been completed
# is_draft=False is_active=False => even first page has not been completed
# is_draft=True is_active=True => impossible variant
is_active = models.BooleanField(
'active',
default=False,
help_text=(
'Designates whether this user should be treated as active. '
'Unselect this instead of deleting accounts.'
),
)
password = models.CharField(max_length=128, verbose_name='password', null=True)
from django.forms import BaseModelForm
from django.http import HttpResponse
from django.urls import reverse
from accounts import forms
from django.views import generic
from django.contrib.auth import login, get_user_model
from django.shortcuts import redirect, render
from django.shortcuts import redirect
from django.core import exceptions
class RegisterView(generic.CreateView):
model = get_user_model()
template_name = 'auth/register.html'
form_class = forms.RegisterForm
FORM_TYPE_FORMS = {
'primary': forms.PrimaryRegisterForm,
'secondary': forms.SecondaryRegisterForm,
}
def dispatch(self, request, *args, **kwargs):
self.form_type = self.request.GET.get('form_type', 'primary')
if self.form_type.lower() not in self.FORM_TYPE_FORMS:
raise exceptions.BadRequest
return super().dispatch(request, *args, **kwargs)
def get_form_class(self):
return self.FORM_TYPE_FORMS.get(self.form_type, self.form_class)
def get_initial(self):
initial = super().get_initial()
if self.request.method == 'GET' and self.form_type == 'secondary':
initial |= {'id': self.request.GET.get('id')}
return initial
def form_valid(self, form):
if self.form_type == 'primary':
user = form.save()
login(self.request, user)
return redirect(reverse('register') + f'?form_type=secondary&id={user.id}')
elif self.form_type == 'secondary':
user = get_user_model().objects.get(id=form.cleaned_data['id'])
user.is_active = True
user.set_password(form.cleaned_data['password1'])
user.save()
login(self.request, user)
return redirect(self.get_success_url())
def get_success_url(self):
return self.request.POST.get(
'next',
self.request.GET.get('next', '')
self.request.GET.get('next', 'index')
)
No preview for this file type
......@@ -7,7 +7,7 @@
<div class="alert alert-danger">{{ error }}</div>
{% endfor %}
{% for field in form %}
{% for field in form.visible_fields %}
<div class="my-4">
{{ field.label }}
......@@ -24,6 +24,10 @@
{% endfor %}
{% for field in form.hidden_fields %}
{{ field }}
{% endfor %}
{% if not fields_only %}
<button class="btn btn-success">{{ btn_text|default:'Submit' }}</button>
</form>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment