Skip to content

Commit

Permalink
Fixed line indentations and removed one unused import
Browse files Browse the repository at this point in the history
  • Loading branch information
vkaravir committed Jan 7, 2013
1 parent 1d33434 commit 1d42616
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 46 deletions.
89 changes: 44 additions & 45 deletions oembed/views.py
Expand Up @@ -7,54 +7,53 @@


def get_oembed(request):
# according to spec, only GET should be supported..
if request.method != "GET":
return HttpResponse("%s not allowed" % request.method, status=405)
# .. and url is a required parameter
if "url" not in request.GET:
return HttpResponseBadRequest()
# check if the user is authenticated, if not respond with status 403
if not request.user.is_authenticated():
return HttpResponse('Unauthorized', status=401)

# parse the given url..
resource_url = urlparse(request.GET['url'])
# .. and make it "relative" by stripping scheme and domain parts ..
relative_url = urlunparse(('', '', resource_url.path, resource_url.params,
resource_url.query, resource_url.fragment))
# .. to be able to do a resolve to django URLs to get the exercise_id
exercise_id = resolve(relative_url).kwargs['exercise_id']

# render the original exercise view with oembed template, returns 404 if no such exercise
html = original_view_exercise(request, exercise_id, template="oembed/view_exercise.html").content

# prepare the data to be rendered
response_data = {
"version": "1.0",
"type": "rich",
"html": html,
"width": "800", # oembed requires width and height, A+ has no way of knowing them
"height": "600", # we'll use sensible defaults here
}

# default to JSON format
format = request.GET.get('format', 'json')
if format == 'json':
return _jsonresponse(request, response_data)
elif format == 'xml':
return _xmlresponse(request, response_data)
else:
# if not a known format, return HTTP Error 501 Not implemented (as per specs)
return HttpResponse("Unknown format: %s" % format, status=501)
# according to spec, only GET should be supported..
if request.method != "GET":
return HttpResponse("%s not allowed" % request.method, status=405)
# .. and url is a required parameter
if "url" not in request.GET:
return HttpResponseBadRequest()
# check if the user is authenticated, if not respond with status 403
if not request.user.is_authenticated():
return HttpResponse('Unauthorized', status=401)

# parse the given url..
resource_url = urlparse(request.GET['url'])
# .. and make it "relative" by stripping scheme and domain parts ..
relative_url = urlunparse(('', '', resource_url.path, resource_url.params,
resource_url.query, resource_url.fragment))
# .. to be able to do a resolve to django URLs to get the exercise_id
exercise_id = resolve(relative_url).kwargs['exercise_id']

# render the original exercise view with oembed template, returns 404 if no such exercise
html = original_view_exercise(request, exercise_id, template="oembed/view_exercise.html").content

# prepare the data to be rendered
response_data = {
"version": "1.0",
"type": "rich",
"html": html,
"width": "800", # oembed requires width and height, A+ has no way of knowing them
"height": "600", # we'll use sensible defaults here
}

# default to JSON format
format = request.GET.get('format', 'json')
if format == 'json':
return _jsonresponse(request, response_data)
elif format == 'xml':
return _xmlresponse(request, response_data)
else:
# if not a known format, return HTTP Error 501 Not implemented (as per specs)
return HttpResponse("Unknown format: %s" % format, status=501)


def _jsonresponse(request, data):
respjson = json.dumps(data)
if 'callback' in request.GET:
respjson = '%s(%s);' % (request.GET['callback'], respjson)
return HttpResponse(respjson, mimetype="application/json")
respjson = json.dumps(data)
if 'callback' in request.GET:
respjson = '%s(%s);' % (request.GET['callback'], respjson)
return HttpResponse(respjson, mimetype="application/json")


def _xmlresponse(request, data):
from django.template.defaultfilters import escape
return HttpResponse(render_to_string("oembed/xmlresponse.xml", {"data": data}), mimetype="text/xml")
return HttpResponse(render_to_string("oembed/xmlresponse.xml", {"data": data}), mimetype="text/xml")
2 changes: 1 addition & 1 deletion templates/oembed/xmlresponse.xml
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>{% for key, val in data.items %}
<{{key}}>{{val|force_escape}}</{{key}}>{% endfor %}
<{{key}}>{{val|force_escape}}</{{key}}>{% endfor %}
</oembed>

0 comments on commit 1d42616

Please sign in to comment.