Skip to content

RodolfoFerro/technical-test-w-flask

Repository files navigation

Solution to Python Flask technical challenge 🚀

Code size Last commit License Twitter follow

This repository contains the solution for a particular challenge using Python + Flask to create a web platform with several functionalities.

Challenge description

The whole challenge specs could be listed as follows:

  1. Create a new Python project using Flask 1.1.x.1
  2. Create a User model with properties:
    • Integer ID PK (self-increasing)
    • String Name (30)
    • String First_Last_Name (30)
    • String Second_Last_Name (30) (optional)
    • String Email (80)
    • DateTime BirthDate
    • String Gender (Accepts M, F, O)
    • String Password (Must be encrypted)
  3. Create needed methods for CRUD operations in the user model, using REST.2
    • CREATE
    • READ
    • UPDATE
    • DELETE
  4. Create a login view with route '/login' asking for Email and Password (this field must have a mask). In case of correct login, the app must generate a session for the user.
    • Login view
    • Generate a session
  5. Create a view with route '/users' which should show a table with all registered users. It must contain an action button to delete the user, and a column with an icon for the gender.
  6. Create a view with route '/users?filter={name}' (name could be only text), which must show a table with registered users whose full name includes the sent characters by the name parameter.
  7. Create a view with route '/user/{id}' (id could be only a number), which should show the detailed user corresponding to the id variable, in case it exists. Otherwise, return a 404 screen for that route.3
  8. Create a Role model with properties:
    • Integer ID PK (self-increasing)
    • String Name (30)
    • String Description (20) (optional)
  9. Create two roles, Administrator and Client, directly in the database.4
  10. Add relation of User with Role 1-1.
  11. Create view of creation/edition of a user. The user could only access this view if the user is logged in and the user session must be validated.

Extra considerations

  • Date must be in format DD/MM/YYY.
  • The name must appear concatenated as follows: { Name } { First_Last_Name } { Second_Last_Name }.
  • The gender option must show the tags Male for M, Female for F and Other for O (must use filter tag).
  • If the logged user has type Administrator, the user should be able to see a button to delete the user.

Extra developments

In this section I'll list all the additional features developed in the project.

  • 1 The project has been Dockerized. To test the utility files with Docker up, you must change the base url from http://0.0.0.0:5000/<endpoint> to http://localhost:5000/<endpoint> and so on.
  • 2 An extra script (crud_utils.py) has been added. It uses the requests module to test CRUD via REST. To use the script and test CRUD, while the app is up and running, simply run:
    (docker-flask)$ python crud_utils.py
    This will create a sample table in the database.
  • 3 For each query, a variable named username has been added, which contains the concatenated name.
  • 4 An extra script (roles_utils.py) has been added. This directly writes the Administrator and Client roles to the database. To use the script, while the app is up and running, simply execute:
    (docker-flask)$ python roles_utils.py
    This will create two roles in the database.

Extra comments

  • Since the 1-1 relationship has not been specified in more detail, the code for this section has been added, but it has been commented to avoid any possible malfunction inside the code. You can find this in the models.py script (lines 15 and 24).
  • NOT A SINGLE LINTER HAS BEEN USED. All the code structure is mainly based on my own practices trying to follow Python's philosophy.
  • THE WHOLE CODE HAS BEEN DEVELOPED USING ONLY ATOM, without any packages nor extra tools that may help during the development.
  • Since I'm the only developer for this project, ANY OTHER POSSIBLE BRANCHES WERE NOT CREATED. It is recommended to work using branches, but for this particular exercise I preferred to work directly (and only) with the master branch, as I wanted to finish the challenge ASAP.

Prerequisities

Before you begin, ensure you have met the following requirements:

For only-Docker usage:

  • You have a Windows/Linux/Mac machine with the latest version of Docker installed.

For only-Python usage:

For general purposes, why not installing prerequisites for both cases?

Install/Run with only Python

If you want to install the dependencies and work locally using only Python, you can simply follow this steps. If you want to directly work using Docker, jump to the "Install/Run with Docker" section.

Clone the project repository:

git clone https://github.com/RodolfoFerro/technical-test-w-flask.git
cd technical-test-w-flask

To create and activate the virtual environment, follow these steps:

Using conda:

$ conda create -n docker-flask python=3.7

# Activate the virtual environment:
$ conda activate docker-flask

# To deactivate:
(docker-flask)$ conda deactivate

Using virtualenv:

# In this case I'm supposing that your latest python3 version is +3.6
$ virtualenv docker-flask --python=python3

# Activate the virtual environment:
$ source docker-flask/bin/activate

# To deactivate:
(docker-flask)$ deactivate

To install the requirements using pip, once the virtual environment is active:

(docker-flask)$ pip install -r requirements.txt

Finally, if you want to run the app locally, simply run:

(docker-flask)$ python app.py

Now you should be able to test the API at http://0.0.0.0:5000/.

Install/Run with Docker

If you want to install the dependencies and work using Docker, you can simply follow this steps. If you want to simply work locally using only Python, jump back to the "Install/Run with only Python" section.

Clone the project repository:

git clone https://github.com/RodolfoFerro/technical-test-w-flask.git
cd technical-test-w-flask

To build the Docker image, simply run:

$ docker build -t technical-test-w-flask .

To run the Docker image, run the following:

$ docker run -it -d -p 5000:5000 -v $(pwd):/app technical-test-w-flask

Now you should be able to test the API at http://localhost:5000/.

To stop the Docker container:

$ docker ps
$ docker stop <container-id>

Contact

If you want to contact me you can reach me at rodolfoferroperez@gmail.com. There, or through any other of my social profiles your can find at: https://rodolfoferro.glitch.me/

License

This project uses an MIT License.

About

Solution for Python + Flask technical challenge.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published