Signs for Trucks
TOC
URL: https://github.com/HPetersen2/truck-signs-api
Checkout this repository to see the code/implementation
Description
Signs for Trucks is an online store to buy pre-designed vinyls with custom lines of letters (often called truck letterings). The store also allows clients to upload their own designs and to customize them on the website as well. Aside from the vinyls that are the main product of the store, clients can also purchase simple lettering vinyls with no truck logo, a fire extinguisher vinyl, and/or a vinyl with only the truck unit number (or another number selected by the client).
This page documents the Django backend (REST API and admin panel).
Tech Stack
- Python 3.8.10
- Django 2.2.8
- Django REST Framework 3.12.4
- PostgreSQL
- Stripe for payments
Project Overview
Settings
The settings folder inside the truck_signs_designs folder contains the settings for each environment (development, Docker testing, and production). Those files extend base.py, which contains the basic configuration shared among the environments (for example, the location of the template directory).
In addition, the .env file inside this folder holds the environment variables, mostly sensitive information, which must always be configured before use. By default, the environment in use is Docker testing. To switch between environments, modify the __init__.py file.
Models
Most of the models do what can be inferred from their name. The following notes make the purpose of some of them clearer:
- Category: The category of the vinyls in the store. It contains the title of the category as well as the basic properties shared among products of the same category. For example, Truck Logo is a category for all vinyls that have a truck logo plus some lines of lettering (the vinyls are instances of the model Product). Another category is Fire Extinguisher, for all vinyls with a fire extinguisher logo.
- Lettering Item Category: The category of the lettering, for example Company Name or VIN Number. Each has a different price.
- Lettering Item Variations: Contains a foreign key to the Lettering Item Category and the text added by the client.
- Product Variation: Has the original product as a foreign key, plus the lettering lines (instances of Lettering Item Variations) added by the client.
- Order: Contains the cart (just one vinyl, as only one product can be purchased at a time), plus the contact and shipping information of the client.
- Payment: Holds the payment information such as the time of the purchase and the client ID in Stripe.
Brief Explanation of the Views
Most views are class-based views (CBVs) from rest_framework.generics. They provide the basic CRUD operations of the API and inherit from ListAPIView, CreateAPIView, RetrieveAPIView, and so on.
Some views had to be customized. For example, creating an order and the payment are implemented in the same view, which therefore inherits from GenericAPIView. Another example is the UploadCustomerImage view, which takes the vinyl template uploaded by the client and creates a new product based on it.
Installation
-
Clone the repository:
git clone https://github.com/HPetersen2/truck-signs-api.gitcd truck-signs-api -
Configure a virtual environment and set up the database. See configuring a virtual environment and database setup.
-
Configure the environment variables.
-
Copy the example env file from the
truck_signs_designs/settingsfolder to a.envfile:cd truck_signs_designs/settingscp simple_env_config.env .env -
The new
.envfile contains all variables needed to run the Django app in every environment. For the development environment, only the following are required:SECRET_KEYDB_NAMEDB_USERDB_PASSWORDDB_HOSTDB_PORTSTRIPE_PUBLISHABLE_KEYSTRIPE_SECRET_KEYEMAIL_HOST_USEREMAIL_HOST_PASSWORD -
Example database configuration for local development (use your own, strong password):
DB_NAME=trucksigns_dbDB_USER=trucksigns_userDB_PASSWORD=<choose-a-strong-password>DB_HOST=localhostDB_PORT=5432 -
SECRET_KEYis the Django secret key. To generate a new one, see this Stack Overflow answer. -
STRIPE_PUBLISHABLE_KEYandSTRIPE_SECRET_KEYcome from a Stripe developer account (not required for the exercise):- Log in to your Stripe account at stripe.com or create a new one. This redirects to the Dashboard.
- Go to Developers > API keys and copy both the publishable key and the secret key.
-
EMAIL_HOST_USERandEMAIL_HOST_PASSWORDare the credentials used to send emails when a client makes a purchase. Sending is currently disabled; the code to activate it is commented out in the create-order view inviews.py. Any valid email and password will therefore work.
-
-
Run the migrations and then the app:
python manage.py migratepython manage.py runserver -
The app is now running at http://localhost:8000.
-
(Optional) Create a superuser for the admin panel:
python manage.py createsuperuser
To create truck vinyls with truck logos, first create the Category Truck Sign, then the Product (any name). The frontend only fetches products of the category Truck Sign for the product grid.
Never commit your .env file. It contains the Django secret key, database credentials, and Stripe keys.
Useful Links
PostgreSQL Database
Docker
- Docker Official Documentation
- Dockerizing Django, PostgreSQL, Gunicorn, and Nginx:
Django and DRF
- Django Official Documentation
- Generate a new secret key
- Modify the Django admin:
- Django Rest Framework Official Documentation
- More about nested serializers
- More about generic views