Skip to main content

Signs for Trucks

TOC​

team-collaboration/version-control/githubGithub Tip

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​

  1. Clone the repository:

    git clone https://github.com/HPetersen2/truck-signs-api.git
    cd truck-signs-api
  2. Configure a virtual environment and set up the database. See configuring a virtual environment and database setup.

  3. Configure the environment variables.

    1. Copy the example env file from the truck_signs_designs/settings folder to a .env file:

      cd truck_signs_designs/settings
      cp simple_env_config.env .env
    2. The new .env file contains all variables needed to run the Django app in every environment. For the development environment, only the following are required:

      SECRET_KEY
      DB_NAME
      DB_USER
      DB_PASSWORD
      DB_HOST
      DB_PORT
      STRIPE_PUBLISHABLE_KEY
      STRIPE_SECRET_KEY
      EMAIL_HOST_USER
      EMAIL_HOST_PASSWORD
    3. Example database configuration for local development (use your own, strong password):

      DB_NAME=trucksigns_db
      DB_USER=trucksigns_user
      DB_PASSWORD=<choose-a-strong-password>
      DB_HOST=localhost
      DB_PORT=5432
    4. SECRET_KEY is the Django secret key. To generate a new one, see this Stack Overflow answer.

    5. STRIPE_PUBLISHABLE_KEY and STRIPE_SECRET_KEY come from a Stripe developer account (not required for the exercise):

      1. Log in to your Stripe account at stripe.com or create a new one. This redirects to the Dashboard.
      2. Go to Developers > API keys and copy both the publishable key and the secret key.
    6. EMAIL_HOST_USER and EMAIL_HOST_PASSWORD are 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 in views.py. Any valid email and password will therefore work.

  4. Run the migrations and then the app:

    python manage.py migrate
    python manage.py runserver
  5. The app is now running at http://localhost:8000.

  6. (Optional) Create a superuser for the admin panel:

    python manage.py createsuperuser
note

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.

warning

Never commit your .env file. It contains the Django secret key, database credentials, and Stripe keys.

PostgreSQL Database​

Docker​

Django and DRF​

Miscellaneous​