Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python 3 compatibility #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions djangodav/base/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from hashlib import md5
from mimetypes import guess_type

from django.utils.encoding import force_bytes
from django.utils.http import urlquote
from djangodav.utils import rfc3339_date, rfc1123_date, safe_join

Expand Down Expand Up @@ -191,8 +192,8 @@ def getetag(self):
file system. The etag is used to detect changes to a resource between HTTP calls. So this
needs to change if a resource is modified."""
hashsum = md5()
hashsum.update(self.displayname)
hashsum.update(str(self.creationdate))
hashsum.update(str(self.getlastmodified))
hashsum.update(str(self.getcontentlength))
hashsum.update(force_bytes(self.displayname))
hashsum.update(force_bytes(self.creationdate))
hashsum.update(force_bytes(self.getlastmodified))
hashsum.update(force_bytes(self.getcontentlength))
return hashsum.hexdigest()
4 changes: 2 additions & 2 deletions djangodav/fs/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ def move_object(self, destination):

class DummyReadFSDavResource(BaseFSDavResource):
def read(self):
with open(self.get_abs_path(), 'r') as f:
with open(self.get_abs_path(), 'rb') as f:
return f.read()


class DummyWriteFSDavResource(BaseFSDavResource):
def write(self, request):
with file(self.get_abs_path(), 'w') as dst:
with open(self.get_abs_path(), 'wb') as dst:
shutil.copyfileobj(request, dst)


Expand Down
3 changes: 2 additions & 1 deletion djangodav/locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# along with DjangoDav. If not, see <http://www.gnu.org/licenses/>.
from uuid import uuid4

from django.utils.encoding import force_text
from djangodav.base.locks import BaseLock


Expand All @@ -28,7 +29,7 @@ def get(self, *args, **kwargs):
pass

def acquire(self, *args, **kwargs):
return unicode(uuid4())
return force_text(uuid4())

def release(self, token):
return True
Expand Down
4 changes: 3 additions & 1 deletion djangodav/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import datetime, time, calendar
from wsgiref.handlers import format_date_time

from django.utils.encoding import force_text
from django.utils.feedgenerator import rfc2822_date

try:
Expand Down Expand Up @@ -61,7 +63,7 @@ def get_property_tag(res, name):
return D(name)
try:
if hasattr(res, name):
return D(name, unicode(getattr(res, name)))
return D(name, force_text(getattr(res, name)))
except AttributeError:
return

Expand Down
14 changes: 12 additions & 2 deletions djangodav/views/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import urllib, re

from django.utils.encoding import force_text

try:
import urlparse
except ImportError:
Expand All @@ -15,6 +18,7 @@
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from django.utils import six

from djangodav.responses import ResponseException, HttpResponsePreconditionFailed, HttpResponseCreated, HttpResponseNoContent, \
HttpResponseConflict, HttpResponseMediatypeNotSupported, HttpResponseBadGateway, \
Expand Down Expand Up @@ -247,7 +251,13 @@ def relocate(self, request, path, method, *args, **kwargs):
raise Http404("Resource doesn't exists")
if not self.has_access(self.resource, 'read'):
return self.no_access()
dst = urlparse.unquote(request.META.get('HTTP_DESTINATION', '')).decode(self.xml_encoding)
if six.PY2:
# in Python 2.6, urlparse requires bytestrings
dst = urlparse.unquote(request.META.get('HTTP_DESTINATION', '')).decode(self.xml_encoding)
else:
# in Python 3, urlparse understands string
dst = urlparse.unquote(request.META.get('HTTP_DESTINATION', ''))

if not dst:
return HttpResponseBadRequest('Destination header missing.')
dparts = urlparse.urlparse(dst)
Expand Down Expand Up @@ -339,7 +349,7 @@ def lock(self, request, path, xbody=None, *args, **kwargs):
body = D.activelock(*([
D.locktype(locktype_obj),
D.lockscope(lockscope_obj),
D.depth(unicode(depth)),
D.depth(force_text(depth)),
D.timeout("Second-%s" % timeout),
D.locktoken(D.href('opaquelocktoken:%s' % token))]
+ ([owner_obj] if owner_obj is not None else [])
Expand Down