commands.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from __future__ import print_function
  2. from flask import current_app as app
  3. from flask_script import prompt, prompt_pass, prompt_choices
  4. from project import db
  5. from project import Role, User
  6. from project.manage import manager
  7. from library.get_or_create import get_or_create
  8. @manager.command
  9. @manager.option('-n', '--name', help='Role name')
  10. @manager.option('-d', '--description', help='Role description')
  11. def createrole(name=None, description=None):
  12. """Create a role"""
  13. if not name:
  14. name = prompt("Role Name")
  15. if not description:
  16. description = prompt("Role description")
  17. if all([name, description]):
  18. role = Role.createrole(
  19. db.session,
  20. name=name,
  21. description=description
  22. )
  23. db.session.commit()
  24. else:
  25. role = "Can't create the role"
  26. print(role)
  27. @manager.command
  28. @manager.option('-e', '--email', help='User e-mail')
  29. @manager.option('-p', '--password', help='User password')
  30. def createsuperuser(email=None, password=None):
  31. """Create a supersuer"""
  32. if not email:
  33. email = prompt("A valid email address")
  34. if not password:
  35. password = prompt_pass("Password")
  36. if all([email, password]):
  37. admin = get_or_create(db.session, Role, name='admin')
  38. user = User.createuser(db.session, email, password, roles=[admin])
  39. db.session.commit()
  40. else:
  41. user = "Can't create the supersuser"
  42. print(user)
  43. @manager.command
  44. @manager.option('-e', '--email', help='User e-mail')
  45. @manager.option('-p', '--password', help='User password')
  46. @manager.option('-r', '--role', help='User role')
  47. def createuser(email=None, password=None, role=None):
  48. """Create a user"""
  49. if not email:
  50. email = prompt("A valid email address")
  51. if not password:
  52. password = prompt_pass("Password")
  53. if not role:
  54. roles = [r.name for r in db.session.query(Role)]
  55. role_name = prompt_choices("Role", choices=roles,
  56. no_choice=('none', ''))
  57. if role_name:
  58. role = get_or_create(db.session, Role, name=role_name)
  59. else:
  60. role = None
  61. else:
  62. role = get_or_create(db.session, Role, name=role)
  63. if all([email, password]):
  64. user = User.createuser(db.session, email, password, roles=[role])
  65. db.session.commit()
  66. else:
  67. user = "Can't create the user"
  68. print(user)