Flask: Micro blog admin app with Postgresql
Ensure that development libraries for PostgreSQL are installed.
Clone the project to application folder:
$ git clone git@github.com:Leo-g/Flask-Scaffold.git FlaskBlog && cd FlaskBlog
Let's setup virtualenv with Python3:
$ virtualenv -p python3 venv3 Running virtualenv with interpreter /usr/bin/python3 Using base prefix '/usr' New python executable in venv3/bin/python3 Also creating executable in venv3/bin/python $ source venv3/bin/activate
Install the requirements using requirements.txt:
(venv3)$ pip install -r requirements.txt ... Successfully installed Flask Flask-Mail Flask-Migrate Flask-RESTful Flask-SQLAlchemy Flask-Script PyJWT PyMySQL PyYAML autopep8 inflect marshmallow marshmallow-jsonapi psycopg2 pycrypto uWSGI Werkzeug Jinja2 itsdangerous blinker alembic pytz aniso8601 six SQLAlchemy pep8 MarkupSafe Mako python-editor python-dateutil
To exit from virtualenv, just type deactivate.
Login to local Postgresql:
$ sudo -i -u postgres [sudo] password for k: postgres@laptop:~$ postgres@laptop:~$ psql Password: psql (9.3.9) Type "help" for help. postgres=# postgres=# CREATE DATABASE flask_blog_db OWNER postgres; CREATE DATABASE postgres=# GRANT ALL PRIVILEGES ON DATABASE flask_blog_db TO postgres; GRANT
Let's create and see our current databases:
postgres-# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges ------------------------+----------+----------+-------------+-------------+----------------------- ... flask_blog_db | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | :\q
Create a user:
postgres-# CREATE USER flask WITH PASSWORD 'password'; CREATE ROLE
Now, all we need to do is give our database user access rights to the database we created:
postgres=# GRANT ALL PRIVILEGES ON DATABASE flask_blog_db TO flask; GRANT
Listing users:
postgres=# \du List of roles Role name | Attributes | Member of ------------+------------------------------------------------+----------- ... postgres | Superuser, Create role, Create DB, Replication | {} ...
We need to fill our database username, password, name, host etc. into config.py:
pg_db_username = 'postgres' pg_db_password = 'postgres' pg_db_name = 'flask_blog_db' pg_db_hostname = 'localhost'
Our Resource and it's fields are defined in a YAML file (scaffold/blog.yaml):
posts: - tittle:string - body:text - author:string - creation_date:date - published:boolean comments: - author:string - body:text - author_url:url - created_on:date - approved:boolean authors: - name:string - profile:text - url:url
Let's run the scaffolding:
(venv3)$ python scaffold.py scaffold/blog.yaml
Let's run the database migrations:
(venv3)$ python db.py db init
Migrate:
(venv3)$ python db.py db migrate
We can continue:
(venv3)$ python db.py db upgrade
Let's run the server:
(venv3)$ python run.py * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger pin code: 228-809-454
Now, we should be able to see the Login Page at http://localhost:5000, Sign Up and Login:
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization