Skip to content

Commit

Permalink
Merge 11c101a into adf43cb
Browse files Browse the repository at this point in the history
  • Loading branch information
roksys committed Feb 18, 2019
2 parents adf43cb + 11c101a commit b951ed6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
20 changes: 16 additions & 4 deletions invenio_files_rest/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@

import mimetypes
import re
import sys
import uuid
from datetime import datetime
from functools import wraps
from os.path import basename

import six
from flask import current_app
from future.utils import python_2_unicode_compatible
from invenio_db import db
from sqlalchemy.dialects import mysql
from sqlalchemy.ext.hybrid import hybrid_property
Expand Down Expand Up @@ -964,10 +966,20 @@ def validate_key(self, key, key_):
"""Validate key."""
return validate_key(key_)

def __repr__(self):
"""Return representation of location."""
return '{0}:{2}:{1}'.format(
self.bucket_id, self.key, self.version_id)
def __unicode__(self):
"""Return unicoded object."""
return u"{0}:{1}:{2}".format(
self.bucket_id, self.version_id, self.key)

# https://docs.python.org/3.3/howto/pyporting.html#str-unicode
if sys.version_info[0] >= 3: # Python 3
def __repr__(self):
"""Return representation of location."""
return self.__unicode__()
else: # Python 2
def __repr__(self):
"""Return representation of location."""
return self.__unicode__().encode('utf8')

@hybrid_property
def mimetype(self):
Expand Down
20 changes: 17 additions & 3 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from __future__ import absolute_import, print_function

import sys
import uuid
from os.path import getsize

Expand Down Expand Up @@ -200,12 +201,25 @@ def test_object_create(app, db, dummy_location):
# I.e. it is considered a delete marker.
obj3 = ObjectVersion.create(b, "deleted_obj")

# Create a new object containing key in unicode
obj4 = ObjectVersion.create(b, u"hellö")

# Object __repr__
assert str(obj1) == \
"{0}:{1}:{2}".format(obj1.bucket_id, obj1.version_id, obj1.key)
assert repr(obj1) == \
u"{0}:{1}:{2}".format(
obj1.bucket_id, obj1.version_id, obj1.key)

if sys.version_info[0] >= 3: # python3
assert repr(obj4) == \
u"{0}:{1}:{2}".format(
obj4.bucket_id, obj4.version_id, obj4.key)
else: # python2
assert repr(obj4) == \
u"{0}:{1}:{2}".format(
obj4.bucket_id, obj4.version_id, obj4.key).encode('utf-8')

# Sanity check
assert ObjectVersion.query.count() == 3
assert ObjectVersion.query.count() == 4

# Assert that obj2 is the head version
obj = ObjectVersion.get(b.id, "test", version_id=obj1.version_id)
Expand Down

0 comments on commit b951ed6

Please sign in to comment.