From 77634ae303458cbeba8e705a68ea29e2ac85b2c1 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 30 Jun 2016 17:31:58 +0200 Subject: [PATCH] pep8 it --- api/__init__.py | 2 +- api/core.py | 6 ++++-- api/extensions/esales.py | 3 ++- api/helpers.py | 4 +++- api/models/category.py | 2 +- api/models/market.py | 8 ++++++-- api/models/product.py | 2 +- api/resources/server.py | 1 + api/validators.py | 3 ++- esales/__init__.py | 2 +- tests/config.py | 4 ++-- tests/models/test_market.py | 3 ++- tests/resources/test_designers.py | 14 +++++++++----- tests/test_validators.py | 5 +++-- tests/test_web.py | 5 +++-- 15 files changed, 41 insertions(+), 23 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index a8ce586..bb67a43 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -1 +1 @@ -from .core import * \ No newline at end of file +from .core import * diff --git a/api/core.py b/api/core.py index 621e2c2..121afdd 100644 --- a/api/core.py +++ b/api/core.py @@ -7,6 +7,7 @@ from api.validators import ValidationError db = SQLAlchemy() esales = Esales() + def create_app(config_name='config'): app = Flask(__name__) app.config.from_object(config_name) @@ -17,7 +18,8 @@ def create_app(config_name='config'): # register errors handlers @app.errorhandler(404) def on_404(error): - response = jsonify({'status': 404, 'message': 'The requested resource was not found'}) + response = jsonify( + {'status': 404, 'message': 'The requested resource was not found'}) response.status_code = 404 return response @@ -36,4 +38,4 @@ def create_app(config_name='config'): # authentication app.before_request(check_api_key) - return app \ No newline at end of file + return app diff --git a/api/extensions/esales.py b/api/extensions/esales.py index d64e4bd..f740a6d 100644 --- a/api/extensions/esales.py +++ b/api/extensions/esales.py @@ -1,5 +1,6 @@ from esales.cluster import Cluster + class Esales(object): def __init__(self, app=None): @@ -9,4 +10,4 @@ class Esales(object): self.init_app(app) def init_app(self, app): - self.cluster.url = app.config.get('ESALES_URL') \ No newline at end of file + self.cluster.url = app.config.get('ESALES_URL') diff --git a/api/helpers.py b/api/helpers.py index 4441b78..af181e9 100644 --- a/api/helpers.py +++ b/api/helpers.py @@ -4,13 +4,15 @@ from flask import request, current_app, Response import unidecode import re + def check_api_key(): api_keys = current_app.config.get("API_KEYS") if not api_keys: return auth = request.authorization if not auth or not auth.username in api_keys: - response = json.dumps({'status': 401, 'message': 'No valid API key provided'}) + response = json.dumps( + {'status': 401, 'message': 'No valid API key provided'}) return Response( response, 401, {'WWW-Authenticate': 'Basic realm="Login Required"'} diff --git a/api/models/category.py b/api/models/category.py index ddb26c6..0a6229b 100644 --- a/api/models/category.py +++ b/api/models/category.py @@ -16,7 +16,7 @@ class Category(db.Model): if not row: raise Exception( 'Could not find a path for category: {}, locale: {}'.format(self.id, locale)) - path = row.path.split('/', 1)[-1] # get all text after the first / + path = row.path.split('/', 1)[-1] # get all text after the first / path = slugify(path) return path diff --git a/api/models/market.py b/api/models/market.py index 105c66d..5885ab9 100644 --- a/api/models/market.py +++ b/api/models/market.py @@ -28,7 +28,8 @@ class Market(object): @property def price_adjustments(self): if self._price_adjustments is None: - row = PriceAdjustments.query.filter_by(territory=self.territory).first() + row = PriceAdjustments.query.filter_by( + territory=self.territory).first() if row: self._price_adjustments = float(row.adjustment) self._price_adjustments = 1 @@ -86,18 +87,21 @@ _markets = [ Market('International', 'en_EU', 'EUR', 1, None), ] + def iter_markets(): for market in _markets: yield market + def from_locale(locale): for market in iter_markets(): if market.locale == locale: return market return None + def from_territory(territory): for market in iter_markets(): if market.territory == territory.upper(): return market - return None \ No newline at end of file + return None diff --git a/api/models/product.py b/api/models/product.py index aa30c2a..ea59c4c 100644 --- a/api/models/product.py +++ b/api/models/product.py @@ -8,4 +8,4 @@ class Currencies(db.Model): __tablename__ = 'product-currencies' iso3char = db.Column(db.String(3), primary_key=True) - exchange_rate = db.Column(db.Numeric, default=1) \ No newline at end of file + exchange_rate = db.Column(db.Numeric, default=1) diff --git a/api/resources/server.py b/api/resources/server.py index c41cb3e..6e8ff5a 100644 --- a/api/resources/server.py +++ b/api/resources/server.py @@ -2,6 +2,7 @@ from flask import Blueprint, jsonify mod = Blueprint('server', __name__) + @mod.route("/", methods=["GET"]) def status(): data = { diff --git a/api/validators.py b/api/validators.py index ffcdd2c..defe4cd 100644 --- a/api/validators.py +++ b/api/validators.py @@ -1,5 +1,6 @@ # coding=UTF-8 + class ValidationError(Exception): def __init__(self, message): @@ -11,4 +12,4 @@ def validate_keys_exists(obj, *keys): for key in keys: if not key in obj: raise ValidationError('No {} is specified'.format(key)) - return True \ No newline at end of file + return True diff --git a/esales/__init__.py b/esales/__init__.py index c7630a1..758589f 100644 --- a/esales/__init__.py +++ b/esales/__init__.py @@ -1 +1 @@ -from .cluster import Cluster \ No newline at end of file +from .cluster import Cluster diff --git a/tests/config.py b/tests/config.py index 54082ae..eb1cd7e 100644 --- a/tests/config.py +++ b/tests/config.py @@ -1,5 +1,5 @@ SQLALCHEMY_DATABASE_URI = 'sqlite://' SQLALCHEMY_TRACK_MODIFICATIONS = True PRESERVE_CONTEXT_ON_EXCEPTION = False -TESTING=True -DEBUG = True \ No newline at end of file +TESTING = True +DEBUG = True diff --git a/tests/models/test_market.py b/tests/models/test_market.py index b8a4516..0b38be1 100644 --- a/tests/models/test_market.py +++ b/tests/models/test_market.py @@ -3,9 +3,10 @@ import unittest2 from api.models import market + class TestMarket(unittest2.TestCase): def test_sweden(self): sweden = market.from_territory('SE') self.assertEqual('sv', sweden.language) - self.assertEqual('SE', sweden.territory) \ No newline at end of file + self.assertEqual('SE', sweden.territory) diff --git a/tests/resources/test_designers.py b/tests/resources/test_designers.py index c02acec..8990be8 100644 --- a/tests/resources/test_designers.py +++ b/tests/resources/test_designers.py @@ -36,8 +36,10 @@ class TestEndpoints(flask_testing.TestCase): self.assert200(response) def test_create_designer(self): - data = {'name': 'Spider-Man', 'username': 'spiderman', 'territory': 'US', 'currency': 'USD'} - response = self.client.post('/designers/', data=json.dumps(data), content_type='application/json') + data = {'name': 'Spider-Man', 'username': 'spiderman', + 'territory': 'US', 'currency': 'USD'} + response = self.client.post( + '/designers/', data=json.dumps(data), content_type='application/json') self.assertEqual(201, response.status_code) designer = response.json self.assertEqual('Spider-Man', designer['name']) @@ -45,14 +47,16 @@ class TestEndpoints(flask_testing.TestCase): def test_create_designer_missing_data(self): data = {} - response = self.client.post('/designers/', data=json.dumps(data), content_type='application/json') + response = self.client.post( + '/designers/', data=json.dumps(data), content_type='application/json') self.assert400(response) @patch('api.tasks.esales.update_designer') def test_update_designer(self, esales_update_designer): designer = self._add_designer('designer1') data = {'name': 'test'} - response = self.client.put('/designers/1', data=json.dumps(data), content_type='application/json') + response = self.client.put( + '/designers/1', data=json.dumps(data), content_type='application/json') self.assertTrue(esales_update_designer.called) self.assertEqual('test', response.json['name']) - self.assertEqual('test', Designer.query.get(1).name) \ No newline at end of file + self.assertEqual('test', Designer.query.get(1).name) diff --git a/tests/test_validators.py b/tests/test_validators.py index 50a933f..1e901dc 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -5,8 +5,9 @@ from api.validators import ValidationError, validate_keys_exists class TestValidation(unittest2.TestCase): def test_validate_keys_exists(self): - obj = {'a':1, 'b':2} + obj = {'a': 1, 'b': 2} self.assertRaises(ValidationError, validate_keys_exists, obj, 'c') - self.assertRaises(ValidationError, validate_keys_exists, obj, 'a', 'b', 'c') + self.assertRaises( + ValidationError, validate_keys_exists, obj, 'a', 'b', 'c') self.assertTrue(validate_keys_exists(obj, 'a')) self.assertTrue(validate_keys_exists(obj, 'a', 'b')) diff --git a/tests/test_web.py b/tests/test_web.py index 8eae726..1fbe60b 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -16,7 +16,8 @@ class TestHttpBasicAuth(flask_testing.TestCase): self.app.config['API_KEYS'] = [self.api_key] def _create_auth_headers(self, username, password=''): - data = base64.b64encode(bytes(':'.join([username, password]), 'ascii')).decode('ascii') + data = base64.b64encode( + bytes(':'.join([username, password]), 'ascii')).decode('ascii') headers = [('Authorization', 'Basic %s' % data)] return headers @@ -33,4 +34,4 @@ class TestHttpBasicAuth(flask_testing.TestCase): def test_basic_auth_disabled(self): response = self.client.get('/') - self.assert200(response) \ No newline at end of file + self.assert200(response)