Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FoD
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Schmitz
FoD
Commits
57bea46b
Commit
57bea46b
authored
5 years ago
by
Tomáš Čejka
Browse files
Options
Downloads
Patches
Plain Diff
registration: fixed activation process (activate user), improved error messages
parent
d4508aea
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
accounts/views.py
+55
-27
55 additions, 27 deletions
accounts/views.py
flowspy/urls.py
+3
-1
3 additions, 1 deletion
flowspy/urls.py
templates/django_registration/activate.html
+4
-1
4 additions, 1 deletion
templates/django_registration/activate.html
with
62 additions
and
29 deletions
accounts/views.py
+
55
−
27
View file @
57bea46b
...
...
@@ -19,6 +19,7 @@
from
django
import
forms
from
django.conf
import
settings
from
django.core.mail
import
send_mail
from
smtplib
import
SMTPRecipientsRefused
from
django.contrib.sites.models
import
Site
from
django.contrib.auth.models
import
User
from
django.shortcuts
import
render
,
HttpResponse
...
...
@@ -30,9 +31,19 @@ from rest_framework.authtoken.models import Token
from
accounts.models
import
UserProfile
from
peers.models
import
Peer
from
flowspec.forms
import
UserProfileForm
# TODO from registration.models import RegistrationProfile
from
django_registration.backends.activation.views
import
ActivationView
from
django_registration.exceptions
import
ActivationError
import
os
,
logging
LOG_FILENAME
=
os
.
path
.
join
(
settings
.
LOG_FILE_LOCATION
,
'
flowspec_accounts_view.log
'
)
formatter
=
logging
.
Formatter
(
'
%(asctime)s %(levelname)s: %(message)s
'
)
logger
=
logging
.
getLogger
(
__name__
)
logger
.
setLevel
(
logging
.
DEBUG
)
handler
=
logging
.
FileHandler
(
LOG_FILENAME
)
handler
.
setFormatter
(
formatter
)
logger
.
addHandler
(
handler
)
def
generate_token
(
request
):
user
=
request
.
user
try
:
...
...
@@ -45,29 +56,31 @@ def generate_token(request):
@never_cache
def
activate
(
request
,
activation_key
):
account
=
None
actview
=
ActivationView
()
if
request
.
method
==
"
GET
"
:
activation_key
=
activation_key
.
lower
()
# Normalize before trying anything with it.
try
:
rp
=
User
.
objects
.
get
(
activation_key
=
activation_key
)
username
=
actview
.
validate_key
(
activation_key
=
activation_key
)
except
User
.
DoesNotExist
:
except
ActivationError
as
e
:
return
render
(
request
,
'
registration/activate.html
'
,
'
django_
registration/activate.html
'
,
{
'
account
'
:
account
,
'
expiration_days
'
:
settings
.
ACCOUNT_ACTIVATION_DAYS
'
expiration_days
'
:
settings
.
ACCOUNT_ACTIVATION_DAYS
,
'
error
'
:
str
(
e
.
message
)
}
)
try
:
userProfile
=
rp
.
user
.
get_
profile
()
userProfile
=
User
.
objects
.
get
(
username
=
username
).
user
profile
except
User
.
DoesNotExist
:
return
render
(
request
,
'
registration/activate.html
'
,
'
django_
registration/activate.html
'
,
{
'
account
'
:
account
,
'
expiration_days
'
:
settings
.
ACCOUNT_ACTIVATION_DAYS
'
expiration_days
'
:
settings
.
ACCOUNT_ACTIVATION_DAYS
,
'
error
'
:
"
Activation failed
"
}
)
...
...
@@ -75,7 +88,7 @@ def activate(request, activation_key):
return
render
(
request
,
'
registration/activate_edit.html
'
,
'
django_
registration/activate_edit.html
'
,
{
'
account
'
:
account
,
'
expiration_days
'
:
settings
.
ACCOUNT_ACTIVATION_DAYS
,
...
...
@@ -87,7 +100,7 @@ def activate(request, activation_key):
request_data
=
request
.
POST
.
copy
()
try
:
user
=
User
.
objects
.
get
(
pk
=
request_data
[
'
user
'
])
up
=
user
.
get_
profile
()
up
=
user
.
user
profile
# use getlist to get the list of peers (might be multiple)
profile_peers
=
request
.
POST
.
getlist
(
'
peers
'
)
...
...
@@ -104,39 +117,54 @@ def activate(request, activation_key):
except
:
return
render
(
request
,
'
registration/activate_edit.html
'
,
'
django_
registration/activate_edit.html
'
,
{
'
account
'
:
account
,
'
expiration_days
'
:
settings
.
ACCOUNT_ACTIVATION_DAYS
'
expiration_days
'
:
settings
.
ACCOUNT_ACTIVATION_DAYS
,
},
)
activation_key
=
activation_key
.
lower
()
# Normalize before trying anything with it.
try
:
rp
=
User
.
objects
.
get
(
activation_key
=
activation_key
)
account
=
User
.
objects
.
activate_user
(
activation_key
)
except
Exception
:
pass
account
=
actview
.
activate
(
activation_key
=
activation_key
)
except
ActivationError
as
e
:
return
render
(
request
,
'
django_registration/activate.html
'
,
{
'
account
'
:
account
,
'
expiration_days
'
:
settings
.
ACCOUNT_ACTIVATION_DAYS
,
'
error
'
:
str
(
e
.
message
)
}
)
if
account
:
# A user has been activated
email
=
render_to_string
(
'
registration/activation_complete.txt
'
,
'
django_
registration/activation_complete.txt
'
,
{
'
site
'
:
Site
.
objects
.
get_current
(),
'
user
'
:
account
}
)
send_mail
(
_
(
"
%sUser account activated
"
)
%
settings
.
EMAIL_SUBJECT_PREFIX
,
email
,
settings
.
SERVER_EMAIL
,
[
account
.
email
]
)
error
=
None
try
:
send_mail
(
_
(
"
%sUser account activated
"
)
%
settings
.
EMAIL_SUBJECT_PREFIX
,
email
,
settings
.
SERVER_EMAIL
,
[
account
.
email
]
)
except
SMTPRecipientsRefused
as
e
:
logger
.
error
(
"
Failed to send a notification e-mail to the user: %s
"
%
e
)
error
=
e
logger
.
info
(
'
Activated account %s
'
%
account
)
return
render
(
request
,
'
registration/activate.html
'
,
'
django_
registration/activate.html
'
,
{
'
account
'
:
account
,
'
expiration_days
'
:
settings
.
ACCOUNT_ACTIVATION_DAYS
'
expiration_days
'
:
settings
.
ACCOUNT_ACTIVATION_DAYS
,
'
error
'
:
"
Failed to send a notification e-mail to the user
"
},
)
This diff is collapsed.
Click to expand it.
flowspy/urls.py
+
3
−
1
View file @
57bea46b
...
...
@@ -50,10 +50,12 @@ urlpatterns = [
url
(
r
'
^selectinst/?$
'
,
flowspec_views
.
selectinst
,
name
=
"
selectinst
"
),
url
(
r
'
^profile/token/$
'
,
accounts_views
.
generate_token
,
name
=
"
user-profile-token
"
),
# Account registration process - activation is partially done by FoD, other URLs are included from django_registration
url
(
r
'
^accounts/activate/(?P<activation_key>[-:\w]+)/$
'
,
accounts_views
.
activate
,
name
=
'
activate_account
'
),
path
(
'
accounts/
'
,
include
(
'
django_registration.backends.activation.urls
'
)),
url
(
r
'
^activate/complete/$
'
,
TemplateView
.
as_view
(
template_name
=
'
django_registration/activation_complete.html
'
),
name
=
'
registration_activation_complete
'
),
url
(
r
'
^load_js/(?P<file>[\w\s\d_-]+)/$
'
,
flowspec_views
.
load_jscript
,
name
=
"
load-js
"
),
url
(
r
'
^activate/complete/$
'
,
TemplateView
.
as_view
(
template_name
=
'
templates/registration/activation_complete.html
'
),
name
=
'
registration_activation_complete
'
),
path
(
'
altlogin/
'
,
LoginView
.
as_view
(
template_name
=
'
overview/login.html
'
),
name
=
"
altlogin
"
),
path
(
'
admin/
'
,
admin
.
site
.
urls
),
path
(
'
tinymce/
'
,
include
(
'
tinymce.urls
'
)),
...
...
This diff is collapsed.
Click to expand it.
templates/django_registration/activate.html
+
4
−
1
View file @
57bea46b
...
...
@@ -30,9 +30,12 @@
</div>
<!-- /.panel-heading -->
<div
class=
"panel-body"
>
<div
class=
"alert {% if account %}alert-success{% else %}alert-info{% endif %}"
>
{% if error and account %}
<div
class=
"alert alert-info"
>
{{error}}
</div>
{% endif %}
<div
class=
"alert {% if account %}alert-success{% else %}alert-info{% endif %}"
>
{% if account %}
{{account}} {% trans "succesfully activated" %}
{% elif error %}
{{error}}
{% else %}
{% trans "The user has probably been already activated." %}
{% endif %}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment