"inventory-backend" is a Flask application hosted on PythonAnywhere that provides information to a React frontend via RESTful APIs.
-
Inventory management system that can be accessed via the internet, and updated by a number of different employees
-
CRUD capabilities for adding new items, updating existing ones, as well as deletion of old products
-
Daily snapshots of total inventory at closing time
- In order to meet the client's needs, I decided to link a Flask application with a PythonAnywhere PostgreSQL database. I considered using FastAPI because my main goal was to design RESTful APIs that interacted with a simple database schema, but PythonAnywhere only allows for WSGI hosting at the moment. Designing API routes for the create, read, update, and delete functions was relatively simple, but I had to add a separate route for photos that can be uploaded to the frontend. The upload API downloads the photograph into a static directory in my backend and then generates and stores a URL in the database so that the photo can be served to the frontend. Daily snapshots occur via PythonAnywhere's scheduled tasks and involve a SQL dump.
This RESTful API provides CRUD (Create, Read, Update, Delete) operations for managing product inventory.
https://kegsouth.pythonanywhere.com
- URL:
/ - Method:
GET - Description: Retrieves a list of products sorted from lowest amount to highest amount.
- Response:
[ { "id": 1, "item": "Coors Light", "amount": 10, "imageUrl": "http://example.com/image.jpg" }, "..." ] - Method:
POST - URL:
/ - Description: Adds item and amount to the database.
- Body
{ "item" : "Yeungling", "amount" : 2 } - Response:
{ "message": "New inventory item added with ID: 93", "item": { "id": 93, "item": "Yeungling", "image": "", "amount": 2 } - Error:
{ "message": "Item already exists or other error" } - Method:
PUT - URL:
/ - Description: Updates item amount in the database.
- Body
{ "item" : "Yeungling", "amount" : 5 } - Response:
{ "message": "Yeungling updated!", } - Error:
{ "message": "Item already exists or other error" } - Method:
DELETE - Description: Deletes item from the database.
- URL:
/<item> - Request:
https://kegsouth.pythonanywhere.com/Yeungling - Response:
{ "message": "Yeungling updated!", } - Error:
{ "message": "Item doesn't exist or other error" }
This RESTful API stores a user-specified image in the backend then generates and stores a URL in the database to serve the frontend.
https://kegsouth.pythonanywhere.com
- URL:
/upload - Method:
POST - Description: Stores image file, generates URL, and stores URL in database to serve '/' GET request
- Response:
{ "message": "File uploaded successfully", "imageUrl": "image_url" } - List of Errors:
{ "error": "No file part", "error": "No selected file", "error": "Invalid item ID", "error": "Item not found", }
- when no image is selected by user
- when image filename is empty
- when item associated with image doesn't exist
- when item associated with image has been deleted
- Create an account on PythonAnywhere
- Click on the 'database' tab and create a database
- Clone the repository using the bash console on PythonAnywhere
- Due to many dependencies being provided by PythonAnywhere, I would recommend downloading dependencies one at a time**
- Create a .env file at the same directory level as the application and add relevant information for the database connection
- Initilize the database for the first time using the class created in the python script. (PythonAnywhere has a good tutorial)
- Test out the database by using Postman website to send requests or by opening a MySQL console and manually adding items
- Visiting your website should display JSON formatted items in your database
- Congrats! You can start designing your frontend or use the repo here as inspiration: Front-End Repository
**A requirements.txt has been included to mass download dependencies but create a virtual environment before doing this to avoid duplicate dependencies between the PythonAywhere local directory and your project .local directory.