Skip to content

Commit

Permalink
Merge pull request #149 from kelockhart/py3
Browse files Browse the repository at this point in the history
Made Python 3 compatible
  • Loading branch information
kelockhart authored Mar 3, 2021
2 parents ec88797 + 6b30185 commit 3be1793
Show file tree
Hide file tree
Showing 24 changed files with 101 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: python

python:
- "2.7"
- "3.8"

install:
- "pip install -r requirements.txt"
Expand Down
2 changes: 1 addition & 1 deletion alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_app_config(key):
app = application.create_app()

with app.app_context() as c:
print 'Getting actual config for', key, app.config.get(key)
print('Getting actual config for', key, app.config.get(key))
return app.config.get(key)

def run_migrations_online():
Expand Down
4 changes: 2 additions & 2 deletions biblib/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import logging.config

from werkzeug.serving import run_simple
from views import UserView, LibraryView, DocumentView, PermissionView, \
from .views import UserView, LibraryView, DocumentView, PermissionView, \
TransferView, ClassicView, TwoPointOhView, OperationsView
from flask_restful import Api
from flask_discoverer import Discoverer
from flask.ext.mail import Mail
from flask_mail import Mail
from adsmutils import ADSFlask

def create_app(**config):
Expand Down
2 changes: 1 addition & 1 deletion biblib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
client = lambda: Client(current_app.config)


class Client:
class Client(object):
"""
The Client class is a thin wrapper around requests; Use it as a centralized
place to set application specific parameters, such as the oauth2
Expand Down
6 changes: 3 additions & 3 deletions biblib/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(PROJECT_HOME)
from flask import current_app
from flask.ext.script import Manager, Command, Option
from flask.ext.migrate import Migrate, MigrateCommand
from models import Base, User, Permissions, Library
from flask_script import Manager, Command, Option
from flask_migrate import Migrate, MigrateCommand
from .models import Base, User, Permissions, Library
from biblib.app import create_app
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
Expand Down
2 changes: 1 addition & 1 deletion biblib/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def get_bibcodes(self):
"""
Returns the bibcodes of the library
"""
return self.bibcode.keys()
return list(self.bibcode.keys())

def add_bibcodes(self, bibcodes):
"""
Expand Down
4 changes: 2 additions & 2 deletions biblib/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
import json
from flask import current_app
from flask.ext.testing import TestCase
from flask_testing import TestCase
from biblib import app
from biblib.models import Base
from httpretty import HTTPretty
Expand Down Expand Up @@ -175,7 +175,7 @@ def request_callback(request, uri, headers):
canonical_bibcodes = self.kwargs.get('canonical_bibcode')
for i in range(len(canonical_bibcodes)):
docs.append({'bibcode': canonical_bibcodes[i]})
print docs
print(docs)
else:
docs = [{'bibcode': 'bibcode'} for i
in range(self.kwargs.get('number_of_bibcodes', 1))]
Expand Down
2 changes: 1 addition & 1 deletion biblib/tests/functional_tests/test_big_share_admin_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_big_share_admin(self):
url = url_for('documentview', library=library_id_dave)

