add support for specifying the contacts group the get

This commit is contained in:
mixedpuppy 2010-10-14 14:02:16 -07:00
Родитель d68bb3dda1
Коммит 3e02d6cf1f
2 изменённых файлов: 38 добавлений и 5 удалений

Просмотреть файл

@ -59,6 +59,9 @@ Start index used for paging results.
"""),
api_arg('maxresults', 'integer', False, 25, None, """
Max results to be returned per request, used with startindex for paging.
"""),
api_arg('group', 'string', False, 'Contacts', None, """
Name of the group to return.
"""),
],
response={'type': 'object', 'doc': 'Portable Contacts Collection'}
@ -66,6 +69,7 @@ Max results to be returned per request, used with startindex for paging.
def get(self, domain):
username = request.params.get('username')
userid = request.params.get('userid')
group = request.params.get('group', None)
startIndex = int(request.params.get('startindex','0'))
maxResults = int(request.params.get('maxresults','25'))
keys = session.get('account_keys', '').split(',')
@ -92,6 +96,6 @@ Max results to be returned per request, used with startindex for paging.
}
return {'result': None, 'error': error}
result, error = provider.api(acct).getcontacts(startIndex, maxResults)
result, error = provider.api(acct).getcontacts(startIndex, maxResults, group)
return {'result': result, 'error': error}

Просмотреть файл

@ -184,18 +184,47 @@ Subject: %s
result = {"status": "message sent"}
return result, error
def getcontacts(self, start=0, page=25):
def getgroup_id(self, group):
url = 'http://www.google.com/m8/feeds/groups/default/full?v=2'
method = 'GET'
client = oauth.Client(self.consumer, self.oauth_token)
resp, content = client.request(url, method)
feed = gdata.contacts.GroupsFeedFromString(content)
for entry in feed.entry:
this_group = entry.content.text
if this_group.startswith('System Group: '):
this_group = this_group[14:]
if group == this_group:
return entry.id.text
def getcontacts(self, start=0, page=25, group=None):
contacts = []
url = 'http://www.google.com/m8/feeds/contacts/default/full?max-results=%d' % (page,)
url = 'http://www.google.com/m8/feeds/contacts/default/full?v=1&max-results=%d' % (page,)
method = 'GET'
if start > 0:
url = url + "&start-index=%d" % (start,)
if group:
gid = self.getgroup_id(group)
if not gid:
error={"provider": domain,
"message": "Group '%s' not available" % group,
}
return None, error
url = url + "&group=%s" % (gid,)
# itemsPerPage, startIndex, totalResults
client = oauth.Client(self.consumer, self.oauth_token)
resp, content = client.request(url, method)
if int(resp.status) != 200:
error={"provider": domain,
"message": content,
"status": int(resp.status)
}
return None, error
feed = gdata.contacts.ContactsFeedFromString(content)
for entry in feed.entry:
#print entry.group_membership_info
email = entry.email[0]
p = {
'displayName': entry.title.text or email.address,
@ -204,8 +233,8 @@ Subject: %s
contacts.append(p)
result = {
'entry': contacts,
'totalResults': feed.total_results.text,
'itemsPerPage': feed.items_per_page.text,
'startIndex': feed.start_index.text
'startIndex': feed.start_index.text,
'totalResults': feed.total_results.text,
}
return result, None