Skip to content

Instantly share code, notes, and snippets.

@PeterJohnson
Created September 8, 2011 04:25
Show Gist options
  • Save PeterJohnson/1202609 to your computer and use it in GitHub Desktop.
Save PeterJohnson/1202609 to your computer and use it in GitHub Desktop.
roster v1 to v2 database conversion
#!/usr/bin/env python
import os
import json
if __name__ == "__main__":
import sys
print "Importing json..."
data = json.load(open(sys.argv[1]))
print "Mapping person..."
people = {}
teams = {}
programs = {}
for row in data:
if row["model"] == "roster.person":
people[row["pk"]] = row["fields"]
for key in ["school", "grad_year", "company",
"badge", "position",
"birth_month", "birth_day", "birth_year"]:
row["fields"][key] = None
for key in ["prospective_source", "medications",
"medical", "comments", "shirt_size",
"status"]:
row["fields"][key] = ""
elif row["model"] == "roster.team":
teams[row["pk"]] = row["fields"]["name"]
programs.setdefault(row["fields"]["program"], 1000)
if row["pk"] < row["fields"]["program"]:
row["fields"]["program"] = row["pk"]
person_team = {}
print "Merging member..."
for row in data:
if row["model"] == "roster.member":
for key in ["prospective_source", "medications",
"position", "medical", "comments",
"shirt_size", "birth_month", "birth_day",
"birth_year", "badge", "status"]:
people[row["pk"]][key] = row["fields"][key]
if not row["fields"]["teams"] and row["fields"]["joined"]:
print "No team:", people[row["pk"]]["firstname"], people[row["pk"]]["lastname"]
for team in row["fields"]["teams"]:
pt = person_team.setdefault((row["pk"], team), dict(role=None, status=None, joined=None, left=None))
if row["fields"]["status"] == "Too Young":
pt["status"] = "Prospective"
else:
pt["status"] = row["fields"]["status"]
pt["joined"] = row["fields"]["joined"]
pt["left"] = row["fields"]["left"]
print "Merging student, adult..."
for row in data:
if row["model"] == "roster.student":
for key in ["school", "grad_year"]:
people[row["pk"]][key] = row["fields"][key]
for team in row["fields"]["teams"]:
pt = person_team[(row["pk"], team)]
pt["role"] = "Student"
elif row["model"] == "roster.adult":
for key in ["company"]:
people[row["pk"]][key] = row["fields"][key]
if row["fields"]["mentor"]:
for team in row["fields"]["teams"]:
pt = person_team[(row["pk"], team)]
pt["role"] = "Mentor"
else:
for team in row["fields"]["teams"]:
print "Parent,%s,%s,%s,%s" % (people[row["pk"]]["firstname"], people[row["pk"]]["lastname"], people[row["pk"]]["status"], teams[team])
print "Merging waitlistentry..."
for row in data:
if row["model"] == "roster.waitlistentry":
if row["fields"]["team"]:
pt = person_team.setdefault((row["fields"]["student"], row["fields"]["team"]), dict(role="Student", status="Prospective", joined=None, left=None))
if pt["joined"]:
print "Joined:", people[row["fields"]["student"]]["firstname"], people[row["fields"]["student"]]["lastname"]
else:
pt["joined"] = row["fields"]["date"]
else:
found = False
for team in teams.keys():
if (row["fields"]["student"], team) in person_team:
found = True
pt = person_team[(row["fields"]["student"], team)]
if pt["joined"]:
print "Joined:", people[row["fields"]["student"]]["firstname"], people[row["fields"]["student"]]["lastname"]
else:
pt["joined"] = row["fields"]["date"]
break
if not found:
person_team[(row["fields"]["student"], programs[row["fields"]["program"]])] = dict(role="Student", status="Prospective", joined=row["fields"]["date"], left=None)
print "Deleting member, student, adult, contact, waitlistentry..."
data = [x for x in data if x["model"] not in
("roster.member", "roster.student", "roster.adult",
"roster.contact", "roster.waitlistentry")]
print "Cleaning up person..."
for person in people.itervalues():
del person["status"]
print "Creating personteam..."
for pk, (key, fields) in enumerate(sorted(person_team.iteritems())):
if not fields["role"]:
continue
data.append(dict(pk=pk+1, model="roster.personteam",
fields=dict(person=key[0], team=key[1],
**fields)))
print "Writing json..."
json.dump(data, open(sys.argv[2], "wt"), indent=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment