Code-Poets / praktyki-2018

1 stars 0 forks source link

Prepare yourself #1

Open TheCM opened 6 years ago

TheCM commented 6 years ago

Overview

Everyone should install Django and django rest framework. One person (@MartynaAnnaGottschling) runs git init and then push all changes to repository. The rest of you (@Karrp @maciejSamerdak ) run only git clone https://github.com/Code-Poets/praktyki-2018 when Martyna will finish all these points. Steps 2, 7, 8 are also required for all.

Implementation

Every single step should be in the separate commit.

  1. Install latest Django using pip.
    pip install django
  2. Create new virtualenv based on Python 3.6
  3. Add .gitignore:
    
    # Byte-compiled / optimized / DLL files
    __pycache__/
    *.py[cod]
    *$py.class

C extensions

*.so

Distribution / packaging

.Python env/ build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ .egg-info/ .installed.cfg .egg

PyInstaller

Usually these files are written by a python script from a template

before PyInstaller builds the exe, so as to inject date/other infos into it.

.manifest .spec

Installer logs

pip-log.txt pip-delete-this-directory.txt

Unit test / coverage reports

htmlcov/ .tox/ .coverage .coverage. .cache nosetests.xml coverage.xml ,cover .hypothesis/

Translations

.mo .pot

Django stuff:

*.log local_settings.py

Flask stuff:

instance/ .webassets-cache

Scrapy stuff:

.scrapy

Sphinx documentation

docs/_build/

PyBuilder

target/

IPython Notebook

.ipynb_checkpoints

pyenv

.python-version

celery beat schedule file

celerybeat-schedule

dotenv

.env

virtualenv

venv/ ENV/

Spyder project settings

.spyderproject

Rope project settings

.ropeproject

Vim's temporary files

*.sw[ponm]

Local vim settings sourced by the localvimrc plugin

/.lvimrc

Temporary files created by Windows and OS X

.DS_Store Thumbs.db Desktop.ini

Binary and automatically generated files are not well suited for versioning.

They should be stored somewhere else and maybe only included in release packges if needed.

If you're trying to commit one of them you're probably doing something wrong.

.exe .dll .msi .zip .rar .tar.gz .tar.bz2 .tar.xz .ttf .otf .pdf .doc *.xls

Binary database storage

*.sqlite3


4. Add `requirements.txt` and `requirements.in` files.
Use `pip freeze > requirements.txt` command to add new requirement from your virtualenv.
Copy these requirements to `requirements.in` without versions.
Do it any time you have to add new requirement to application like `Django`, `pytz` or `djangorestframework`.

5. Create new Django project using command `django-admin startproject <your-project-name>`
6. Install Django Rest Framework and add it to requirements and settings.
7. Install postgresql on you computer. For Ubuntu use these commands:
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

There is one thing to do.
Edit with root privileges `/etc/postgresql/<version>/main/pg_hba.conf`.
Change this line:

local all postgres


Set `<method>` to `trust` and restart postgres deamon (for Ubuntu `sudo service postgresql restart`).
Default method ca be `peer` or maybe `md5`.
We have to change that because we want Django to have rights to our database using admin user through local socket.
For more information about `pg_hba.conf` file: https://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html

And remember - to control postgresql service use:

sudo service postgresql start/restart/stop


8. Create new database:

createdb -U postgres


If you want to drob database for some reason (ie. recreate it) use:

dropdb -U postgres


9. Set up database in Django default settings:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'training_app', 'ATOMIC_REQUESTS': True, } }

Setting `ATOMIC_REQUESTS` to `True` gives us assurance that every transaction in database will be atomic. For more informations about Django transactions: https://docs.djangoproject.com/en/2.0/topics/db/transactions/.

10. Split django settings like this:

training_app/ settings/ init.py defaults.py development.py production.py local_settings.py


Copy all your settings to `defaults.py` and remove old `settings.py`.
Then copy these lines to adequate file:

 - development

pylint: disable=unused-wildcard-import

from .defaults import * # NOQA # pylint: disable=wildcard-import

ENVIRONMENT = 'development'

DEBUG = True

SECRET_KEY = `12345abcdef' # Can be any string

DATABASES['default']['USER'] = 'postgres' DATABASES['default']['PASSWORD'] = '' DATABASES['default']['HOST'] = '' DATABASES['default']['PORT'] = ''


 - production:

pylint: disable=unused-wildcard-import

from .defaults import * # NOQA # pylint: disable=wildcard-import

ENVIRONMENT = 'production'

DEBUG = False


 - local_settings:

from .development import *


 - __init__.py:

try:

local_settings.py is required by the application to work but is not provided by default

# to minimize the risk of someone accidentally running a production site with development settings.
# You need to choose one of the templates (production or development) and rename it to local_settings.py
from .local_settings import *  # NOQA  # pylint: disable=wildcard-import

except ImportError as exception:

This condition is not foolproof but should reduce the number of cases where we catch unrelated ImportErrors

if 'local_settings' in str(exception):
    raise ImportError("Failed to import 'local_settings' module. Use a production or development template to create one.")
else:
    raise

assert 'ENVIRONMENT' in vars(), 'A valid local_settings module should set up ENVIRONMENT setting'



As you can see default settings are always imported. Here we have basic settings and comments about settings that have to be specified in `development.py`, `production.py` or `local_settings.py`.
Development settings are used when we want to test application. They are imported in `local_settings.py`. This file is actually used for settings and has to be created although it will not be added to git commits due to `.gitignore` file.
For production we simply use `production.py` settings.

It is all about our basic settings.
TheCM commented 6 years ago

Done in 498f671c2edabc605076bb83f4129581e0ad4677