Repo to store back end infrastructure for Message in a Bottle

Overview


Message in a Bottle Backend API

RESTful API for Message in a Bottle frontend application consumption.

contributors_badge forks_badge stars_badge issues_badge

About The ProjectTools UsedLocal Set UpInstallationHow To UseDatabase SchemaContributingAcknowledgements

About The Project

Message in a Bottle is an application where users can discover stories about the communities around them. This app was designed as a way to interact with cities, neighborhoods, and the people that inhabit them. The Message in a Bottle backend is built with a Django framework that stores story and user data through PostgreSQL. We expose this data to our frontend team to build out the user interface.

Learning Goals

  • Building a RESTful API with a Python Django backend
  • Collaborating with a Front-End dev team
  • Geolocation calls and tracking
  • Applying best practices learned during Turing to a new language and framework
    • e.g. TDD, OOP, REST, MVC(Rails) <--> MTV(Django)
  • Making use of the git rebase workflow

Tools Used

Development Testing Packages
Python 3.9.7 Pytest-Django Django
Django Pytest-Cov Django CORS Headers
CircleCI Postman Django Heroku
PostgreSQL VCRPY Django REST Framework
Git/Github Gunicorn
Heroku Psycopg2
Pycodestyle
Python-Decouple
Python-DotENV
Requests

Local Set Up

  1. To clone and run this application, you'll need Python 3.9.7 and Django 3.2.8. Using the official Python docs, follow instructions to install python3 for your local OS.

  2. You can check for a successful installation using this command:

$ python3 -V
> Python 3.9.7

Installation

  1. Fork this repo
  2. Clone your new repo:
git clone https://github.com/MIBFB-COLLAB/message_in_a_bottle_backend.git
  1. Create and Invoke your virtual environment:
python3 -m virtualenv venv
source venv/bin/activate
  1. Install dependencies:
(venv) python -m pip install -r requirements.txt
  1. Set up the database:
$ psql

CREATE DATABASE <project name>;
CREATE USER <user name> WITH PASSWORD <password>;
ALTER ROLE <user name> SET client_encoding TO 'utf8';
ALTER ROLE <user name> SET default_transaction_isolation TO 'read committed';
ALTER ROLE <user name> SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE <project name> TO <user name>;
\q
  1. Add PostgreSQL database info to settings.py file:
DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.postgresql_psycopg2',
      'NAME': '
   
    '
   ,
      'USER': '
   
    '
   ,
      'PASSWORD': '
   
    '
   ,
      'HOST': '
   
    '
   ,
      'PORT': '
   
    '
   ,
  }
}
  1. Migrate database:
(venv) python manage.py makemigrations
(venv) python manage.py migrate
  1. Update CORS allowed origins in settings.py. Domains currently allowed are:
CORS_ALLOWED_ORIGINS = [
  'http://localhost:3000',
  'https://message-in-a-bottle-fe-app.herokuapp.com',
  'https://app-message-in-a-bottle.herokuapp.com',
]
  1. Run the test suite with:
(venv) export DJANGO_SETTINGS_MODULE=message_in_a_bottle.settings
(venv) pytest --cov
  1. Run your local Python server with:
(venv) python manage.py runserver

How To Use

To experience the front-end UI, please visit the application here. You can also hit our endpoints through an API client, such as Postman or HTTPie.

Endpoint Documentation

Domain: https://message-in-a-bottle-api.herokuapp.com

Stories Index Endpoint
The GET stories endpoint retrieves stories near you. You must supply valid longitude and latitude coordinates.

Query Params Required? Example Notes
latitude Yes /api/v1/stories?latitude=12.345&longitude=4.5968 requires longitude
longitude Yes /api/v1/stories?latitude=12.345&longitude=4.5968 requires latitude

Request: GET /api/v1/stories?latitude=12.3456&longitude=4.5968

Response:

{
  "data": {
    "input_location": "phoenix,az",
    "stories":[
      {
      "id": 1,
      "type": "story",
      "attributes": {
        "title": "my cool story",
        "latitude": 13.201,
        "longitude": 9.2673,
        "distance_in_miles": 1.2,
        "created_at": "2021-10-27T03:45:34.165600Z",
        "updated_at": "2021-10-27T03:45:36.855162Z"
        }
      },
      {
      "id": 2,
      "type": "story",
      "attributes": {
        "title": "story",
        "latitude": 13.563,
        "longitude": 10.2673,
        "distance_in_miles": 3,
        "created_at": "2021-10-27T04:45:34.165600Z",
        "updated_at": "2021-10-27T04:45:36.855162Z"
        }
      }
    ]
  }
}

Story Show Endpoint
Request: GET /api/v1/stories/:id?latitude=12.3456&longitude=-4.5968

Response:

{
  "data": {
    "id": 1,
    "type": "story",
    "attributes": {
      "title": "my cool story",
      "message": "This one time I saw a bird",
      "name": "Anonymous",
      "created_at": "2021-10-08T23:28:51.897746Z",
      "updated_at": "2021-10-08T23:28:51.897746Z",
      "latitude": 30.071945143440377,,
      "longitude": 31.225164325479227,
      "distance_in_miles": 1.2,
      "location": "Cairo Governorate, EG"
      }
   }
}

Directions Endpoint
Request: GET /api/v1/stories/:id/directions?latitude=1230&longitude=1209.3

Response:

{
  "data": [
    {
      "id": null,
      "type": "directions",
      "attributes": {
        "narrative": "Turn left on Bob St.",
        "distance": ".8 miles"
      }
    },
    {
      "id": null,
      "type": "directions",
      "attributes": {
        "narrative": "Turn right on Starry Road",
        "distance": ".2 miles"
      }
    }
  ]
}

Create Story Endpoint
Request: POST /api/v1/stories

Request Body:

{
  "title": "A new title",
  "message": "I'm coming up",
  "latitude": 27.717311514603534,
  "longitude": 85.32098499247293
}

Response:

{
  "data": {
    "id": 2,
    "type": "story",
    "attributes": {
      "title": "A new title",
      "message": "I'm coming up",
      "name": "Anonymous",
      "created_at": "2021-10-08T23:28:51.897746Z",
      "updated_at": "2021-10-08T23:28:51.897746Z",
      "latitude": 27.717311514603534,
      "longitude": 85.32098499247293,
      "location": "Kathmandu, NP"
    }
  }
}

Update Story Endpoint
Request: PATCH /api/v1/stories/:id

Request Body:

{
  "title": "My Fixed Title",
  "message": "Hello World.",
  "name": "Sally"
}

Response:

{
  "data": {
    "id": 1,
    "type": "story",
    "attributes": {
      "title": "My Fixed Title",
      "message": "Hello World.",
      "name": "Sally",
      "created_at": "2021-10-08T23:28:51.897746Z",
      "updated_at": "2021-10-18T23:28:51.897746Z",
      "latitude": 40.3830,
      "longitude": 105.5190,
      "location": "Estes Park, CO"
    }
  }
}

Delete Story Endpoint
Request: DELETE /api/v1/stories/:id

Error Handling
Here are some examples of error messages you could receive if you send an invalid request:

Bad Request URI: GET /api/v1/stories/:id or /api/v1/stories/:id?latitude=&longitude=

Response:

{
    "errors": {
        "messages": [
            "Latitude or longitude can't be blank."
        ],
        "code": 1
    }
}

Bad Request URI: GET /api/v1/stories/:id?latitude=1000&longitude=1000

Response:

{
    "errors": {
        "messages": [
            "Invalid latitude or longitude."
        ],
        "code": 1
    }
}

Bad Request URI: GET /api/v1/stories/:id?latitude=0&longitude=0

Response:

{
    "errors": {
        "messages": [
            "Impossible route."
        ],
        "code": 2
    }
}

POST /api/v1/stories Bad Request Body:

{
    "title":"Here's Johnny!",
    "message": "allworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboyallworkandnoplaymakesjackadullboy",
    "name":"Jack Torrance",
    "latitude":40.3830,
    "longitude":105.5190
}

Response:

{
    "errors": {
        "message": [
            "Ensure this field has no more than 1000 characters."
        ],
        "location": [
            "This field may not be blank."
        ]
    }
}

Database Schema

Screen Shot 2021-10-27 at 17 33 14

Contributing

👤 Marla Schulz

👤 Taylor Varoglu

👤 Matt Kragen

👤 Mae Duphorne

👤 Fara Akhatova

👤 Justin Anthony

Acknowledgements

A quick experiment to demonstrate Metamath formula parsing, where the grammar is embedded in a few additional 'syntax axioms'.

Warning: Hacked-up code ahead. (But it seems to work...) What it does This demonstrates an idea which I posted about several times on the Metamath mai

Marnix Klooster 1 Oct 21, 2021
A novel dual model approach for categorization of unbalanced skin lesion image classes (Presented technical paper 📃)

A novel dual model approach for categorization of unbalanced skin lesion image classes (Presented technical paper 📃)

1 Jan 19, 2022
Python library for parsing Godot scene files

Godot Parser This is a python library for parsing Godot scene (.tscn) and resource (.tres) files. It's intended to make it easier to automate certain

Steven Arcangeli 30 Jan 04, 2023
firefox session recovery

firefox session recovery

Ahmad Sadraei 5 Nov 29, 2022
🌈Python cheatsheet for all standard libraries(Continuously Updated)

Python Standard Libraries Cheatsheet Depend on Python v3.9.8 All code snippets have been tested to ensure they work properly. Fork me on GitHub. 中文 En

nick 12 Dec 27, 2022
Predicting Global Crop Yield for World Hunger

Crop Yield And Global Famine - The fifth project I created during my time at General Assembly. I completed this project with three other classmates in the span of three weeks. Most of my work was dir

Adam Muhammad Klesc 2 Jun 19, 2022
PyPI package for scaffolding out code for decision tree models that can learn to find relationships between the attributes of an object.

Decision Tree Writer This package allows you to train a binary classification decision tree on a list of labeled dictionaries or class instances, and

2 Apr 23, 2022
The docker-based Open edX distribution designed for peace of mind

Tutor: the docker-based Open edX distribution designed for peace of mind Tutor is a docker-based Open edX distribution, both for production and local

Overhang.IO 696 Dec 31, 2022
Stocks Trading News Alert Using Python

Stocks-Trading-News-Alert-Using-Python Ever Thought of Buying Shares of your Dream Company, When their stock price got down? But It is not possible to

Ayush Verma 3 Jul 29, 2022
Nmap script to detect a Microsoft Exchange instance version with OWA enabled.

Nmap script to detect a Microsoft Exchange instance version with OWA enabled.

Luciano Righetti 27 Nov 17, 2022
Python AVL Protocols Server for Codec 8 and Codec 8 Extended Protocols

pycodecs Package provides python AVL Protocols Server for Codec 8 and Codec 8 Extended Protocols This package will parse the AVL Data and log it in hu

Vardharajulu K N 2 Jun 21, 2022
Sathal's Python Projects Repository

Sathal's Python Projects Repository Purpose and Motivation I come from a mainly C Programming Language background and have previous classroom experien

Sam 1 Oct 20, 2021
Python program to start your zoom meetings

zoomstarter Python programm to start your zoom meetings More about Initially this was a bash script for starting zoom meetings, but as i started devel

Viktor Cvetanovic 2 Nov 24, 2021
Ballistic calculator for Airsoft

Ballistic-calculator-for-Airsoft 用于Airsoft的弹道计算器 This is a ballistic calculator for airsoft gun. To calculate your airsoft gun's ballistic, you should

3 Jan 20, 2022
A Python wrapper around Bacting

pybacting Python wrapper around bacting. Usage Based on the example from the bacting page, you can do: from pybacting import cdk print(cdk.fromSMILES

Charles Tapley Hoyt 5 Jan 03, 2022
This library is an ongoing effort towards bringing the data exchanging ability between Java/Scala and Python

PyJava This library is an ongoing effort towards bringing the data exchanging ability between Java/Scala and Python

Byzer 6 Oct 17, 2022
Interactivity Lab: Household Pulse Explorable

Interactivity Lab: Household Pulse Explorable Goal: Build an interactive application that incorporates fundamental Streamlit components to offer a cur

1 Feb 10, 2022
This is sample project needed for security course to connect web service to database

secufaku This is sample project needed for security course to "connect web service to database". Why it suits alignment purpose It connects to postgre

Mark Nicholson 6 May 15, 2022
Supercharge your NFTs with new behaviours and superpowers!

WrapX Supercharge your NFTs with new behaviours and superpowers! WrapX is a collection of Wrappers (currently one - WrapXSet) to decorate your NTFs ad

Emiliano Bonassi 9 Jun 13, 2022
BridgeWalk is a partially-observed reinforcement learning environment with dynamics of varying stochasticity.

BridgeWalk is a partially-observed reinforcement learning environment with dynamics of varying stochasticity. The player needs to walk along a bridge to reach a goal location. When the player walks o

Danijar Hafner 6 Jun 13, 2022