📜 ⬆️ ⬇️

Ready site template with registration, users and admins on Flask with SQL or MongoDB databases

flask

Happens, it is necessary to do sites on flask which have users and administrators. Purely for myself, I decided to standardize it somehow and, most importantly, not to lose time when such a task appears. The goal - in a few teams to get a working site that has:


Screenshots


Here is what comes out of the box:


')




User profile after authorization:



Admin- flask admin :



You can get this site in two ways:

I Option. Using flask-user and SQL database


Install and Run

Installation


cd ~ #    virtualenv env #    . env/bin/activate mkdir -p ~/www/my_app cd www git clone https://github.com/Alexmod/Flask-User-and-Flask-admin.git my_app cd my_app/ #    pip install -r requirements.txt #    python manage.py init_db #   py.test tests/ #    ,   python manage.py runserver 

After these commands, the address http://localhost:5000/ should open the site as in the screenshots.

User: user@example.com Password: Password1.
Admin: admin@example.com Password: Password1.

I ran and tested on linux with Python version 3.4, but I assume that it should "take off" on any 3.x.

Link to github.

Console Setup Sample



Details of the first option

Details


I took the Flask-User module as a basis and this repository. What has been added / changed:

  1. Localization of both templates and flash messages has been added. Language support: (German, English, Spanish, Farsi, Finnish, French, Italian, Danish, Russian, Swedish, Turkish, Chinese). If the language is not in the list, then by default, the Russian language is set as default in the config file.

     # local_settings.py BABEL_DEFAULT_LOCALE = 'ru' 

    In Russian, I made my localization.

     app/translations/ru/LC_MESSAGES/flask_user.po 

    Flask-Babel did a great job with flash messages, but in order for the templates to work, a Russian translation of the strings like:

     {%trans%}Forgot your Password?{%endtrans%}  {{ _('Sign in') }} 

    I had to sabotage. Added by:

     import gettext 

    and this function:

     def set_lang(lang): i18n_dir = app.config['BABEL_TRANSLATION_DIRECTORIES'] gettext.install('lang', i18n_dir) trans_file = i18n_dir + lang + '/LC_MESSAGES/flask_user' tr = gettext.translation(trans_file, 'locale', languages=[lang]) tr.install(True) app.jinja_env.install_gettext_translations(tr) 

  2. Removed the field name and surname during registration. In my experience it is rarely required, but annoying users.
  3. All * .py files are referenced to the pep-8 standard.
  4. Cleaned up modules that are loaded but not used.
  5. Routes are divided into 3 parts: public, admin and user (public_view.py, members_views.py, about the admin below)
  6. The flask_bootstrap module has been added and templates and flash messages have been changed with it.

     {% import "bootstrap/utils.html" as utils %} {{ utils.flashed_messages(dismissible=True, container=False) }} 

    In the file layout.html.
  7. Added admin Flask-Admin ( article on Habré about it ). After simple manipulations, she allowed to activate, deactivate and delete users, gave the opportunity to add roles. Plus work with static files (upload to the server, delete, create folders, etc.). No localization yet.



    Friends Flask-Admin and Flask-User did not want to share when using user_models.py together.

    I had to create a separate models.py file for Flask-Admin, and I could not figure out why they interfered with each other.



Option II. Using flask-security and MongoDB





Screenshots

Screenshots


Here is what comes out of the box:








Install and Run

Installation



I hope you already have git, virtualenv and mongoDB installed.

 cd ~ #    virtualenv env #   . env/bin/activate #    mkdir -p ~/www/my_app cd www git clone https://github.com/Alexmod/flask-security-flask-admin-mongodb.git my_app cd my_app #     pip install -r requirements.txt #   python manage.py runserver 


After these commands, the address http://localhost:5000/ should open the site as in the screenshots.

User: user@example.com Password: Password1.
Admin: admin@example.com Password: Password1.

I ran on linux with Python version 3.6, but I assume that it should "take off" on any 3.x.

Link to GitHub for MongoDB


Depla first and second options


Deploying methods are many. Here is one of the most simple.
We put gunicorn:
 pip install gunicorn 

In the folder my_app create a new file wsgi.py with the following content:
 from app import create_app app = create_app() 


And run:
 gunicorn wsgi:app 


If everything goes smoothly, then at http://127.0.0.1:8000 will be a site that can be bolted to nginx or apache. Next we configure systemd so that it starts automatically, and do not forget to change the line in the local_settings.py file:

 DEBUG = True #   False 

Source: https://habr.com/ru/post/349264/


All Articles