Skip to content

Commit

Permalink
Merge pull request #2 from appdotnet/fix-file-uploads
Browse files Browse the repository at this point in the history
Fix file uploads for alpha
  • Loading branch information
berg committed May 10, 2014
2 parents b261c6a + e03a0e2 commit e3841a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
6 changes: 2 additions & 4 deletions pau/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def __init__(self, user=None, app=None, scopes=None):
self.enabled_migrations = '&'.join('%s=1' % m for m in self.migrations)
self.access_token = None

def call_api(self, request, path, params=None, data=None, method='GET', post_type='json', headers=None):
def call_api(self, request, path, params=None, data=None, method='GET', post_type='json', headers=None, files=None):
headers = headers or {}
api_params = {
'include_annotations': '1',
Expand All @@ -218,12 +218,10 @@ def call_api(self, request, path, params=None, data=None, method='GET', post_typ
if post_type == 'json':
# adnpy will json.dumps in this case
api_method = request.omo_api.request_json
else:
data = json.dumps(data)

# TODO remove all the alpha specific exceptions and just use the adnpy ones
try:
response = api_method(method, path, params=api_params, data=data, headers=headers)
response = api_method(method, path, params=api_params, data=data, headers=headers, files=files)
except adnpy.errors.AdnBadRequestAPIException, e:
# 400
raise AlphaBadRequestAPIException(e.response)
Expand Down
13 changes: 11 additions & 2 deletions pau/views/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,35 @@
from pau import bridge

PASS_THROUGH_HEADERS = (
('CONTENT_TYPE', 'Content-Type'),
('HTTP_X_ADN_MIGRATION_OVERRIDES', 'X-Adn-Migration-Overrides'),
)


def ajax_api_proxy(request, path, *args, **kwargs):
data = request.POST
post_type = 'form_data' # default to form data unless the client sent us json

if request.META.get('CONTENT_TYPE', None) == 'application/json':

try:
data = json.loads(request.body)
post_type = 'json'
except:
pass

headers = {}
files = {}

for file_slug, f in request.FILES.iteritems():
files[file_slug] = (f.name, f, f.content_type)

for dj_header, header in PASS_THROUGH_HEADERS:
if dj_header in request.META:
headers[header] = request.META[dj_header]

path = '/' + path
resp = bridge.api.call_api(request, path, params=request.GET, data=data, method=request.method, headers=headers)

resp = bridge.api.call_api(request, path, params=request.GET, data=data, method=request.method, headers=headers,
post_type=post_type, files=files)

return HttpResponse(json.dumps(resp), content_type="application/json", status=resp['meta']['code'])

0 comments on commit e3841a9

Please sign in to comment.