|
| 1 | +# `__init__.py` |
| 2 | + |
| 3 | +## CSRF Protection |
| 4 | + |
| 5 | +Note about CSRF protection. This basically prevents hackers |
| 6 | +from being able to post to our POST routes without having actually |
| 7 | +loaded a form on our website. E.g. they could potentially create |
| 8 | +users if they found out the URL for our register routes and |
| 9 | +the params we expect (its fairly easy to do). But with |
| 10 | +CSRF protection, all forms have a hidden field that is verified on |
| 11 | +our end. This is a bit low level, but there is a SESSION object |
| 12 | +stored on the flask server in memory. Each user has their |
| 13 | +own session containing things like their username, password, etc |
| 14 | +When a form created, a random string called a CSRF token is |
| 15 | +created and is sent along with the form in a hidden field. |
| 16 | +Simultaneously, this string is added to the user session |
| 17 | +stored on the server. When the user submits a form, then |
| 18 | +the server will check to see if the hidden form field with the |
| 19 | +CSRF token matches the CSRF token stored in the user's session |
| 20 | +on the server. If it does, then everything is fine and the |
| 21 | +POST request can proceed normally. If not, then the POST request |
| 22 | +is aborted as a 403 (i think) error is thrown...basically |
| 23 | +the user is not able to POST. This is great for forms, but |
| 24 | +if you want to create a public API that does not require a session, |
| 25 | +then you'll want to include a decorator on your route `@csrf.exempt` |
| 26 | + |
| 27 | +## Flask-Login |
| 28 | + |
| 29 | + |
| 30 | +```python |
| 31 | +login_manager = LoginManager() |
| 32 | +login_manager.session_protection = 'strong' |
| 33 | +login_manager.login_view = 'account.login' |
| 34 | +``` |
| 35 | + |
| 36 | +Flask-login provides us with a bunch of easy ways to do secure and |
| 37 | +simple login techniques. LoginManager() is the main class that |
| 38 | +will handle all of this. Session protection makes sure the |
| 39 | +user session is very secure and login_manager.login_view |
| 40 | +Is the view that the a non-authenticated user will get redirected |
| 41 | +to. Otherwise it is a 401 error. |
| 42 | + |
| 43 | +## `init_app(app)` |
| 44 | + |
| 45 | +```python |
| 46 | +mail.init_app(app) |
| 47 | +db.init_app(app) |
| 48 | +login_manager.init_app(app) |
| 49 | +csrf.init_app(app) |
| 50 | +compress.init_app(app) |
| 51 | +RQ(app) |
| 52 | +``` |
| 53 | + |
| 54 | +init_app(app) are methods in each of these packages |
| 55 | +More on init_app. It binds each instance of the respective |
| 56 | +application to the flask app. However, we do need to specify |
| 57 | +an application context while using things like db, mail, |
| 58 | +login_manager, and compress since they are not bound to our |
| 59 | +application _exclusively_. |
| 60 | + |
| 61 | +## Set up Asset Pipeline |
| 62 | + |
| 63 | +This one is a bit complex. First an Environment instance is created |
| 64 | +that holds references to a single path to the 'static' folder. We don't |
| 65 | +really care about that since the url_for() method allows us to specify |
| 66 | +access to resources in the static/ directory. But we then append all the |
| 67 | +folders and files within the 'dirs' array to the environment. This |
| 68 | +action provides context for the subsequence set of register actions. |
| 69 | +Looking in app/assets.py there are some Bundle instances created with |
| 70 | +3 parameters mainly: what type of file(s) to bundle, a type of filter/ |
| 71 | +transpiler to apply, and then a final output file. E.g. for the |
| 72 | +app_css bundle, it looks within assets/styles, assets/scripts for any |
| 73 | +*.scss files, converts them to css with the scss transpiler and then |
| 74 | +outputs it to the styles/app.css file. |
| 75 | +See the templates/partials/_head.html |
| 76 | +file for more information on how to actually include the file. |
| 77 | + |
| 78 | +## Blueprints |
| 79 | + |
| 80 | +```python |
| 81 | +from account import account as account_blueprint |
| 82 | +from admin import admin as admin_blueprint |
| 83 | +from main import main as main_blueprint |
| 84 | + |
| 85 | +app.register_blueprint(main_blueprint) |
| 86 | +app.register_blueprint(account_blueprint, url_prefix='/account') |
| 87 | +app.register_blueprint(admin_blueprint, url_prefix='/admin') |
| 88 | +``` |
| 89 | + |
| 90 | +Blueprints allow us to set up url prefixes for routes contained |
| 91 | +within the views file of each of the divisions we specify to be |
| 92 | +registered with a blueprint. Blueprints are meant to distinguish between |
| 93 | +the variable different bodies within a large application. |
| 94 | +In the case of flask-base, we have 'main', 'account', and 'admin' |
| 95 | +sections. The 'main' section contains error handling and views. |
| 96 | +The other sections contain mainly just views. The folders for each of |
| 97 | +these sections also contain an __init__ file which actually creates the |
| 98 | +Blueprint itself with a name and a default __name__ param as well. |
| 99 | +After that, the views file and any other files that depend upon the |
| 100 | +blueprint are imported and can use the variable name assigned to the |
| 101 | +blueprint to reference things like decorators for routes. e.g. if my |
| 102 | +blueprint is name 'first_component', I would use the following as |
| 103 | +a decorator for my routes '@first_component.route'. By specifying |
| 104 | +the url_prefix, all of the functions and routes etc of the blueprint |
| 105 | +will be read with the base url_prefix specified. E.g. if I wanted |
| 106 | +to access the '/blah' route within the 'acount' blueprint, I need only |
| 107 | +specify @account.router('/blah') def ... as my method in views.py under |
| 108 | +the account/ directory. But I would be able to access it in the |
| 109 | +browser with yourdomain.com/accounts/blah |
| 110 | +# |
| 111 | +A note on why we are importing here: Because stuff will break...and for |
| 112 | +a good reason! The account import in turn imports the views.py file under |
| 113 | +the account/ directory. The views.py in turn references db |
| 114 | +db is the database instance which was created after the import statements |
| 115 | +If we had included these import statements at the very top, views.py |
| 116 | +under account would have refered to a db instance which was not created! |
| 117 | +hence errors...all the errors (at least in files relying upon a created |
| 118 | +db instance...and any instance created beyond that. |
| 119 | + |
0 commit comments