- Try to always test every module. Use coverage.
- Comment your code. Pycco will be used to create documentation.
- Use PEP 8 to maintain good coding style.
- Create a new branch.
- Push the branch with your changes to the remote.
- Create a new pull request for your code to be reviewed by at least one other collaborator.
This short tutorial assumes you have already installed Python, virtualenv, Django etc.
It focuses primarily on installing and configuring PostgreSQL, which will be used in the final product.
Note that the steps related to installing PostgreSQL can actually be considered optional as SQLite is also suitable for development.
First, install and start PostgreSQL:
sudo apt-get install postgresql-9.4
sudo service postgresql-9.4 initdb
sudo service postgresql-9.4 start
Creata a local database:
sudo su - postgres
createdb cooknomics
createuser -P username
Find the file pg_hba.conf (path may vary) and modify it:
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
Install libpython3.5-dev:
sudo apt-get install libpython3.5-dev
If you use PyCharm, go to File -> Settings -> Project: Cooknomics -> Project Interpreter. Otherwise use pip. Install the following packages:
psycopg2
autoslug
django-tinymce
coverage
requests
unidecode
Pycco
django-disqus
easy_thumbnails
Pillow
lxml
If you encounter some errors while installing lxml, try the following command:
sudo apt-get install python-dev libxml2-dev libxslt1-dev zlib1g-dev
Clone the repo:
git clone https://github.com/mihal277/Cooknomics.git
Create a standard settings.py file and change the database settings into:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'cooknomics',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
Also, let Django know about any apps used in the project. For now it's:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'videos',
'news',
'tinymce',
'coverage',
'easy_thumbnails'
]
Inform Django about your static_files folder. Replace '?' with path to your project folder.
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static_files")
]
Unless you've made changes in models you don't have to create new migration files. If you did, however, type:
python manage.py makemigrations
Finish by telling Django to create or modify tables in your PostreSQL database:
python manage.py migrate
Use coverage to determine what parts of your code are still not tested:
coverage run manage.py test -v 2
coverage html
Create or modify a specific file in app/tests folder. If you've created a file, modify __init.py__ accordingly.
First, add this code to your setting.py file in order to use SQLite for testing, which is much faster:
import sys
if 'test' in sys.argv:
DATABASES = {
'default': {'ENGINE': 'django.db.backends.sqlite3'}
}
PASSWORD_HASHERS = (
'django.contrib.auth.hashers.MD5PasswordHasher',
'django.contrib.auth.hashers.SHA1PasswordHasher',
)
Now you can run coverage inside your virtualenv:
coverage run manage.py test appname -v 2
There is a smiple database fixture included in Cooknomics/fixtures directory.
In PyCharm press alt+r (ctrl+alt+r if you're not on OSX) and type the following into your manage.py console:
(manage.py) loaddata fixtures/fixture.json
(manage.py) dumpdata --natural-foreign --natural-primary --format=json -o fixtures/fixture_name.json
Use:
pyccoo appname/*.py -p