{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "##Atlatl Django Project | By Andrew Conti" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create my project and call it mysite:\n", " \n", " !django-admin.py startproject mysite" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cd /home/agconti/my_dev/github/Atlatl_django/mysite" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The line below clears all of the sqlite database so that I can run this notebook multiple times:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py sqlclear houses | python manage.py dbshell" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "class dev_server(object):\n", " '''\n", " for easy server management\n", " '''\n", " def __init__(self):\n", " self.server = 'no_server'\n", " \n", " def start(self):\n", " '''\n", " starts the server\n", " '''\n", " import os\n", " import subprocess\n", " \n", " #get cwd\n", " cdir = os.getcwd()\n", " \n", " # change dir to make sure manage.py is avaliable\n", " os.chdir('/home/agconti/my_dev/github/Atlatl_django/mysite')\n", " \n", " # start dev server as a subprocess so we can still develop interactively\n", " self.server = subprocess.Popen([\"python\", \"manage.py\", \"runserver\"])\n", " \n", " #change dir back\n", " os.chdir(cdir)\n", " \n", " self.server = 'running'\n", " \n", " print \"Im running!\\nGo here: http://127.0.0.1:8000/\\nCurrent Working Dir: %s\"%(cdir)\n", " \n", " \n", " def link(self):\n", " '''\n", " outputs the server's link for convience\n", " '''\n", " if self.server == 'running': \n", " print \"Im running!\\nGo here: http://127.0.0.1:8000\"\n", " else:\n", " print \"Sorry, I'm not running.\"\n", " \n", " def link_admin(self):\n", " '''\n", " outputs the server's admin link for convience\n", " '''\n", " if self.server == 'running': \n", " print \"Im running!\\nGo here: http://127.0.0.1:8000/admin/\"\n", " else:\n", " print \"Sorry, I'm not running.\"\n", " \n", " def end(self):\n", " '''\n", " ends the sever\n", " '''\n", " if self.server == 'no_server':\n", " print \"there is no sever!\"\n", " else:\n", " self.server.terminate()\n", " self.server = 'no_server'\n", " print \"I've Stopped.\" " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "my_server = dev_server()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "my_server.start()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Im running!\n", "Go here: http://127.0.0.1:8000/\n", "Current Working Dir: /home/agconti/my_dev/github/Atlatl_django/mysite\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create my app and call it houses:\n", " \n", " !python manage.py startapp houses" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ls -l" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "total 52\r\n", "drwxrwxr-x 4 agconti agconti 4096 Jul 21 15:55 \u001b[0m\u001b[01;34mhouses\u001b[0m/\r\n", "-rw-r--r-- 1 agconti agconti 249 Jul 19 12:07 manage.py\r\n", "drwxrwxr-x 2 agconti agconti 4096 Jul 21 15:54 \u001b[01;34mmysite\u001b[0m/\r\n", "-rw-r--r-- 1 agconti agconti 33792 Jul 21 16:26 sqlite3.db\r\n" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "cd houses/" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/houses\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "ll" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "total 48\r\n", "-rw-rw-r-- 1 agconti 126 Jul 21 15:53 admin.py\r\n", "-rw-rw-r-- 1 agconti 346 Jul 21 15:54 admin.pyc\r\n", "-rw-r--r-- 1 agconti 0 Jul 19 12:34 __init__.py\r\n", "-rw-r--r-- 1 agconti 154 Jul 19 12:55 __init__.pyc\r\n", "drwxrwxr-x 3 agconti 4096 Jul 21 15:53 \u001b[0m\u001b[01;34mmanagement\u001b[0m/\r\n", "-rw-r--r-- 1 agconti 398 Jul 21 15:53 models.py\r\n", "-rw-r--r-- 1 agconti 953 Jul 21 15:53 models.pyc\r\n", "drwxrwxrwx 3 agconti 4096 Jul 21 15:35 \u001b[34;42mtemplates\u001b[0m/\r\n", "-rw-r--r-- 1 agconti 383 Jul 19 12:34 tests.py\r\n", "-rw-rw-r-- 1 agconti 140 Jul 21 15:53 urls.py\r\n", "-rw-rw-r-- 1 agconti 372 Jul 21 15:54 urls.pyc\r\n", "-rw-r--r-- 1 agconti 249 Jul 21 15:55 views.py\r\n", "-rw-r--r-- 1 agconti 621 Jul 21 15:55 views.pyc\r\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ " ###1." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile models.py\n", "from django.db import models\n", "\n", "# Create your models here.\n", "\n", "class Owner(models.Model):\n", " name = models.TextField(max_length=200, unique=True)\n", " date_created = models.DateTimeField(auto_now_add=True, blank=True)\n", "\n", "class House(models.Model):\n", " address = models.TextField(max_length=200)\n", " date_created = models.DateTimeField(auto_now_add=True, blank=True)\n", " owner = models.OneToOneField(Owner)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting models.py\n" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile admin.py\n", "from django.contrib import admin\n", "from houses.models import Owner, House\n", "\n", "admin.site.register(House)\n", "admin.site.register(Owner)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting admin.py\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "cd /home/agconti/my_dev/github/Atlatl_django/mysite/mysite" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/mysite\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile settings.py\n", "\n", "# Django settings for mysite project.\n", "\n", "DEBUG = True\n", "TEMPLATE_DEBUG = DEBUG\n", "\n", "ADMINS = (\n", " # ('Your Name', 'your_email@example.com'),\n", ")\n", "\n", "MANAGERS = ADMINS\n", "\n", "DATABASES = {\n", " 'default': {\n", " 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.\n", " 'NAME': '/home/agconti/my_dev/github/Atlatl_django/mysite/sqlite3.db', # Or path to database file if using sqlite3.\n", " # The following settings are not used with sqlite3:\n", " 'USER': '',\n", " 'PASSWORD': '',\n", " 'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.\n", " 'PORT': '', # Set to empty string for default.\n", " }\n", "}\n", "\n", "# Hosts/domain names that are valid for this site; required if DEBUG is False\n", "# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts\n", "ALLOWED_HOSTS = []\n", "\n", "# Local time zone for this installation. Choices can be found here:\n", "# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name\n", "# although not all choices may be available on all operating systems.\n", "# In a Windows environment this must be set to your system time zone.\n", "TIME_ZONE = 'America/Chicago'\n", "\n", "# Language code for this installation. All choices can be found here:\n", "# http://www.i18nguy.com/unicode/language-identifiers.html\n", "LANGUAGE_CODE = 'en-us'\n", "\n", "SITE_ID = 1\n", "\n", "# If you set this to False, Django will make some optimizations so as not\n", "# to load the internationalization machinery.\n", "USE_I18N = True\n", "\n", "# If you set this to False, Django will not format dates, numbers and\n", "# calendars according to the current locale.\n", "USE_L10N = True\n", "\n", "# If you set this to False, Django will not use timezone-aware datetimes.\n", "USE_TZ = True\n", "\n", "# Absolute filesystem path to the directory that will hold user-uploaded files.\n", "# Example: \"/var/www/example.com/media/\"\n", "MEDIA_ROOT = ''\n", "\n", "# URL that handles the media served from MEDIA_ROOT. Make sure to use a\n", "# trailing slash.\n", "# Examples: \"http://example.com/media/\", \"http://media.example.com/\"\n", "MEDIA_URL = ''\n", "\n", "# Absolute path to the directory static files should be collected to.\n", "# Don't put anything in this directory yourself; store your static files\n", "# in apps' \"static/\" subdirectories and in STATICFILES_DIRS.\n", "# Example: \"/var/www/example.com/static/\"\n", "STATIC_ROOT = ''\n", "\n", "# URL prefix for static files.\n", "# Example: \"http://example.com/static/\", \"http://static.example.com/\"\n", "STATIC_URL = '/static/'\n", "\n", "# Additional locations of static files\n", "STATICFILES_DIRS = (\n", " # Put strings here, like \"/home/html/static\" or \"C:/www/django/static\".\n", " # Always use forward slashes, even on Windows.\n", " # Don't forget to use absolute paths, not relative paths.\n", ")\n", "\n", "# List of finder classes that know how to find static files in\n", "# various locations.\n", "STATICFILES_FINDERS = (\n", " 'django.contrib.staticfiles.finders.FileSystemFinder',\n", " 'django.contrib.staticfiles.finders.AppDirectoriesFinder',\n", "# 'django.contrib.staticfiles.finders.DefaultStorageFinder',\n", ")\n", "\n", "# Make this unique, and don't share it with anybody.\n", "SECRET_KEY = '(p(3gth07w54-0@+2^cx&@b5tdmd1p5v_&)(b0^8q-uop!^7(-'\n", "\n", "# List of callables that know how to import templates from various sources.\n", "TEMPLATE_LOADERS = (\n", " 'django.template.loaders.filesystem.Loader',\n", " 'django.template.loaders.app_directories.Loader',\n", "# 'django.template.loaders.eggs.Loader',\n", ")\n", "\n", "MIDDLEWARE_CLASSES = (\n", " 'django.middleware.common.CommonMiddleware',\n", " 'django.contrib.sessions.middleware.SessionMiddleware',\n", " 'django.middleware.csrf.CsrfViewMiddleware',\n", " 'django.contrib.auth.middleware.AuthenticationMiddleware',\n", " 'django.contrib.messages.middleware.MessageMiddleware',\n", " # Uncomment the next line for simple clickjacking protection:\n", " # 'django.middleware.clickjacking.XFrameOptionsMiddleware',\n", ")\n", "\n", "ROOT_URLCONF = 'mysite.urls'\n", "\n", "# Python dotted path to the WSGI application used by Django's runserver.\n", "WSGI_APPLICATION = 'mysite.wsgi.application'\n", "\n", "TEMPLATE_DIRS = (\n", " # Put strings here, like \"/home/html/django_templates\" or \"C:/www/django/templates\".\n", " # Always use forward slashes, even on Windows.\n", " # Don't forget to use absolute paths, not relative paths.\n", ")\n", "\n", "INSTALLED_APPS = (\n", " 'django.contrib.auth',\n", " 'django.contrib.contenttypes',\n", " 'django.contrib.sessions',\n", " 'django.contrib.sites',\n", " 'django.contrib.messages',\n", " 'django.contrib.staticfiles',\n", " # Uncomment the next line to enable the admin:\n", " 'django.contrib.admin',\n", " # Uncomment the next line to enable admin documentation:\n", " # 'django.contrib.admindocs',\n", " 'houses' # register my new app\n", ")\n", "\n", "# A sample logging configuration. The only tangible logging\n", "# performed by this configuration is to send an email to\n", "# the site admins on every HTTP 500 error when DEBUG=False.\n", "# See http://docs.djangoproject.com/en/dev/topics/logging for\n", "# more details on how to customize your logging configuration.\n", "LOGGING = {\n", " 'version': 1,\n", " 'disable_existing_loggers': False,\n", " 'filters': {\n", " 'require_debug_false': {\n", " '()': 'django.utils.log.RequireDebugFalse'\n", " }\n", " },\n", " 'handlers': {\n", " 'mail_admins': {\n", " 'level': 'ERROR',\n", " 'filters': ['require_debug_false'],\n", " 'class': 'django.utils.log.AdminEmailHandler'\n", " }\n", " },\n", " 'loggers': {\n", " 'django.request': {\n", " 'handlers': ['mail_admins'],\n", " 'level': 'ERROR',\n", " 'propagate': True,\n", " },\n", " }\n", "}\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting settings.py\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile urls.py\n", "from django.conf.urls import patterns, include, url\n", "\n", "# Uncomment the next two lines to enable the admin:\n", "from django.contrib import admin\n", "admin.autodiscover()\n", "\n", "urlpatterns = patterns('',\n", " # Examples:\n", " # url(r'^$', 'mysite.views.home', name='home'),\n", " # url(r'^mysite/', include('mysite.foo.urls')),\n", "\n", " # Uncomment the admin/doc line below to enable admin documentation:\n", " # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),\n", "\n", " # Uncomment the next line to enable the admin:\n", " url(r'^admin/', include(admin.site.urls)),\n", ")\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting urls.py\n" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "cd /home/agconti/my_dev/github/Atlatl_django/mysite" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "I'm using subprocess to preserve the interactive nature of the workbook:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import subprocess" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "subprocess.Popen([\"python\", \"manage.py\", \"syncdb\"])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "ll" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "total 52\r\n", "drwxrwxr-x 4 agconti 4096 Jul 21 16:27 \u001b[0m\u001b[01;34mhouses\u001b[0m/\r\n", "-rw-r--r-- 1 agconti 249 Jul 19 12:07 manage.py\r\n", "drwxrwxr-x 2 agconti 4096 Jul 21 16:27 \u001b[01;34mmysite\u001b[0m/\r\n", "-rw-r--r-- 1 agconti 33792 Jul 21 16:27 sqlite3.db\r\n" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "import os\n", "os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create The superuser account:\n", " \n", " from django.contrib.auth.models import User \n", " user = User.objects.create_user('andrew', 'andrew@agconti.com', 'password')\n", " user.is_staff = True\n", " user.save()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check out the admin console" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_server.link_admin()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Im running!\n", "Go here: http://127.0.0.1:8000/admin/\n" ] } ], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Start Testing for first custom manage.py function:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# this lets me easily manipulate these models from within the notebook\n", "from houses.models import Owner,House" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "o = Owner(name='Luke Skywalker')\n", "o.save()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "Owner.objects.all()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "[]" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "jedi = Owner.objects.filter(id=1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "jedi.values()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": [ "[{'date_created': datetime.datetime(2013, 7, 21, 20, 27, 18, 851819, tzinfo=), u'id': 1, 'name': u'Luke Skywalker'}]" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "jedi.delete()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 26 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the dir structure for the custom manage.py commands:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cd houses/ " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/houses\n" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ " %%bash\n", " mkdir management" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cd management" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/houses/management\n" ] } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile __init__.py\n", "#Make management a Python package" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting __init__.py\n" ] } ], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": {}, "source": [ " %%bash \n", " mkdir commands" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cd commands" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/houses/management/commands\n" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile __init__.py\n", "#Make commands a Python package" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting __init__.py\n" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile add_owner.py\n", "#creates an add owner manage.py custom command" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting add_owner.py\n" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "cd /home/agconti/my_dev/github/Atlatl_django/mysite/houses/management/commands" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/houses/management/commands\n" ] } ], "prompt_number": 33 }, { "cell_type": "markdown", "metadata": {}, "source": [ "2." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add_Owner Function:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile add_owner.py\n", "from optparse import make_option\n", "from django.core.management.base import BaseCommand, CommandError\n", "from houses.models import Owner\n", "\n", "class Command(BaseCommand):\n", " option_list = BaseCommand.option_list + (\n", " make_option('--name',\n", " action='store',\n", " type = 'string',\n", " dest='name',\n", " default=False,\n", " help='Add Owner to owner Model'),\n", " )\n", "\n", " def handle(self, *args, **options):\n", " if options['name']:\n", " try:\n", " o = Owner(name=options['name'])\n", " o.save()\n", " except Owner.unique_error_message:\n", " raise CommandError('Owner \"%s\" already exists' % options['name'])\n", " \n", " self.stdout.write('Successfully Added Owner Name=\"%s\"' % options['name'])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting add_owner.py\n" ] } ], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "cd ~/my_dev/github/Atlatl_django/mysite/" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite\n" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "ll" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "total 52\r\n", "drwxrwxr-x 4 agconti 4096 Jul 21 15:27 \u001b[0m\u001b[01;34mhouses\u001b[0m/\r\n", "-rw-r--r-- 1 agconti 249 Jul 19 11:07 manage.py\r\n", "drwxrwxr-x 2 agconti 4096 Jul 21 15:27 \u001b[01;34mmysite\u001b[0m/\r\n", "-rw-r--r-- 1 agconti 33792 Jul 21 15:27 sqlite3.db\r\n" ] } ], "prompt_number": 36 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test custom function:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py add_owner --name='Luke Skywalker'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Successfully Added Owner Name=\"Luke Skywalker\"\r\n" ] } ], "prompt_number": 37 }, { "cell_type": "code", "collapsed": false, "input": [ "o = Owner.objects.filter(id=1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "o.values_list()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 39, "text": [ "[(1, u'Luke Skywalker', datetime.datetime(2013, 7, 21, 20, 27, 23, 464939, tzinfo=))]" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py add_owner --name='Luke Skywalker'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\u001b[31;1mIntegrityError: column name is not unique\r\n", "\u001b[0m" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "o.delete()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "Owner.objects.all()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 42, "text": [ "[]" ] } ], "prompt_number": 42 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add add_house function:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "House.objects.all()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 43, "text": [ "[]" ] } ], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": [ "# add Luke to test adding a house to an existing owner\n", "o = Owner(name='Luke Skywalker')\n", "o.save()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 44 }, { "cell_type": "code", "collapsed": false, "input": [ "Owner.objects.all()[0].name" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 45, "text": [ "u'Luke Skywalker'" ] } ], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "Owner.objects.all()[0].name # Luke added" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 46, "text": [ "u'Luke Skywalker'" ] } ], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "o.name # beter notation" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 47, "text": [ "'Luke Skywalker'" ] } ], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": [ "House.objects.all() # check for existing houses" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 48, "text": [ "[]" ] } ], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "o,create = Owner.objects.get_or_create(name='Luke Skywalker')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "subprocess.Popen([\"python\", \"manage.py\", \"syncdb\"])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 50, "text": [ "" ] } ], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": [ "# add Luke's house\n", "h,create = House.objects.get_or_create(address='Way to close to the Sand People', owner=o)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": [ "h.address" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 52, "text": [ "'Way to close to the Sand People'" ] } ], "prompt_number": 52 }, { "cell_type": "code", "collapsed": false, "input": [ "h.owner.name" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 53, "text": [ "u'Luke Skywalker'" ] } ], "prompt_number": 53 }, { "cell_type": "code", "collapsed": false, "input": [ "House.objects.all()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 54, "text": [ "[]" ] } ], "prompt_number": 54 }, { "cell_type": "code", "collapsed": false, "input": [ "House.objects.all()[0].owner.name # it does belong to Luke" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 55, "text": [ "u'Luke Skywalker'" ] } ], "prompt_number": 55 }, { "cell_type": "code", "collapsed": false, "input": [ "cd /home/agconti/my_dev/github/Atlatl_django/mysite/houses/management/commands" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/houses/management/commands\n" ] } ], "prompt_number": 56 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile add_house.py\n", "from optparse import make_option\n", "from django.core.management.base import BaseCommand, CommandError\n", "from django.db import IntegrityError\n", "from houses.models import House, Owner\n", "\n", "class Command(BaseCommand):\n", " option_list = BaseCommand.option_list + (\n", " make_option('--owner',\n", " action='store',\n", " type = 'string',\n", " dest='owner',\n", " default=False,\n", " help='Add Owner to owner Model'),\n", " \n", " make_option('--address',\n", " action='store',\n", " type = 'string',\n", " dest='address',\n", " default=False,\n", " help='Add house address to house model'),\n", " )\n", "\n", " def handle(self, *args, **options):\n", " '''\n", " '''\n", " if (options['address'] != None) and (options['owner'] != None):\n", " try:\n", " # to add house and owner\n", " o,create_owner = Owner.objects.get_or_create(name = options['owner'])\n", " \n", " # if an owner was created sync the database\n", " if (create_owner == True):\n", " import subprocess\n", " subprocess.Popen([\"python\", \"manage.py\", \"syncdb\"])\n", " \n", " # create / get the house \n", " o,create_house_owner = House.objects.get_or_create(address=options['address'], owner=(o))\n", " \n", " if create_owner == True:\n", " self.stdout.write(\n", " 'Successfully Added House: %s and Owner=%s' \n", " % (options['address'], options['owner'])\n", " )\n", " \n", " elif create_house_owner == True: \n", " self.stdout.write(\n", " 'Successfully Added House: %s to Owner=%s' \n", " % (options['address'], options['owner'])\n", " )\n", " else:\n", " self.stdout.write('method failed to create an owner or a house')\n", " \n", " except IntegrityError:\n", " self.stdout.write('There is already a house associated with that Owner')\n", " else:\n", " print 'Please include address AND owner!'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting add_house.py\n" ] } ], "prompt_number": 57 }, { "cell_type": "code", "collapsed": false, "input": [ "cd ~/my_dev/github/Atlatl_django/mysite/" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite\n" ] } ], "prompt_number": 58 }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py add_house --address=\"123 Main St.\" --owner='Mary'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Successfully Added House: 123 Main St. and Owner=Mary\r\n", "Creating tables ...\r\n" ] } ], "prompt_number": 59 }, { "cell_type": "code", "collapsed": false, "input": [ "Owner.objects.all()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 60, "text": [ "[, ]" ] } ], "prompt_number": 60 }, { "cell_type": "code", "collapsed": false, "input": [ "Owner.objects.all()[1].name" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 61, "text": [ "u'Mary'" ] } ], "prompt_number": 61 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tests:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "h = House.objects.all()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 62 }, { "cell_type": "code", "collapsed": false, "input": [ "h.values()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 63, "text": [ "[{'date_created': datetime.datetime(2013, 7, 21, 20, 27, 31, 472669, tzinfo=), 'owner_id': 1, u'id': 1, 'address': u'Way to close to the Sand People'}, {'date_created': datetime.datetime(2013, 7, 21, 20, 28, 27, 606807, tzinfo=), 'owner_id': 2, u'id': 2, 'address': u'123 Main St.'}]" ] } ], "prompt_number": 63 }, { "cell_type": "code", "collapsed": false, "input": [ "o = Owner.objects.all()\n", "o.values()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 64, "text": [ "[{'date_created': datetime.datetime(2013, 7, 21, 20, 27, 28, 956290, tzinfo=), u'id': 1, 'name': u'Luke Skywalker'}, {'date_created': datetime.datetime(2013, 7, 21, 20, 28, 27, 446686, tzinfo=), u'id': 2, 'name': u'Mary'}]" ] } ], "prompt_number": 64 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Show_house Fucntion:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "h = House.objects.all()\n", "h" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 65, "text": [ "[, ]" ] } ], "prompt_number": 65 }, { "cell_type": "code", "collapsed": false, "input": [ "str(h[0].address)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 66, "text": [ "'Way to close to the Sand People'" ] } ], "prompt_number": 66 }, { "cell_type": "code", "collapsed": false, "input": [ "home_owner = h[0].owner\n", "home_owner" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 67, "text": [ "" ] } ], "prompt_number": 67 }, { "cell_type": "code", "collapsed": false, "input": [ "str(home_owner.name)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 68, "text": [ "'Luke Skywalker'" ] } ], "prompt_number": 68 }, { "cell_type": "code", "collapsed": false, "input": [ "Owner.objects.filter(name='Luke Skywalker')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 69, "text": [ "[]" ] } ], "prompt_number": 69 }, { "cell_type": "code", "collapsed": false, "input": [ "Owner(name='Mary')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 70, "text": [ "" ] } ], "prompt_number": 70 }, { "cell_type": "code", "collapsed": false, "input": [ "h = House.objects.filter(owner=Owner.objects.filter(name='Luke Skywalker'))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 71 }, { "cell_type": "code", "collapsed": false, "input": [ "h[0].address" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 72, "text": [ "u'Way to close to the Sand People'" ] } ], "prompt_number": 72 }, { "cell_type": "code", "collapsed": false, "input": [ "#get all of the fields associated with the model\n", "fields = House.objects.model._meta.fields" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 73 }, { "cell_type": "code", "collapsed": false, "input": [ "fields" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 74, "text": [ "[,\n", " ,\n", " ,\n", " ]" ] } ], "prompt_number": 74 }, { "cell_type": "code", "collapsed": false, "input": [ "address = fields[1]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 75 }, { "cell_type": "code", "collapsed": false, "input": [ "address.value_from_object(h[0])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 76, "text": [ "u'Way to close to the Sand People'" ] } ], "prompt_number": 76 }, { "cell_type": "code", "collapsed": false, "input": [ "for f in fields:\n", " print '%s=[%s],'% (f.attname,f.value_from_object(h[0]))," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "id=[1], address=[Way to close to the Sand People], date_created=[2013-07-21 20:27:31.472669+00:00], owner_id=[1],\n" ] } ], "prompt_number": 77 }, { "cell_type": "code", "collapsed": false, "input": [ "Owner.objects.get(name='Mary')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 78, "text": [ "" ] } ], "prompt_number": 78 }, { "cell_type": "code", "collapsed": false, "input": [ "House.objects.filter(address__contains='Main', owner=Owner.objects.get(name='Mary'))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 79, "text": [ "[]" ] } ], "prompt_number": 79 }, { "cell_type": "code", "collapsed": false, "input": [ "cd /home/agconti/my_dev/github/Atlatl_django/mysite/houses/management/commands" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/houses/management/commands\n" ] } ], "prompt_number": 80 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile show_house.py\n", "from optparse import make_option\n", "from django.core.management.base import BaseCommand, CommandError\n", "from django.forms.models import model_to_dict\n", "from houses.models import House, Owner\n", "\n", "class Command(BaseCommand):\n", " option_list = BaseCommand.option_list + (\n", " make_option('--owner',\n", " action='store',\n", " type = 'string',\n", " dest='owner',\n", " default=False,\n", " help='Specify Owner to show'),\n", " \n", " make_option('--addr-contains',\n", " action='store',\n", " type = 'string',\n", " dest='addr',\n", " default=False,\n", " help='Specify Owner to show')\n", " \n", " )\n", " \n", " def handle(self, *args, **options):\n", " '''\n", " '''\n", " #get fields for house model\n", " fields = House.objects.model._meta.fields\n", " \n", " if options['addr'] and options['owner']:\n", " \n", " try:\n", " house_querry = House.objects.filter(\n", " address__contains=options['addr'], \n", " owner=Owner.objects.get(name=options['owner'])\n", " )\n", " #print houses for owner to console\n", " for i,val in enumerate(house_querry):\n", " for f in fields:\n", " self.stdout.write('%s=[%s],' % (f.attname,f.value_from_object(house_querry[i])), ending='')\n", " self.stdout.write('')\n", " except:\n", " self.stdout.write(\n", " 'There is no house containing [%s] in the dataset Or there is no Owner=[%s]' \n", " % (options['addr'], options['owner'])\n", " )\n", " \n", " elif options['owner']:\n", " try:\n", " #get houses for owner\n", " house_querry = House.objects.filter(owner=Owner.objects.filter(name=options['owner']))\n", " #print houses for owner to console\n", " for i,val in enumerate(house_querry):\n", " for f in fields:\n", " self.stdout.write('%s=[%s],'\n", " % (f.attname,f.value_from_object(house_querry[i])), \n", " ending=''\n", " )\n", " self.stdout.write('')\n", " except Owner.DoesNotExist:\n", " self.stdout.write(\n", " 'Specified Owner=[%s] does not exist in the dataset' \n", " % options['owner']\n", " )\n", " else:\n", " #get all houses\n", " house_querry = House.objects.all()\n", " #print all houses to console\n", " for i,val in enumerate(house_querry):\n", " for f in fields:\n", " self.stdout.write(\n", " '%s=[%s],' \n", " % (f.attname,f.value_from_object(house_querry[i])), \n", " ending=''\n", " )\n", " self.stdout.write('')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting show_house.py\n" ] } ], "prompt_number": 81 }, { "cell_type": "code", "collapsed": false, "input": [ "cd ~/my_dev/github/Atlatl_django/mysite/" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite\n" ] } ], "prompt_number": 82 }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py show_house" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "id=[1],address=[Way to close to the Sand People],date_created=[2013-07-21 20:27:31.472669+00:00],owner_id=[1],\r\n", "id=[2],address=[123 Main St.],date_created=[2013-07-21 20:28:27.606807+00:00],owner_id=[2],\r\n" ] } ], "prompt_number": 83 }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py show_house --owner='Mary'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "id=[2],address=[123 Main St.],date_created=[2013-07-21 20:28:27.606807+00:00],owner_id=[2],\r\n" ] } ], "prompt_number": 84 }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py show_house --owner='Mary' --addr-contains='Main'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "id=[2],address=[123 Main St.],date_created=[2013-07-21 20:28:27.606807+00:00],owner_id=[2],\r\n" ] } ], "prompt_number": 85 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Delete_house command:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "o = Owner(name='Star Trek Red Shirt') # soon to be deleted owner\n", "o.save()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 86 }, { "cell_type": "code", "collapsed": false, "input": [ "h = House(address='1 House of Cards Lane', owner=o) # soon to be gone house\n", "h.save()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 87 }, { "cell_type": "code", "collapsed": false, "input": [ "House.objects.filter(address__contains='Cards') #find it " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 88, "text": [ "[]" ] } ], "prompt_number": 88 }, { "cell_type": "code", "collapsed": false, "input": [ "House.objects.filter(address__contains='Cards')[0].owner.name #Check it " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 89, "text": [ "u'Star Trek Red Shirt'" ] } ], "prompt_number": 89 }, { "cell_type": "code", "collapsed": false, "input": [ "House.objects.filter(address__contains='Cards') #house to be deleted" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 90, "text": [ "[]" ] } ], "prompt_number": 90 }, { "cell_type": "code", "collapsed": false, "input": [ "# Check if Owner has houses\n", "len(House.objects.filter(owner=(House.objects.filter(address__contains='Cards')[0].owner))) " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 91, "text": [ "1" ] } ], "prompt_number": 91 }, { "cell_type": "code", "collapsed": false, "input": [ "House.objects.filter(address__contains='Cards')[0].owner # Owner to be deleted" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 92, "text": [ "" ] } ], "prompt_number": 92 }, { "cell_type": "code", "collapsed": false, "input": [ "#setup owner \n", "o = House.objects.filter(address__contains='Cards')[0].owner" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 93 }, { "cell_type": "code", "collapsed": false, "input": [ "# delete house\n", "House.objects.filter(address__contains='Cards').delete()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 94 }, { "cell_type": "code", "collapsed": false, "input": [ "#delete owner if no houses\n", "if len(House.objects.filter(owner=(o))) < 1:\n", " o.delete()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 95 }, { "cell_type": "code", "collapsed": false, "input": [ "cd /home/agconti/my_dev/github/Atlatl_django/mysite/houses/management/commands" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/houses/management/commands\n" ] } ], "prompt_number": 96 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile delete_house.py\n", "from optparse import make_option\n", "from exceptions import IndexError\n", "from django.core.management.base import BaseCommand, CommandError\n", "from houses.models import House, Owner\n", "\n", "class Command(BaseCommand):\n", " option_list = BaseCommand.option_list + (\n", " make_option('--addr-contains',\n", " action='store',\n", " type = 'string',\n", " dest='addr',\n", " default=False,\n", " help='Specify Owner to show'),\n", " )\n", " \n", " def handle(self, *args, **options):\n", " '''\n", " '''\n", " if options['addr']:\n", " # delete house\n", " try:\n", " #setup owner list \n", " house_owners = []\n", " # delete house\n", " h = House.objects.filter(address__contains=options['addr'])\n", " for i in h:\n", " house_owners.append(i.owner)\n", " i.delete()\n", " self.stdout.write('House at [%s] was deleted' % i.address)\n", " except:\n", " self.stdout.write(\n", " 'There is no house containing [%s] in the dataset' \n", " % options['addr']\n", " )\n", " \n", " #delete owner\n", " try:\n", " for o in house_owners:\n", " # check their # of houses\n", " if len(House.objects.filter(owner=(o))) < 1:\n", " o.delete()\n", " self.stdout.write('Owner [%s] was deleted' % o.name )\n", " except IndexError:\n", " self.stdout.write('There was an Error deleting Owner [%s]' % o.name )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting delete_house.py\n" ] } ], "prompt_number": 97 }, { "cell_type": "code", "collapsed": false, "input": [ "cd ~/my_dev/github/Atlatl_django/mysite/" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite\n" ] } ], "prompt_number": 98 }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py delete_house --addr-contains='Cards'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 99 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Delete house tests:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py add_house --address='123 Lake Lane' --owner='Mr.Lake'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Successfully Added House: 123 Lake Lane and Owner=Mr.Lake\r\n", "Creating tables ...\r\n" ] } ], "prompt_number": 100 }, { "cell_type": "code", "collapsed": false, "input": [ "Owner.objects.all()[2].name" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 101, "text": [ "u'Mr.Lake'" ] } ], "prompt_number": 101 }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py delete_house --addr-contains='Lake'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "House at [123 Lake Lane] was deleted\r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Owner [Mr.Lake] was deleted\r\n" ] } ], "prompt_number": 102 }, { "cell_type": "code", "collapsed": false, "input": [ "Owner.objects.all()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 103, "text": [ "[, ]" ] } ], "prompt_number": 103 }, { "cell_type": "code", "collapsed": false, "input": [ "House.objects.all()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 104, "text": [ "[, ]" ] } ], "prompt_number": 104 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add two similar houses:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py add_house --address='123 Lake Lane' --owner='Mr.Lake'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Successfully Added House: 123 Lake Lane and Owner=Mr.Lake\r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Creating tables ...\r\n" ] } ], "prompt_number": 105 }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py add_house --address='124 Lake Lane' --owner='Mr.Lake' ## only when owner is manytomany and not onetoone" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "There is already a house associated with that Owner\r\n" ] } ], "prompt_number": 106 }, { "cell_type": "code", "collapsed": false, "input": [ "!python manage.py delete_house --addr-contains='Lake'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "House at [123 Lake Lane] was deleted\r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Owner [Mr.Lake] was deleted\r\n" ] } ], "prompt_number": 107 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Handle Admin Site Error:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from django.contrib.sites.models import Site\n", "if len(Site.objects.all()) ==0 :\n", " Site.objects.create(pk=1)\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 108 }, { "cell_type": "code", "collapsed": false, "input": [ "Site.objects.all()[0].pk" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 109, "text": [ "1" ] } ], "prompt_number": 109 }, { "cell_type": "code", "collapsed": false, "input": [ "my_server.link_admin()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Im running!\n", "Go here: http://127.0.0.1:8000/admin/\n" ] } ], "prompt_number": 110 }, { "cell_type": "code", "collapsed": false, "input": [ "cd ~/my_dev/github/Atlatl_django/mysite/houses/" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/houses\n" ] } ], "prompt_number": 111 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile views.py\n", "from django.http import HttpResponse\n", "\n", "def index(request):\n", " return HttpResponse(\"Hello, world. You're at the houses index.\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting views.py\n" ] } ], "prompt_number": 112 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile urls.py\n", "from django.conf.urls import patterns, url\n", "\n", "from houses import views\n", "\n", "urlpatterns = patterns('',\n", " url(r'^$', views.index, name='index')\n", ")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting urls.py\n" ] } ], "prompt_number": 113 }, { "cell_type": "code", "collapsed": false, "input": [ "cd ~/my_dev/github/Atlatl_django/mysite/mysite/" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/mysite\n" ] } ], "prompt_number": 114 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile urls.py\n", "from django.conf.urls import patterns, include, url\n", "\n", "# Uncomment the next two lines to enable the admin:\n", "from django.contrib import admin\n", "admin.autodiscover()\n", "\n", "urlpatterns = patterns('',\n", " # Examples:\n", " # url(r'^$', 'mysite.views.home', name='home'),\n", " # url(r'^mysite/', include('mysite.foo.urls')),\n", "\n", " # Uncomment the admin/doc line below to enable admin documentation:\n", " # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),\n", "\n", " # Uncomment the next line to enable the admin:\n", " url(r'^admin/', include(admin.site.urls)),\n", " url(r'^houses/', include('houses.urls')),\n", ")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting urls.py\n" ] } ], "prompt_number": 115 }, { "cell_type": "code", "collapsed": false, "input": [ "ll" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "total 32\r\n", "-rw-r--r-- 1 agconti 0 Jul 19 11:07 __init__.py\r\n", "-rw-r--r-- 1 agconti 154 Jul 19 11:13 __init__.pyc\r\n", "-rw-r--r-- 1 agconti 5430 Jul 21 15:27 settings.py\r\n", "-rw-r--r-- 1 agconti 3006 Jul 21 15:27 settings.pyc\r\n", "-rw-r--r-- 1 agconti 597 Jul 21 15:28 urls.py\r\n", "-rw-r--r-- 1 agconti 483 Jul 21 14:54 urls.pyc\r\n", "-rw-r--r-- 1 agconti 1419 Jul 19 11:07 wsgi.py\r\n", "-rw-r--r-- 1 agconti 1048 Jul 19 11:13 wsgi.pyc\r\n" ] } ], "prompt_number": 116 }, { "cell_type": "markdown", "metadata": {}, "source": [ "http://127.0.0.1:8000/houses/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Improve the view:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cd ~/my_dev/github/Atlatl_django/mysite/" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite\n" ] } ], "prompt_number": 117 }, { "cell_type": "code", "collapsed": false, "input": [ "Owner.objects.all()[0].house.address" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 118, "text": [ "u'Way to close to the Sand People'" ] } ], "prompt_number": 118 }, { "cell_type": "code", "collapsed": false, "input": [ "for i in House.objects.all().order_by('owner'):\n", " print i.owner.name, i.address" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Luke Skywalker Way to close to the Sand People\n", "Mary 123 Main St.\n" ] } ], "prompt_number": 119 }, { "cell_type": "code", "collapsed": false, "input": [ "cd ~/my_dev/github/Atlatl_django/mysite/houses/" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/houses\n" ] } ], "prompt_number": 120 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile views.py\n", "from django.shortcuts import render\n", "\n", "from houses.models import House, Owner\n", "\n", "def index(request):\n", " house_list = House.objects.all().order_by('owner')\n", " context = {'house_list': house_list}\n", " return render(request, 'houses/index.html', context)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting views.py\n" ] } ], "prompt_number": 121 }, { "cell_type": "markdown", "metadata": {}, "source": [ " %%bash\n", " mkdir templates" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ll" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "total 48\r\n", "-rw-rw-r-- 1 agconti 126 Jul 21 15:27 admin.py\r\n", "-rw-rw-r-- 1 agconti 346 Jul 21 14:54 admin.pyc\r\n", "-rw-r--r-- 1 agconti 0 Jul 19 11:34 __init__.py\r\n", "-rw-r--r-- 1 agconti 154 Jul 19 11:55 __init__.pyc\r\n", "drwxrwxr-x 3 agconti 4096 Jul 21 15:27 \u001b[0m\u001b[01;34mmanagement\u001b[0m/\r\n", "-rw-r--r-- 1 agconti 398 Jul 21 15:27 models.py\r\n", "-rw-r--r-- 1 agconti 953 Jul 21 15:27 models.pyc\r\n", "drwxrwxrwx 3 agconti 4096 Jul 21 14:35 \u001b[34;42mtemplates\u001b[0m/\r\n", "-rw-r--r-- 1 agconti 383 Jul 19 11:34 tests.py\r\n", "-rw-rw-r-- 1 agconti 140 Jul 21 15:28 urls.py\r\n", "-rw-rw-r-- 1 agconti 372 Jul 21 14:54 urls.pyc\r\n", "-rw-r--r-- 1 agconti 249 Jul 21 15:28 views.py\r\n", "-rw-r--r-- 1 agconti 621 Jul 21 14:55 views.pyc\r\n" ] } ], "prompt_number": 122 }, { "cell_type": "code", "collapsed": false, "input": [ "cd templates" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/houses/templates\n" ] } ], "prompt_number": 123 }, { "cell_type": "markdown", "metadata": {}, "source": [ " %%bash\n", " mkdir houses\n", " subilme index.html" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cd /home/agconti/my_dev/github/Atlatl_django/mysite/houses/templates/houses" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django/mysite/houses/templates/houses\n" ] } ], "prompt_number": 124 }, { "cell_type": "code", "collapsed": false, "input": [ "ls -l" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "total 4\r\n", "-rw-rw-r-- 1 agconti agconti 403 Jul 21 15:09 index.html\r\n" ] } ], "prompt_number": 125 }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile index.html\n", "\n", "\n", "\t\n", "\t\tWhat houses are in the Neigborhood?\n", "\t\n", "

What houses are in the Neigborhood?

\n", "\n", "\n", "\t{% if house_list %}\n", "\t\t{% for house in house_list %}\n", "\t\t

Owner: {{house.owner.name}}

\n", "\t\t
    \n", "\t\t
  • House address: {{ house.address }}
  • \n", "\t\t
\n", " {% endfor %}\n", "\t{% else %}\n", "\t\t

No houses in the neighborhood.

\n", "\t{% endif %}\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Overwriting index.html\n" ] } ], "prompt_number": 126 }, { "cell_type": "code", "collapsed": false, "input": [ "my_server.link()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Im running!\n", "Go here: http://127.0.0.1:8000\n" ] } ], "prompt_number": 127 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Version Control " ] }, { "cell_type": "code", "collapsed": false, "input": [ "cd ~/my_dev/github/Atlatl_django/" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/agconti/my_dev/github/Atlatl_django\n" ] } ], "prompt_number": 128 }, { "cell_type": "code", "collapsed": false, "input": [ "!git add ." ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 129 }, { "cell_type": "code", "collapsed": false, "input": [ "!git commit -a -m \"final walk through\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[master 42b4f03] final walk through\r\n", " 10 files changed, 118 insertions(+), 98 deletions(-)\r\n" ] } ], "prompt_number": 130 }, { "cell_type": "code", "collapsed": false, "input": [ "!git commit --amend -m \"added webpage to show all houses and owners\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[master e7ab387] added webpage to show all houses and owners\r\n" ] } ], "prompt_number": 165 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }