Extended User Model
Learn how to code an extended user model in Flask
This page explains how to code an extended user model in Flask. Bsed on the code, a simple User model that manages only the authentication is enhanced with more features: Admin role, state (active & suspended), password strenght during the registration process
Topics covered
- Starting from a simple starter
- Enhance the exising
Usermodel with roles and state (users can be supended or active) - Add the new model
UserProfileto manage other fields related to the useraddress,phone number,Full name,website
- CLI improvements
- to manage and migarte the database
- create
admins
- The UI changes
- for ordinary users
- for admins
\
Starting from a simple starter
The codebase used during the demostration is Flask Datta Able, a simple and open-source starter.
\
Enhance the exising User model
- Add new fields
role,status,failed_logins\
Add new UserProfile model
- UserProfile table fileds
full_name,bio,address,zipcode,phone,email,website,image
\
CLI improvements
- Used for database init & migration
- python manage.py db init (for create migrations)
- python manage.py db migrate (for apply migrations)
- python manage.py db create_admin (for create admin user)
- python manage.py db runserver (for run app)
\
relation with the parent (User model)
- ForeignKey realtion with UserProfile
Signals
- Create Userprofile(function name: create_profile_by_user)
- Delete Userprofile(function name: delete_profile_by_user)
\
Routing updates
/register(methods=['GET', 'POST'])/login(methods=['GET', 'POST'])/profile(methods=['GET'])/user_list(methods=['GET'])/edit_user(methods=['GET', 'PUT'])/update_status(methods=['PUT'])/email_exists(methods=['GET'])/delete_user(methods=['DELETE'])
\
UI Changes
- Bootstrap model
- edit button popup model (for edit users details)
- edit button popup model (for user profile details)
- validate the password strength using a bar colored from red (week password) to green (strong password).
- status update toggle button (Active/Suspend)
- user edit email check valid or not
- delete user display popup (Do you want to delte)
\
events front-end the backend
- onchange (for status update)
- onclick (for get user details and delete user)
- onSubmit (for form submit)
- onkeyUp (for check email exists or not)
\
events from the backend
1.GET, POST, PUT, DELETE
\
FTP Server
- connect FTP server
- upload image to save FTP server
- save image url to db
- get image to saved image url