~mhall119/onehundredscopes/loco-team-portal

« back to all changes in this revision

Viewing changes to locodir.py

  • Committer: Michael Hall
  • Date: 2011-12-21 02:40:57 UTC
  • Revision ID: mhall119@ubuntu.com-20111221024057-mzv7rwg776kw5jk1
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import urllib
 
2
try:
 
3
    import json
 
4
except ImportError:
 
5
    import simplejson as json
 
6
 
 
7
SERVICE_ROOT = 'http://loco.ubuntu.com/services'
 
8
 
 
9
class LocoDirectory(object):
 
10
 
 
11
    def __init__(self, service_root=None):
 
12
        self.service_root = service_root or SERVICE_ROOT
 
13
        self.cache = {}
 
14
        
 
15
    def clearCache(self, resource=None):
 
16
        if resource is None:
 
17
            self.cache = {}
 
18
        elif self.cache.has_key(resource):
 
19
            self.cache[resource] = {}
 
20
        
 
21
    # Generic, caching Collection
 
22
    def getCollection(self, resource, id_field='id', **kargs):
 
23
        if not self.cache.has_key(resource):
 
24
            self.cache[resource] = {}
 
25
        url = '/'.join([self.service_root, resource, ''])
 
26
        if len(kargs) > 0:
 
27
            url = '?'.join([url, urllib.urlencode(kargs)])
 
28
        s = urllib.urlopen(url)
 
29
        col = dict([(o[id_field], o) for o in json.load(s)])
 
30
        self.cache[resource].update(col)
 
31
        return col
 
32
 
 
33
    # Generic, cacheable Entity
 
34
    def getEntity(self, resource, entity_id):
 
35
        if not self.cache.has_key(resource):
 
36
            self.cache[resource] = {}
 
37
        if not self.cache[resource].has_key(entity_id):
 
38
            url = '/'.join([self.service_root, resource, str(entity_id)])
 
39
            s = urllib.urlopen(url)
 
40
            self.cache[resource][entity_id] = json.load(s)
 
41
        return self.cache[resource][entity_id]
 
42
 
 
43
    # LoCo Teams        
 
44
    def getTeam(self, team_id):
 
45
        return self.getEntity('teams', team_id)
 
46
    def getTeams(self, **kargs):
 
47
        return self.getCollection('teams', **kargs)
 
48
 
 
49
    # Event Venues
 
50
    def getVenue(self, venue_id):
 
51
        return self.getEntity('venues', venue_id)
 
52
    def getVenues(self, **kargs):
 
53
        return self.getCollection('venues', **kargs)
 
54
 
 
55
    # Global Events
 
56
    def getGlobalEvent(self, gevent_id):
 
57
        return self.getEntity('global', gevent_id)
 
58
    def getGlobalEvents(self, **kargs):
 
59
        return self.getCollection('global', **kargs)
 
60
 
 
61
    # Team Events
 
62
    def getTeamEvent(self, tevent_id):
 
63
        return self.getEntity('events', tevent_id)
 
64
    def getTeamEvents(self, **kargs):
 
65
        return self.getCollection('events', **kargs)
 
66
 
 
67
    # Event Comments
 
68
    def getComment(self, comment_id):
 
69
        return self.getEntity('comments', comment_id)
 
70
    def getComments(self, **kargs):
 
71
        return self.getCollection('comments', **kargs)
 
72
 
 
73
    # Event Attendance
 
74
    def getAttendee(self, attendee_id):
 
75
        return self.getEntity('attendees', attendee_id)
 
76
    def getAttendees(self, **kargs):
 
77
        return self.getCollection('attendees', **kargs)
 
78
 
 
79
    # Team Meetings
 
80
    def getTeamMeeting(self, mevent_id):
 
81
        return self.getEntity('meeting', mevent_id)
 
82
    def getTeamMeetings(self, **kargs):
 
83
        return self.getCollection('meeting', **kargs)
 
84
 
 
85
    # LD Users
 
86
    def getUser(self, user_id):
 
87
        return self.getEntity('users', user_id)
 
88
    def getUsers(self, **kargs):
 
89
        return self.getCollection('users', **kargs)
 
90
 
 
91
    # LD Groups
 
92
    def getGroup(self, group_id):
 
93
        return self.getEntity('groups', group_id)
 
94
    def getGroups(self, **kargs):
 
95
        return self.getCollection('groups', **kargs)
 
96
 
 
97
    # LD Users
 
98
    def getUser(self, user_id):
 
99
        return self.getEntity('users', user_id)
 
100
    def getUsers(self, **kargs):
 
101
        return self.getCollection('users', **kargs)
 
102
 
 
103
    # Team Admins
 
104
    def getAdmin(self, admin_id):
 
105
        return self.getEntity('admins', admin_id)
 
106
    def getAdmins(self, **kargs):
 
107
        return self.getCollection('admins', **kargs)
 
108
 
 
109
    # Countries
 
110
    def getCountry(self, country_id):
 
111
        return self.getEntity('countries', country_id)
 
112
    def getCountries(self, **kargs):
 
113
        return self.getCollection('countries', **kargs)
 
114
 
 
115
    # Continents
 
116
    def getContinent(self, continent_id):
 
117
        return self.getEntity('continents', continent_id)
 
118
    def getContinents(self, **kargs):
 
119
        return self.getCollection('continents', **kargs)
 
120
 
 
121
    # Languages
 
122
    def getLanguage(self, lang_id):
 
123
        return self.getEntity('languages', lang_id)
 
124
    def getLanguages(self, **kargs):
 
125
        return self.getCollection('languages', **kargs)
 
126
 
 
127