Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rukbotto/reviews-django

A simple REST API for submitting and retrieving user reviews, powered by Django.
https://github.com/rukbotto/reviews-django

Last synced: 15 days ago
JSON representation

A simple REST API for submitting and retrieving user reviews, powered by Django.

Awesome Lists containing this project

README

        

# Reviews Django

[![Build Status](https://travis-ci.org/rukbotto/reviews-django.svg?branch=master)](https://travis-ci.org/rukbotto/reviews-django)

A simple REST API for submitting and retrieving user reviews powered by *Django*. It uses [*Django REST Framework*](http://www.django-rest-framework.org).

## Installation

Install *PostgreSQL* and *Git*:

```
# On MacOS
$ brew install postgresql git

# On Debian GNU/Linux
$ sudo apt install postgresql git
```

Install `virtualenv` or `virtualenvwrapper`:

```
# Install virtualenv
$ sudo pip install virtualenv

# Or virtualenvwrapper
$ sudo pip install virtualenvwrapper
```

Clone this repository:

```
$ git clone https://github.com/rukbotto/reviews-django ~/reviews-django
```

Create a virtualenv for installing all dependencies:

```
# Using virtualenv
$ virtualenv ~/.virtualenvs/reviews-django --python=python3

# Or using virtualenvwrapper
$ mkvirtualenv reviews-django --python=python3
```

Activate the newly created virtualenv:

```
# Using virtualenv
$ source ~/.virtualenvs/reviews-django/bin/activate

# Or using virtualenvwrapper
$ workon reviews-django
```

Install all dependencies:

```
(reviews-django) $ cd ~/reviews-django
(reviews-django) $ pip install -r requirements.txt
```

## Database configuration

Create a new PostgreSQL role:

```
(reviews-django) $ psql postgres
postgres=# CREATE ROLE reviews_django WITH LOGIN PASSWORD 'strong_password_here';
postgres=# ALTER ROLE reviews_django CREATEDB;
postgres=# \q
```

Then create the database using the recently created user:

```
(reviews-django) $ psql postgres -U reviews_django
postgres=> CREATE DATABASE reviews_django;
```

Finally grant permissions:

```
postgres=> GRANT ALL PRIVILEGES ON DATABASE reviews_django TO reviews_django;
postgres=> \q
```

## Django configuration

Create a local configuration file:

```
(reviews-django) $ touch reviews_django/local_settings.py
```

And add the following lines of code in order to configure local development settings:

```python
SECRET_KEY='secret_key_here'

DEBUG = True

ALLOWED_HOSTS = []

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'reviews_django',
'USER': 'reviews_django',
'PASSWORD': 'strong_password_here',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
```

The secret key can be generated by *Django* using the interactive *Python* shell:

```
(reviews-django) $ python
Python 3.7.0 (default, Jun 29 2018, 20:13:13)
>>> from django.core.management.utils import get_random_secret_key
>>> get_random_secret_key()
>>> '3z^w%y!...'
```

Copy the generated secret key and paste it into the `SECRET_KEY` setting in the local configuration file.

## Usage

Run database migrations:

```
(reviews-django) $ python manage.py migrate
```

Run the local development server:

```
(reviews-django) $ python manage.py runserver
```

To make queries to the API, first create a superuser:

```
(reviews-django) $ python manage.py createsuperuser --username your_username
```

An email address and a password must be provided after running the command.

Then run the provided `test-api` script:

```
(reviews-django) $ script/test-api http://localhost:8000/api/reviews/ your_username:your_password --file data.json
```

The above command will make a POST request to the `/api/reviews/` endpoint as the user `your_username`, in order to create a new review. The review data must be provided in the file `data/data.json`.

The script output will be something like this:

```
201
{"id":1,"user":"john","title":"My review","summary":"This is my first review.","rating":1,"company":"Some Company","reviewer":"Some Reviewer","created_at":"2018-08-30T16:30:10.223029Z"}
```

To retrieve all the reviews for a given user, make a GET request to the `/api/reviews/` endpoint:

```
(reviews-django) $ script/test-api http://localhost:8000/api/reviews/ your_username:your_password
```

To retrieve a single review, make a GET request to the `/api/review/` endpoint:

```
(reviews-django) $ script/test-api http://localhost:8000/api/review/1/ your_username:your_password
```