diff --git a/lib/api/auth.py b/lib/api/auth.py new file mode 100644 index 0000000000..74d68a27ad --- /dev/null +++ b/lib/api/auth.py @@ -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() diff --git a/lib/api/resources/organizations.py b/lib/api/resources/organizations.py index ba4d75e4d3..cffec2f84d 100644 --- a/lib/api/resources/organizations.py +++ b/lib/api/resources/organizations.py @@ -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() + diff --git a/lib/cli/main.py b/lib/cli/main.py index feb4fb5fcd..bfbaf0dbf9 100644 --- a/lib/cli/main.py +++ b/lib/cli/main.py @@ -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) diff --git a/lib/main/models/__init__.py b/lib/main/models/__init__.py index 064532c3a4..769d09ed28 100644 --- a/lib/main/models/__init__.py +++ b/lib/main/models/__init__.py @@ -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)