Skip to content

Commit

Permalink
Merge pull request #421 from ExCiteS/py3_upgrade
Browse files Browse the repository at this point in the history
Python 3 upgrade
  • Loading branch information
JamesBradbury committed Nov 19, 2018
2 parents d650e11 + bcade26 commit 70819cf
Show file tree
Hide file tree
Showing 57 changed files with 136 additions and 202 deletions.
9 changes: 4 additions & 5 deletions .travis.yml
Expand Up @@ -5,7 +5,8 @@ services:
- postgresql

python:
- '2.7'
- '2.7_with_system_site_packages'
- '3.6'

addons:
postgresql: '9.4'
Expand All @@ -16,13 +17,10 @@ addons:
env:
- DJANGO='>=1.8.19,<1.12'

virtualenv:
system_site_packages: true

install:
- sudo -E apt-get -yq update &>> ~/apt-get-update.log
- sudo apt-get install binutils libav-tools
- sudo apt-get -yq install libgdal-dev python-gdal
- sudo apt-get -yq install libgdal-dev
- gdal-config --version
- export C_INCLUDE_PATH=/usr/include/gdal
- export CPLUS_INCLUDE_PATH=/usr/include/gdal
Expand Down Expand Up @@ -56,3 +54,4 @@ deploy:
secure: EPsnf69HqWA8nT9ncgVuyhIJGZnR3Nrg8NUEzG4t1B1CTJfmwODC0Fb8Hybq25/0y6Fq3mBWE482xhscVHYvNh/7UnehU+y2riIj5iP+VYrfEuLzBN6ZkjYXOezafq6pzotwsj6JCDWKGmBE6Jy++FDsOFSzLK/R2p3HqnrnRpc=
on:
tags: true
python: '3.6'
1 change: 1 addition & 0 deletions Dockerfile
Expand Up @@ -13,6 +13,7 @@ ADD /geokey/local_settings /app/local_settings
ADD /geokey /app

WORKDIR /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN pip install -r requirements-dev.txt
RUN pip install -e /app
Expand Down
8 changes: 4 additions & 4 deletions README.rst
Expand Up @@ -65,7 +65,7 @@ In order for Grunt to compile the Handlebars templates within the container, in
Install for development
=======================

GeoKey can be run on Python 2.7 only.
GeoKey can be run on Python 2.7 or Python 3 (Geokey 1.8 onwards).

1. Update your system:

Expand Down Expand Up @@ -192,7 +192,7 @@ You may need to add *sudo* before the pip commands, unless you are logged in as
4. Inside the *local_settings* open *settings.py* in a text editor and...

Add your `database settings <https://docs.djangoproject.com/en/1.8/ref/settings/#databases>`_:
Add your `database settings <https://docs.djangoproject.com/en/1.11/ref/settings/#databases>`_:

.. code-block:: python
Expand All @@ -207,14 +207,14 @@ Add your `database settings <https://docs.djangoproject.com/en/1.8/ref/settings/
}
}
Set the `secret key <https://docs.djangoproject.com/en/1.8/ref/settings/#std:setting-SECRET_KEY>`_:
Set the `secret key <https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-SECRET_KEY>`_:

.. code-block:: python
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
Set the `STATIC_ROOT directory <https://docs.djangoproject.com/en/1.8/howto/static-files/#deployment>`_:
Set the `STATIC_ROOT directory <https://docs.djangoproject.com/en/1.11/howto/static-files/#deployment>`_:

.. code-block:: python
Expand Down
18 changes: 11 additions & 7 deletions geokey/categories/models.py
Expand Up @@ -16,6 +16,13 @@

from .managers import CategoryManager, FieldManager, LookupValueManager
from .base import STATUS, DEFAULT_STATUS
from six import PY2
if PY2:
STR_TYPES = (str, unicode)
NUM_TYPES = (int, long, float, complex)
else:
STR_TYPES = (str, bytes)
NUM_TYPES = (int, float, complex)


class Category(models.Model):
Expand Down Expand Up @@ -334,9 +341,6 @@ def validate_required(self, value):
-----
Raises InputError if no value is provided
"""
if isinstance(value, str) or isinstance(value, unicode):
value = value.encode('utf-8')

if self.status == STATUS.active and self.required and (
value is None or len(str(value)) == 0):
raise InputError('The field %s is required.' % self.name)
Expand Down Expand Up @@ -417,13 +421,13 @@ def validate_input(self, value):
-----
Raises InputError if an invalid value is provided
"""
if isinstance(value, (str, unicode)) and len(value) == 0:
if isinstance(value, STR_TYPES) and len(value) == 0:
value = None

self.validate_required(value)

if value is not None:
if isinstance(value, (str, unicode)):
if isinstance(value, STR_TYPES):
try:
value = float(value) if '.' in value else int(value)
except ValueError:
Expand All @@ -432,7 +436,7 @@ def validate_input(self, value):
self.name
)

if isinstance(value, (int, long, float, complex)):
if isinstance(value, NUM_TYPES):
if self.minval and self.maxval and (
not (value >= self.minval) and (value <= self.maxval)):
raise InputError('The value provided for field %s must be '
Expand Down Expand Up @@ -892,7 +896,7 @@ def validate_input(self, provided_vals):
valid = True

if provided_vals is not None:
if isinstance(provided_vals, (str, unicode)):
if isinstance(provided_vals, STR_TYPES):
provided_vals = json.loads(provided_vals)

accepted_values = [value.id for value in self.lookupvalues.all()]
Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/managers.py
Expand Up @@ -593,7 +593,7 @@ def create(self, the_file=None, **kwargs):
)

