commands.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import datetime
  2. import json
  3. import random
  4. import string
  5. import click
  6. from libs.password import password_pattern, valid_password, hash_password
  7. from libs.helper import email as email_validate
  8. from extensions.ext_database import db
  9. from models.account import InvitationCode
  10. from models.model import Account, AppModelConfig, ApiToken, Site, App, RecommendedApp
  11. import secrets
  12. import base64
  13. @click.command('reset-password', help='Reset the account password.')
  14. @click.option('--email', prompt=True, help='The email address of the account whose password you need to reset')
  15. @click.option('--new-password', prompt=True, help='the new password.')
  16. @click.option('--password-confirm', prompt=True, help='the new password confirm.')
  17. def reset_password(email, new_password, password_confirm):
  18. if str(new_password).strip() != str(password_confirm).strip():
  19. click.echo(click.style('sorry. The two passwords do not match.', fg='red'))
  20. return
  21. account = db.session.query(Account). \
  22. filter(Account.email == email). \
  23. one_or_none()
  24. if not account:
  25. click.echo(click.style('sorry. the account: [{}] not exist .'.format(email), fg='red'))
  26. return
  27. try:
  28. valid_password(new_password)
  29. except:
  30. click.echo(
  31. click.style('sorry. The passwords must match {} '.format(password_pattern), fg='red'))
  32. return
  33. # generate password salt
  34. salt = secrets.token_bytes(16)
  35. base64_salt = base64.b64encode(salt).decode()
  36. # encrypt password with salt
  37. password_hashed = hash_password(new_password, salt)
  38. base64_password_hashed = base64.b64encode(password_hashed).decode()
  39. account.password = base64_password_hashed
  40. account.password_salt = base64_salt
  41. db.session.commit()
  42. click.echo(click.style('Congratulations!, password has been reset.', fg='green'))
  43. @click.command('reset-email', help='Reset the account email.')
  44. @click.option('--email', prompt=True, help='The old email address of the account whose email you need to reset')
  45. @click.option('--new-email', prompt=True, help='the new email.')
  46. @click.option('--email-confirm', prompt=True, help='the new email confirm.')
  47. def reset_email(email, new_email, email_confirm):
  48. if str(new_email).strip() != str(email_confirm).strip():
  49. click.echo(click.style('Sorry, new email and confirm email do not match.', fg='red'))
  50. return
  51. account = db.session.query(Account). \
  52. filter(Account.email == email). \
  53. one_or_none()
  54. if not account:
  55. click.echo(click.style('sorry. the account: [{}] not exist .'.format(email), fg='red'))
  56. return
  57. try:
  58. email_validate(new_email)
  59. except:
  60. click.echo(
  61. click.style('sorry. {} is not a valid email. '.format(email), fg='red'))
  62. return
  63. account.email = new_email
  64. db.session.commit()
  65. click.echo(click.style('Congratulations!, email has been reset.', fg='green'))
  66. @click.command('generate-invitation-codes', help='Generate invitation codes.')
  67. @click.option('--batch', help='The batch of invitation codes.')
  68. @click.option('--count', prompt=True, help='Invitation codes count.')
  69. def generate_invitation_codes(batch, count):
  70. if not batch:
  71. now = datetime.datetime.now()
  72. batch = now.strftime('%Y%m%d%H%M%S')
  73. if not count or int(count) <= 0:
  74. click.echo(click.style('sorry. the count must be greater than 0.', fg='red'))
  75. return
  76. count = int(count)
  77. click.echo('Start generate {} invitation codes for batch {}.'.format(count, batch))
  78. codes = ''
  79. for i in range(count):
  80. code = generate_invitation_code()
  81. invitation_code = InvitationCode(
  82. code=code,
  83. batch=batch
  84. )
  85. db.session.add(invitation_code)
  86. click.echo(code)
  87. codes += code + "\n"
  88. db.session.commit()
  89. filename = 'storage/invitation-codes-{}.txt'.format(batch)
  90. with open(filename, 'w') as f:
  91. f.write(codes)
  92. click.echo(click.style(
  93. 'Congratulations! Generated {} invitation codes for batch {} and saved to the file \'{}\''.format(count, batch,
  94. filename),
  95. fg='green'))
  96. def generate_invitation_code():
  97. code = generate_upper_string()
  98. while db.session.query(InvitationCode).filter(InvitationCode.code == code).count() > 0:
  99. code = generate_upper_string()
  100. return code
  101. def generate_upper_string():
  102. letters_digits = string.ascii_uppercase + string.digits
  103. result = ""
  104. for i in range(8):
  105. result += random.choice(letters_digits)
  106. return result
  107. @click.command('gen-recommended-apps', help='Number of records to generate')
  108. def generate_recommended_apps():
  109. print('Generating recommended app data...')
  110. apps = App.query.filter(App.is_public == True).all()
  111. for app in apps:
  112. recommended_app = RecommendedApp(
  113. app_id=app.id,
  114. description={
  115. 'en': 'Description for ' + app.name,
  116. 'zh': '描述 ' + app.name
  117. },
  118. copyright='Copyright ' + str(random.randint(1990, 2020)),
  119. privacy_policy='https://privacypolicy.example.com',
  120. category=random.choice(['Games', 'News', 'Music', 'Sports']),
  121. position=random.randint(1, 100),
  122. install_count=random.randint(100, 100000)
  123. )
  124. db.session.add(recommended_app)
  125. db.session.commit()
  126. print('Done!')
  127. def register_commands(app):
  128. app.cli.add_command(reset_password)
  129. app.cli.add_command(reset_email)
  130. app.cli.add_command(generate_invitation_codes)
  131. app.cli.add_command(generate_recommended_apps)