Files
2009-11-04 11:08:17 +01:00

75 lines
2.5 KiB
Python

from django.http import HttpResponse
from django.utils.datastructures import MultiValueDict
from django.core.files.uploadedfile import SimpleUploadedFile
class HttpResponseNotImplemented(HttpResponse):
status_code = 501
class HttpResponseUnauthorized(HttpResponse):
status_code = 401
def __init__(self, realm):
HttpResponse.__init__(self)
self['WWW-Authenticate'] = 'Basic realm="%s"' % realm
def parse_weird_post_data(raw_post_data):
"""For some reason Django can't parse the HTTP POST data
sent by ``distutils`` register/upload commands.
This parser should be able to so, and returns a
:class:`django.utils.datastructures.MultiValueDict`
as you would expect from a regular ``request.POST`` object.
"""
# If anyone knows any better way to do this, you're welcome
# to give me a solid beating. [askh@opera.com]
sep = raw_post_data.splitlines()[1]
items = raw_post_data.split(sep)
post_data = {}
files = {}
for part in filter(lambda e: not e.isspace(), items):
item = part.splitlines()
if len(item) < 2:
continue
header = item[1].replace("Content-Disposition: form-data; ", "")
kvpairs = header.split(";")
headers = {}
for kvpair in kvpairs:
if not kvpair:
continue
key, value = kvpair.split("=")
headers[key] = value.strip('"')
if "name" not in headers:
continue
content = part[len("\n".join(item[0:2]))+2:len(part)-1]
if "filename" in headers:
file = SimpleUploadedFile(headers["filename"], content,
content_type="application/gzip")
files["distribution"] = [file]
elif headers["name"] in post_data:
post_data[headers["name"]].append(content)
else:
# Distutils sends UNKNOWN for empty fields (e.g platform)
# [russell.sim@gmail.com]
if content == 'UNKNOWN':
post_data[headers["name"]] = [None]
else:
post_data[headers["name"]] = [content]
return MultiValueDict(post_data), MultiValueDict(files)
def login_basic_auth(request):
authentication = request.META.get("HTTP_AUTHORIZATION")
if not authentication:
return
(authmeth, auth) = authentication.split(' ', 1)
if authmeth.lower() != "basic":
return
auth = auth.strip().decode("base64")
username, password = auth.split(":", 1)
return authenticate(username=username, password=password)