# Using error because output file is not specified
if not video_stream.search(error):
if not video_stream.search(error.decode()):
content_type[0] = 'audio'

converted_file = os.path.join(
Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0001_initial.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import models, migrations
try:
Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0002_auto_20150106_1338.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import models, migrations
from django.conf import settings
Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0003_auto_20150121_1544.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import models, migrations

Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0004_auto_20150121_1455.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import migrations

Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0005_auto_20150202_1135.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import migrations

Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0006_auto_20150312_1247.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import migrations
try:
Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0007_auto_20150312_1249.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import migrations

Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0008_auto_20150312_1508.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import models, migrations

Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0009_auto_20150420_1549.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import models, migrations
import django.db.models.deletion
Expand Down
8 changes: 4 additions & 4 deletions geokey/contributions/migrations/0010_auto_20150511_1132.py
@@ -1,26 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


import json
from django.db import migrations


def clean_list(val):
if val is not None and (isinstance(val, str) or isinstance(val, unicode)):
if val is not None and (isinstance(val, str) or isinstance(val, str)):
return json.loads(val)

return val


def clean_int(val):
if val is not None and (isinstance(val, str) or isinstance(val, unicode)):
if val is not None and (isinstance(val, str) or isinstance(val, str)):
return int(val)

return val


def clean_number(val):
if val is not None and (isinstance(val, str) or isinstance(val, unicode)):
if val is not None and (isinstance(val, str) or isinstance(val, str)):
try: # it's an int
return int(val)
except ValueError:
Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0011_auto_20150527_1255.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import models, migrations

Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0012_auto_20150807_0854.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import models, migrations

Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0013_auto_20150907_1345.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import models, migrations
import django.db.models.deletion
Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0014_auto_20150907_1345.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import models, migrations
import re
Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0015_auto_20150907_1345.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import models, migrations

Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0016_audiofile.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import models, migrations

Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0017_auto_20160531_1434.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


from django.db import migrations, models

Expand Down
2 changes: 1 addition & 1 deletion geokey/contributions/migrations/0018_historicalcomment.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2017-02-24 15:49
from __future__ import unicode_literals


from django.conf import settings
from django.db import migrations, models
Expand Down
4 changes: 2 additions & 2 deletions geokey/contributions/models.py
Expand Up @@ -282,7 +282,7 @@ def create_search_index(self):

for field in self.category.fields.all().select_subclasses():
value = None
if self.properties and field.key in self.properties.keys():
if self.properties and field.key in list(self.properties.keys()):
if field.fieldtype == 'TextField':
value = self.properties.get(field.key)

Expand All @@ -304,7 +304,7 @@ def create_search_index(self):
value = ' '.join([val.name for val in lookupvalues])

if value:
cleaned = re.sub(r'[\W_]+', ' ', value)
cleaned = re.sub('[\W_]+', ' ', str(value))
terms = cleaned.lower().split()

search_index = search_index + list(
Expand Down
10 changes: 5 additions & 5 deletions geokey/contributions/serializers.py
Expand Up @@ -140,8 +140,8 @@ def replace_null(self, properties):
Contribution properties with replaced null values
"""
for key, value in properties.iteritems():
if isinstance(value, (str, unicode)) and len(value) == 0:
for key, value in properties.items():
if isinstance(value, str) and len(value) == 0:
properties[key] = None

return properties
Expand Down Expand Up @@ -176,7 +176,7 @@ def validate_properties(self, properties, category=None, status=None):
Observation.validate_partial(category, properties)
else:
Observation.validate_full(category, properties)
except ValidationError, e:
except ValidationError as e:
errors.append(e)

self._validated_data['properties'] = properties
Expand Down Expand Up @@ -206,9 +206,9 @@ def validate_location(self, project, location_id):
project.id,
location_id
)
except PermissionDenied, error:
except PermissionDenied as error:
errors.append(error)
except Location.DoesNotExist, error:
except Location.DoesNotExist as error:
errors.append(error)

if errors:
Expand Down
7 changes: 3 additions & 4 deletions geokey/contributions/templatetags/kml_tags.py
@@ -1,8 +1,7 @@
"""Template tags for KML."""

from osgeo import ogr

from django import template
from six import PY2

from geokey.categories.models import Field, LookupField, MultipleLookupField

Expand Down Expand Up @@ -64,11 +63,11 @@ def kml_desc(place):
except Field.DoesNotExist:
pass

if type(value) in [str, unicode]:
if isinstance(value, str) and PY2:
value = value.encode('utf-8')

if properties[key] is not None:
description = '{desc}<tr><td>{name}</td><td>{value}</td></tr>'.format(
description = u'{desc}<tr><td>{name}</td><td>{value}</td></tr>'.format(
desc=description,
name=name,
value=value
Expand Down
4 changes: 2 additions & 2 deletions geokey/contributions/tests/comments/test_models.py
Expand Up @@ -33,6 +33,6 @@ def test_delete_nested(self):
comment = CommentFactory()
response = CommentFactory(**{'respondsto': comment})
CommentFactory.create_batch(3, **{'respondsto': response})
self.assertEquals(len(Comment.objects.all()), 5)
self.assertEqual(len(Comment.objects.all()), 5)
comment.delete()
self.assertEquals(len(Comment.objects.all()), 0)
self.assertEqual(len(Comment.objects.all()), 0)

0 comments on commit 70819cf

Please sign in to comment.