Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ap-12_django
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Давид Ли
ap-12_django
Commits
88de06d4
Commit
88de06d4
authored
Feb 28, 2024
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
web 29
parent
b81f7faa
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
135 additions
and
11 deletions
+135
-11
forms.py
ap_12/accounts/forms.py
+18
-0
0002_user_is_draft.py
ap_12/accounts/migrations/0002_user_is_draft.py
+18
-0
0003_alter_user_password.py
ap_12/accounts/migrations/0003_alter_user_password.py
+18
-0
0004_alter_user_is_active.py
ap_12/accounts/migrations/0004_alter_user_is_active.py
+18
-0
models.py
ap_12/accounts/models.py
+16
-1
views.py
ap_12/accounts/views.py
+41
-8
db.sqlite3
ap_12/db.sqlite3
+0
-0
form.html
ap_12/templates/partial/form.html
+6
-2
No files found.
ap_12/accounts/forms.py
View file @
88de06d4
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'
)
ap_12/accounts/migrations/0002_user_is_draft.py
0 → 100644
View file @
88de06d4
# 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
),
),
]
ap_12/accounts/migrations/0003_alter_user_password.py
0 → 100644
View file @
88de06d4
# 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'
),
),
]
ap_12/accounts/migrations/0004_alter_user_is_active.py
0 → 100644
View file @
88de06d4
# 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'
),
),
]
ap_12/accounts/models.py
View file @
88de06d4
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
)
ap_12/accounts/views.py
View file @
88de06d4
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
form_valid
(
self
,
form
):
user
=
form
.
save
()
login
(
self
.
request
,
user
)
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
redirect
(
self
.
get_success_url
())
return
initial
def
form_valid
(
self
,
form
):
if
self
.
form_type
==
'primary'
:
user
=
form
.
save
()
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
'
)
)
ap_12/db.sqlite3
View file @
88de06d4
No preview for this file type
ap_12/templates/partial/form.html
View file @
88de06d4
...
...
@@ -7,11 +7,11 @@
<div
class=
"alert alert-danger"
>
{{ error }}
</div>
{% endfor %}
{% for field in form %}
{% for field in form
.visible_fields
%}
<div
class=
"my-4"
>
{{ field.label }}
{{ field }}
{{ field }}
</div>
{% if field.errors %}
...
...
@@ -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>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment