Skip to content

Commit c1fb9fb

Browse files
authored
First stage of the application-level Python 2 to Python 3 conve… (#3285)
First stage of the application-level Python 2 to Python 3 conversion
2 parents f63cfb9 + ccef808 commit c1fb9fb

File tree

264 files changed

+737
-998
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

264 files changed

+737
-998
lines changed

opentreemap/api/auth.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
from __future__ import division
2+
53

64
import base64
75
import hashlib
86
import hmac
97
import re
10-
import urllib
8+
import urllib.request
9+
import urllib.parse
10+
import urllib.error
1111

1212
from django.http import HttpResponse
1313
from django.contrib.auth import authenticate
@@ -29,9 +29,9 @@ def get_signature_for_request(request, secret_key):
2929
# This used to use request.REQUEST, but after some testing and analysis it
3030
# seems that both iOS & Android always pass named parameters in the query
3131
# string, even for non-GET requests
32-
params = sorted(request.GET.iteritems(), key=lambda a: a[0])
32+
params = sorted(iter(request.GET.items()), key=lambda a: a[0])
3333

34-
paramstr = '&'.join(['%s=%s' % (k, urllib.quote_plus(str(v)))
34+
paramstr = '&'.join(['%s=%s' % (k, urllib.parse.quote_plus(str(v)))
3535
for (k, v) in params
3636
if k.lower() != "signature"])
3737

opentreemap/api/decorators.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
from __future__ import division
2+
53

64
import datetime
75

opentreemap/api/instance.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
from __future__ import division
2+
53

64
import json
75
import copy
@@ -53,13 +51,13 @@ def wrapper(request, *args, **kwargs):
5351
if request.api_version < 4:
5452
multichoice_fields = {
5553
field for field, info
56-
in instance_info_dict['fields'].iteritems()
54+
in instance_info_dict['fields'].items()
5755
if info['data_type'] == 'multichoice'}
5856

5957
# Remove multichoice fields from perms
6058
instance_info_dict['fields'] = {
6159
field: info for field, info
62-
in instance_info_dict['fields'].iteritems()
60+
in instance_info_dict['fields'].items()
6361
if field not in multichoice_fields}
6462

6563
# Remove multichoice fields from field groups
@@ -300,7 +298,7 @@ def public_instances(request):
300298

301299
def _contextify_instances(instances):
302300
""" Converts instances to context dictionary"""
303-
return map(_instance_info_dict, instances)
301+
return list(map(_instance_info_dict, instances))
304302

305303

306304
def _instance_info_dict(instance):

opentreemap/api/migrations/0001_initial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import unicode_literals
2+
33

44
from django.db import models, migrations
55

opentreemap/api/migrations/0002_apiaccesscredential_user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import unicode_literals
2+
33

44
from django.db import models, migrations
55
from django.conf import settings

opentreemap/api/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
from __future__ import division
2+
53

64
import uuid
75
import base64

opentreemap/api/plots.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
from __future__ import division
2+
53

64
import json
75
from functools import wraps
@@ -42,7 +40,7 @@ def plots_closest_to_point(request, instance, lat, lng):
4240
try:
4341
max_plots = int(request.GET.get('max_plots', '1'))
4442

45-
if max_plots not in xrange(1, 501):
43+
if max_plots not in range(1, 501):
4644
raise ValueError()
4745
except ValueError:
4846
raise HttpBadRequestException(
@@ -80,7 +78,7 @@ def update_or_create_plot(request, instance, plot_id=None):
8078

8179
for model in ["plot", "tree"]:
8280
if model in request_dict:
83-
for key, val in request_dict[model].iteritems():
81+
for key, val in request_dict[model].items():
8482
data["%s.%s" % (model, key)] = val
8583

8684
# We explicitly disallow setting a plot's tree id.

opentreemap/api/test_utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
from __future__ import division
2+
53

64
from django.contrib.gis.geos.collections import MultiPolygon
75
from django.contrib.gis.geos.polygon import Polygon

opentreemap/api/tests.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
from __future__ import division
52

6-
from StringIO import StringIO
3+
4+
from io import StringIO
75
from json import loads, dumps
8-
from urlparse import urlparse
6+
from urllib.parse import urlparse
97

10-
import urllib
8+
import urllib.request
9+
import urllib.parse
10+
import urllib.error
1111
import os
1212
import json
1313
import base64
@@ -84,9 +84,9 @@ def _get_path(parsed_url):
8484
"""
8585
# If there are parameters, add them
8686
if parsed_url[3]:
87-
return urllib.unquote(parsed_url[2] + ";" + parsed_url[3])
87+
return urllib.parse.unquote(parsed_url[2] + ";" + parsed_url[3])
8888
else:
89-
return urllib.unquote(parsed_url[2])
89+
return urllib.parse.unquote(parsed_url[2])
9090

9191

9292
def send_json_body(url, body_object, client, method, user=None):
@@ -794,7 +794,7 @@ def test_update_plot_with_pending(self):
794794
self.assertEqual(3, len(Audit.pending_audits()),
795795
"Expected 3 pends, one for each edited field")
796796

797-
self.assertEqual(3, len(response_json['pending_edits'].keys()),
797+
self.assertEqual(3, len(list(response_json['pending_edits'].keys())),
798798
"Expected the json response to have a "
799799
"pending_edits dict with 3 keys, one for each field")
800800

@@ -808,10 +808,10 @@ def test_invalid_field_returns_200_field_is_not_in_response(self):
808808

809809
self.assertEqual(200, response.status_code)
810810
response_json = loads(response.content)
811-
self.assertFalse("error" in response_json.keys(),
811+
self.assertFalse("error" in list(response_json.keys()),
812812
"Did not expect an error")
813813

814-
self.assertFalse("foo" in response_json.keys(),
814+
self.assertFalse("foo" in list(response_json.keys()),
815815
"Did not expect foo to be added to the plot")
816816

817817
def test_update_creates_tree(self):
@@ -924,7 +924,7 @@ def test_update_tree_with_pending(self):
924924
"Expected 1 pend record for the edited field.")
925925

926926
response_json = loads(response.content)
927-
self.assertEqual(1, len(response_json['pending_edits'].keys()),
927+
self.assertEqual(1, len(list(response_json['pending_edits'].keys())),
928928
"Expected the json response to have a"
929929
" pending_edits dict with 1 keys")
930930

@@ -1418,7 +1418,7 @@ def test_multichoice_fields_v4(self):
14181418

14191419
response = instance_info_endpoint(request, 4, self.instance.url_name)
14201420
info_dict = json.loads(response.content)
1421-
self.assertIn('plot.udf:multi', info_dict['fields'].keys())
1421+
self.assertIn('plot.udf:multi', list(info_dict['fields'].keys()))
14221422
self.assertTrue(any('plot.udf:multi' in group.get('field_keys', [])
14231423
for group in info_dict['field_key_groups']))
14241424

@@ -1428,7 +1428,7 @@ def test_multichoice_removed_in_v3(self):
14281428
response = instance_info_endpoint(request, 3, self.instance.url_name)
14291429
info_dict = json.loads(response.content)
14301430

1431-
self.assertNotIn('plot.udf:multi', info_dict['fields'].keys())
1431+
self.assertNotIn('plot.udf:multi', list(info_dict['fields'].keys()))
14321432
self.assertFalse(any('plot.udf:multi' in group.get('field_keys', [])
14331433
for group in info_dict['field_key_groups']))
14341434

@@ -1630,7 +1630,7 @@ def testUploadPhoto(self):
16301630
response = update_profile_photo_endpoint(req, LATEST_API,
16311631
str(peon.pk))
16321632

1633-
self.assertEquals(response.status_code, 200)
1633+
self.assertEqual(response.status_code, 200)
16341634

16351635
peon = User.objects.get(pk=peon.pk)
16361636
self.assertIsNotNone(peon.photo)
@@ -1657,15 +1657,15 @@ def testCanOnlyUploadAsSelf(self):
16571657
response = update_profile_photo_endpoint(req, LATEST_API,
16581658
str(grunt.pk))
16591659

1660-
self.assertEquals(response.status_code, 403)
1660+
self.assertEqual(response.status_code, 403)
16611661

16621662
def testCreateUser(self):
16631663
rslt = create_user(self.make_post_request(self.defaultUserDict))
16641664
pk = rslt['id']
16651665

16661666
user = User.objects.get(pk=pk)
16671667

1668-
for field, target_value in self.defaultUserDict.iteritems():
1668+
for field, target_value in self.defaultUserDict.items():
16691669
if field != 'password':
16701670
self.assertEqual(getattr(user, field), target_value)
16711671

@@ -1758,12 +1758,12 @@ def updatePeonRequest(d):
17581758
updatePeonRequest({'last_name': 'l1'})
17591759

17601760
peon = User.objects.get(pk=peon.pk)
1761-
self.assertEquals(peon.last_name, 'l1')
1761+
self.assertEqual(peon.last_name, 'l1')
17621762

17631763
updatePeonRequest({'last_name': 'l2'})
17641764

17651765
peon = User.objects.get(pk=peon.pk)
1766-
self.assertEquals(peon.last_name, 'l2')
1766+
self.assertEqual(peon.last_name, 'l2')
17671767

17681768
updatePeonRequest({'password': 'whateva'})
17691769

@@ -1785,12 +1785,12 @@ def updatePeonRequest(d):
17851785
updatePeonRequest({'lastname': 'l1'})
17861786

17871787
peon = User.objects.get(pk=peon.pk)
1788-
self.assertEquals(peon.last_name, 'l1')
1788+
self.assertEqual(peon.last_name, 'l1')
17891789

17901790
updatePeonRequest({'lastname': 'l2'})
17911791

17921792
peon = User.objects.get(pk=peon.pk)
1793-
self.assertEquals(peon.last_name, 'l2')
1793+
self.assertEqual(peon.last_name, 'l2')
17941794

17951795
def testCantRemoveRequiredFields(self):
17961796
peon = make_user(username='peon', password='pw')
@@ -1802,7 +1802,7 @@ def testCantRemoveRequiredFields(self):
18021802
resp = put_json(url, {'username': ''},
18031803
self.client, user=peon)
18041804

1805-
self.assertEquals(resp.status_code, 400)
1805+
self.assertEqual(resp.status_code, 400)
18061806

18071807
def testCanOnlyUpdateLoggedInUser(self):
18081808
peon = make_user(username='peon', password='pw')
@@ -1817,7 +1817,7 @@ def testCanOnlyUpdateLoggedInUser(self):
18171817
resp = put_json(url, {'password': 'whateva'},
18181818
self.client, user=grunt)
18191819

1820-
self.assertEquals(resp.status_code, 403)
1820+
self.assertEqual(resp.status_code, 403)
18211821

18221822

18231823
class SigningTest(OTMTestCase):
@@ -1876,7 +1876,7 @@ def testAwsExample(self):
18761876
sig = get_signature_for_request(
18771877
req, b'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY')
18781878

1879-
self.assertEquals(
1879+
self.assertEqual(
18801880
sig, 'i91nKc4PWAt0JJIdXwz9HxZCJDdiy6cf/Mj6vPxyYIs=')
18811881

18821882
def testTimestampVoidsSignature(self):
@@ -2035,7 +2035,7 @@ def test_user_has_rep(self):
20352035
ijim.save()
20362036

20372037
auth = base64.b64encode("jim:password")
2038-
withauth = dict(self.sign.items() +
2038+
withauth = dict(list(self.sign.items()) +
20392039
[("HTTP_AUTHORIZATION", "Basic %s" % auth)])
20402040

20412041
ret = self.client.get("%s/user" % API_PFX, **withauth)
@@ -2060,18 +2060,18 @@ def _test_requires_admin_access(self, endpoint_name):
20602060
iuser.save_with_user(iuser)
20612061

20622062
resp = get_signed(self.client, url, user=self.user1)
2063-
self.assertEquals(resp.status_code, 403)
2063+
self.assertEqual(resp.status_code, 403)
20642064

20652065
iuser.admin = True
20662066
iuser.save_with_user(self.user1)
20672067

20682068
resp = get_signed(self.client, url, user=self.user1)
2069-
self.assertEquals(resp.status_code, 200)
2069+
self.assertEqual(resp.status_code, 200)
20702070

20712071
iuser.delete_with_user(self.user1)
20722072

20732073
resp = get_signed(self.client, url, user=self.user1)
2074-
self.assertEquals(resp.status_code, 401)
2074+
self.assertEqual(resp.status_code, 401)
20752075

20762076
def test_csv_requires_admin(self):
20772077
self._test_requires_admin_access('users_csv')
@@ -2089,7 +2089,7 @@ def test_send_password_reset_email_url(self):
20892089
url = "%s/send-password-reset-email?email=%s"
20902090
response = post_json(url % (API_PFX, self.jim.email),
20912091
{}, self.client, None)
2092-
self.assertEquals(response.status_code, 200)
2092+
self.assertEqual(response.status_code, 200)
20932093

20942094

20952095
class SpeciesListTest(OTMTestCase):

opentreemap/api/urls.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
from __future__ import division
2+
53

64
from django.conf.urls import url
75

0 commit comments

Comments
 (0)