~mhall119/onehundredscopes/loco-team-portal

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import urllib
try:
    import json
except ImportError:
    import simplejson as json

SERVICE_ROOT = 'http://loco.ubuntu.com/services'

class LocoDirectory(object):

    def __init__(self, service_root=None):
        self.service_root = service_root or SERVICE_ROOT
        self.cache = {}
        
    def clearCache(self, resource=None):
        if resource is None:
            self.cache = {}
        elif self.cache.has_key(resource):
            self.cache[resource] = {}
        
    # Generic, caching Collection
    def getCollection(self, resource, id_field='id', **kargs):
        if not self.cache.has_key(resource):
            self.cache[resource] = {}
        url = '/'.join([self.service_root, resource, ''])
        if len(kargs) > 0:
            url = '?'.join([url, urllib.urlencode(kargs)])
        s = urllib.urlopen(url)
        col = dict([(o[id_field], o) for o in json.load(s)])
        self.cache[resource].update(col)
        return col

    # Generic, cacheable Entity
    def getEntity(self, resource, entity_id):
        if not self.cache.has_key(resource):
            self.cache[resource] = {}
        if not self.cache[resource].has_key(entity_id):
            url = '/'.join([self.service_root, resource, str(entity_id)])
            s = urllib.urlopen(url)
            self.cache[resource][entity_id] = json.load(s)
        return self.cache[resource][entity_id]

    # LoCo Teams        
    def getTeam(self, team_id):
        return self.getEntity('teams', team_id)
    def getTeams(self, **kargs):
        return self.getCollection('teams', **kargs)

    # Event Venues
    def getVenue(self, venue_id):
        return self.getEntity('venues', venue_id)
    def getVenues(self, **kargs):
        return self.getCollection('venues', **kargs)

    # Global Events
    def getGlobalEvent(self, gevent_id):
        return self.getEntity('global', gevent_id)
    def getGlobalEvents(self, **kargs):
        return self.getCollection('global', **kargs)

    # Team Events
    def getTeamEvent(self, tevent_id):
        return self.getEntity('events', tevent_id)
    def getTeamEvents(self, **kargs):
        return self.getCollection('events', **kargs)

    # Event Comments
    def getComment(self, comment_id):
        return self.getEntity('comments', comment_id)
    def getComments(self, **kargs):
        return self.getCollection('comments', **kargs)

    # Event Attendance
    def getAttendee(self, attendee_id):
        return self.getEntity('attendees', attendee_id)
    def getAttendees(self, **kargs):
        return self.getCollection('attendees', **kargs)

    # Team Meetings
    def getTeamMeeting(self, mevent_id):
        return self.getEntity('meeting', mevent_id)
    def getTeamMeetings(self, **kargs):
        return self.getCollection('meeting', **kargs)

    # LD Users
    def getUser(self, user_id):
        return self.getEntity('users', user_id)
    def getUsers(self, **kargs):
        return self.getCollection('users', **kargs)

    # LD Groups
    def getGroup(self, group_id):
        return self.getEntity('groups', group_id)
    def getGroups(self, **kargs):
        return self.getCollection('groups', **kargs)

    # LD Users
    def getUser(self, user_id):
        return self.getEntity('users', user_id)
    def getUsers(self, **kargs):
        return self.getCollection('users', **kargs)

    # Team Admins
    def getAdmin(self, admin_id):
        return self.getEntity('admins', admin_id)
    def getAdmins(self, **kargs):
        return self.getCollection('admins', **kargs)

    # Countries
    def getCountry(self, country_id):
        return self.getEntity('countries', country_id)
    def getCountries(self, **kargs):
        return self.getCollection('countries', **kargs)

    # Continents
    def getContinent(self, continent_id):
        return self.getEntity('continents', continent_id)
    def getContinents(self, **kargs):
        return self.getCollection('continents', **kargs)

    # Languages
    def getLanguage(self, lang_id):
        return self.getEntity('languages', lang_id)
    def getLanguages(self, **kargs):
        return self.getCollection('languages', **kargs)