Merge lp:~mhall119/summit/import-live-schedule into lp:summit

Proposed by Michael Hall
Status: Merged
Approved by: Chris Johnston
Approved revision: 252
Merged at revision: 253
Proposed branch: lp:~mhall119/summit/import-live-schedule
Merge into: lp:summit
Diff against target: 240 lines (+236/-0)
1 file modified
summit/schedule/management/commands/import_live_data.py (+236/-0)
To merge this branch: bzr merge lp:~mhall119/summit/import-live-schedule
Reviewer Review Type Date Requested Status
Chris Johnston Approve
Review via email: mp+89563@code.launchpad.net

Commit message

Add management command to import summit schedule data.

Description of the change

Add management command to import summit schedule data. Importing users that go along with it will come later

To post a comment you must log in.
Revision history for this message
Chris Johnston (cjohnston) :
review: Approve
Revision history for this message
Chris Johnston (cjohnston) wrote :

Approving.. One question though that could come with the rest of the script:

some scripts don't require -s to define a summit.. iirc init-slots works with just initslots lcq1-12... Can we either make it required for all, or required for none?

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'summit/schedule/management/commands/import_live_data.py'
--- summit/schedule/management/commands/import_live_data.py 1970-01-01 00:00:00 +0000
+++ summit/schedule/management/commands/import_live_data.py 2012-01-22 02:48:26 +0000
@@ -0,0 +1,236 @@
1# Ubuntu Developer Summit web application
2# Copyright (C) 2008, 2009, 2010 Canonical Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as
6# published by the Free Software Foundation, either version 3 of the
7# License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17import sys
18import urllib2
19import json
20import datetime
21import re
22
23import urllib
24try:
25 import json
26except ImportError:
27 import simplejson as json
28
29from django.core.management.base import BaseCommand, CommandError
30from optparse import make_option
31
32from summit.schedule.models import (
33 Summit,
34 Slot,
35 Room,
36 Attendee,
37 Meeting,
38 Agenda,
39 Participant,
40 Track,
41 Crew,
42 Lead,
43)
44
45__all__ = (
46 'Command',
47)
48
49SERVICE_ROOT = 'http://summit.ubuntu.com/api'
50
51class Command(BaseCommand):
52 help = "Import data from the services API of the live site"
53 option_list = BaseCommand.option_list + (
54 make_option("-s", "--summit",
55 dest="summit",
56 help="Supply a remote summit to import."),
57 make_option("-u", "--url",
58 dest="service_root",
59 default='http://summit.ubuntu.com/api/',
60 help="Live instance service root."),
61 make_option("-A", "--all", dest="all", action="store_true",
62 help="Import all data, not just scheduling", default=False),
63 )
64
65
66 def handle(self, *args, **options):
67 summit_name = options["summit"]
68 if summit_name is None or summit_name == '':
69 print "You must supply a summit name to import. Run manage.py import_live_data --help for more info."
70 return
71
72 import_all = options["all"]
73 service = SummitImporter(options["service_root"])
74 print "Service root: %s" % service.service_root
75 print "Summit: %s" % summit_name
76
77 service.import_summit(summit_name)
78 print "Import complete."
79
80class SummitImporter(object):
81
82 def __init__(self, service_root=None):
83 self.service_root = service_root or SERVICE_ROOT
84 self.cache = {}
85 self.summit_map = {}
86 self.slot_map = {}
87 self.meeting_map = {}
88 self.room_map = {}
89 self.agenda_map = {}
90 self.track_map = {}
91
92 def clearCache(self, resource=None):
93 if resource is None:
94 self.cache = {}
95 elif self.cache.has_key(resource):
96 self.cache[resource] = {}
97
98 # Generic, caching Collection
99 def getCollection(self, resource, id_field='id', **kargs):
100 if not self.cache.has_key(resource):
101 self.cache[resource] = {}
102 url = '/'.join([self.service_root, resource, ''])
103 if len(kargs) > 0:
104 url = '?'.join([url, urllib.urlencode(kargs)])
105 s = urllib.urlopen(url)
106 col = dict([(o[id_field], o) for o in json.load(s)])
107 self.cache[resource].update(col)
108 return col
109
110 # Generic, cacheable Entity
111 def getEntity(self, resource, entity_id):
112 if not self.cache.has_key(resource):
113 self.cache[resource] = {}
114 if not self.cache[resource].has_key(entity_id):
115 url = '/'.join([self.service_root, resource, str(entity_id)])
116 s = urllib.urlopen(url)
117 self.cache[resource][entity_id] = json.load(s)
118 return self.cache[resource][entity_id]
119
120 def import_summit(self, summit_name):
121 try:
122 Summit.objects.get(name=summit_name).delete()
123 print "Deleting existing summit."
124 except Summit.DoesNotExist:
125 # If we don't already have a summit by that name, there's nothing to
126 # delete
127 pass
128
129 print "Importing summit..."
130 collection = self.getCollection('summit', name=summit_name)
131 data = collection.values()[0]
132 summit = Summit.objects.create(
133 name = data['name'],
134 title = data['title'],
135 date_start = datetime.datetime.strptime(data['date_start'], '%Y-%m-%d'),
136 date_end = datetime.datetime.strptime(data['date_end'], '%Y-%m-%d'),
137 state = data['state'],
138 location = data['location'],
139 timezone = data['timezone'],
140 )
141 self.summit_map[summit.id] = data['id']
142
143 self.import_slots(summit)
144 self.import_tracks(summit)
145 self.import_rooms(summit)
146 self.import_meetings(summit)
147 self.import_agenda(summit)
148
149 def import_tracks(self, summit):
150 print "Importing tracks..."
151 collection = self.getCollection('track', summit=self.summit_map[summit.id])
152 for track_id, data in collection.items():
153 #print "Track: %s" % data
154 track = Track.objects.create(
155 summit = summit,
156 title = data['title'],
157 slug = data['slug'],
158 color = data['color'],
159 allow_adjacent_sessions = data['allow_adjacent_sessions'],
160 )
161 self.track_map[track_id] = track.id
162
163 def import_slots(self, summit):
164 print "Importing slots..."
165 collection = self.getCollection('slot', summit=self.summit_map[summit.id])
166 for slot_id, data in collection.items():
167 #print "Slot: %s" % data
168 slot = Slot.objects.create(
169 summit = summit,
170 type = data['type'],
171 start_utc = datetime.datetime.strptime(data['start_utc'], '%Y-%m-%d %H:%M:%S'),
172 end_utc = datetime.datetime.strptime(data['end_utc'], '%Y-%m-%d %H:%M:%S'),
173 )
174 self.slot_map[slot_id] = slot.id
175
176 def import_rooms(self, summit):
177 print "Importing rooms..."
178 collection = self.getCollection('room', summit=self.summit_map[summit.id])
179 for room_id, data in collection.items():
180 #print "Room: %s" % data
181 room = Room.objects.create(
182 summit = summit,
183 name = data['name'],
184 title = data['title'],
185 icecast_url = data['icecast_url'],
186 type = data['type'],
187 size = data['size'],
188 has_dial_in = data['has_dial_in'],
189 start_utc = datetime.datetime.strptime(data['start_utc'] or '1970-01-01 00:00:00', '%Y-%m-%d %H:%M:%S'),
190 end_utc = datetime.datetime.strptime(data['end_utc'] or '1970-01-01 23:59:59', '%Y-%m-%d %H:%M:%S'),
191 )
192 for track_id in data['tracks']:
193 try:
194 room.tracks.add(self.track_map[track_id])
195 except KeyError, e:
196 print "Warning. Unknown track: %s" % track_id
197
198 self.room_map[room_id] = room.id
199
200 def import_meetings(self, summit):
201 print "Importing meetings..."
202 collection = self.getCollection('meeting', summit=self.summit_map[summit.id])
203 for meeting_id, data in collection.items():
204 #print "Meeting: %s" % data
205 meeting = Meeting.objects.create(
206 summit = summit,
207 name = data['name'],
208 title = data['title'],
209 description = data['description'],
210 wiki_url = data['wiki_url'],
211 spec_url = data['spec_url'],
212 pad_url = data['pad_url'],
213 priority = data['priority'],
214 status = data['status'],
215 slots = data['slots'],
216 )
217 for track_id in data['tracks']:
218 try:
219 meeting.tracks.add(self.track_map[track_id])
220 except KeyError, e:
221 print "Warning. Unknown track: %s" % track_id
222 self.meeting_map[meeting_id] = meeting.id
223
224 def import_agenda(self, summit):
225 print "Importing agenda..."
226 collection = self.getCollection('agenda', room__summit=self.summit_map[summit.id])
227 for agenda_id, data in collection.items():
228 #print "Agenda: %s" % data
229 agenda = Agenda.objects.create(
230 room_id = self.room_map[data['room']],
231 slot_id = self.slot_map[data['slot']],
232 meeting_id = self.meeting_map[data['meeting']],
233 auto = data['auto'],
234 )
235 self.agenda_map[agenda_id] = agenda.id
236

Subscribers

People subscribed via source and target branches