make value_in_json=True
This commit is contained in:
parent
1a76d2b5f3
commit
200a7672f2
4 changed files with 23 additions and 24 deletions
|
@ -8,7 +8,7 @@ from uncloud.common import logger
|
|||
|
||||
|
||||
class EtcdEntry:
|
||||
def __init__(self, meta_or_key, value, value_in_json=False):
|
||||
def __init__(self, meta_or_key, value, value_in_json=True):
|
||||
if hasattr(meta_or_key, 'key'):
|
||||
# if meta has attr 'key' then get it
|
||||
self.key = meta_or_key.key.decode('utf-8')
|
||||
|
@ -30,8 +30,8 @@ def readable_errors(func):
|
|||
raise UncloudException('Cannot connect to etcd: is etcd running as configured in uncloud.conf?')
|
||||
except etcd3.exceptions.ConnectionTimeoutError as err:
|
||||
raise etcd3.exceptions.ConnectionTimeoutError('etcd connection timeout.') from err
|
||||
except Exception:
|
||||
logger.exception('Some etcd error occured. See syslog for details.')
|
||||
except Exception as err:
|
||||
logger.exception('Some etcd error occured. See syslog for details.', err)
|
||||
|
||||
return wrapper
|
||||
|
||||
|
@ -42,14 +42,14 @@ class EtcdWrapper:
|
|||
self.client = etcd3.client(*args, **kwargs)
|
||||
|
||||
@readable_errors
|
||||
def get(self, *args, value_in_json=False, **kwargs):
|
||||
def get(self, *args, value_in_json=True, **kwargs):
|
||||
_value, _key = self.client.get(*args, **kwargs)
|
||||
if _key is None or _value is None:
|
||||
return None
|
||||
return EtcdEntry(_key, _value, value_in_json=value_in_json)
|
||||
|
||||
@readable_errors
|
||||
def put(self, *args, value_in_json=False, **kwargs):
|
||||
def put(self, *args, value_in_json=True, **kwargs):
|
||||
_key, _value = args
|
||||
if value_in_json:
|
||||
_value = json.dumps(_value)
|
||||
|
@ -60,16 +60,16 @@ class EtcdWrapper:
|
|||
return self.client.put(_key, _value, **kwargs)
|
||||
|
||||
@readable_errors
|
||||
def get_prefix(self, *args, value_in_json=False, raise_exception=True, **kwargs):
|
||||
def get_prefix(self, *args, value_in_json=True, **kwargs):
|
||||
event_iterator = self.client.get_prefix(*args, **kwargs)
|
||||
for e in event_iterator:
|
||||
yield EtcdEntry(*e[::-1], value_in_json=value_in_json)
|
||||
|
||||
@readable_errors
|
||||
def watch_prefix(self, key, raise_exception=True, value_in_json=False):
|
||||
def watch_prefix(self, key, value_in_json=True):
|
||||
event_iterator, cancel = self.client.watch_prefix(key)
|
||||
for e in event_iterator:
|
||||
if hasattr(e, '_event'):
|
||||
e = e._event
|
||||
e = getattr('e', '_event')
|
||||
if e.type == e.PUT:
|
||||
yield EtcdEntry(e.kv.key, e.kv.value, value_in_json=value_in_json)
|
||||
|
|
|
@ -168,6 +168,7 @@ class ProductOrderSchema(BaseSchema):
|
|||
def product_id_validation(self):
|
||||
product = resolve_product(self.product_id.value, etcd_client)
|
||||
if product:
|
||||
product['quantity'] = float(product['quantity'])
|
||||
self.product_id.value = product['uuid']
|
||||
self.objects['product'] = product
|
||||
logging.debug('Got product {}'.format(product))
|
||||
|
@ -181,7 +182,8 @@ class ProductOrderSchema(BaseSchema):
|
|||
raise ValidationException('No such product exists.')
|
||||
|
||||
def validation(self):
|
||||
customer_previous_orders = etcd_client.get_prefix('/v1/user/{}'.format(self.username.value), value_in_json=True)
|
||||
username = self.objects['user'].uid
|
||||
customer_previous_orders = etcd_client.get_prefix('/v1/user/{}'.format(username), value_in_json=True)
|
||||
customer_previous_orders = [o.value for o in customer_previous_orders]
|
||||
membership = next(filter(lambda o: o['product'] == 'membership', customer_previous_orders), None)
|
||||
if membership is None and self.objects['product']['usable-id'] != 'membership':
|
||||
|
|
|
@ -272,14 +272,14 @@ class StripeUtils(object):
|
|||
all_stripe_plans = client.get("/v1/stripe_plans")
|
||||
all_stripe_plans_set = set()
|
||||
if all_stripe_plans:
|
||||
all_stripe_plans_obj = json.loads(all_stripe_plans.value)
|
||||
all_stripe_plans_obj = all_stripe_plans.value
|
||||
if all_stripe_plans_obj and len(all_stripe_plans_obj['plans']) > 0:
|
||||
all_stripe_plans_set = set(all_stripe_plans_obj["plans"])
|
||||
return all_stripe_plans_set
|
||||
|
||||
@staticmethod
|
||||
def _save_all_stripe_plans(stripe_plans):
|
||||
client.put("/v1/stripe_plans", json.dumps({"plans": list(stripe_plans)}))
|
||||
client.put("/v1/stripe_plans", {"plans": list(stripe_plans)})
|
||||
|
||||
@handle_stripe_error
|
||||
def get_or_create_stripe_plan(self, product_name, amount, stripe_plan_id,
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import json
|
||||
import logging
|
||||
|
||||
from datetime import datetime
|
||||
|
@ -19,10 +18,10 @@ from helper import get_plan_id_from_product, calculate_charges
|
|||
class ListProducts(Resource):
|
||||
@staticmethod
|
||||
def get():
|
||||
products = client.get_prefix('/v1/products/', value_in_json=False)
|
||||
products = client.get_prefix('/v1/products/')
|
||||
products = [
|
||||
product
|
||||
for product in [json.loads(p.value) for p in products]
|
||||
for product in [p.value for p in products]
|
||||
if product['active']
|
||||
]
|
||||
prod_dict = {}
|
||||
|
@ -63,7 +62,7 @@ class AddProduct(Resource):
|
|||
product_value['uuid'] = product_uuid
|
||||
|
||||
logger.debug('Adding product data: {}'.format(str(product_value)))
|
||||
client.put(product_key, product_value, value_in_json=True)
|
||||
client.put(product_key, product_value)
|
||||
if not previous_product:
|
||||
return make_return_message('Product created.')
|
||||
else:
|
||||
|
@ -201,7 +200,7 @@ class ProductOrder(Resource):
|
|||
)
|
||||
)
|
||||
|
||||
with client.client.lock('product-order') as lock:
|
||||
with client.client.lock('product-order') as _:
|
||||
# Initiate a one-time/subscription based on product type
|
||||
if recurring_charge > 0:
|
||||
logger.debug('Product {} is recurring payment'.format(product['name']))
|
||||
|
@ -234,11 +233,10 @@ class ProductOrder(Resource):
|
|||
client.put(
|
||||
'/v1/user/{}/orders/{}'.format(
|
||||
cleaned_values['username'], order_obj['order-id']
|
||||
),
|
||||
order_obj, value_in_json=True
|
||||
), order_obj
|
||||
)
|
||||
product['quantity'] -= 1
|
||||
client.put('/v1/products/{}'.format(product['uuid']), product, value_in_json=True)
|
||||
client.put('/v1/products/{}'.format(product['uuid']), product)
|
||||
|
||||
return {
|
||||
'message': 'Order Successful.',
|
||||
|
@ -246,6 +244,7 @@ class ProductOrder(Resource):
|
|||
}
|
||||
else:
|
||||
logger.error('Could not create plan {}'.format(plan_id))
|
||||
return make_return_message('Something wrong happened. Contact administrator', 400)
|
||||
|
||||
elif recurring_charge == 0 and one_time_charge > 0:
|
||||
logger.debug('Product {} is one-time payment'.format(product['name']))
|
||||
|
@ -268,10 +267,10 @@ class ProductOrder(Resource):
|
|||
}
|
||||
client.put(
|
||||
'/v1/user/{}/orders/{}'.format(cleaned_values['username'], order_obj['order-id']),
|
||||
order_obj, value_in_json=True
|
||||
order_obj
|
||||
)
|
||||
product['quantity'] -= 1
|
||||
client.put('/v1/products/{}'.format(product['uuid']), product, value_in_json=True)
|
||||
client.put('/v1/products/{}'.format(product['uuid']), product)
|
||||
|
||||
return {'message': 'Order successful', **order_obj}, 200
|
||||
|
||||
|
@ -288,9 +287,7 @@ class OrderList(Resource):
|
|||
return make_return_message(err, 400)
|
||||
else:
|
||||
cleaned_values = validator.get_cleaned_values()
|
||||
orders = client.get_prefix(
|
||||
'/v1/user/{}/orders'.format(cleaned_values['username']), value_in_json=True
|
||||
)
|
||||
orders = client.get_prefix('/v1/user/{}/orders'.format(cleaned_values['username']))
|
||||
orders_dict = {
|
||||
order.value['order-id']: {
|
||||
**order.value
|
||||
|
|
Loading…
Reference in a new issue