override User.create to set password, create GenEmail, set trial period

This commit is contained in:
Son NK 2019-07-12 10:34:15 +02:00 committed by Son NK
parent 631e251d8c
commit 0817e45abe
7 changed files with 33 additions and 43 deletions

View file

@ -97,12 +97,7 @@ def facebook_callback():
# create user # create user
else: else:
LOG.d("create facebook user with %s", facebook_user_data) LOG.d("create facebook user with %s", facebook_user_data)
user = User.create(email=email, name=facebook_user_data["name"]) user = User.create(email=email, name=facebook_user_data["name"], activated=True)
# set a random password
user.set_password(random_string(20))
user.activated = True
if picture_url: if picture_url:
LOG.d("set user profile picture to %s", picture_url) LOG.d("set user profile picture to %s", picture_url)

View file

@ -87,13 +87,7 @@ def github_callback():
# create user # create user
else: else:
LOG.d("create github user") LOG.d("create github user")
user = User.create(email=email, name=github_user_data["name"]) user = User.create(email=email, name=github_user_data["name"], activated=True)
# set a random password
user.set_password(random_string(20))
user.activated = True
db.session.commit() db.session.commit()
login_user(user) login_user(user)

View file

@ -96,12 +96,7 @@ def google_callback():
# create user # create user
else: else:
LOG.d("create google user with %s", google_user_data) LOG.d("create google user with %s", google_user_data)
user = User.create(email=email, name=google_user_data["name"]) user = User.create(email=email, name=google_user_data["name"], activated=True)
# set a random password
user.set_password(random_string(20))
user.activated = True
if picture_url: if picture_url:
LOG.d("set user profile picture to %s", picture_url) LOG.d("set user profile picture to %s", picture_url)

View file

@ -10,7 +10,7 @@ from app.config import URL
from app.email_utils import notify_admin from app.email_utils import notify_admin
from app.extensions import db from app.extensions import db
from app.log import LOG from app.log import LOG
from app.models import User, ActivationCode, PlanEnum, GenEmail from app.models import User, ActivationCode
from app.utils import random_string, encode_url from app.utils import random_string, encode_url
@ -38,16 +38,9 @@ def register():
flash(f"Email {form.email.data} already exists", "warning") flash(f"Email {form.email.data} already exists", "warning")
else: else:
LOG.debug("create user %s", form.email.data) LOG.debug("create user %s", form.email.data)
user = User.create(email=form.email.data, name=form.name.data) user = User.create(
user.set_password(form.password.data) email=form.email.data, name=form.name.data, password=form.password.data
)
# by default new user will be trial period
user.plan = PlanEnum.trial
user.plan_expiration = arrow.now().shift(days=+15)
db.session.flush()
# create a first alias mail to show user how to use when they login
GenEmail.create_new_gen_email(user_id=user.id)
db.session.commit() db.session.commit()
send_activation_email(user, next_url) send_activation_email(user, next_url)

View file

@ -116,6 +116,25 @@ class User(db.Model, ModelMixin, UserMixin):
profile_picture = db.relationship(File) profile_picture = db.relationship(File)
@classmethod
def create(cls, email, name, password=None, **kwargs):
user = super(User, cls).create(email=email, name=name, **kwargs)
if not password:
# set a random password
user.set_password(random_string(20))
# by default new user will be trial period
user.plan = PlanEnum.trial
user.plan_expiration = arrow.now().shift(days=+15)
db.session.flush()
# create a first alias mail to show user how to use when they login
GenEmail.create_new_gen_email(user_id=user.id)
db.session.flush()
return user
def should_upgrade(self): def should_upgrade(self):
"""User is invited to upgrade if they are in free plan or their trial ends soon""" """User is invited to upgrade if they are in free plan or their trial ends soon"""
if self.plan == PlanEnum.free: if self.plan == PlanEnum.free:

View file

@ -82,7 +82,10 @@ def authorize():
else: else:
# after user logs in, redirect user back to this page # after user logs in, redirect user back to this page
return render_template( return render_template(
"oauth/authorize_nonlogin_user.html", client=client, next=request.url, Scope=Scope, "oauth/authorize_nonlogin_user.html",
client=client,
next=request.url,
Scope=Scope,
) )
else: # user allows or denies else: # user allows or denies
gen_new_email = request.form.get("gen-email") == "on" gen_new_email = request.form.get("gen-email") == "on"

View file

@ -29,15 +29,7 @@ from app.discover.base import discover_bp
from app.extensions import db, login_manager, migrate from app.extensions import db, login_manager, migrate
from app.jose_utils import get_jwk_key from app.jose_utils import get_jwk_key
from app.log import LOG from app.log import LOG
from app.models import ( from app.models import Client, User, ClientUser, GenEmail, RedirectUri, Partner
Client,
User,
ClientUser,
GenEmail,
RedirectUri,
PlanEnum,
Partner,
)
from app.monitor.base import monitor_bp from app.monitor.base import monitor_bp
from app.oauth.base import oauth_bp from app.oauth.base import oauth_bp
from app.partner.base import partner_bp from app.partner.base import partner_bp
@ -94,13 +86,12 @@ def fake_data():
user = User.create( user = User.create(
email="nguyenkims+local@gmail.com", email="nguyenkims+local@gmail.com",
name="Son Local", name="Son Local",
password="password",
activated=True, activated=True,
is_admin=True, is_admin=True,
is_developer=True, is_developer=True,
) )
user.set_password("password")
user.plan = PlanEnum.trial
user.plan_expiration = arrow.now().shift(weeks=2)
db.session.commit() db.session.commit()
GenEmail.create_new_gen_email(user_id=user.id) GenEmail.create_new_gen_email(user_id=user.id)