Showing posts with label set-up. Show all posts
Showing posts with label set-up. Show all posts

Wednesday, May 21, 2014

Version control with git

I've used UberSVN and TortoiseSVN for version control before, but I figured this time I will join the rest of the world and start using git.

I've played around with git before, but never started using it for real.  Why? I don't know. I'm sort of an idiot that doesn't version control a lot of my software. I would use subversion for work stuff, but for my own personal projects, I just build the software outright.  Generally, I don't really take on giant projects to pursue in my free time.

Anyway, here are my notes for setting up git.

Download

Unpack and then navigate over to your powershell window. It doesn't matter if you are inside your virutalenv.

Initialize git:
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay> git init
Add the project:
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay> git add rebates
Commit the project with a message (-m):
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay> git commit -m 'Initial commit of rebate_info api'
If you don't already have a git repository online, make one.  When you have one, create an online 'origin' for the local project.
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay> git remote add origin https://github.com/jtarlecki/rebate_info.git
Check on the origin:
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay> git remote -v
origin  https://github.com/jtarlecki/rebate_info.git (fetch)
origin  https://github.com/jtarlecki/rebate_info.git (push)
Push the code to your git repository.
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay> git push -u origin master
Magic.

Wednesday, May 14, 2014

Windows issues with using django inside a virtualenv

Following up on a post with starting from scratch with a Python install, I found that starting a django project (without any dependencies -- truly in a virtual environment) was a little more difficult than I expected.

Turns out that when I was another machine, I was using the global install of django rather than running the isolated one contained in my virtual environment.

When did I realize this? I tried to run the standard django-admin.py
(django-jay) PS C:\Python27\home\jtarlecki\djcode> django-admin.py startprjoect rebates
And I got this:

