2020-01-20 07:30:12 +00:00
|
|
|
import logging
|
|
|
|
import config
|
2020-01-27 08:40:57 +00:00
|
|
|
import json
|
|
|
|
import math
|
2020-01-20 07:30:12 +00:00
|
|
|
|
2020-02-19 06:59:54 +00:00
|
|
|
from config import ldap_manager, etcd_client
|
2020-01-27 08:40:57 +00:00
|
|
|
from helper import resolve_product
|
2020-02-19 19:12:11 +00:00
|
|
|
from ungleich_common.schemas import BaseSchema, Field, ValidationException
|
2020-01-20 07:30:12 +00:00
|
|
|
|
|
|
|
|
2020-01-27 08:40:57 +00:00
|
|
|
class AddProductSchema(BaseSchema):
|
|
|
|
def __init__(self, data):
|
|
|
|
super().__init__()
|
|
|
|
self.add_schema(UserCredentialSchema, data)
|
|
|
|
self.specs = Field('specs', dict, **self.get(data, 'specs'))
|
|
|
|
self.update = Field('update', bool, **self.get(data, 'update', return_default=True, default=False))
|
2020-01-20 07:30:12 +00:00
|
|
|
|
2020-01-27 08:40:57 +00:00
|
|
|
def validation(self):
|
|
|
|
user = self.objects['user']
|
|
|
|
user = json.loads(user.entry_to_json())
|
|
|
|
uid, ou, *dc = user['dn'].replace('ou=', '').replace('dc=', '').replace('uid=', '').split(',')
|
2020-02-19 06:59:54 +00:00
|
|
|
if ou != config.config.get('ldap', 'internal_user_ou', fallback='users'):
|
2020-01-27 08:40:57 +00:00
|
|
|
raise ValidationException('You do not have access to create product.')
|
2020-01-20 07:30:12 +00:00
|
|
|
|
2020-01-27 08:40:57 +00:00
|
|
|
product = resolve_product(self.specs.value['usable-id'], etcd_client)
|
|
|
|
if product:
|
|
|
|
self.objects['product'] = product
|
|
|
|
|
|
|
|
|
|
|
|
class AddressSchema(BaseSchema):
|
2020-01-20 07:30:12 +00:00
|
|
|
def __init__(self, data):
|
|
|
|
super().__init__()
|
2020-01-27 08:40:57 +00:00
|
|
|
self.line1 = Field('line1', str, **self.get(data, 'line1'))
|
|
|
|
self.line2 = Field('line2', str, **self.get(data, 'line2', return_default=True))
|
|
|
|
self.city = Field('city', str, **self.get(data, 'city'))
|
|
|
|
self.country = Field('country', str, **self.get(data, 'country'))
|
|
|
|
self.state = Field('state', str, **self.get(data, 'state', return_default=True))
|
|
|
|
self.postal_code = Field('postal_code', str, **self.get(data, 'postal_code', return_default=True))
|
2020-01-20 07:30:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserRegisterPaymentSchema(BaseSchema):
|
|
|
|
def __init__(self, data):
|
2020-01-27 08:40:57 +00:00
|
|
|
super().__init__()
|
2020-01-20 07:30:12 +00:00
|
|
|
|
2020-01-27 08:40:57 +00:00
|
|
|
self.add_schema(UserCredentialSchema, data)
|
|
|
|
self.add_schema(AddressSchema, data, under_field_name='address')
|
|
|
|
|
|
|
|
self.card_number = Field('card_number', str, **self.get(data, 'card_number'))
|
|
|
|
self.cvc = Field('cvc', str, **self.get(data, 'cvc'))
|
|
|
|
self.expiry_year = Field('expiry_year', int, **self.get(data, 'expiry_year'))
|
|
|
|
self.expiry_month = Field('expiry_month', int, **self.get(data, 'expiry_month'))
|
|
|
|
self.card_holder_name = Field('card_holder_name', str, **self.get(data, 'card_holder_name'))
|
|
|
|
|
|
|
|
|
|
|
|
class UserCredentialSchema(BaseSchema):
|
|
|
|
def __init__(self, data):
|
2020-01-20 07:30:12 +00:00
|
|
|
super().__init__()
|
2020-01-27 08:40:57 +00:00
|
|
|
self.username = Field('username', str, **self.get(data, 'username'))
|
|
|
|
self.password = Field('password', str, **self.get(data, 'password'))
|
|
|
|
|
|
|
|
def validation(self):
|
|
|
|
try:
|
|
|
|
entry = ldap_manager.is_password_valid(self.username.value, self.password.value, query_key='uid')
|
|
|
|
except ValueError:
|
|
|
|
raise ValidationException('No user with \'{}\' username found. You can create account at '
|
|
|
|
'https://account.ungleich.ch'.format(self.username.value))
|
|
|
|
except Exception:
|
|
|
|
raise ValidationException('Invalid username/password.')
|
|
|
|
else:
|
|
|
|
self.objects['user'] = entry
|
2020-01-20 07:30:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProductOrderSchema(BaseSchema):
|
|
|
|
def __init__(self, data):
|
|
|
|
super().__init__()
|
2020-01-27 08:40:57 +00:00
|
|
|
self.product_id = Field(
|
|
|
|
'product_id', str, **self.get(data, 'product_id'), validators=[self.product_id_validation]
|
|
|
|
)
|
|
|
|
self.pay_consent = Field('pay', bool, **self.get(data, 'pay', return_default=True, default=False))
|
|
|
|
self.add_schema(UserCredentialSchema, data)
|
2020-01-20 07:30:12 +00:00
|
|
|
|
|
|
|
def product_id_validation(self):
|
2020-01-27 08:40:57 +00:00
|
|
|
product = resolve_product(self.product_id.value, etcd_client)
|
|
|
|
if product:
|
2020-01-27 09:55:26 +00:00
|
|
|
product['quantity'] = float(product['quantity'])
|
2020-01-27 08:40:57 +00:00
|
|
|
self.product_id.value = product['uuid']
|
|
|
|
self.objects['product'] = product
|
|
|
|
logging.debug('Got product {}'.format(product))
|
|
|
|
|
|
|
|
if not product['active']:
|
|
|
|
raise ValidationException('Product is not active at the moment.')
|
|
|
|
|
|
|
|
if product['quantity'] <= 0:
|
|
|
|
raise ValidationException('Out of stock.')
|
2020-01-20 07:30:12 +00:00
|
|
|
else:
|
2020-01-27 08:40:57 +00:00
|
|
|
raise ValidationException('No such product exists.')
|
|
|
|
|
|
|
|
def validation(self):
|
2020-01-27 09:55:26 +00:00
|
|
|
username = self.objects['user'].uid
|
|
|
|
customer_previous_orders = etcd_client.get_prefix('/v1/user/{}'.format(username), value_in_json=True)
|
2020-01-27 08:40:57 +00:00
|
|
|
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':
|
|
|
|
raise ValidationException('Please buy membership first to use this facility')
|
|
|
|
max_quantity_user_can_order = float(self.objects['product'].get('max_per_user', math.inf))
|
|
|
|
previous_order_of_same_product = [
|
|
|
|
o for o in customer_previous_orders if o['product'] == self.objects['product']['usable-id']
|
|
|
|
]
|
|
|
|
if len(previous_order_of_same_product) >= max_quantity_user_can_order:
|
|
|
|
raise ValidationException(
|
|
|
|
'You cannot buy {} more than {} times'.format(
|
|
|
|
self.objects['product']['name'], int(max_quantity_user_can_order)
|
|
|
|
)
|
|
|
|
)
|
2020-01-20 07:30:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OrderListSchema(BaseSchema):
|
|
|
|
def __init__(self, data):
|
|
|
|
super().__init__()
|
2020-01-27 08:40:57 +00:00
|
|
|
self.add_schema(UserCredentialSchema, data)
|
|
|
|
|
2020-01-20 07:30:12 +00:00
|
|
|
|
|
|
|
def make_return_message(err, status_code=200):
|
|
|
|
logging.debug('message: {}'.format(str(err)))
|
|
|
|
return {'message': str(err)}, status_code
|
|
|
|
|
|
|
|
|
|
|
|
def create_schema(specification, data):
|
|
|
|
fields = {}
|
|
|
|
for feature_name, feature_detail in specification['features'].items():
|
|
|
|
if not feature_detail['constant']:
|
2020-01-27 08:40:57 +00:00
|
|
|
fields[feature_name] = Field(
|
|
|
|
feature_name, eval(feature_detail['unit']['type']), **BaseSchema.get(data, feature_name)
|
|
|
|
)
|
2020-01-20 07:30:12 +00:00
|
|
|
|
|
|
|
return type('{}Schema'.format(specification['name']), (BaseSchema,), fields)
|