advanced.txt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 1 Advanced
  2. ----------
  3. 1.1 Bootstrap
  4. ~~~~~~~~~~~~~
  5. You can add you .js file or .css file with flask-assets
  6. .. code-block:: python
  7. maple = MapleBootstrap(css=('style/xxx.css',),js=('style/xxx.js',))
  8. maple.init_app(app)
  9. or you can add js or css in **templates**
  10. .. code-block:: html
  11. {% block style -%}
  12. {{super()}}
  13. You css file
  14. {% endblock -%}
  15. {% block script -%}
  16. {{super()}}
  17. You js file
  18. {% endblock -%}
  19. other **block**
  20. .. code-block:: html
  21. {% block title -%}
  22. {% endblock -%}
  23. 1.2 Error
  24. ~~~~~~~~~
  25. You can custom you template for error
  26. .. code-block:: html
  27. templates/templet/error_404.html
  28. templates/templet/error_403.html
  29. templates/templet/error_500.html
  30. 1.3 Auth
  31. ~~~~~~~~
  32. **Auth** api
  33. .. code-block:: python
  34. Auth(app=None, db=none, mail=none, user_model=none, use_principal=false,
  35. login_form=loginform, register_form=registerform, forget_form=forgetpasswordform):
  36. **db**
  37. .. code-block:: python
  38. db = SQLAlchemy(app)
  39. app = Flask(__name__)
  40. app.config.from_object(Config)
  41. db = SQLAlchemy(app)
  42. maplec = MapleCaptcha(app)
  43. mapleb = MapleBootstrap(app)
  44. mail = Mail(app)
  45. babel = Babel(app)
  46. **mail**
  47. .. code-block:: python
  48. mail = Mail(app)
  49. **user_model**
  50. .. code-block:: python
  51. class User(db.Model, UserMixin):
  52. id = db.Column(db.Integer, primary_key=True)
  53. username = db.Column(db.String(80), unique=True)
  54. email = db.Column(db.String(120), unique=True)
  55. password = db.Column(db.String(120), unique=True)
  56. def __repr__(self):
  57. return '<User %r>' % self.name
  58. @staticmethod
  59. def set_password(password):
  60. pw_hash = generate_password_hash(password)
  61. return pw_hash
  62. def check_password(self, password):
  63. return check_password_hash(self.password, password)
  64. **use_principal**
  65. if you use flask-principal,then set *use_principal = True*
  66. **form**
  67. You can customize form
  68. **Custom Model**
  69. 1.3.1 register_models
  70. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  71. .. code-block:: python
  72. from flask_maple import Auth
  73. class MyAuth(Auth):
  74. def register_models(self, form):
  75. user = self.User()
  76. user.username = form.username.data
  77. user.password = user.set_password(form.password.data)
  78. user.email = form.email.data
  79. self.db.session.add(user)
  80. self.db.session.commit()
  81. return user
  82. 1.3.2 confirm_models
  83. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  84. .. code-block:: python
  85. class MyAuth(Auth):
  86. def confirm_models(self, user):
  87. user.is_confirmed = True
  88. user.confirmed_time = datetime.now()
  89. user.roles = 'writer'
  90. self.db.session.commit()
  91. 1.3.3 email_models
  92. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  93. .. code-block:: python
  94. class MyAuth(Auth):
  95. def email_models(self):
  96. current_user.send_email_time = datetime.now()
  97. self.db.session.commit()