Basic API object creation. Auth still stubbed out.

This commit is contained in:
Michael DeHaan 2013-03-15 17:53:44 -04:00
parent 366fefe419
commit 91c8d122be
4 changed files with 80 additions and 6 deletions

31
lib/api/auth.py Normal file
View File

@ -0,0 +1,31 @@
from tastypie.authentication import Authentication
from tastypie.authorization import Authorization
# FIXME: this is completely stubbed out at this point!
class AcomAuthentication(Authentication):
def is_authenticated(self, request, **kwargs):
return True
#if 'admin' in request.user.username:
# return True
#return False
# Optional but recommended
def get_identifier(self, request):
return request.user.username
class AcomAuthorization(Authorization):
def is_authorized(self, request, object=None):
return True
#if request.user.username == 'admin':
# return True
#else:
# return False
# Optional but useful for advanced limiting, such as per user.
def apply_limits(self, request, object_list):
#if request and hasattr(request, 'user'):
# return object_list.filter(author__username=request.user.username)
#return object_list.none()
return object_list.all()

View File

@ -1,5 +1,8 @@
# myapp/api.py
from tastypie.resources import ModelResource
from lib.api.auth import AcomAuthentication, AcomAuthorization
import lib.main.models as models
class Organizations(ModelResource):
@ -7,4 +10,7 @@ class Organizations(ModelResource):
class Meta:
queryset = models.Organization.objects.all()
resource_name = 'organizations'
authentication = AcomAuthentication()
authorization = AcomAuthorization()

View File

@ -6,17 +6,30 @@ import json
# this is temporary
username = os.getenv("ACOM_USER","admin")
password = os.getenv("ACOM_PASS""admin")
password = os.getenv("ACOM_PASS","admin")
server = os.getenv("ACOM_SERVER","127.0.0.1:8000")
handle = hammock.Hammock("http://%s/api/v1" % server, auth=(username,password))
# TODO: error handling/output/etc
# TODO: format into actual command line
PARAMS = {
'format' : 'json',
}
HEADERS = {
'Content-Type' : 'application/json'
}
AUTH = (username, password)
handle = hammock.Hammock("http://%s/api/v1" % server, auth=AUTH, append_slash=True, params=PARAMS, headers=HEADERS)
class Collection(object):
def __init__(self, handle):
self.handle = handle
self.response = self.accessor().GET()
self.response = self.accessor().GET(auth=AUTH, headers=HEADERS)
assert self.response.status_code == 200
# TODO: error handling on non-200
print "RESPONSE=%s" % self.response.text
self.data = json.loads(self.response.text)
self.meta = self.data['meta']
self.objects = self.data['objects']
@ -26,6 +39,18 @@ class Collection(object):
def accessor(self):
return exceptions.NotImplementedError()
def add(self, data):
# TODO: error handling
json_data = json.dumps(data)
response = self.accessor().POST(data=json_data)
print response.status_code
print response.text
assert response.status_code == 201
# FIXME: error handling
data2 = response.text
print data2
return Entry(data)
def __iter__(self):
for x in self.objects:
yield Entry(x)
@ -34,8 +59,9 @@ class Entry(object):
def __init__(self, data):
self.data = data
self.resource_uri = data['resource_uri']
self.accessor = hammock.Hammock(self.resource_uri, auth=(username,password))
self.resource_uri = data.get('resource_uri', None)
print "LOADING"
self.accessor = hammock.Hammock(self.resource_uri, auth=AUTH, append_slash=True, params=PARAMS, headers=HEADERS)
def __repr__(self):
return repr(self.data)
@ -50,9 +76,20 @@ class Organizations(Collection):
#
try:
print "---"
orgs = Organizations(handle)
for x in orgs:
print x
print "---"
orgs.add(dict(description="new org?", name="new org"))
print "---"
print "---"
orgs = Organizations(handle)
for x in orgs:
print x
except requests.exceptions.ConnectionError:
print "connect failed"
sys.exit(1)

View File

@ -15,7 +15,7 @@ class CommonModel(models.Model):
name = models.CharField(max_length=512)
description = models.TextField()
creation_date = models.DateField()
creation_date = models.DateField(auto_now_add=True)
tags = models.ManyToManyField('Tag', related_name='%(class)s_tags', blank=True)
audit_trail = models.ManyToManyField('AuditTrail', related_name='%(class)s_audit_trails', blank=True)
active = models.BooleanField(default=True)