commands.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import datetime
  2. import random
  3. import string
  4. import click
  5. from libs.password import password_pattern, valid_password, hash_password
  6. from libs.helper import email as email_validate
  7. from extensions.ext_database import db
  8. from models.account import InvitationCode
  9. from models.model import Account
  10. import secrets
  11. import base64
  12. @click.command('reset-password', help='Reset the account password.')
  13. @click.option('--email', prompt=True, help='The email address of the account whose password you need to reset')
  14. @click.option('--new-password', prompt=True, help='the new password.')
  15. @click.option('--password-confirm', prompt=True, help='the new password confirm.')
  16. def reset_password(email, new_password, password_confirm):
  17. if str(new_password).strip() != str(password_confirm).strip():
  18. click.echo(click.style('sorry. The two passwords do not match.', fg='red'))
  19. return
  20. account = db.session.query(Account). \
  21. filter(Account.email == email). \
  22. one_or_none()
  23. if not account:
  24. click.echo(click.style('sorry. the account: [{}] not exist .'.format(email), fg='red'))
  25. return
  26. try:
  27. valid_password(new_password)
  28. except:
  29. click.echo(
  30. click.style('sorry. The passwords must match {} '.format(password_pattern), fg='red'))
  31. return
  32. # generate password salt
  33. salt = secrets.token_bytes(16)
  34. base64_salt = base64.b64encode(salt).decode()
  35. # encrypt password with salt
  36. password_hashed = hash_password(new_password, salt)
  37. base64_password_hashed = base64.b64encode(password_hashed).decode()
  38. account.password = base64_password_hashed
  39. account.password_salt = base64_salt
  40. db.session.commit()
  41. click.echo(click.style('Congratulations!, password has been reset.', fg='green'))
  42. @click.command('reset-email', help='Reset the account email.')
  43. @click.option('--email', prompt=True, help='The old email address of the account whose email you need to reset')
  44. @click.option('--new-email', prompt=True, help='the new email.')
  45. @click.option('--email-confirm', prompt=True, help='the new email confirm.')
  46. def reset_email(email, new_email, email_confirm):
  47. if str(new_email).strip() != str(email_confirm).strip():
  48. click.echo(click.style('Sorry, new email and confirm email do not match.', fg='red'))
  49. return
  50. account = db.session.query(Account). \
  51. filter(Account.email == email). \
  52. one_or_none()
  53. if not account:
  54. click.echo(click.style('sorry. the account: [{}] not exist .'.format(email), fg='red'))
  55. return
  56. try:
  57. email_validate(new_email)
  58. except:
  59. click.echo(
  60. click.style('sorry. {} is not a valid email. '.format(email), fg='red'))
  61. return
  62. account.email = new_email
  63. db.session.commit()
  64. click.echo(click.style('Congratulations!, email has been reset.', fg='green'))
  65. @click.command('generate-invitation-codes', help='Generate invitation codes.')
  66. @click.option('--batch', help='The batch of invitation codes.')
  67. @click.option('--count', prompt=True, help='Invitation codes count.')
  68. def generate_invitation_codes(batch, count):
  69. if not batch:
  70. now = datetime.datetime.now()
  71. batch = now.strftime('%Y%m%d%H%M%S')
  72. if not count or int(count) <= 0:
  73. click.echo(click.style('sorry. the count must be greater than 0.', fg='red'))
  74. return
  75. count = int(count)
  76. click.echo('Start generate {} invitation codes for batch {}.'.format(count, batch))
  77. codes = ''
  78. for i in range(count):
  79. code = generate_invitation_code()
  80. invitation_code = InvitationCode(
  81. code=code,
  82. batch=batch
  83. )
  84. db.session.add(invitation_code)
  85. click.echo(code)
  86. codes += code + "\n"
  87. db.session.commit()
  88. filename = 'storage/invitation-codes-{}.txt'.format(batch)
  89. with open(filename, 'w') as f:
  90. f.write(codes)
  91. click.echo(click.style(
  92. 'Congratulations! Generated {} invitation codes for batch {} and saved to the file \'{}\''.format(count, batch,
  93. filename),
  94. fg='green'))
  95. def generate_invitation_code():
  96. code = generate_upper_string()
  97. while db.session.query(InvitationCode).filter(InvitationCode.code == code).count() > 0:
  98. code = generate_upper_string()
  99. return code
  100. def generate_upper_string():
  101. letters_digits = string.ascii_uppercase + string.digits
  102. result = ""
  103. for i in range(8):
  104. result += random.choice(letters_digits)
  105. return result
  106. def register_commands(app):
  107. app.cli.add_command(reset_password)
  108. app.cli.add_command(reset_email)
  109. app.cli.add_command(generate_invitation_codes)