mirror of
https://github.com/kennethreitz-archive/python-convore.git
synced 2026-06-05 23:40:18 +00:00
UserList
This commit is contained in:
+37
-23
@@ -2,6 +2,8 @@ import requests
|
||||
import json
|
||||
import models
|
||||
|
||||
from UserList import UserList
|
||||
|
||||
API_URL = 'https://convore.com/api/'
|
||||
|
||||
def login(username, password):
|
||||
@@ -9,32 +11,44 @@ def login(username, password):
|
||||
requests.add_autoauth(API_URL, auth)
|
||||
|
||||
|
||||
class Groups(object):
|
||||
|
||||
class Groups(UserList):
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
def __iter__(self):
|
||||
r = requests.get(API_URL + 'groups.json')
|
||||
groups = json.loads(r.content)['groups']
|
||||
|
||||
for group in groups:
|
||||
_group = models.Group()
|
||||
_group.import_from_api(group)
|
||||
yield _group
|
||||
self.data = []
|
||||
self.sync()
|
||||
|
||||
|
||||
def __getitem__(self, key):
|
||||
r = requests.get(API_URL + 'groups/%s.json' % key)
|
||||
group = models.Group()
|
||||
_group = json.loads(r.content)['group']
|
||||
group.import_from_api(_group)
|
||||
|
||||
return _group
|
||||
if isinstance(key, int):
|
||||
key = unicode(key)
|
||||
|
||||
for group in self.data:
|
||||
if key in (group.id, group.slug):
|
||||
return group
|
||||
|
||||
def __contains__(self, key):
|
||||
|
||||
def iterkeys():
|
||||
return []
|
||||
|
||||
def iteritems():
|
||||
return
|
||||
if isinstance(key, int):
|
||||
key = unicode(key)
|
||||
|
||||
for group in self.data:
|
||||
if key in (group.id, group.slug):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def __iter__(self):
|
||||
for group in self.data:
|
||||
yield group
|
||||
|
||||
def sync(self):
|
||||
|
||||
self.data = []
|
||||
|
||||
r = requests.get(API_URL + 'groups.json')
|
||||
for _group in json.loads(r.content)['groups']:
|
||||
|
||||
group = models.Group()
|
||||
group.import_from_api(_group)
|
||||
self.data.append(group)
|
||||
|
||||
Reference in New Issue
Block a user