Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
trincadev edited this page Jan 29, 2016 · 2 revisions

README

This is a simple GeoDjango project. The admin interface is ready to use. The source code is based on geodjango tutorial. Before to start:

  • be sure to know django and geodjango fundamentals.
  • This webapp is built with a PostgreSQL database and the PostGis extension; be sure to check the requirements.
  • This Django project is powered by OpenShift, using a modified version of this guide.
  • You could modify the SECRET_KEY variable from the file secrets.txt.
  • Before to deploy in production remember to check security issues: python manage.py check --deploy (see also the related docs).

Installing cloning my repo

Quick 'n' dirty (see here for the output from my debian box):

# bash
git clone [email protected]:aletrn/djangogeo.git "gistest"
cd gistest
mkvirtualenv testdjango18	# from now the bash commands are within the virtualenv
pip install -r requirements.txt 
python manage.py shell	# test

# psql 
postgres=# create database gistest;
postgres=# create user gistest SUPERUSER;
postgres=# \connect gistest 
gistest=# CREATE EXTENSION IF NOT EXISTS postgis;

# bash
python manage.py sqlmigrate world 0001 	# only a CONTROL
python manage.py migrate
python manage.py createsuperuser

python manage.py shell
>>> from world import load; load.run()

Finally, try it.

# bash
python manage.py runserver localhost:8000

Before pushing on another repository or eventually on openshift, remember to use a proper ~/.gitignore_global (or equivalent):

# ignoring
.Python
env
bin
lib
include
.DS_Store
*.

If you change the database name, remember to change also the dictionary entry in djangogeo/settings.py. After the app creation, remember to repeat again the configuration commands for PostgreSQL (db and ROLE creation, postgis extension, )

# bash
rhc app create -t python-2.7 -a testgis
rhc cartridge add -c postgresql-9.2 -a testgis
# psql
postgres=# create database gistest;
postgres=# create user gistest SUPERUSER;
postgres=# \connect gistest 
gistest=# CREATE EXTENSION IF NOT EXISTS postgis;
# bash
python manage.py sqlmigrate world 0001 	# only a CONTROL
python manage.py migrate
python manage.py createsuperuser

python manage.py shell
>>> from world import load; load.run()

In this case git push must be forced with the option -f because the remote destination lacks the local commits you are importing.

# bash
git remote set-url origin --push --add ssh://[email protected]/~/git/testgis.git/
# vim .git/config 
git log --oneline 
git add --all -n
git status 
git push -u origin master -v --dry-run -f
git push -u origin master -v -f

Troubleshooting

If your db user can't login, check its rights.

# psql 
postgres=# \du gistest 
                  List of roles
 Role name |       Attributes        | Member of 
-----------+-------------------------+-----------
 gistest   | Superuser, Cannot login | {}
postgres=# alter user gistest LOGIN;
ALTER ROLE
postgres=# \du gistest 
           List of roles
 Role name | Attributes | Member of 
-----------+------------+-----------
 gistest   | Superuser  | {}

Some tips to install from scratch

Some preliminary steps to configure git and ssh:

# bash
echo "#ignoring
.Python
env
bin
lib
include
.DS_Store
*.pyc" >> ~/.gitignore_global

ssh-add ~/.ssh-dir/id_rsa_PRIVATEKEY             # ssh-add ~/.ssh/id_rsa_custom; 
rhc sshkey add ~/.ssh-dir/id_rsa_PRIVATEKEY.pub  # rhc sshkey add ~/.ssh-dir/id_rsa_custom.pub

Reference folder: ~/workspace/PROGETTI_ARCHIVIATI/djangogeo_bitbucket_openshift/
To execute arbitrary commands with django shell on the rhc python app "appname":

# bash
rhc ssh appname "cd ~/app-deployments/current/repo/; echo 'import django; print django.get_version()' | python manage.py shell"

To create and start a new app on OPENSHIFT use simply git push (automatically this rebuild and deploy):

# bash
cd ~/workspace
rhc app-create -a djangogeo -t python-2.7
#ask to confirm for upload ssh key to rhcloud if not present; best to upload later

rhc cartridge add -c postgresql-9.2 -a djangogeo
cd ~/workspace/djangogeo
# customize requirements.txt:
echo -e "Django >= 1.8, < 1.9\npsycopg2" >> requirements.txt
# within `setup.py` set the variable: `install_requires=['Django>=1.8,<1.9'],`
rhc ssh djangogeo 'psql djangogeo -c "CREATE EXTENSION IF NOT EXISTS postgis; CREATE USER djangogeo SUPERUSER;"'
git remote set-url origin --push --add [email protected]:aletrn/djangogeo.git
git remote set-url origin --push --add ssh://[email protected]/~/git/djangogeo.git/
git remote -v show
git commit -m "initial commit (openshift integration)"

# REMOTE DEPLOY!
git push origin master

Configure Django project through eclipse. If missing, install/configure virtualenvwrapper (add libraries loaded from interpreter from eclipse options):

# bash
mkvirtualenv django18
workon django18
cd ~/workspace/djangogeo
pip install -r ./requirements.txt 

# if a command-line method is preferred:
django-admin startproject djangogeo .  # startproject into the current folder!

DJANGO TUTORIAL

Customize setting in ~/workspace/djangogeo/djangogeo/settings.py and start a local test. Follow the GeoDjango tutorial. If all is ok, prepare a remote deploy:

# bash
# ... DJANGO TUTORIAL ...
python manage.py runserver localhost:8888

git add --all -n  # git-add dry-run
git add --all 
git commit -m "test for Django start, with custom djangogeo/settings.py"

STACKOVERFLOW ANSWER

  • Add the action_hooks scripts for automating openshift operations (from this StackOverflow answer)
  • create folders for static and media files
# bash
touch wsgi/media/.gitkeep   wsgi/static/.gitkeep   djangogeo/static/.gitkeep
git commit -m "added action_hooks scripts, static folders: .openshift/action_hooks/build,deploy"

# (optional) remote deploy!
git push -u origin master

DJANGO TUTORIAL

  • Create and customize a new app world and copy the shapefile
  • modify djangogeo/settings.py to load the new app
  • modify djangogeo/urls.py to load the gis admin version
  • modify wsgi.py to load the new app
  • create the tables (makemigrations, migrate)
# bash
# ...customize the app before commit, then execute local makemigrations!
# ...DJANGO TUTORIAL...

# djangogeo/urls.py
from django.contrib.gis import admin

# djangogeo/settings.py
INSTALLED_APPS = ( # ...,

# wsgi.py # erase the entire local function `application`, put this:
from testdjango.wsgi import application
"""
def application(environ, start_response):

    ctype = 'text/plain'
    # ...
"""

### DJANGO TUTORIAL
python manage.py makemigrations
python manage.py sqlmigrate world 0001  # check if the producted SQL is ok
python manage.py migrate
git commit -m "startapp world: world/admin.py,models.py; executed makemigrations"

# execute this part both local and remote, interactive mode only
rhc ssh djangogeo;
    (rhc) cd ~/app-deployments/current/repo
    (rhc) python manage.py createsuperuser

# REMOTE DEPLOY!
git push -u origin master

geographic world border: use world/load.py to load the gis data into postgis.

# bash
echo "from world import load; load.run()" | python manage.py shell'
git commit -m "load nations borders in postgis: world/load.py, shp in world/data/" 
rhc ssh djangogeo 'cd ~/app-deployments/current/repo; echo "from world import load; load.run()" | python manage.py shell'