django-admin.py : The term 'django-admin.py' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ django-admin.py startprjoect rebates
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (django-admin.py:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
What's the issue? Well, windows really doesn't know about the Python executable created in the Scripts folder when the virtual environment is created, so instead we have to tell it explicitly about the module ((django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay> python -m django-admin startproject rebates) in which it should look to fire up python.  That and, we need to invoke the command with a leading python.  We also drop the .py from django-admin.py  Observe:

(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay> python -m django-admin startproject rebates
Thanks stackoverflow.  Also, check out more info about in the python docs.

Setting up python

This is pretty trivial, but I just booted up a brand new computer and have been in the process of downloading things for hours.

1. Download/install python.  I use Python 2.7.

2. From powershell, run this to make sure you can run python from anywhere:
(django-jay) PS C:\Python27\> [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
3. Install PIP. Download get-pip.py, then run it from your powershell window:
(django-jay) PS C:\Python27\> python get-pip.py
4. Set the PATH environment variable to include the C:\Python27\Scripts directory
(django-jay) PS C:\Python27\> [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27\Scripts", "User")
5. Quit out of powershell, and reboot it.  Now run the following command:
PS C:\Python27> pip install virtualenv
7. Download/install PostgreSQL (this gives you access to psql shell)

That's it for the stuff I need! I'll update this as it evolves.

Sunday, April 27, 2014

Connect to a database

If you are connecting to a remote database, create the database with your remote host first.  Go to the cpanel or whatever you have, and add a new PostgreSQL database (In this example, I will be using PostgreSQL).  Every hosting account is probably different, so I'm not going to bother with the specifics.
Added the database “rebates”.

I like to also use a database management tool locally. So, at this stage, I like to add my IP address so that I can connect locally to my remote database server. (This is fairly standard for authentication with most hosting sites, I believe). Generally, you will need a specific username, password, and database at this point for authentication purposes.

Added:  rebates with user admin from XX.XXX.XXX.0/24
Now, locally I open up my database management tool (I'm using SQL Manager Lite for PostgreSQL) and register my newly created database.




Then, simply connect to the newly registered database.  Success!

Great-- but that has nothing to do with our django app. Let's configure the database in the settings.py, which exists in the project directory file. Our project is called "rebates". Inside the "rebates" directory, there is a project specific directory (automatically created by django when you start the project),which also has the same name of "rebates".   Inside this directory, you will find your settings.py file. I realize this is a little confusing.

The tree looks like this, where:
rebates is the project and
rebate is the app
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay\rebates> tree /f
Folder PATH listing for volume OS
Volume serial number is BA99-C83E
C:.
¦   manage.py
¦
+---rebate
¦       admin.py
¦       models.py
¦       tests.py
¦       views.py
¦       __init__.py
¦
+---rebates
        settings.py
        settings.pyc
        urls.py
        wsgi.py
        __init__.py
        __init__.pyc

I like to use notepad++ as my default editor.  I'm going to open my settings.py file with notepad++

(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay\rebates> start notepad++ rebates\settings.py

Inside settings.py, there is a default area for DATABASE which looks like this.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase',
    }
}
You can find out more about these configurations for each type of database inside the django documentation:
https://docs.djangoproject.com/en/1.6/ref/settings/#databases
Let's configure our database with the remote PostgreSQL database
DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.postgresql_psycopg2',
        'NAME': 'jaytarle_rebates',
        'USER': 'jaytarle_admin',
        'PASSWORD': 'XXXXXXXXXXXX',
        'HOST': 'jaytarlecki.com',
        'PORT': '5432',
    }
}

That should do the trick for the database, but before we close the settings.py file let's add rebate to our list of INSTALLED_APPS
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rebate',
)

Now that we have linked up the rebate app to our django project, let's go into the rebate directory and launch the models.py file:
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay\rebates> start notepad++ rebate\models.py

Time to create our database model! So, I already have an idea of how I want to structure my first test table, which is called zipcodes.  As you may be able to guess, its a table of zipcodes and other associated info.  I already have the data in another database.  For the sake of my own sanity, I'm just going to copy the CREATE statement here, so I can translate it into the django database model language, as needed.

In PostgreSQL, our zipcodes table:
CREATE TABLE zipcodes (
  id SERIAL,
  zipcode VARCHAR(5) NOT NULL,
  state CHAR(2) NOT NULL,
  city VARCHAR(28) NOT NULL,
  type CHAR(1) NOT NULL,
  countyfips CHAR(5) NOT NULL,
  latitude DOUBLE PRECISION NOT NULL,
  longitude DOUBLE PRECISION NOT NULL,
  areacode CHAR(3) NOT NULL,
  financecode CHAR(6) NOT NULL,
  lastline CHAR(4),
  fac CHAR(1),
  msa CHAR(4),
  pmsa CHAR(4),
  modifieduser VARCHAR(128) NOT NULL,
  modifieddate TIMESTAMP WITHOUT TIME ZONE NOT NULL
) 
So, we can skip the id field, as django's backend will handle that for us.

from django.db import models

# Create your models here.
class Zipcodes(models.Model):
 zipcode = models.CharField(max_length=5)
 state = models.CharField(max_length=2)
 city = models.CharField(max_length=100)
 type = models.CharField(max_length=1)
 countyfips = models.CharField(max_length=5)
 latitude = models.FloatField()
 longitude = models.FloatField()
 areacode = models.CharField(max_length=3)
 financecode = models.CharField(max_length=6)
 lastline = models.CharField(max_length=4, null=True)
 fac = models.CharField(max_length=1, null=True)
 msa = models.CharField(max_length=4, null=True)
 pmsa = models.CharField(max_length=4, null=True)
 modifieduser = models.CharField(max_length=128, default='admin')
 modifieddate = models.DateField(auto_now=True)

 def __unicode__(self):
  return self.zipcode


Inside a virtual environment, you will need to get the windows binary that allows proper syncing with a postgre database (don't ask, just do it)
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay\rebates> easy_install http://www.stickpeople.com/projects/python/win-psycopg/psycopg2-2.4.win32-pyx.x-pg9.0.3-release.exe

Now, create the database by syncing it in powershell
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay\rebates> python manage.py syncdb

Create a superuser if prompted, and viola! You have just synced the database. Django will create a whole slew of tables which are very useful, but I won't explain in this post.

What we care about right now is the zipcodes table. Refresh your local rebates database inside your SQL Manager and lets see how we did.  Let's script out a CREATE TABLE statement and view how our model in Django created the table in PostgreSQL

CREATE TABLE public.rebate_zipcodes (
  id SERIAL,
  zipcode VARCHAR(5) NOT NULL,
  state VARCHAR(2) NOT NULL,
  city VARCHAR(100) NOT NULL,
  type VARCHAR(1) NOT NULL,
  countyfips VARCHAR(5) NOT NULL,
  latitude DOUBLE PRECISION NOT NULL,
  longitude DOUBLE PRECISION NOT NULL,
  areacode VARCHAR(3) NOT NULL,
  financecode VARCHAR(6) NOT NULL,
  lastline VARCHAR(4),
  fac VARCHAR(1),
  msa VARCHAR(4),
  pmsa VARCHAR(4),
  modifieduser VARCHAR(128) NOT NULL,
  modifieddate DATE NOT NULL,
  CONSTRAINT rebate_zipcodes_pkey PRIMARY KEY(id)
) 
WITH (oids = false);
Not too shabby... Looks just like the table we used as our template. Its time to start building out that model!

Setting up a django project

In powershell, navigate to directory where your apps reside
PS C:\Python27\home\jtarlecki\djcode> virtualenv .\django-jay

Then change directory and activate the virtualenv:
PS C:\Python27\home\jtarlecki\djcode> cd django-jay
PS C:\Python27\home\jtarlecki\djcode\django-jay> Scripts\activate.ps1

Afterwords, your cursor should look like this:
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay>

Now, install an instance of django inside this virtual environment:
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay> pip install django

 After this downloads and unpacks, start your django project
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay> django-admin.py startproject rebates

Then, navigate to newly created directory, and start your app within that project
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay> cd rebates(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay\rebates> python manage.py startapp rebate

Now you are ready to begin programming!

If at any point, you want to kill the virtual environment, do the following:
(django-jay) PS C:\Python27\home\jtarlecki\djcode\django-jay\rebates> deactivate

Now you have a new project and app started contained within its own virtual environment. Time to start coding!