Automatic handling of serialization format.
authorAntony Chazapis <chazapis@gmail.com>
Wed, 20 Apr 2011 11:18:27 +0000 (14:18 +0300)
committerAntony Chazapis <chazapis@gmail.com>
Wed, 20 Apr 2011 11:18:27 +0000 (14:18 +0300)
api/functions.py
api/models.py
api/util.py

index 71e65d7..15ee94d 100644 (file)
@@ -31,8 +31,6 @@ def authenticate(request):
        #       raise Unauthorized()
        
        response = HttpResponse(status = 204)
-       # TODO: Automate the Content-Type reply.
-       response['Content-Type'] = 'text/plain; charset=UTF-8'
        response['X-Auth-Token'] = 'eaaafd18-0fed-4b3a-81b4-663c99ec1cbb'
        # TODO: Do we support redirections?
        #response['X-Storage-Url'] = 'https://storage.grnet.gr/pithos/v1.0/<some reference>'
@@ -66,6 +64,8 @@ def object_demux(request, v_account, v_container, v_object):
                return object_read(request, v_account, v_container, v_object)
        elif request.method == 'PUT':
                return object_write(request, v_account, v_container, v_object)
+       elif request.method == 'POST':
+               return object_update(request, v_account, v_container, v_object)
        elif request.method == 'DELETE':
                return object_delete(request, v_account, v_container, v_object)
        else:
@@ -75,7 +75,7 @@ def object_demux(request, v_account, v_container, v_object):
 def account_meta(request, v_account):
        return HttpResponse("account_meta: %s" % v_account)
 
-@api_method('GET')
+@api_method('GET', format_allowed = True)
 def container_list(request, v_account):
        return HttpResponse("container_list: %s" % v_account)
 
@@ -107,6 +107,10 @@ def object_read(request, v_account, v_container, v_object):
 def object_write(request, v_account, v_container, v_object):
        return HttpResponse("object_write: %s %s %s" % (v_account, v_container, v_object))
 
+@api_method('POST')
+def object_update(request, v_account, v_container, v_object):
+       return HttpResponse("object_update: %s %s %s" % (v_account, v_container, v_object))
+
 @api_method('DELETE')
 def object_delete(request, v_account, v_container, v_object):
        return HttpResponse("object_delete: %s %s %s" % (v_account, v_container, v_object))
index ef8c58a..322e8b5 100755 (executable)
@@ -29,4 +29,6 @@ class Object(models.Model):
 class Metadata(models.Model):\r
     object = models.ForeignKey(Object)\r
     name = models.CharField(max_length = 256)\r
-    value = models.CharField(max_length = 1024)
\ No newline at end of file
+    value = models.CharField(max_length = 1024)\r
+    date_created = models.DateTimeField(auto_now_add = True)\r
+    date_modified = models.DateTimeField(auto_now = True)
\ No newline at end of file
index 9ca49ba..4053c40 100644 (file)
@@ -125,15 +125,13 @@ import logging
 #         raise BadRequest('Unsupported Content-Type.')
 
 def update_response_headers(request, response):
-#     if request.serialization == 'xml':
-#         response['Content-Type'] = 'application/xml'
-#     elif request.serialization == 'atom':
-#         response['Content-Type'] = 'application/atom+xml'
-#     else:
-#         response['Content-Type'] = 'application/json'
-#     
-    response['Content-Type'] = 'text/plain; charset=UTF-8'
-    response['Server'] = 'GRNET Pithos v.0.1'
+    if request.serialization == 'xml':
+        response['Content-Type'] = 'application/xml; charset=UTF-8'
+    elif request.serialization == 'json':
+        response['Content-Type'] = 'application/json; charset=UTF-8'
+    else:
+        response['Content-Type'] = 'text/plain; charset=UTF-8'
+
     if settings.TEST:
         response['Date'] = format_date_time(time())
 
@@ -167,40 +165,38 @@ def render_fault(request, fault):
     update_response_headers(request, resp)
     return resp
 
-# def request_serialization(request, atom_allowed=False):
-#     """Return the serialization format requested.
-#        
-#        Valid formats are 'json', 'xml' and 'atom' if `atom_allowed` is True.
-#     """
-#     
-#     path = request.path
-#     
-#     if path.endswith('.json'):
-#         return 'json'
-#     elif path.endswith('.xml'):
-#         return 'xml'
-#     elif atom_allowed and path.endswith('.atom'):
-#         return 'atom'
-#     
+def request_serialization(request, format_allowed=False):
+    """Return the serialization format requested.
+       
+       Valid formats are 'text' and 'json', 'xml' if `format_allowed` is True.
+    """
+    
+    if not format_allowed:
+        return 'text'
+    
+    format = request.GET.get('format')
+    if format == 'json':
+        return 'json'
+    elif format == 'xml':
+        return 'xml'
+    
 #     for item in request.META.get('HTTP_ACCEPT', '').split(','):
 #         accept, sep, rest = item.strip().partition(';')
 #         if accept == 'application/json':
 #             return 'json'
 #         elif accept == 'application/xml':
 #             return 'xml'
-#         elif atom_allowed and accept == 'application/atom+xml':
-#             return 'atom'
-#     
-#     return 'json'
+    
+    return 'text'
 
-def api_method(http_method=None, atom_allowed=False):
+def api_method(http_method = None, format_allowed = False):
     """Decorator function for views that implement an API method."""
     
     def decorator(func):
         @wraps(func)
         def wrapper(request, *args, **kwargs):
             try:
-                #request.serialization = request_serialization(request, atom_allowed)
+                request.serialization = request_serialization(request, format_allowed)
                 if http_method and request.method != http_method:
                     raise BadRequest('Method not allowed.')