import locale
locale.setlocale(locale.LC_ALL, '')

data_object['subscription']


def convert_int_to_currency(price):
    return locale.currency(int(price) / 100)


def convert_int_to_decimal(price):
    return Decimal(str(price)[:-2] + '.' + str(price)[-2:])


def find_shipping_cost(data):
    for x in data:
        if x['description'] == 'Shipping':
            return convert_int_to_currency(x['amount'])
            break
        else:
            continue


def format_product(data, unit_price):
    return {
        'product': Product.objects.get(pk=data['pk']),
        'quantity': data['quantity']
    }


def deserialize_subscription(data):
    sub_data = {}

    for x in data:
        if 'products_and_quantities' in x['metadata']:
            sub_data['customer_note'] = f"Grind: {x['metadata']['grind']}"
            sub_data['unit_price'] = convert_int_to_decimal(x['price']['unit_amount'])
            sub_data['items'] = map(format_product, x['metadata']['products_and_quantities'])
            sub_data['total_weight'] = x['metadata']['total_weight']

        if x['description'] == 'Shipping':
            sub_data['shipping_cost'] = convert_int_to_decimal(x['amount'])
        continue

    return sub_data


# shipping_cost = find_shipping_cost(data_object['lines']['data'])
# items = find_products(data_object['lines']['data'])
# unit_price = find_unit_price(data_object['lines']['data'])

sub_data = deserialize_subscription(data_object['lines']['data'])

order = Order.objects.create(
    customer=,
    status=,
    billing_address=,
    shipping_address=,
    subtotal_amount=,
    shipping_total=,
    total_amount=data_object['total'],
    weight=
)

order.lines.add(
    [OrderLine(
        product=item['product'],
        quantity=item['quantity'],
        customer_note=sub_data['customer_note'],
        unit_price=sub_data['unit_price']
    ) for item in sub_data['items']]
)

order.save()