libraries_removed = []
for i in range(number_of_documents/2):
for i in range(number_of_documents // 2):
# Remove documents
response = self.client.post(
url,
Expand Down
4 changes: 2 additions & 2 deletions biblib/tests/functional_tests/test_big_share_editor_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_big_share_editor(self):
url = url_for('documentview', library=library_id_dave)

libraries_removed = []
for i in range(number_of_documents/2):
for i in range(number_of_documents // 2):
# Remove documents
response = self.client.post(
url,
Expand All @@ -141,7 +141,7 @@ def test_big_share_editor(self):
headers=user_mary.headers
)
self.assertTrue(
len(response.json['documents']) == number_of_documents/2
len(response.json['documents']) == number_of_documents // 2
)

# Dave asks Mary to re-add the ones she removed because they were
Expand Down
9 changes: 5 additions & 4 deletions biblib/tests/functional_tests/test_returned_data_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ def test_returned_data_user_view_epic(self):
# Mary sees that the number of users of the library has increased by 1
url = url_for('userview')
with MockEmailService(user_mary, end_type='uid'):
response = self.client.get(
url,
headers=user_mary.headers
)
with MockEmailService(user_dave, end_type='uid'):
response = self.client.get(
url,
headers=user_mary.headers
)

library = response.json['libraries'][0]

Expand Down
6 changes: 3 additions & 3 deletions biblib/tests/stubdata/stub_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class UserFactory(factory.Factory):
"""
Factory for creating fake User models
"""
class Meta:
class Meta(object):
"""
Defines the model that describes this factory
"""
Expand All @@ -67,7 +67,7 @@ class LibraryFactory(factory.Factory):
Factory for creating fake Library models
"""

class Meta:
class Meta(object):
"""
Defines the model that describes this factory
"""
Expand Down Expand Up @@ -245,7 +245,7 @@ def get_bibcodes(self):
how the models treat libraries.
:return: list of bibcodes
"""
return self.bibcode.keys()
return list(self.bibcode.keys())

def create_user_view_post_data(self):
"""
Expand Down
44 changes: 24 additions & 20 deletions biblib/tests/unit_tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,11 @@ def test_user_retrieves_correct_library_content(self):

# Get the library created
with MockEmailService(stub_user_2, end_type='uid'):
libraries = self.user_view.get_libraries(
service_uid=user_other.id,
absolute_uid=user_other.absolute_uid
)
with MockEmailService(stub_user_1, end_type='uid'):
libraries = self.user_view.get_libraries(
service_uid=user_other.id,
absolute_uid=user_other.absolute_uid
)

self.assertTrue(len(libraries) == 2)

Expand Down Expand Up @@ -540,12 +541,13 @@ def test_returned_permissions_are_right(self):
permission=permission)
# Get the library created
with MockEmailService(stub_user_other, end_type='uid'):
libraries = self.user_view.get_libraries(
service_uid=user_other.id,
absolute_uid=user_other.absolute_uid
)
with MockEmailService(self.stub_user, end_type='uid'):
libraries = self.user_view.get_libraries(
service_uid=user_other.id,
absolute_uid=user_other.absolute_uid
)

self.assertEqual(permission.keys()[0], libraries[0]['permission'])
self.assertEqual(list(permission.keys())[0], libraries[0]['permission'])

def test_can_only_see_number_of_people_with_admin_or_owner(self):
"""
Expand Down Expand Up @@ -577,10 +579,11 @@ def test_can_only_see_number_of_people_with_admin_or_owner(self):
# Get the library created
# For user admin
with MockEmailService(self.stub_user_2, end_type='uid'):
libraries = self.user_view.get_libraries(
service_uid=user_admin.id,
absolute_uid=user_admin.absolute_uid
)[0]
with MockEmailService(self.stub_user_1, end_type='uid'):
libraries = self.user_view.get_libraries(
service_uid=user_admin.id,
absolute_uid=user_admin.absolute_uid
)[0]
self.assertTrue(libraries['num_users'] > 0)

# For user owner
Expand Down Expand Up @@ -834,11 +837,12 @@ def test_user_can_get_documents_from_library(self):
session.expunge(obj)

# Retrieve the bibcodes using the web services
response_library, meta_data = \
self.library_view.get_documents_from_library(
library_id=library.id,
service_uid=user.id
)
with MockEmailService(self.stub_user, end_type='uid'):
response_library, meta_data = \
self.library_view.get_documents_from_library(
library_id=library.id,
service_uid=user.id
)
self.assertEqual(library.bibcode, response_library.bibcode)

def test_user_retrieves_correct_library_content(self):
Expand Down Expand Up @@ -1516,7 +1520,7 @@ def test_user_can_add_to_library(self):
# Check that the document is in the library
library = session.query(Library).filter(Library.id == library_id).all()
for _lib in library:
self.assertIn(self.stub_library.bibcode.keys()[0], _lib.bibcode)
self.assertIn(list(self.stub_library.bibcode.keys())[0], _lib.bibcode)

# Add a different document to the library
number_added = self.document_view.add_document_to_library(
Expand All @@ -1528,7 +1532,7 @@ def test_user_can_add_to_library(self):
# Check that the document is in the library
library = session.query(Library).filter(Library.id == library_id).all()
for _lib in library:
self.assertIn(self.stub_library_2.bibcode.keys()[0], _lib.bibcode)
self.assertIn(list(self.stub_library_2.bibcode.keys())[0], _lib.bibcode)

def test_user_cannot_duplicate_same_document_in_library(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion biblib/tests/unit_tests/test_webservices.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ def test_library_union_many(self):
headers=stub_user.headers
)

self.assertEquals(response.status_code, 200)
self.assertEqual(response.status_code, 200)
self.assertIn('Union', response.json['description'])
self.assertIn('9 other libraries', response.json['description'])

Expand Down
16 changes: 8 additions & 8 deletions biblib/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
DEFAULT_LIBRARY_DESCRIPTION = 'My ADS library'
USER_ID_KEYWORD = 'X-Adsws-Uid'

from base_view import BaseView
from user_view import UserView
from library_view import LibraryView
from document_view import DocumentView
from permission_view import PermissionView
from transfer_view import TransferView
from classic_view import ClassicView, TwoPointOhView
from operations_view import OperationsView
from .base_view import BaseView
from .user_view import UserView
from .library_view import LibraryView
from .document_view import DocumentView
from .permission_view import PermissionView
from .transfer_view import TransferView
from .classic_view import ClassicView, TwoPointOhView
from .operations_view import OperationsView
7 changes: 4 additions & 3 deletions biblib/views/base_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
USER_ID_KEYWORD
from flask import request, current_app
from flask_restful import Resource
from flask.ext.mail import Message
from flask_mail import Message
from ..models import User, Library, Permissions
from ..client import client
from sqlalchemy.exc import IntegrityError
Expand All @@ -35,10 +35,11 @@ def helper_uuid_to_slug(library_uuid):
youtube-style-short-id-and-back
:param library_uuid: unique identifier for the library
:return: library_slug: base64 URL safe slug
:return: library_slug: base64 URL safe slug, string
"""
library_slug = base64.urlsafe_b64encode(library_uuid.bytes)
library_slug = library_slug.rstrip('=\n').replace('/', '_')
library_slug = library_slug.rstrip(b'=\n').replace(b'/', b'_')
library_slug = library_slug.decode('utf-8')
current_app.logger.info('Converted uuid: {0} to slug: {1}'
.format(library_uuid, library_slug))
return library_slug
Expand Down
4 changes: 2 additions & 2 deletions biblib/views/classic_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from ..utils import err
from ..models import User, Library, Permissions
from ..client import client
from base_view import BaseView
from .base_view import BaseView
from flask import current_app
from flask_discoverer import advertise
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound
from http_errors import MISSING_USERNAME_ERROR
from .http_errors import MISSING_USERNAME_ERROR
from sqlalchemy import Boolean


Expand Down
14 changes: 7 additions & 7 deletions biblib/views/document_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

from ..utils import err, get_post_data
from ..models import Library, Permissions
from base_view import BaseView
from .base_view import BaseView
from flask import request, current_app
from flask_discoverer import advertise
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import Boolean
from http_errors import MISSING_USERNAME_ERROR, DUPLICATE_LIBRARY_NAME_ERROR, \
from .http_errors import MISSING_USERNAME_ERROR, DUPLICATE_LIBRARY_NAME_ERROR, \
WRONG_TYPE_ERROR, NO_PERMISSION_ERROR, MISSING_LIBRARY_ERROR, BAD_LIBRARY_ID_ERROR
from ..biblib_exceptions import PermissionDeniedError

Expand Down Expand Up @@ -260,7 +260,7 @@ def post(self, library):
try:
data = get_post_data(
request,
types=dict(bibcode=list, action=unicode)
types=dict(bibcode=list, action=str)
)
except TypeError as error:
current_app.logger.error('Wrong type passed for POST: {0} [{1}]'
Expand Down Expand Up @@ -355,8 +355,8 @@ def put(self, library):
library_data = get_post_data(
request,
types=dict(
name=unicode,
description=unicode,
name=str,
description=str,
public=bool
)
)
Expand All @@ -365,8 +365,8 @@ def put(self, library):
.format(request.data, error))
return err(WRONG_TYPE_ERROR)

# Remove content that is empty
for key in library_data.keys():
# Remove content that is empty (note that the list() is necessary to create a copy, so pop will work)
for key in list(library_data.keys()):
if library_data[key] == ''.strip(' '):
current_app.logger.warning('Removing key: {0} as its empty.'
.format(key))
Expand Down
4 changes: 2 additions & 2 deletions biblib/views/library_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from ..utils import err
from ..models import User, Library, Permissions
from ..client import client
from base_view import BaseView
from .base_view import BaseView
from flask import request, current_app
from flask_discoverer import advertise
from sqlalchemy import Boolean
from http_errors import MISSING_USERNAME_ERROR, SOLR_RESPONSE_MISMATCH_ERROR, \
from .http_errors import MISSING_USERNAME_ERROR, SOLR_RESPONSE_MISMATCH_ERROR, \
MISSING_LIBRARY_ERROR, NO_PERMISSION_ERROR, BAD_LIBRARY_ID_ERROR


Expand Down
Loading

0 comments on commit 3be1793

Please sign in to comment.