# README #

#Long version

Step by step to configure and run the ShopAid project


## **Enviroment configuration** ##

**Operative system:** 14.04 LTS


### **Database** ###

You need to install Postgresql 9.4.

**Create and update the Postgresql repository**

First, run command:

```
sudo vi /etc/apt/sources.list.d/pgdg.list

```

then, type the key 'i' on keyboard and add the next line:

```
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main
```

**Download and import repository's key**

```
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
```

**Update system**

```
sudo apt-get update && sudo apt-get upgrade
```

**Install Postgresql**

```
sudo apt-get install postgresql-9.4
```


## **Configure database ** ##

**Postgres user**

By default Postgresql creates the user ``` postgres ``` . Before starting is necesary to assign a password for the user, then run the next command:


```
sudo passwd postgres
```

Type and verify password. Now, for using the ``` postgres ``` user, run the next command:

```
su postgres
```

**Create the project's database**

After begin as ``` postgres ``` user, run the command to enable the Postgresql shell:

```
psql
```
Run the next command to create the database for the project:
```
CREATE DATABASE shop_aid;
```

**Django user**

Now, create one user to connect the ``` shop_aid ``` database with the Django project, to do it, run the next command:
```
CREATE USER django WITH PASSWORD 'django';
```
To give all privileges on ``` shop_aid ``` database for the ``` django ``` user, run the next command:
```
GRANT ALL PRIVILEGES ON DATABASE shop_aid TO django;
```
Additionally is necesary to define the ``` django ``` user like 'superuser'. Run the next command to do it:
```
ALTER USER django WITH SUPERUSER;
```

**Database dump**

The database is ready to load the structure and data from the dump file. This file could be provided by database administrator or project's leader and its location must be accesible.

As ``` postgres ``` user (run ``` /q ``` if you are into psql shell), run the next command:
``````
psql shop_aid < url/of/file/.sql
``````   
With it finish the database configuration and all is ready to configure the django project.


## ** Django project configuration ** ##


**Version control system Git**

The project is on bitbucket, then is necessary to install Git. To do it, run the next command:
```
sudo apt-get install git-core
```

** Package management system Pip **
``` pip ``` is the package management of Pyhton, used to install easily the Python's packages. For install it run the next command:
```
sudo apt-get -y install python-pip
```

** Configure virtual enviroment **

Install virtualenvwrapper:
```
sudo pip install virtualenvwrapper
```

Create project's directory:
```
mkdir /home/<user>/shop_aid
```

```
cd /home/<user>/shop_aid

```
Execute virtualenvwrapper scripts:

```
source /usr/local/bin/virtualenvwrapper.sh
```

Create virtual enviroment with Python 3.4:

```
mkvirtualenv shop_aid --python=/usr/bin/python3.4

```
Test it:


```
workon shop_aid
```


**Install project from repository

Within ``` shop_aid ``` virtual enviroment and ``` /home/<user>/shop_aid ``` directory, run the next commmands to get the project in your local host:

```
git init
```
```
git clone https://<user>@bitbucket.org/sofhouse/shop_aid_backend.git
```

**Install requirements**

Before of install the requierements for ``` shop_aid_backend ``` django project, is necessary get some dependencies. To do it, run te next commands:
```
sudo apt-get install python-psycopg2
```
```
sudo apt-get install libpq-dev
```
```
sudo apt-get install python3-dev
```
```
sudo apt-get install libjpeg-dev
```

Now all is ready to run the ``` requirements.txt ``` file, it contains the requirements for the project. Run the next command to do it:
```
pip install -r requirements.txt
```

** Local settings file **

Create the ``` local_settings.py ``` file in the ``` shop_aid_backend/shop_aid_backend ``` directory. This file should contain all local settings variables like ``` DATABASES = { ... } ```, ``` MEDIA_ROOT ```, ``` MEDIA_URL ```, ``` STATIC_ROOT ```, ``` STATIC_URL ```, etc.   

## ** Other settings ** ##

** Elasticsearch **

For using the indexation module of ``` shop_aid_backend ``` is necessary install Elasticsearch, it is a highly scalable open-source full-text search and analytics engine. The instructions for installing are at https://www.elastic.co/guide/en/elasticsearch/reference/current/_installation.html

#short Version
##tldr##

## Prerequisites

Make sure your system has these packages installed and
working properly as setup scripts requires them.

- [`postgres`](https://wiki.postgresql.org/wiki/Detailed_installation_guides) sql of choice
- `python3` main project lang
- `pip` package manager for python
- [`virtualenvwrapper`](http://virtualenvwrapper.readthedocs.io/en/latest/install.html) manages project's py dependencies
- additional packages on ubuntu: `python-psycopg2 libpq-dev python3-dev libjpeg-dev`
```bash
  sudo apt-get install python-psycopg2 libpq-dev python3-dev libjpeg-dev
```
- [elasticsearch](https://www.elastic.co/guide/en/elasticsearch/reference/current/_installation.html]elasticsearch)

## Setup Env mac

Make sure no instances of postgres are currently running and run setup script
```bash
cd shopaid-backend
pg_ctl stop
source setup-env.sh
```

## Load Data

Ask around for the latest database dump that works with your repository and do
```bash
psql shop_aid < path/to/dump.sql
```
## Run the project
`./start-env.sh ` or `source start-env.sh` to initialize virtual environment and start db services
`./manage.py` or `python3 manage.py` to run the project. you can also open up your IDE of choice at this point

### to view the REST API visit  
`<server>:8000/docs/`