def create(self, request, *args, **kwargs): user = self.request.user products = self.request.data['products'] amounts = self.request.data['amounts'] customer_id = self.request.data['customer'] owner = self.request.data['owner'] total_price = self.request.data['total_price'] total_tax = self.request.data['total_tax'] order = Order.objects.create(owner=owner, products=products, amounts=amounts, customer_id=customer_id, total_price=total_price, total_tax=total_tax) order_serializer = OrderSerializer(order) sio.emit('create_change_order', { 'data': { 'state': 'created', 'order': order_serializer.data, 'user': { 'email': user.email, 'id': user.id } } }, namespace='/test') return Response(order_serializer.data)
def post(self, request, *args, **kwargs): print("=-======>>>>>>>>>>>>>>>>") print(sio) sio.emit('my response', {'data': 'Server generated event'}, namespace='/test') sio.emit('create_product', {'data': "testfdfdfdsf"}, namespace='/test') return Response('success')
def update(self, request, *args, **kwargs): customer_id = self.kwargs['pk'] user = self.request.user first_name = self.request.data['first_name'] last_name = self.request.data['last_name'] email = self.request.data['email'] company = self.request.data['company'] phone = self.request.data['phone'] address = self.request.data['address'] apartment = self.request.data['apartment'] city = self.request.data['city'] country = self.request.data['country'] region = self.request.data['region'] postal_code = self.request.data['postal_code'] image = self.request.data['image'] customer = Customer.objects.filter(pk=customer_id).first() if first_name: customer.first_name = first_name if last_name: customer.last_name = last_name if email: customer.email = email if company: customer.company = company if phone: customer.phone = phone if address: customer.address = address if apartment: customer.apartment = apartment if city: customer.city = city if country: customer.country = country if region: customer.region = region if postal_code: customer.postal_code = postal_code if image: customer.image = image customer.save() customer_serializer = CustomerSerializer(customer) sio.emit('create_change_customer', { 'data': { 'state': 'updated', 'customer': customer_serializer.data, 'user': { 'email': user.email, 'id': user.id } } }, namespace='/test') return Response(customer_serializer.data)
def get_queryset(self): customer_id = self.kwargs['pk'] user = self.request.user queryset = Customer.objects.filter(pk=customer_id) customer = Customer.objects.filter(pk=customer_id).first() customer_serializer = CustomerSerializer(customer) sio.emit('create_change_customer', { 'data': { 'state': 'deleted', 'customer': customer_serializer.data, 'user': { 'email': user.email, 'id': user.id } } }, namespace='/test') return queryset
def get_queryset(self): product_id = self.kwargs['pk'] queryset = Product.objects.filter(pk=product_id) product = Product.objects.filter(pk=product_id).first() product_serializer = ProductSerializer(product) user = self.request.user sio.emit('create_change_product', { 'data': { 'state': 'deleted', 'product': product_serializer.data, 'user': { 'email': user.email, 'id': user.id } } }, namespace='/test') return queryset
def post(self, request, *args, **kwargs): user = self.request.user first_name = self.request.data['first_name'] last_name = self.request.data['last_name'] email = self.request.data['email'] company = self.request.data['company'] phone = self.request.data['phone'] address = self.request.data['address'] apartment = self.request.data['apartment'] city = self.request.data['city'] country = self.request.data['country'] region = self.request.data['region'] postal_code = self.request.data['postal_code'] image = self.request.data['image'] customer = Customer.objects.create( owner=user, first_name=first_name, last_name=last_name, email=email, company=company, phone=phone, address=address, apartment=apartment, city=city, country=country, region=region, postal_code=postal_code, image=image) customer_serializer = CustomerSerializer(customer) sio.emit('create_change_customer', { 'data': { 'state': 'created', 'customer': customer_serializer.data, 'user': { 'email': user.email, 'id': user.id } } }, namespace='/test') return Response(customer_serializer.data)
def update(self, request, *args, **kwargs): order_id = self.kwargs['pk'] user = self.request.user products = self.request.data['products'] amounts = self.request.data['amounts'] customer_id = self.request.data['customer'] owner = self.request.data['owner'] total_price = self.request.data['total_price'] total_tax = self.request.data['total_tax'] order = Order.objects.filter(pk=order_id).first() if products: order.products = products if amounts: order.amounts = amounts if customer_id: order.customer_id = customer_id if total_price: order.total_price = total_price if total_tax: order.total_tax = total_tax order.save() order_serializer = OrderSerializer(order) sio.emit('create_change_order', { 'data': { 'state': 'updated', 'order': order_serializer.data, 'user': { 'email': user.email, 'id': user.id } } }, namespace='/test') return Response(order_serializer.data)
def create(self, request, *args, **kwargs): user = self.request.user name = self.request.data['name'] description = self.request.data['description'] sku = self.request.data['sku'] price = self.request.data['price'] company_id = self.request.data['company'] image = self.request.data['image'] company = Company.objects.filter(id=company_id).first() product = Product.objects.create(name=name, description=description, image=image, sku=sku, price=price, company=company) produc_serializer = ProductSerializer(product) variation_types = self.request.data['variation_type'] for variation_type in variation_types: variation_type_entry = VariationType.objects.create( product=product, type=variation_type['name']) variation_type_attributes = variation_type['value'] for variation_type_attribute in variation_type_attributes: VariationTypeAttribute.objects.create( variation_type=variation_type_entry, attribute=variation_type_attribute) variations = self.request.data['variations'] for variation in variations: if "sku" in variation: sku = variation["sku"] else: sku = '' if "image" in variation: image = variation["image"] else: image = '' if "price" in variation: price = variation["price"] else: price = '' if "barcode" in variation: barcode = variation["barcode"] else: barcode = '' Variation.objects.create(product=product, title=variation['title'], sku=sku, barcode=barcode, image=image, price=price) print("socket emited") sio.emit('create_product', { 'data': { 'state': 'created', 'product': produc_serializer.data, 'user': { 'email': user.email, 'id': user.id } } }, namespace='/test') print("socket closed") return Response(produc_serializer.data)
def post(self, request, *args, **kwargs): sio.emit('my response', {'data': "fdasfasdfasdfsd"}, namespace='/test') return self.create(request, *args, **kwargs)
def update(self, request, *args, **kwargs): product_id = self.kwargs['pk'] user = self.request.user name = self.request.data['name'] description = self.request.data['description'] sku = self.request.data['sku'] price = self.request.data['price'] image = self.request.data['image'] product = Product.objects.filter(pk=product_id).first() if name: product.name = name if description: product.description = description if sku: product.sku = sku if price: product.price = price if image: product.image = image product.save() product_serializer = ProductSerializer(product) variations = self.request.data['variations'] for variation in variations: old_variation = Variation.objects.filter( id=variation['id']).first() if variation["sku"]: sku = variation["sku"] else: sku = old_variation.sku if variation["image"]: image = variation["image"] else: image = old_variation.image if variation["price"]: price = variation["price"] else: price = old_variation.price if variation["barcode"]: barcode = variation["barcode"] else: barcode = old_variation.barcode Variation.objects.update(title=old_variation.title, sku=sku, barcode=barcode, image=image, price=price) sio.emit('create_change_product', { 'data': { 'state': 'updated', 'product': product_serializer.data, 'user': { 'email': user.email, 'id': user.id } } }, namespace='/test') return Response(product_serializer.data)