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

Subscribers

People subscribed via source and target branches