Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. ## Flask Stuff
  2. ### Notes from 6/28
  3.  
  4. #### Today's tasks:
  5. + Use flask-migrate to manage db state
  6. + manage flash messages with session
  7. + hash + salt with flask bcrypt
  8.  
  9.  
  10. #### Setup instructions
  11. + mkvirtualenv flask_migrate
  12. + structure: root
  13. + templates
  14. + base.html
  15. + index.html
  16. + new.html
  17. + app.py
  18. + pip install flask flask_sqlalchemy psycopg2 flask_migrate
  19. + pip freeze > requirements.txt
  20. + createdb learn-flask-migrate
  21.  
  22.  
  23. #### Making the application
  24.  
  25. ```
  26. from flask import Flask
  27. from flask_sqlalchemy import SQLAlchemy
  28.  
  29.  
  30.  
  31. app = Flask(__name__)
  32. db = SQLAlchemy(app)
  33.  
  34. app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://localhost/learn-flask-migrate'
  35.  
  36.  
  37. if __name__ == '__main__':
  38. app.run(debug=True, port=3000)
  39.  
  40. ```
  41. Make a model next. At this point, the password is still insecure, but we will refactor later.
  42.  
  43. ```
  44. class User(db.Model):
  45. id = db.Column(db.Integer, primary_key=True)
  46. username = db.Column(db.Text, nullable=False)
  47. password = db.Column(db.Text, nullable=False)
  48.  
  49. def __init__(self, username, password):
  50. self.username = username
  51. self.password = password
  52.  
  53. ```
  54.  
  55. Create a new file, `manage.py`
  56. <br>
  57. In manage.py, insert the following:
  58.  
  59. ```
  60. from app import app, db
  61.  
  62. from flask_script import Manager # this class handles command line script initialization
  63.  
  64. from flask_migrate import Migrate, MigrateCommand
  65. # Migrate - handles migration initialization
  66. # MigrateCommand - specifies what command line task runs our migrations
  67.  
  68. # Initialize flask_migrate
  69. # Need to specify the application and db you are using
  70. migrate = Migrate(app, db)
  71.  
  72. # Initialize flask_script
  73. manager = Manager(app)
  74.  
  75.  
  76. # Specify what string gets attached to the MigrateCommand
  77. manager.add_command('db', MigrateCommand)
  78.  
  79.  
  80.  
  81.  
  82. if __name__ == '__main__':
  83. manager.run()
  84. ```
  85.  
  86. Now, `python manage.py db` in the terminal will bring up a list arguments available to flask_migrate
  87.  
  88. #### Steps
  89.  
  90. 1 Run `python manage.py db init` to create a migrations folder with alembic setup files
  91. 2 `python manage.py db migrate`
  92. 3 `python manage.py db upgrade`
  93.  
  94.  
  95. You can then upgrade or downgrade as necessary.
  96.  
  97. <hr>
  98. ## Notes
  99. + flask_migrate is an extension of something called "alembic"
  100. + flask_script allows you to run shell scripts out of flask
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement