12:55 pm

PragetX

PPP

Msg91

Tried msg91 Created api for sending otp to users and verify otp

def send_otp(to_phone, otp):
	conn = http.client.HTTPSConnection("control.msg91.com")
	payload = '{\n "name": "Vijay"\n}'
	headers = {"Content-Type": "application/JSON"}
	template_id = os.getenv("TEMPLATE_ID")
	auth_key = os.getenv("MSG91_API_KEY")
	if len(to_phone) == 10:
		to_phone = f"91{to_phone}"
		conn.request(
		"POST",
		f"/api/v5/otp?template_id={template_id}&mobile={to_phone}&authkey={auth_key}&otp={otp}",
		payload,
		headers,
		)
	res = conn.getresponse()
	data = res.read()
	return data.decode("utf-8")

Upadted Serializer for forgot password

class ForgotPasswordSerializer(serializers.Serializer):
	phone = serializers.CharField()
	def validate(self, attrs):
		user = User.objects.filter(phone=attrs["phone"]).first()
		if user is None:
			raise serializers.ValidationError(
			{"message": "This phone number is not found"}
			)
		otp = generate_otp()
		otp_sent = send_otp(user.phone, otp)
		if otp_sent["type"] == "success":
			user.otp = otp
			user.is_otp_used = False
			user.save()
		return super().validate(attrs)

Stuck because of no DLT. Still have coded already and need not to worry.

Need to update the following when we get dlt and messaging starts working

Django Backend

Server was not loading static files Along with Het Vaghasiya I updated Nginx conf slightly to keep

	location /static/ {  
        root /home/ubuntu/PPP;  
    }

This should work but gave error permission denied

changed permission still doesn’t work.

Look though error.log and nginx.conf

Found that user www-data is trying to nginx it is default user of nginx.

So 2 approaches to solve this are

  • change www-data user to ubuntu
gpasswd -a www-data username
  • or add www-data to ubuntu group so that it can access that group’s file.

We did the second thing.

And Solved the issue.

WebSocket in Django

Starting with websocket in django. Chat app for InfinityTalks.

PPP

Product Update Functionality in Orders Update api.

# serializers.py
class OrderProductUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderProduct
        fields = ["id", "quantity", "product"]
 
 
class OrderUpdateProductSerializer(serializers.ModelSerializer):
    # ...
    def update(self, instance, validated_data):
        orders_data = validated_data.pop("order_products", [])
 
        order_products = OrderProduct.objects.filter(order=instance).all()
        for order_data in orders_data:
            print(order_data)
            print(order_data["product"])
            order_product = order_products.filter(
                product=order_data["product"].id
            ).first()
            if order_product:
                order_data["product"] = order_data["product"]
                order_data["per_product_price"] = order_data["product"].price
                order_data["amount"] = (
                    int(order_data["quantity"]) * order_data["per_product_price"]
                )
                OrderProduct.objects.filter(id=order_product.id).update(**order_data)
                order_products = order_products.exclude(id=order_product.id)
            else:
                product = Product.objects.filter(id=order_data["product"].id).first()
                product_per_product_price = ProductPrice.objects.filter(
                    product=product
                ).first()
                if product_per_product_price:
                    order_data["product"] = product
                    order_data["per_product_price"] = (
                        product_per_product_price.discount_price
                    )
                else:
                    order_data["product"] = product
                    order_data["per_product_price"] = product.price
                # order_data["amount"] = int(order_data["quantity"]) * order_data["per_product_price"]
                order_product_created = OrderProduct.objects.create(
                    order=instance, **order_data
                )
                order_products = order_products.exclude(id=order_product_created.id)
                print(order_product_created)
 
        # delete the remaining order_products
        for order_product in order_products:
            order_product.delete()
        validated_data["amount"] = OrderProduct.objects.filter(order=instance).aggregate(
            total_amount=Sum("amount")
        )["total_amount"]
        super().update(instance, validated_data)
        return instance

This is not yet perfect cuz it doesn’t take discount in consideration

<<Yesterday Tomorrow>>


Links : Tags : Year : 2024 Month : March 24, March Date : 18th March, Monday, 2024, 18th March Category : Daily