serializers.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from django.core.exceptions import SuspiciousOperation
  2. from djstripe.models import Customer, Price, Product, SubscriptionItem
  3. from rest_framework import serializers
  4. from rest_framework.serializers import ModelSerializer
  5. from organizations_ext.models import OrganizationUserRole
  6. from .rest_framework.serializers import (
  7. SubscriptionSerializer as BaseSubscriptionSerializer,
  8. )
  9. class BaseProductSerializer(ModelSerializer):
  10. class Meta:
  11. model = Product
  12. fields = ("id", "name", "description", "type", "metadata")
  13. class BasePriceSerializer(ModelSerializer):
  14. class Meta:
  15. model = Price
  16. fields = (
  17. "id",
  18. "nickname",
  19. "currency",
  20. "type",
  21. "unit_amount",
  22. "human_readable_price",
  23. "metadata",
  24. )
  25. class PriceSerializer(BasePriceSerializer):
  26. product = BaseProductSerializer()
  27. class Meta(BasePriceSerializer.Meta):
  28. fields = (
  29. "id",
  30. "nickname",
  31. "currency",
  32. "unit_amount",
  33. "human_readable_price",
  34. "metadata",
  35. "product",
  36. )
  37. class ProductSerializer(BaseProductSerializer):
  38. prices = BasePriceSerializer(many=True)
  39. class Meta(BaseProductSerializer.Meta):
  40. fields = ("id", "name", "description", "type", "prices", "metadata")
  41. class SubscriptionItemSerializer(ModelSerializer):
  42. price = PriceSerializer()
  43. class Meta:
  44. model = SubscriptionItem
  45. fields = ("id", "price")
  46. class SubscriptionSerializer(BaseSubscriptionSerializer):
  47. items = SubscriptionItemSerializer(many=True)
  48. class OrganizationPrimaryKeySerializer(serializers.PrimaryKeyRelatedField):
  49. def get_queryset(self):
  50. user = self.context["request"].user
  51. return user.organizations_ext_organization.filter(
  52. organization_users__role=OrganizationUserRole.OWNER
  53. )
  54. class OrganizationSelectSerializer(serializers.Serializer):
  55. """Organization in which user is owner of"""
  56. organization = OrganizationPrimaryKeySerializer()
  57. class PriceForOrganizationSerializer(OrganizationSelectSerializer):
  58. price = serializers.SlugRelatedField(queryset=Price.objects.all(), slug_field="id")
  59. class CreateSubscriptionSerializer(PriceForOrganizationSerializer):
  60. """A serializer used to create a Subscription. Only works with free prices."""
  61. subscription = SubscriptionSerializer(read_only=True)
  62. def create(self, validated_data):
  63. organization = validated_data["organization"]
  64. price = validated_data["price"]
  65. if price.unit_amount != 0.0:
  66. raise SuspiciousOperation(
  67. "Cannot subscribe to non-free plan without payment"
  68. )
  69. customer, _ = Customer.get_or_create(subscriber=organization)
  70. subscription = customer.subscribe(items=[{"price": price}])
  71. return {
  72. "price": price,
  73. "organization": organization,
  74. "subscription": subscription,
  75. }