Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f95f3916cf
Fetching contributors…

Cannot retrieve contributors at this time

file 93 lines (79 sloc) 2.675 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
datamunge.py
Creates users, sisters, and residences from data/sisters.csv and data/residences.csv files.
'''
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"

import csv
import re
from django.contrib.auth.models import User
from website.models import Sister, Residence, Major

def add_residences():
  reader = csv.reader(open('data/residences.csv', 'rb'), delimiter=';')
  for row in reader:
    residence = Residence(name=unicode(row[0], "utf-8"))
    residence.save()
    print 'added residence to database:', residence

def add_majors():
  reader = csv.reader(open('data/majors.csv', 'rb'), delimiter=';')
  for row in reader:
    major = Major(number=row[0], description=row[1])
    major.save()
    print 'added major to database:', major

class Data:
  ID=0
  FIRST_NAME=1
  LAST_NAME=2
  CLASS_YEAR=3
  RESIDENCE=5
  USERNAME=6
  PHONE_NUMBER=7
  STATUS=8
  HOMETOWN=10
  PHOTO_URL=11
  INTERESTS=14
  BIO=18

def add_sisters():
  reader = csv.reader(open('data/sisters.csv', 'rb'), delimiter=';')
  for row in reader:
    try:
      user = User.objects.get(username=row[Data.USERNAME])
      print 'got user from database:', user
    except:
      user = User(username=row[Data.USERNAME],
                  email=row[Data.USERNAME]+'@mit.edu',
                  first_name=row[Data.FIRST_NAME],
                  last_name=row[Data.LAST_NAME])
      user.save()
      print 'added user to database:', user

    # assume only Active or Alum sisters
    status = 0 if row[Data.STATUS] == 'Active' else 1

    # clean up phone numbers and get rid of invalid phone numbers
    raw_phone_number = re.sub(r'\D', '', row[Data.PHONE_NUMBER])
    phone_number = raw_phone_number if len(raw_phone_number) == 10 else ''

    hometown = row[Data.HOMETOWN]
    if hometown == 'NULL' or hometown == '0': hometown = ''

    interests = row[Data.INTERESTS]
    if interests == 'NULL' or interests == '0': interests = ''

    bio = row[Data.BIO]
    if bio == 'NULL' or bio == '0': bio = ''

    sister = Sister(user=user,
                    class_year=row[Data.CLASS_YEAR],
                    status=status,
                    phone_number=phone_number,
                    hometown=hometown,
                    photo_url=row[Data.PHOTO_URL],
                    interests=interests,
                    bio=bio)

    residence_name = row[Data.RESIDENCE]
    try:
      sister.residence = Residence.objects.get(name=residence_name)
    except:
      print sister, 'has an invalid residence:', residence_name

    sister.save()
    print 'added sister to database:', sister

#add_residences()
add_majors()
#add_sisters()
Something went wrong with that request. Please try again.