Skip to content

Setting up for local dev

Danang Rahmatullah edited this page Oct 31, 2020 · 28 revisions

1. Setting up the website

1.1 Using PostgreSQL

The website uses PostgreSQL as the database server.

Installing postgresql

Before proceeding, please install according to your respective operating system and distro.

Set up databases

Enter the postgresql shell and enter the following commands:

CREATE ROLE cosmos_website_tester WITH
	LOGIN
	NOSUPERUSER
	NOCREATEDB
	NOCREATEROLE
	INHERIT
	NOREPLICATION
	CONNECTION LIMIT -1
    PASSWORD '2020123';

CREATE DATABASE cosmos_website_test WITH 
    OWNER = postgres
    ENCODING = 'UTF8'
    CONNECTION LIMIT = -1;

GRANT ALL PRIVILEGES ON DATABASE cosmos_website_test TO cosmos_website_tester;

\q

1.2 Downloading the project

The project is stored in this git repository. To download either use a GUI application for git, which is highly recommended in general as git is difficult to use from the command line, or run the following command:

git clone https://github.com/bcvandendool/Cosmos.git

1.3 Setting up python

To install the python packages required for this project, without interfering with other projects packages, we use a python virtual environment. To create a python virtual environment, run the following command (on Windows, we recommend setting up the virtual environment in the same folder as your code:

python3 -m venv .venv

To activate the virtual environment run the following command, depending on you OS:

  • Unix/MacOS
source .venv/bin/activate
  • Windows
.venv\Scripts\activate.bat

If you want to deactivate the virtual environment for whatever reason, simply close the shell or run the follwing command:

deactivate

To install the required packages, make sure that the virtual environment is activated, and then first upgrade pip to the newest version, and then install all the required packages by running the following commands:

pip install --upgrade pip
pip install -r requirements.txt

1.4 Setting up npm-packages

Inside of the terminal, run

npm install

1.5 Installing the project

To populate the database with the correct structure, django uses migrations, to apply these migrations, run:

python manage.py migrate

In order to be able to modify the website, an admin user is required, to create one run the following commandL

python manage.py createsuperuser

2. Setting up the development tools

The recommended editor to use is visual studio code. Other editors will also work fine, however this guide will expect vscode.

2.1 Configuring IDE

vscode

First make sure that the python plugin for vscode is installed. Then open the plugin and make sure that the correct Python environment is selected. To change the currently selected python environment press Ctrl + Shift + P and enter 'Python: Select Interpreter' and then select the one that you just created in the virtual environment.

PyCharm

If you decide to work on PyCharm, be sure to exclude the folder .venv by right-clicking on it and selecting Mark Directory as -> Excluded. This will ensure the folder is excluded in project-wide searches and modifications.

2.2 Linting

In order to check the code for errors, we use flake8 as linter. To add additional capabilities to flake8, we install several plugins. Flake8-django gives django specific linting, pep8-naming gives warnings on non pep8 compliant function and variable names, and isort with flake8-isort makes sure that import statements are sorted in a consistent way. The configuration for flake8 and isort can be found in the tox.ini config file. To install flake8, isort and all plugins, run the following command:

pip install flake8 flake8-django pep8-naming flake8-isort

To integrate Flake8 into vscode select the linter by pressing Ctrl + Shift + P and enter 'Python: Select Linter' and select flake8, or set the following options in the .vscode/settings.json config file (on Windows, this file can be accessed via the VSCode settings):

"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.enabled": true

2.3 Formatting

For formatting we use black, which strictly enforces a pep8-compliant code style. The configuration for black can be found in the pyproject.toml config file. To install black run:

pip install black

To integrate black into vscode set the following option in the .vscode/settings.json config file (on Windows, this file can be accessed via the VSCode settings):

"python.formatting.provider": "black"

To format python files, open them, and then press Ctrl + Shift + I to automatically format them. To sort the import statement using isort press Ctrl + Shift + P and enter 'Python Refactor: Sort Imports'

2.4 Testing

For testing we use tox, which manages all the testing environments and runs all the tests. The configuration for tox can be found in the tox.ini config file. To install tox, run the following command:

pip install tox

To run all tests via tox simply run:

python -m tox

3. Setting up the git hooks

Pull requests can only be accepted if the linting, formatting and testing give no errors. In order to catch small mistakes before you create a commit, a pre-commit hook can be installed. This hook checks the linting and formatting and will give an error if they are not correct. To install this, first install pre-commit, and then install the hook itself by running the following commands:

pip install pre-commit
pre-commit install

4. Running the website locally

To run the website locally first you must start up the database server, and then start the django debug server. If you want to run the website on a server visit Setting up the server. To start the database server run:

sudo systemctl start postgresql.service

To then run the server, run the following command:

python manage.py runserver

If everything went right, the website should be running now at localhost:8000.

Clone this wiki locally