def get(self, request): products = Products.objects.all() product_list = [] for product in products: product_list.append(product.__str__()) success = update_dict(dict(list=product_list)) return Response(success)
def post(self, request): resp = products.Products.get_products(products.Products(), param=dict(limit=50)) data = resp count = int(data['meta']['pagination']['count']) for index in range(count): param = { 'name': data['data'][index]['name'], 'bc_product_id': data['data'][index]['id'], 'type': data['data'][index]['type'], 'price': data['data'][index]['price'], 'option_set_id': data['data'][index]['option_set_id'], 'date_modified': data['data'][index]['date_modified'], 'date_created': data['data'][index]['date_created'], 'sku': data['data'][index]['sku'], 'description': data['data'][index]['description'], 'quantity': random.randint(30, 1000), } try: product = Products.objects.create(**param) except BaseException: raise status.HTTP_400_BAD_REQUEST return Response(FAILURE_BAD) success = update_dict(dict(productId=product.id)) return Response(success)
def get(self, request, pk): data = request.data token = data.get('token', None) jwt_decode = jwt.decode(token, "SECRET_KEY", algorithms='HS256') signature = token.split('.')[2] user_id = jwt_decode['user_id'] try: user = Users.objects.get(id=user_id) except Users.DoesNotExist: return Response(RESPONSE.FAILURE_NOT_EXIST) if signature == user.token and user: product_list = [] cart_product_list = CartProduct.objects.filter(cart_id=pk) for cart_product in cart_product_list: tmp = cart_product.__str__() tmp.update( dict(cart_id=tmp.get('cart_id').__str__().get('id'))) tmp.update( dict(product_id=tmp.get('product_id').__str__().get('id'))) tmp.pop('id') product = cart_product.product_id product_list.append(tmp) success = update_dict(dict(list=product_list)) return Response(success) return Response(RESPONSE.FAILURE_BAD)
def get(self, request, pk): user = self.get_object(pk) data = { 'email': user.email, 'name': user.name, 'password': user.password, } success = update_dict(data) return Response(success)
def get(self, request): try: users = Users.objects.all() except Users.DoesNotExist: raise Http404 return Response(RESPONSE.FAILURE_NOT_EXIST) user_list = [] for user in users: user_list.append(user.__str__()) success = update_dict(dict(list=user_list)) return Response(success)
def post(self, request): data = request.data try: user = Users.objects.get(email=data.get('email', None)) except Users.DoesNotExist: raise Http404 return Response(RESPONSE.FAILURE_NOT_EXIST) if data.get('password', None) == user.password: payload = {'user_id': user.id, 'email': user.email} jwt_token = { 'token': jwt.encode(payload, "SECRET_KEY", algorithm='HS256') } signature = jwt_token['token'].split('.')[2] user.token = signature user.save() success = update_dict(dict(jwt_token)) return Response(success) return Response(RESPONSE.FAILURE_BAD)
def put(self, request, cart_id, item_id): data = request.data line_item = data.get('line_item', None) token = data.get('token', None) jwt_decode = jwt.decode(token, "SECRET_KEY", algorithms='HS256') signature = token.split('.')[2] user_id = jwt_decode['user_id'] try: user = Users.objects.get(id=user_id) except Users.DoesNotExist: return Response(RESPONSE.FAILURE_NOT_EXIST) try: if signature == user.token and user and line_item: cart = Carts.objects.get(pk=cart_id) product = Products.objects.get( bc_product_id=line_item.get('product_id', None)) try: cart_product = CartProduct.objects.get(cart_id=cart_id, bc_item_id=item_id) except CartProduct.DoesNotExist: cart_product = None if cart_product: product.quantity += cart_product.quantity cart_product.quantity = line_item.get('quantity') product.quantity -= line_item.get('quantity') cart_product.save() product.save() else: return Response(RESPONSE.FAILURE_BAD) bc_param = { "line_item": { "quantity": line_item.get('quantity'), "product_id": line_item.get('product_id'), } } carts.Carts.put_cart_items(carts.Carts(), bc_cart_id=cart.bc_cart_id, item_id=item_id, data=bc_param) success = update_dict(dict(cart_id=cart.id)) return Response(success) except Exception: return Response(RESPONSE.FAILURE_BAD)
def put(self, request, pk): user = self.get_object(pk) data = request.data token = data.get('token', None) signature = token.split('.')[2] if token else None password = data.get('password', None) if signature == user.token and password: user.password = password user.save() pwd = [{ "id": 7870, "authentication": { "force_password_reset": True, "new_password": password } }] customers.Customers.update_pwd(customers.Customers(), pwd) success = update_dict(dict()) return Response(success) return Response(RESPONSE.FAILURE_BAD)
def post(self, request): data = request.data token = data.get('token', None) jwt_decode = jwt.decode(token, 'SECRET_KEY', algorithms='HS256') signature = token.split('.')[2] user_id = jwt_decode['user_id'] product_list = data.get('list', None) try: user = Users.objects.get(id=user_id) except Users.DoesNotExist: return Response(RESPONSE.FAILURE_NOT_EXIST) if signature == user.token and user and product_list: bc_param = {"line_items": product_list} resp = carts.Carts.post_cart(carts.Carts(), bc_param) param = { 'user_id': user, 'status': '1', } cart = Carts.objects.create(**param) for item in product_list: product = Products.objects.get( bc_product_id=item.get('product_id', None)) item_list = resp.get('data').get('line_items').get( 'physical_items') for physical_item in item_list: _cart_product = { 'cart_id': cart, 'product_id': product, 'bc_item_id': physical_item.get('id'), 'quantity': item.get('quantity', None), } product.quantity -= item.get('quantity') product.save() CartProduct.objects.create(**_cart_product) cart.bc_cart_id = resp.get('data').get('id') cart.save() success = update_dict( dict(cart_id=cart.id, bc_cart_id=resp.get('data').get('id'))) return Response(success) return Response(status.HTTP_400_BAD_REQUEST)
def get(self, request, pk): product = self.get_object(pk) success = update_dict(dict(product.__str__())) return Response(success)
def post(self, request): result = request.data data = result.get('list') for item in data: param = { "addresses": [ { "address1": address.get('address1', None), "city": address.get('city', None), "country_code": address.get('country_code', None), "first_name": address.get('first_name', None), "last_name": address.get('last_name', None), "postal_code": address.get('postal_code', None), "state_or_province": address.get('state_or_province', None) } for address in item.get('addresses') ], "authentication": { "force_password_reset": item.get('authentication', None).get('force_password_reset', None), "new_password": item.get('authentication', None).get('new_password', None) }, "company": item.get('company', None), "customer_group_id": item.get('customer_group_id', None), "email": item.get('email', None), "first_name": item.get('first_name', None), "last_name": item.get('last_name', None), "password": item.get('authentication', None).get('new_password', None), "phone": item.get('phone', None) } customer = customers.Customers.get_customer(customers.Customers(), email=item.get('email', None)) try: user = Users.objects.get(email=item.get('email', None)) except Users.DoesNotExist: user = None if not customer.get('data') and user is None: resp = customers.Customers.post_customers(customers.Customers(), data=[param]) addresses = param.pop('addresses') param.pop('authentication') user = Users.objects.create(**param) for address in addresses: address.update(dict(user_id=user)) Address.objects.create(**address) if not resp.get('data'): Response(RESPONSE.FAILURE_BAD) bc_id = resp.get('data')[0].get('id') user.bc_id = bc_id user.save() success = update_dict(dict(user_id=user.id, bc_id=bc_id)) return Response(success) if customer.get('data') and user: return Response({ 'code': 405, 'message': '用户已存在!' }) if not customer.get('data') and user: addresses = Address.objects.filter(user_id=user) address_list = [] for address in addresses: tmp = address.__str__() tmp.pop('id') tmp.pop('user_id') address_list.append(tmp) customer_param = { "addresses": address_list, "authentication": { "force_password_reset": True, "new_password": user.password }, 'first_name': user.first_name, 'last_name': user.last_name, 'email': user.email, 'company': user.company, 'customer_group_id': user.customer_group_id, "phone": item.get('phone', None) } resp = customers.Customers.post_customers(customers.Customers(), data=[customer_param]) if not resp.get('data'): Response(RESPONSE.FAILURE_BAD) bc_id = resp.get('data')[0].get('id') user.bc_id = bc_id user.save() return Response({ 'code': 405, 'bc_id': bc_id, 'message': '用户已存在!' }) if customer.get('data') and user is None: user_param = { "company": customer.get('data')[0].get('company', None), "customer_group_id": customer.get('data')[0].get('customer_group_id', None), "email": customer.get('data')[0].get('email', None), "first_name": customer.get('data')[0].get('first_name', None), "last_name": customer.get('data')[0].get('last_name', None), "phone": customer.get('data')[0].get('phone', None), "bc_id": customer.get('data')[0].get('id', None) } user = Users.objects.create(**user_param) addresses = Addresses.get_customer_addresses(Addresses(), customer_id=customer.get('data')[0].get('id', None)) for item in addresses.get('data'): item.pop('address_type') item.pop('country') item.pop('id') item.pop('customer_id') item.pop('phone') item.update({'user_id': user}) Address.objects.create(**item) bc_id = customer.get('data')[0].get('id') user.bc_id = bc_id user.save() return Response({ 'code': 405, 'id': user.id, 'message': '用户已存在!' })
def delete(self, request, pk): user = self.get_object(pk) success = update_dict(dict(userId=user.id)) user.delete() return Response(success)