zhuhao
commited on
Commit
·
eb9eea9
1
Parent(s):
b4d4127
support reset the user email (#1735)
Browse files### What problem does this PR solve?
support reset the user email from old to new
#1723
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
- api/utils/commands.py +31 -1
api/utils/commands.py
CHANGED
@@ -16,6 +16,7 @@
|
|
16 |
|
17 |
import base64
|
18 |
import click
|
|
|
19 |
|
20 |
from flask import Flask
|
21 |
from werkzeug.security import generate_password_hash
|
@@ -44,5 +45,34 @@ def reset_password(email, new_password, password_confirm):
|
|
44 |
click.echo(click.style('Congratulations! Password has been reset.', fg='green'))
|
45 |
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
def register_commands(app: Flask):
|
48 |
-
app.cli.add_command(reset_password)
|
|
|
|
16 |
|
17 |
import base64
|
18 |
import click
|
19 |
+
import re
|
20 |
|
21 |
from flask import Flask
|
22 |
from werkzeug.security import generate_password_hash
|
|
|
45 |
click.echo(click.style('Congratulations! Password has been reset.', fg='green'))
|
46 |
|
47 |
|
48 |
+
@click.command('reset-email', help='Reset the account email.')
|
49 |
+
@click.option('--email', prompt=True, help='The old email address of the account whose email you need to reset')
|
50 |
+
@click.option('--new-email', prompt=True, help='the new email.')
|
51 |
+
@click.option('--email-confirm', prompt=True, help='the new email confirm.')
|
52 |
+
def reset_email(email, new_email, email_confirm):
|
53 |
+
if str(new_email).strip() != str(email_confirm).strip():
|
54 |
+
click.echo(click.style('Sorry, new email and confirm email do not match.', fg='red'))
|
55 |
+
return
|
56 |
+
if str(new_email).strip() == str(email).strip():
|
57 |
+
click.echo(click.style('Sorry, new email and old email are the same.', fg='red'))
|
58 |
+
return
|
59 |
+
user = UserService.query(email=email)
|
60 |
+
if not user:
|
61 |
+
click.echo(click.style('sorry. the account: [{}] not exist .'.format(email), fg='red'))
|
62 |
+
return
|
63 |
+
if not re.match(r"^[\w\._-]+@([\w_-]+\.)+[\w-]{2,4}$", new_email):
|
64 |
+
click.echo(click.style('sorry. {} is not a valid email. '.format(new_email), fg='red'))
|
65 |
+
return
|
66 |
+
new_user = UserService.query(email=new_email)
|
67 |
+
if new_user:
|
68 |
+
click.echo(click.style('sorry. the account: [{}] is exist .'.format(new_email), fg='red'))
|
69 |
+
return
|
70 |
+
user_dict = {
|
71 |
+
'email': new_email
|
72 |
+
}
|
73 |
+
UserService.update_user(user[0].id,user_dict)
|
74 |
+
click.echo(click.style('Congratulations!, email has been reset.', fg='green'))
|
75 |
+
|
76 |
def register_commands(app: Flask):
|
77 |
+
app.cli.add_command(reset_password)
|
78 |
+
app.cli.add_command(reset_email)
|