Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

Prepare yourself #1

Open
TheCM opened this issue Jul 2, 2018 · 1 comment
Open

Prepare yourself #1

TheCM opened this issue Jul 2, 2018 · 1 comment
Assignees
Labels
good first issue Good for newcomers

Comments

@TheCM
Copy link
Contributor

TheCM commented Jul 2, 2018

Overview

  • Init/clone git repository
  • Create new folder in your projects in sandbox
  • Create new .virtualenv on Python 3.6
  • Add our standard .gitignore file
  • Create new Django project
  • Add requirements.txt and requirements.in files
  • Install Django Rest Framework and add it to project
  • Install and set database to postgresql, add psycopg2 dependency
  • Split Django settings

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
  1. Create new virtualenv based on Python 3.6
  2. 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
  1. 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.

  2. Create new Django project using command django-admin startproject <your-project-name>

  3. Install Django Rest Framework and add it to requirements and settings.

  4. 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                                <method>

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
  1. Create new database:
createdb -U postgres <your_app_name>

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

dropdb -U postgres <your_app_name>
  1. 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/.

  1. 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
Copy link
Contributor Author

TheCM commented Jul 9, 2018

Done in 498f671

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants