developing software as a service app with python & django
TRANSCRIPT
Developing Software As A Service with Python & DjangoAllan Spartacus Mangune, CPA, MsCIS
Friday, January 18, 13
Allan Spartacus ManguneCertified Public Accountant
MSCIS - University of Phoenix
Certified Scrum Master
Certifed Ethical Hacker
Microsoft MVP for ASP.NET
6 years in public accounting
13 years in software development and consulting
Friday, January 18, 13
Taughtworks, Inc.
Microsoft Certified Partner for Learning Solutions
2/F Liberty Plaza Building, H. V. Dela Costa Street, Makati City
Friday, January 18, 13
Our New Office
5/F 6784 Ayala Avenue corner V.A. Rufino Street, Makati City
Friday, January 18, 13
AgendaSoftware As A Service For Small Businesses
Procurement and Inventory Management
Sales Order and Invoicing Management
Customer Relationship Management
Technologies
Python, Django, Memcache, & Postgresql
Deployment and Maintenance
Friday, January 18, 13
Primogul
Procurement Inventory Sales Order Sales Invoice Customer Relation
050100150200
2007 2008 2009 2010
Cost Revenue
Reports and Analytics Engines
Friday, January 18, 13
Procurement
Maintain information about vendors
Use purchase orders to order inventory items
Manage receipts of goods
Manage billings from vendors
Friday, January 18, 13
Inventory
Manage inventory information, location and movements
Physical inventory count
Inventory worksheets
Inventory adjustments
Inventory valuation
Friday, January 18, 13
Sales Order
Create quotations for customers
Convert quotations to sales invoice or order
Create sales orders
Allocate inventory items to sales orders
Friday, January 18, 13
Software As A Service
Software on-demand
Multi-tenant environment
Pricing is based on number of seats and usage parameters
Friday, January 18, 13
Python
Powerful and easy to learn programming language
Requires an interpreter to run
Python interpreter and libraries can be downloaded from python.org
Runs on Mac OS X, Unix, and Windows
Friday, January 18, 13
Invoking the InterpreterAllan-Spartacus-Mangunes-MacBook-Pro:~ allan$ pythonPython 2.7.2 (default, Jun 20 2012, 16:23:33) [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwinType "help", "copyright", "credits" or "license" for more information.>>>
Friday, January 18, 13
Variables
The equal (‘=’) sign is used to assign a value
length = 40
A value can be assigned to several variables
a = b = c = d = 6
A variable must be assigned with a value prior to using it
Friday, January 18, 13
ListsExpressed a list of values separated by a comma
Placed between square brackets [ ]
Can be of different types
x = [‘hello’, ‘world’, 101, 88]
Indices begins at 0
x[0] equals ‘hello’
x[3] equals 88
Friday, January 18, 13
while Statement
while loop executes while the condition is true
Example
while {condition}: {run the code} {have it indented}
>>> x, y = 0, 1>>> while y < 12:... print y... x, y = y, x+y... 112358
Friday, January 18, 13
if Statement
If the condition is true, the code block is executed
Example
if {condition}: {run the code} {have it indented}
>>> a = int(raw_input('Please enter a number: '))Please enter a number: 24>>> if a < 0:... print "You entered a number lower than zero"... else:... print "You entered a number greater than zero"... You entered a number greater than zero
>>> a = int(raw_input('Please enter a number: '))Please enter a number: 0>>> if a < 0:... print "You entered a number lower than zero"... elif a == 0:... print "You entered a number zero"... else:... print "You entered a number greater than zero"... You entered a number zero
Friday, January 18, 13
for Statement
Iterates over the items
Example
for {i in list}: {run the code} {have it indented}
>>> list = ['dog', 'cat', 'mouse']>>> for i in list:... print i, len(i)... dog 3cat 3mouse 5
Friday, January 18, 13
Functions
Define
Example
def foo(arguments): {run the code} {have it indented}
>>> def foo(num):... x, y = 0, 1... while y < num:... print y... x, y = y, x+y... >>> foo(20)11235Friday, January 18, 13
ClassesBlueprint of an object
Provide the fundamental features of Object-Oriented Programming
Can inherit from multiple base classes
An instance can be modified during runtime
Data members and member methods are public.
Member methods are virtual
Conventions tells us that member that begins with an underscore ‘_’ shall be treated as private
Friday, January 18, 13
Class Definition/InstanceSimplest form:
Statements inside a class are usually method definitions
class Foo:<statement_1>...<statement_2>
class Foo: def bar(num): x, y = 0, 1 while y < num: print y x, y = y, x+y
Friday, January 18, 13
Django
“Web framework for for perfectionists with deadlines” - djangoproject.com
High-level Python Web framework
Friday, January 18, 13
Django Framework
Object-relational mapper - Define data models entirely in Python
Automatic admin interface - Production-ready user interface to add/update contents
Elegant URL design - cruft-free URL
Template system - Extensible template language
Cache system - Hooked into memcache
Internalization - Multi-language support Source: djangoproject.com
Friday, January 18, 13
Sites that Use Django
Disqus
Mozilla
OpenStack
PolitiFact.com
RdioSource: djangoproject.com
Friday, January 18, 13
Installing DjangoRequires Python
python.org
Usually installed on Mac OS X, Other Unix system
You may need to set up your database engine like Postgresql, Mysql or Oracle
Install a version for specific operating system
Get the official release
Latest version is 1.4.3
python > import djangoFriday, January 18, 13
Creating Django app
Auto-generate the code to establish the project
Create a project directory
Project structure
django-admin.py startproject blog
demo/manage.pyblog/
__init__.pysettings.pyurls.pywsgi.py
Friday, January 18, 13
Django Development Server
Lightweight web server written in Python
You can use this during development
Don’t use this server in the production
Allan-Spartacus-Mangunes-MacBook-Pro:blog allan$ python manage.py runserverValidating models...
0 errors foundDjango version 1.4.1, using settings 'blog.settings'Development server is running at http://127.0.0.1:8000/
Friday, January 18, 13
Setting up DatabaseEdit the blog/settings.py
Python module that contains Django settings
Change the keys in the DATABASESDATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': '/Users/allan/demo/blog/blog.db', # 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.
Friday, January 18, 13
INSTALLED_APPS
Holds the names of activated applications
Each app holds one database table
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles',)
Allan-Spartacus-Mangunes-MacBook-Pro:blog allan$ python manage.py syncdbCreating tables ...Creating table auth_permissionCreating table auth_group_permissionsCreating table auth_groupCreating table auth_user_user_permissionsCreating table auth_user_groupsCreating table auth_userCreating table django_content_typeCreating table django_sessionCreating table django_site
Friday, January 18, 13
ModelsProject vs App
An App is a web application that contains functionalities
A Project is a collection of Apps
To create an App
The entries/ directory holds the blog entries application
Allan-Spartacus-Mangunes-MacBook-Pro:blog allan$ python manage.py startapp entriesAllan-Spartacus-Mangunes-MacBook-Pro:blog allan$ ls entries__init__.py! models.py! tests.py!views.py
Friday, January 18, 13
Models
Define models
database layouts with metadata
Edit entries/models.py
The model tells Django to create the db schema and database-access API
from django.db import models
class Entry(models.Model): text = models.CharField(max_length=2000) pub_date = models.DateTimeField('date published')
Friday, January 18, 13
Model activation
Edit the settings.py file and add ‘entries’
Create the tables
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'entries' )
Allan-Spartacus-Mangunes-MacBook-Pro:blog allan$ python manage.py sql entriesBEGIN;CREATE TABLE "entries_entry" ( "id" integer NOT NULL PRIMARY KEY, "text" varchar(2000) NOT NULL, "pub_date" datetime NOT NULL);COMMIT;
Allan-Spartacus-Mangunes-MacBook-Pro:blog allan$ python manage.py syncdbCreating tables ...Creating table entries_entryInstalling custom SQL ...Installing indexes ...Installed 0 object(s) from 0 fixture(s)Allan-Spartacus-Mangunes-MacBook-Pro:blog allan$
Friday, January 18, 13
Activating Admin SiteNot activated by default
To activate:
Uncomment ‘django.contrib.admin’ in the INSTALLED_APPS
Edit the blog/urls.py
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles',
'django.contrib.admin',)
Allan-Spartacus-Mangunes-MacBook-Pro:blog allan$ python manage.py syncdbCreating tables ...Creating table django_admin_logInstalling custom SQL ...Installing indexes ...Installed 0 object(s) from 0 fixture(s)
# Uncomment the next two lines to enable the admin:from django.contrib import adminadmin.autodiscover()
Allan-Spartacus-Mangunes-MacBook-Pro:blog allan$ python manage.py runserverValidating models...
0 errors found
Friday, January 18, 13
Setting up Views
Represented by a Python function
Requires you to set up the URL in the URLconf module
ROOT_URLCONF setting
Edit blog/urls.py
ROOT_URLCONF = 'blog.urls'
urlpatterns = patterns('', url(r'^entries/$', 'entries.views.index'), # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)),)
Friday, January 18, 13
Setting up Views
Write a view function on entries/views.py
Create a template file
from django.template import Context, loaderfrom entries.models import Entryfrom django.http import HttpResponse
def index(request): latest_entry_list = Entry.objects.all().order_by('-pub_date')[:5] t = loader.get_template('entries/index.html') c = Context({ 'latest_entry_list': latest_entry_list }) return HttpResponse(t.render(c))
TEMPLATE_DIRS = ( '/Users/allan/demo/blog/templates',)
{% if latest_entry_list %} <ul> {% for entry in latest_entry_list %} <li><a href="/entries/{{entry.id}}">{{entry.text}}</a></li> {% endfor %} </ul>{% endif %}
Friday, January 18, 13
Allan Spartacus [email protected]/mangune
Friday, January 18, 13