Skip to content
This repository has been archived by the owner on Jun 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #243 from chosak/python3
Browse files Browse the repository at this point in the history
Add Python 3 compatibility
  • Loading branch information
chosak committed Jun 21, 2019
2 parents 625cfaf + 8487961 commit 7d2b8ec
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -19,6 +19,9 @@ npm-debug.log
__pycache__/
*.py[cod]
.eggs/
.python-version
build
dist

# Django #
#################
Expand Down
13 changes: 6 additions & 7 deletions .travis.yml
@@ -1,17 +1,16 @@
language: python
python:
- '2.7'
- 2.7
- 3.6
cache: pip
env: DJ_KEY=testkey
install:
- pip install coveralls tox
- pip install coveralls tox-travis
before_script:
- npm install -g gulp-cli
script:
- npm install -g gulp-cli
- npm config set package-lock false
script:
- tox
- python2.7 setup.py bdist_wheel
- ls dist/*
- python2.7 setup.py bdist_wheel --universal
after_success:
- coveralls
deploy:
Expand Down
15 changes: 9 additions & 6 deletions retirement_api/models.py
@@ -1,6 +1,9 @@
from django.db import models
from django.template.defaultfilters import slugify

from six import text_type


WORKFLOW_STATE = [
('APPROVED', 'Approved'),
('REVISED', 'Revised'),
Expand Down Expand Up @@ -150,25 +153,25 @@ def translist(self):
]
return fieldlist

def dump_translation_text(self, output=False):
def dump_translation_text(self, output=False, outfile=None):
"""
translation utility
returns a list of phrases to be translated,
or outputs a utf-8 .po file to /tmp/
"""
fieldlist = self.translist()
outfile = "/tmp/%s.po" % self.slug
phrases = [self.__getattribute__(attr) for attr in fieldlist if
self.__getattribute__(attr)]

if output is True:
with open(outfile, 'w') as f:
outfile = outfile or "/tmp/%s.po" % self.slug
with open(outfile, 'wb') as f:
for line in POHEADER:
f.write(line.encode('utf-8'))
for phrase in phrases:
f.write('#: templates/claiming.html\n'.encode('utf-8'))
f.write(unicode('msgid "%s"\n' % phrase).encode('utf-8'))
f.write('msgstr ""\n'.encode('utf-8'))
f.write("\n")
f.write(text_type('msgid "%s"\n' % phrase).encode('utf-8'))
f.write('msgstr ""\n\n'.encode('utf-8'))
else:
return phrases
30 changes: 25 additions & 5 deletions retirement_api/tests/test_models.py
@@ -1,6 +1,8 @@
import os
import sys
import datetime
import tempfile

from retirement_api.models import (AgeChoice,
Question,
Step,
Expand Down Expand Up @@ -48,11 +50,29 @@ def test_question_translist(self):
self.assertTrue(term in tlist)

def test_question_dump(self):
m = mock_open()
with patch("__builtin__.open", m, create=True):
mock_open.return_value = mock.MagicMock(spec=file)
self.testquestion.dump_translation_text(output=True)
self.assertTrue(m.call_count == 1)
with tempfile.NamedTemporaryFile() as f:
self.testquestion.dump_translation_text(
output=True,
outfile=f.name
)

f.seek(0)
translation_po_file_content = f.read()

self.assertEqual(translation_po_file_content, (b'''\
msgid ""
msgstr ""
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Project-Id-Version: retirement\\n"
"Language: es\\n"
#: templates/claiming.html
msgid "Test question."
msgstr ""
'''))

def test_question_dump_no_output(self):
dump = self.testquestion.dump_translation_text()
Expand Down
5 changes: 3 additions & 2 deletions retirement_api/tests/test_views.py
Expand Up @@ -12,6 +12,7 @@
# import unittest
from django.http import HttpRequest

from six import binary_type

from retirement_api.views import (param_check,
income_check,
Expand Down Expand Up @@ -86,7 +87,7 @@ def test_get_full_retirement_age(self):
def test_estimator_url_data(self):
request = self.req_blank
response = estimator(request, dob='1955-05-05', income='40000')
self.assertTrue(type(response.content) == str)
self.assertIsInstance(response.content, binary_type)
rdata = json.loads(response.content)
for each in self.return_keys:
self.assertTrue(each in rdata.keys())
Expand All @@ -105,7 +106,7 @@ def test_estimator_query_data(self):
request = self.req_good
response = estimator(request)
self.assertTrue(response.status_code == 200)
self.assertTrue(type(response.content) == str)
self.assertIsInstance(response.content, binary_type)
rdata = json.loads(response.content)
for each in self.return_keys:
self.assertTrue(each in rdata.keys())
Expand Down
4 changes: 2 additions & 2 deletions retirement_api/utils/ss_calculator.py
Expand Up @@ -449,12 +449,12 @@ def get_retire_data(params, language):
fra_tuple, past_fra, results) = set_up_runvars(params, language=language)
if isinstance(past_fra, bool) is False:
# if past_fra is neither False nor True, there's an error and we bail
if current_age > 70:
if current_age and current_age > 70:
results['past_fra'] = True
results['note'] = past_fra
results['error'] = "visitor too old for tool"
return results
elif current_age < 22:
elif current_age is None or current_age < 22:
results['note'] = past_fra
results['error'] = "visitor too young for tool"
return results
Expand Down
3 changes: 1 addition & 2 deletions retirement_api/utils/ss_update_stats.py
Expand Up @@ -11,11 +11,10 @@
AIME: Average Indexed Monthly Earnings
"""

from StringIO import StringIO

import requests
# from django.template.defaultfilters import slugify
from bs4 import BeautifulSoup as bs
from six import StringIO

TODAY = datetime.datetime.now().date()
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
Expand Down
2 changes: 1 addition & 1 deletion retirement_api/utils/tests/test_ss_utilities.py
Expand Up @@ -75,7 +75,7 @@ def test_check_results(self):
test_data = json.loads(Calibration.objects.first().results_json)
test_msg = check_results(test_data, self.TESTS)
self.assertTrue("pass" in test_msg)
slug = test_data.keys()[0]
slug = list(test_data.keys())[0]
test_data[slug]['current_age'] = 99
test_data[slug]['current_age'] = 99
test_data[slug]['data']['months_past_birthday'] = 13
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -8,6 +8,7 @@
'dj-database-url>=0.4.2,<1',
'python-dateutil>=2.1<3',
'requests>=2.18,<3',
'six>=1.11.0,<2',
]


Expand Down Expand Up @@ -48,8 +49,8 @@ def read_file(filename):
'Topic :: Internet :: WWW/HTTP',
'Intended Audience :: Developers',
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.6',
'Framework :: Django',
'Development Status :: 4 - Beta',
'Operating System :: OS Independent',
Expand Down
6 changes: 5 additions & 1 deletion tox.ini
@@ -1,6 +1,6 @@
[tox]
skipsdist=True
envlist=py{27}-dj{111}
envlist=py{27,36}-dj{111}

[testenv]
install_command=pip install -e ".[testing]" -U {opts} {packages}
Expand All @@ -9,5 +9,9 @@ commands=
coverage run ./manage.py test {posargs} --settings=settings.test
coverage report --skip-covered -m

basepython=
py27: python2.7
py36: python3.6

deps=
dj111: Django>=1.11,<1.12

0 comments on commit 7d2b8ec

Please sign in to comment.