pep8 it
This commit is contained in:
+1
-1
@@ -1 +1 @@
|
|||||||
from .core import *
|
from .core import *
|
||||||
|
|||||||
+4
-2
@@ -7,6 +7,7 @@ from api.validators import ValidationError
|
|||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
esales = Esales()
|
esales = Esales()
|
||||||
|
|
||||||
|
|
||||||
def create_app(config_name='config'):
|
def create_app(config_name='config'):
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config.from_object(config_name)
|
app.config.from_object(config_name)
|
||||||
@@ -17,7 +18,8 @@ def create_app(config_name='config'):
|
|||||||
# register errors handlers
|
# register errors handlers
|
||||||
@app.errorhandler(404)
|
@app.errorhandler(404)
|
||||||
def on_404(error):
|
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
|
response.status_code = 404
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@@ -36,4 +38,4 @@ def create_app(config_name='config'):
|
|||||||
# authentication
|
# authentication
|
||||||
app.before_request(check_api_key)
|
app.before_request(check_api_key)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from esales.cluster import Cluster
|
from esales.cluster import Cluster
|
||||||
|
|
||||||
|
|
||||||
class Esales(object):
|
class Esales(object):
|
||||||
|
|
||||||
def __init__(self, app=None):
|
def __init__(self, app=None):
|
||||||
@@ -9,4 +10,4 @@ class Esales(object):
|
|||||||
self.init_app(app)
|
self.init_app(app)
|
||||||
|
|
||||||
def init_app(self, app):
|
def init_app(self, app):
|
||||||
self.cluster.url = app.config.get('ESALES_URL')
|
self.cluster.url = app.config.get('ESALES_URL')
|
||||||
|
|||||||
+3
-1
@@ -4,13 +4,15 @@ from flask import request, current_app, Response
|
|||||||
import unidecode
|
import unidecode
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
def check_api_key():
|
def check_api_key():
|
||||||
api_keys = current_app.config.get("API_KEYS")
|
api_keys = current_app.config.get("API_KEYS")
|
||||||
if not api_keys:
|
if not api_keys:
|
||||||
return
|
return
|
||||||
auth = request.authorization
|
auth = request.authorization
|
||||||
if not auth or not auth.username in api_keys:
|
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(
|
return Response(
|
||||||
response, 401,
|
response, 401,
|
||||||
{'WWW-Authenticate': 'Basic realm="Login Required"'}
|
{'WWW-Authenticate': 'Basic realm="Login Required"'}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class Category(db.Model):
|
|||||||
if not row:
|
if not row:
|
||||||
raise Exception(
|
raise Exception(
|
||||||
'Could not find a path for category: {}, locale: {}'.format(self.id, locale))
|
'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)
|
path = slugify(path)
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ class Market(object):
|
|||||||
@property
|
@property
|
||||||
def price_adjustments(self):
|
def price_adjustments(self):
|
||||||
if self._price_adjustments is None:
|
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:
|
if row:
|
||||||
self._price_adjustments = float(row.adjustment)
|
self._price_adjustments = float(row.adjustment)
|
||||||
self._price_adjustments = 1
|
self._price_adjustments = 1
|
||||||
@@ -86,18 +87,21 @@ _markets = [
|
|||||||
Market('International', 'en_EU', 'EUR', 1, None),
|
Market('International', 'en_EU', 'EUR', 1, None),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def iter_markets():
|
def iter_markets():
|
||||||
for market in _markets:
|
for market in _markets:
|
||||||
yield market
|
yield market
|
||||||
|
|
||||||
|
|
||||||
def from_locale(locale):
|
def from_locale(locale):
|
||||||
for market in iter_markets():
|
for market in iter_markets():
|
||||||
if market.locale == locale:
|
if market.locale == locale:
|
||||||
return market
|
return market
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def from_territory(territory):
|
def from_territory(territory):
|
||||||
for market in iter_markets():
|
for market in iter_markets():
|
||||||
if market.territory == territory.upper():
|
if market.territory == territory.upper():
|
||||||
return market
|
return market
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -8,4 +8,4 @@ class Currencies(db.Model):
|
|||||||
__tablename__ = 'product-currencies'
|
__tablename__ = 'product-currencies'
|
||||||
|
|
||||||
iso3char = db.Column(db.String(3), primary_key=True)
|
iso3char = db.Column(db.String(3), primary_key=True)
|
||||||
exchange_rate = db.Column(db.Numeric, default=1)
|
exchange_rate = db.Column(db.Numeric, default=1)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from flask import Blueprint, jsonify
|
|||||||
|
|
||||||
mod = Blueprint('server', __name__)
|
mod = Blueprint('server', __name__)
|
||||||
|
|
||||||
|
|
||||||
@mod.route("/", methods=["GET"])
|
@mod.route("/", methods=["GET"])
|
||||||
def status():
|
def status():
|
||||||
data = {
|
data = {
|
||||||
|
|||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
# coding=UTF-8
|
# coding=UTF-8
|
||||||
|
|
||||||
|
|
||||||
class ValidationError(Exception):
|
class ValidationError(Exception):
|
||||||
|
|
||||||
def __init__(self, message):
|
def __init__(self, message):
|
||||||
@@ -11,4 +12,4 @@ def validate_keys_exists(obj, *keys):
|
|||||||
for key in keys:
|
for key in keys:
|
||||||
if not key in obj:
|
if not key in obj:
|
||||||
raise ValidationError('No {} is specified'.format(key))
|
raise ValidationError('No {} is specified'.format(key))
|
||||||
return True
|
return True
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
from .cluster import Cluster
|
from .cluster import Cluster
|
||||||
|
|||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
SQLALCHEMY_DATABASE_URI = 'sqlite://'
|
SQLALCHEMY_DATABASE_URI = 'sqlite://'
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = True
|
SQLALCHEMY_TRACK_MODIFICATIONS = True
|
||||||
PRESERVE_CONTEXT_ON_EXCEPTION = False
|
PRESERVE_CONTEXT_ON_EXCEPTION = False
|
||||||
TESTING=True
|
TESTING = True
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
|
|||||||
@@ -3,9 +3,10 @@
|
|||||||
import unittest2
|
import unittest2
|
||||||
from api.models import market
|
from api.models import market
|
||||||
|
|
||||||
|
|
||||||
class TestMarket(unittest2.TestCase):
|
class TestMarket(unittest2.TestCase):
|
||||||
|
|
||||||
def test_sweden(self):
|
def test_sweden(self):
|
||||||
sweden = market.from_territory('SE')
|
sweden = market.from_territory('SE')
|
||||||
self.assertEqual('sv', sweden.language)
|
self.assertEqual('sv', sweden.language)
|
||||||
self.assertEqual('SE', sweden.territory)
|
self.assertEqual('SE', sweden.territory)
|
||||||
|
|||||||
@@ -36,8 +36,10 @@ class TestEndpoints(flask_testing.TestCase):
|
|||||||
self.assert200(response)
|
self.assert200(response)
|
||||||
|
|
||||||
def test_create_designer(self):
|
def test_create_designer(self):
|
||||||
data = {'name': 'Spider-Man', 'username': 'spiderman', 'territory': 'US', 'currency': 'USD'}
|
data = {'name': 'Spider-Man', 'username': 'spiderman',
|
||||||
response = self.client.post('/designers/', data=json.dumps(data), content_type='application/json')
|
'territory': 'US', 'currency': 'USD'}
|
||||||
|
response = self.client.post(
|
||||||
|
'/designers/', data=json.dumps(data), content_type='application/json')
|
||||||
self.assertEqual(201, response.status_code)
|
self.assertEqual(201, response.status_code)
|
||||||
designer = response.json
|
designer = response.json
|
||||||
self.assertEqual('Spider-Man', designer['name'])
|
self.assertEqual('Spider-Man', designer['name'])
|
||||||
@@ -45,14 +47,16 @@ class TestEndpoints(flask_testing.TestCase):
|
|||||||
|
|
||||||
def test_create_designer_missing_data(self):
|
def test_create_designer_missing_data(self):
|
||||||
data = {}
|
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)
|
self.assert400(response)
|
||||||
|
|
||||||
@patch('api.tasks.esales.update_designer')
|
@patch('api.tasks.esales.update_designer')
|
||||||
def test_update_designer(self, esales_update_designer):
|
def test_update_designer(self, esales_update_designer):
|
||||||
designer = self._add_designer('designer1')
|
designer = self._add_designer('designer1')
|
||||||
data = {'name': 'test'}
|
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.assertTrue(esales_update_designer.called)
|
||||||
self.assertEqual('test', response.json['name'])
|
self.assertEqual('test', response.json['name'])
|
||||||
self.assertEqual('test', Designer.query.get(1).name)
|
self.assertEqual('test', Designer.query.get(1).name)
|
||||||
|
|||||||
@@ -5,8 +5,9 @@ from api.validators import ValidationError, validate_keys_exists
|
|||||||
class TestValidation(unittest2.TestCase):
|
class TestValidation(unittest2.TestCase):
|
||||||
|
|
||||||
def test_validate_keys_exists(self):
|
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, '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'))
|
||||||
self.assertTrue(validate_keys_exists(obj, 'a', 'b'))
|
self.assertTrue(validate_keys_exists(obj, 'a', 'b'))
|
||||||
|
|||||||
+3
-2
@@ -16,7 +16,8 @@ class TestHttpBasicAuth(flask_testing.TestCase):
|
|||||||
self.app.config['API_KEYS'] = [self.api_key]
|
self.app.config['API_KEYS'] = [self.api_key]
|
||||||
|
|
||||||
def _create_auth_headers(self, username, password=''):
|
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)]
|
headers = [('Authorization', 'Basic %s' % data)]
|
||||||
return headers
|
return headers
|
||||||
|
|
||||||
@@ -33,4 +34,4 @@ class TestHttpBasicAuth(flask_testing.TestCase):
|
|||||||
|
|
||||||
def test_basic_auth_disabled(self):
|
def test_basic_auth_disabled(self):
|
||||||
response = self.client.get('/')
|
response = self.client.get('/')
|
||||||
self.assert200(response)
|
self.assert200(response)
|
||||||
|
|||||||
Reference in New Issue
Block a user