Simplify Django organization, add South support.

This commit is contained in:
Michael DeHaan 2013-02-28 19:39:01 -05:00
parent 17bfe1b2e1
commit d444bd0873
62 changed files with 258 additions and 308 deletions

52
SETUP.md Normal file
View File

@ -0,0 +1,52 @@
SETUP
=====
This will be an ansible playbook soon!
For now these are instructions for CentOS 6.
Install ansible-commander
=========================
Before proceeding, be aware that this should be installed on it's own
machine (or virtualmachine) as it is going to lock down the database.
Ansible will install ansible-commander using a playbook, so you first
need to have ansible installed. Do this first and come back here.
You will also need the EPEL yum repository installed if using CentOS6.
First edit app_setup/vars/vars.yml to select a database password.
If you feel like you need to change it, the Django config file is also
templated. See app_setup/templates/setting.py.j2. Most people will
not need to change this.
run "make setup" to run the ansible setup playbook. It should run
without erros.
This playbook will:
* install Django and required software components
* install postgresql and configure authentication
* initialize the database
* initialize the first database table
* synchronize the database and run any migrations
You may run this setup step again as needed as many times
as you like.
Test the server
===============
make runserver
access the server on 127.0.0.1:8000
Running through Apache
======================
TODO.

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,23 +0,0 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

View File

@ -1 +0,0 @@
# Create your views here.

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,23 +0,0 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

View File

@ -1 +0,0 @@
# Create your views here.

View File

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,23 +0,0 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

View File

@ -1 +0,0 @@
# Create your views here.

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,23 +0,0 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

View File

@ -1 +0,0 @@
# Create your views here.

View File

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,23 +0,0 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

View File

@ -1 +0,0 @@
# Create your views here.

BIN
acom/main/__init__.pyc Normal file

Binary file not shown.

View File

@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'Inventory'
db.create_table('inventory', (
('name', self.gf('django.db.models.fields.TextField')()),
('description', self.gf('django.db.models.fields.TextField')()),
('creation_date', self.gf('django.db.models.fields.DateField')()),
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
))
db.send_create_signal('main', ['Inventory'])
def backwards(self, orm):
# Deleting model 'Inventory'
db.delete_table('inventory')
models = {
'main.inventory': {
'Meta': {'object_name': 'Inventory', 'db_table': "'inventory'"},
'creation_date': ('django.db.models.fields.DateField', [], {}),
'description': ('django.db.models.fields.TextField', [], {}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.TextField', [], {})
}
}
complete_apps = ['main']

Binary file not shown.

21
acom/main/models.py Normal file
View File

@ -0,0 +1,21 @@
from django.db import models
class CommonModel(models.Model):
''' common model for all object types that have these standard fields '''
class Meta:
abstract = True
name = models.TextField()
description = models.TextField()
creation_date = models.DateField()
db_table = 'inventories'
class Inventory(CommonModel):
class Meta:
db_table = 'inventory'
id = models.AutoField(primary_key=True)

BIN
acom/main/models.pyc Normal file

Binary file not shown.

View File

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,23 +0,0 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

View File

@ -1 +0,0 @@
# Create your views here.

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,23 +0,0 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

View File

@ -1 +0,0 @@
# Create your views here.

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,23 +0,0 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

View File

@ -1 +0,0 @@
# Create your views here.

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,23 +0,0 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

View File

@ -1 +0,0 @@
# Create your views here.

Binary file not shown.

View File

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,23 +0,0 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

View File

@ -1 +0,0 @@
# Create your views here.

View File

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,23 +0,0 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

View File

@ -1 +0,0 @@
# Create your views here.

66
app_setup/setup.yml Normal file
View File

@ -0,0 +1,66 @@
---
# tested only on CentOS6, may need adaptations for other operating systems
# TODO: install any ansible plugins we need
# TODO: set up Apache or Nginx to proxy this application
# TODO: setup celery and any worker processes/requirements
- hosts: 127.0.0.1
gather_facts: True
user: root
vars_files:
- vars/vars.yml
tasks:
- name: install packages
action: yum name=$item state=installed
with_items:
- postgresql # database client
- postgresql-server # database server
- Django # web stack
- Django-south # migration support
- python-django-tastypie # rest support
- python-psycopg2
# the following could probably be included, though ident identification is fiddly
# so we won't use it to create the database and database user
- name: configure the database authentication more or less open for setup
template: src=templates/pg_hba_low.j2 dest=/var/lib/pgsql/data/pg_hba.conf
- name: restart postgresql
service: name=postgresql state=restarted
- name: setup the postgresql user
postgresql_user: >
name=ansible_commander
db=ansible_commander
password=${database_password}
login_user=postgres
priv=ALL
sudo_user: postgres
- name: create the database
postgresql_db: name=acom state=present
- name: configure the database authentication for runtime mode
template: src=templates/pg_hba.j2 dest=/var/lib/pgsql/data/pg_hba.conf
- name: restart postgresql
service: name=postgresql state=restarted
- name: configure python settings (with database password)
template: src=templates/settings.py.j2 dest=${working_dir}/acom/settings.py
- name: sync Django database
shell: python ${working_dir}/acom/manage.py syncdb
- name: sync django database
django_manage: >
command=syncdb
app_path=${working_dir}/acom
#- name: run south migrations
# command: python ${working_dir}/acom/manage.py migrate db_migrate

View File

@ -0,0 +1,5 @@
[database]
password={{ database_password }}
[inventory]
secret=TheEnemysGateisDown

53
app_setup/templates/db.j2 Normal file
View File

@ -0,0 +1,53 @@
CREATE USER ansible_commander UNENCRYPTED PASSWORD '{{ database_password }}';
GRANT ALL on DATABASE ansible_commander TO ansible_commander;
GRANT ALL on DATABASE ansible_commander_test TO ansible_commander;
\c ansible_commander
CREATE TABLE thing
(
id BIGSERIAL PRIMARY KEY,
type TEXT
);
CREATE TABLE properties
(
id BIGSERIAL PRIMARY KEY,
thing_id BIGSERIAL NOT NULL REFERENCES thing(id) ON DELETE CASCADE,
key TEXT NOT NULL,
value TEXT
);
CREATE UNIQUE INDEX properties_thing_idx ON properties (thing_id, key);
CREATE INDEX properties_key_idx ON properties (key);
CREATE INDEX properties_value_idx ON properties (key, value);
GRANT ALL on TABLE thing TO ansible_commander;
GRANT ALL on TABLE properties TO ansible_commander;
GRANT ALL ON SEQUENCE thing_id_seq TO ansible_commander;
GRANT ALL ON SEQUENCE properties_id_seq TO ansible_commander;
\c ansible_commander_test
CREATE TABLE thing
(
id BIGSERIAL PRIMARY KEY,
type TEXT
);
CREATE TABLE properties
(
id BIGSERIAL PRIMARY KEY,
thing_id BIGSERIAL NOT NULL REFERENCES thing(id) ON DELETE CASCADE,
key TEXT NOT NULL,
value TEXT
);
CREATE UNIQUE INDEX properties_thing_idx ON properties (thing_id, key);
CREATE INDEX properties_key_idx ON properties (key);
CREATE INDEX properties_value_idx ON properties (key, value);
GRANT ALL on TABLE thing TO ansible_commander;
GRANT ALL on TABLE properties TO ansible_commander;
GRANT ALL ON SEQUENCE thing_id_seq TO ansible_commander;
GRANT ALL ON SEQUENCE properties_id_seq TO ansible_commander;

View File

@ -0,0 +1,7 @@
# Database administrative login by UNIX sockets
local all postgres ident
# TYPE DATABASE USER CIDR-ADDRESS METHOD
local all all md5
host all all 127.0.0.1/32 md5
host all all ::1/128 md5

View File

@ -0,0 +1,5 @@
# temporary security setup for initial config only
# TYPE DATABASE USER CIDR-ADDRESS METHOD
local all postgres trust

View File

@ -11,12 +11,12 @@ MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
'ENGINE': 'postgresql_psycopg2',
'NAME': 'acom',
'USER': 'ansible_commander',
'PASSWORD': '{{ database_password }}',
'HOST': '',
'PORT': '',
}
}
@ -27,7 +27,7 @@ DATABASES = {
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/NewYork'
TIME_ZONE = 'America/New_York'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
@ -89,8 +89,11 @@ INSTALLED_APPS = (
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'main',
'south',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
#'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)

2
app_setup/vars/vars.yml Normal file
View File

@ -0,0 +1,2 @@
---
database_password: gateIsDown