2018-02-20 11:22:17 +00:00
|
|
|
from datetime import datetime, timedelta
|
2016-01-19 15:50:31 +00:00
|
|
|
|
2018-02-20 11:22:17 +00:00
|
|
|
from flask import abort, redirect, render_template, session, url_for
|
2016-10-13 17:05:37 +01:00
|
|
|
from flask_login import current_user
|
2016-01-22 17:24:14 +00:00
|
|
|
|
2015-12-01 13:23:54 +00:00
|
|
|
from app.main import main
|
2016-03-02 15:25:04 +00:00
|
|
|
from app.main.forms import (
|
|
|
|
|
RegisterUserForm,
|
2018-02-19 16:53:29 +00:00
|
|
|
RegisterUserFromInviteForm,
|
2018-02-20 11:22:17 +00:00
|
|
|
RegisterUserFromOrgInviteForm,
|
2016-03-02 15:25:04 +00:00
|
|
|
)
|
2017-11-10 12:35:21 +00:00
|
|
|
from app.main.views.verify import activate_user
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
from app.models.user import InvitedOrgUser, InvitedUser, User
|
2020-05-26 17:39:25 +01:00
|
|
|
from app.utils import hide_from_search_engines
|
2015-12-01 13:23:54 +00:00
|
|
|
|
2016-01-11 15:17:00 +00:00
|
|
|
|
2016-01-05 12:41:20 +00:00
|
|
|
@main.route('/register', methods=['GET', 'POST'])
|
2020-05-26 17:39:25 +01:00
|
|
|
@hide_from_search_engines
|
2016-01-08 15:12:14 +00:00
|
|
|
def register():
|
2016-05-04 13:01:55 +01:00
|
|
|
if current_user and current_user.is_authenticated:
|
2018-11-15 15:38:43 +00:00
|
|
|
return redirect(url_for('main.show_accounts_or_dashboard'))
|
2016-01-22 17:24:14 +00:00
|
|
|
|
2016-01-28 16:36:36 +00:00
|
|
|
form = RegisterUserForm()
|
2015-12-01 13:23:54 +00:00
|
|
|
if form.validate_on_submit():
|
2016-07-12 11:53:30 +01:00
|
|
|
_do_registration(form, send_sms=False)
|
|
|
|
|
return redirect(url_for('main.registration_continue'))
|
2016-03-09 15:12:33 +00:00
|
|
|
|
|
|
|
|
return render_template('views/register.html', form=form)
|
2016-03-02 15:25:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route('/register-from-invite', methods=['GET', 'POST'])
|
|
|
|
|
def register_from_invite():
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
invited_user = InvitedUser.from_session()
|
2016-03-02 15:25:04 +00:00
|
|
|
if not invited_user:
|
|
|
|
|
abort(404)
|
|
|
|
|
|
2017-11-14 15:53:38 +00:00
|
|
|
form = RegisterUserFromInviteForm(invited_user)
|
2017-11-14 15:03:05 +00:00
|
|
|
|
2016-03-02 15:25:04 +00:00
|
|
|
if form.validate_on_submit():
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
if form.service.data != invited_user.service or form.email_address.data != invited_user.email_address:
|
2016-03-02 15:25:04 +00:00
|
|
|
abort(400)
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
_do_registration(form, send_email=False, send_sms=invited_user.sms_auth)
|
|
|
|
|
invited_user.accept_invite()
|
|
|
|
|
if invited_user.sms_auth:
|
2017-11-10 12:35:21 +00:00
|
|
|
return redirect(url_for('main.verify'))
|
|
|
|
|
else:
|
|
|
|
|
# we've already proven this user has email because they clicked the invite link,
|
|
|
|
|
# so just activate them straight away
|
|
|
|
|
return activate_user(session['user_details']['id'])
|
2016-03-02 15:25:04 +00:00
|
|
|
|
2017-11-14 15:53:38 +00:00
|
|
|
return render_template('views/register-from-invite.html', invited_user=invited_user, form=form)
|
2016-03-02 15:25:04 +00:00
|
|
|
|
|
|
|
|
|
2018-02-19 16:53:29 +00:00
|
|
|
@main.route('/register-from-org-invite', methods=['GET', 'POST'])
|
|
|
|
|
def register_from_org_invite():
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
invited_org_user = InvitedOrgUser.from_session()
|
2018-02-19 16:53:29 +00:00
|
|
|
if not invited_org_user:
|
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
form = RegisterUserFromOrgInviteForm(
|
|
|
|
|
invited_org_user,
|
|
|
|
|
)
|
|
|
|
|
form.auth_type.data = 'sms_auth'
|
|
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2023-07-12 12:09:44 -04:00
|
|
|
if (form.organization.data != invited_org_user.organization or
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
form.email_address.data != invited_org_user.email_address):
|
2018-02-19 16:53:29 +00:00
|
|
|
abort(400)
|
2023-07-12 12:09:44 -04:00
|
|
|
_do_registration(form, send_email=False, send_sms=True, organization_id=invited_org_user.organization)
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
invited_org_user.accept_invite()
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
return redirect(url_for('main.verify'))
|
|
|
|
|
return render_template('views/register-from-org-invite.html', invited_org_user=invited_org_user, form=form)
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
def _do_registration(form, send_sms=True, send_email=True, organization_id=None):
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
user = User.from_email_address_or_none(form.email_address.data)
|
|
|
|
|
if user:
|
2018-02-19 16:53:29 +00:00
|
|
|
if send_email:
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
user.send_already_registered_email()
|
2018-02-19 16:53:29 +00:00
|
|
|
session['expiry_date'] = str(datetime.utcnow() + timedelta(hours=1))
|
|
|
|
|
session['user_details'] = {"email": user.email_address, "id": user.id}
|
|
|
|
|
else:
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
user = User.register(
|
|
|
|
|
name=form.name.data,
|
|
|
|
|
email_address=form.email_address.data,
|
|
|
|
|
mobile_number=form.mobile_number.data,
|
|
|
|
|
password=form.password.data,
|
|
|
|
|
auth_type=form.auth_type.data,
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-15 16:58:26 +00:00
|
|
|
if send_email:
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
user.send_verify_email()
|
2016-03-17 13:07:52 +00:00
|
|
|
|
|
|
|
|
if send_sms:
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
user.send_verify_code()
|
2016-05-11 11:20:45 +01:00
|
|
|
session['expiry_date'] = str(datetime.utcnow() + timedelta(hours=1))
|
2016-03-02 15:25:04 +00:00
|
|
|
session['user_details'] = {"email": user.email_address, "id": user.id}
|
2023-07-12 12:09:44 -04:00
|
|
|
if organization_id:
|
|
|
|
|
session['organization_id'] = organization_id
|
2016-03-17 13:07:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route('/registration-continue')
|
|
|
|
|
def registration_continue():
|
2017-04-27 11:55:50 +01:00
|
|
|
if not session.get('user_details'):
|
2018-03-08 16:51:53 +00:00
|
|
|
return redirect(url_for('.show_accounts_or_dashboard'))
|
2016-03-17 13:07:52 +00:00
|
|
|
return render_template('views/registration-continue.html')
|