|
6 | 6 | from django.conf import settings |
7 | 7 | from django.db.models import Max, Min, Sum |
8 | 8 | from drf_spectacular.utils import extend_schema_serializer |
9 | | -from rest_framework import serializers |
10 | | -from rest_framework.exceptions import ValidationError |
11 | | - |
12 | 9 | from metering_billing.invoice import generate_balance_adjustment_invoice |
13 | 10 | from metering_billing.models import ( |
14 | 11 | AddOnSpecification, |
|
56 | 53 | ) |
57 | 54 | from metering_billing.utils import convert_to_date, now_utc |
58 | 55 | from metering_billing.utils.enums import ( |
59 | | - CATEGORICAL_FILTER_OPERATORS, |
60 | 56 | CUSTOMER_BALANCE_ADJUSTMENT_STATUS, |
61 | 57 | FLAT_FEE_BEHAVIOR, |
62 | 58 | INVOICE_STATUS_ENUM, |
|
70 | 66 | USAGE_BEHAVIOR, |
71 | 67 | USAGE_BILLING_BEHAVIOR, |
72 | 68 | ) |
| 69 | +from rest_framework import serializers |
| 70 | +from rest_framework.exceptions import ValidationError |
73 | 71 |
|
74 | 72 | SVIX_CONNECTOR = settings.SVIX_CONNECTOR |
75 | 73 | logger = logging.getLogger("django.server") |
@@ -199,36 +197,18 @@ class Meta: |
199 | 197 | comparison_value = serializers.ListField(child=serializers.CharField()) |
200 | 198 |
|
201 | 199 |
|
202 | | -class SubscriptionCategoricalFilterSerializer( |
203 | | - ConvertEmptyStringToNullMixin, TimezoneFieldMixin, serializers.ModelSerializer |
| 200 | +class SubscriptionFilterSerializer( |
| 201 | + ConvertEmptyStringToNullMixin, TimezoneFieldMixin, serializers.Serializer |
204 | 202 | ): |
205 | | - class Meta: |
206 | | - model = CategoricalFilter |
207 | | - fields = ("value", "property_name") |
208 | | - extra_kwargs = { |
209 | | - "property_name": { |
210 | | - "required": True, |
211 | | - }, |
212 | | - "value": {"required": True}, |
213 | | - } |
214 | | - |
215 | 203 | value = serializers.CharField() |
216 | 204 | property_name = serializers.CharField( |
217 | 205 | help_text="The string name of the property to filter on. Example: 'product_id'" |
218 | 206 | ) |
219 | 207 |
|
220 | | - def create(self, validated_data): |
221 | | - comparison_value = validated_data.pop("value") |
222 | | - comparison_value = [comparison_value] |
223 | | - validated_data["comparison_value"] = comparison_value |
224 | | - return CategoricalFilter.objects.get_or_create( |
225 | | - **validated_data, operator=CATEGORICAL_FILTER_OPERATORS.ISIN |
226 | | - ) |
227 | | - |
228 | 208 | def to_representation(self, instance): |
229 | 209 | data = { |
230 | | - "property_name": instance.property_name, |
231 | | - "value": instance.comparison_value[0], |
| 210 | + "property_name": instance[0], |
| 211 | + "value": instance[1], |
232 | 212 | } |
233 | 213 | return data |
234 | 214 |
|
@@ -345,9 +325,7 @@ class Meta: |
345 | 325 | } |
346 | 326 |
|
347 | 327 | subscription_id = SubscriptionUUIDField(source="subscription_record_id") |
348 | | - subscription_filters = SubscriptionCategoricalFilterSerializer( |
349 | | - many=True, source="filters" |
350 | | - ) |
| 328 | + subscription_filters = SubscriptionFilterSerializer(many=True) |
351 | 329 | customer = LightweightCustomerSerializer() |
352 | 330 | billing_plan = LightweightPlanVersionSerializer() |
353 | 331 | addons = LightweightAddOnSubscriptionRecordSerializer( |
@@ -445,11 +423,11 @@ def get_adjustments(self, obj) -> InvoiceLineItemAdjustmentSerializer(many=True) |
445 | 423 |
|
446 | 424 | def get_subscription_filters( |
447 | 425 | self, obj |
448 | | - ) -> SubscriptionCategoricalFilterSerializer(many=True, allow_null=True): |
| 426 | + ) -> SubscriptionFilterSerializer(many=True, allow_null=True): |
449 | 427 | ass_sub_record = obj.associated_subscription_record |
450 | 428 | if ass_sub_record: |
451 | | - return SubscriptionCategoricalFilterSerializer( |
452 | | - ass_sub_record.filters.all(), many=True |
| 429 | + return SubscriptionFilterSerializer( |
| 430 | + ass_sub_record.subscription_filters, many=True |
453 | 431 | ).data |
454 | 432 | return None |
455 | 433 |
|
@@ -1640,7 +1618,7 @@ class Meta: |
1640 | 1618 | help_text="Whether the subscription automatically renews. Defaults to true.", |
1641 | 1619 | ) |
1642 | 1620 | is_new = serializers.BooleanField(required=False) |
1643 | | - subscription_filters = SubscriptionCategoricalFilterSerializer( |
| 1621 | + subscription_filters = SubscriptionFilterSerializer( |
1644 | 1622 | many=True, |
1645 | 1623 | required=False, |
1646 | 1624 | help_text="Add filter key, value pairs that define which events will be applied to this plan subscription.", |
@@ -1683,22 +1661,9 @@ def create(self, validated_data): |
1683 | 1661 | filters = validated_data.pop("subscription_filters", []) |
1684 | 1662 | subscription_filters = [] |
1685 | 1663 | for filter_data in filters: |
1686 | | - sub_cat_filter_dict = { |
1687 | | - "organization": validated_data["customer"].organization, |
1688 | | - "property_name": filter_data["property_name"], |
1689 | | - "operator": CATEGORICAL_FILTER_OPERATORS.ISIN, |
1690 | | - "comparison_value": [filter_data["value"]], |
1691 | | - } |
1692 | | - try: |
1693 | | - cf, _ = CategoricalFilter.objects.get_or_create(**sub_cat_filter_dict) |
1694 | | - except CategoricalFilter.MultipleObjectsReturned: |
1695 | | - cf = ( |
1696 | | - CategoricalFilter.objects.filter(**sub_cat_filter_dict) |
1697 | | - .first() |
1698 | | - .delete() |
1699 | | - ) |
1700 | | - cf = CategoricalFilter.objects.filter(**sub_cat_filter_dict).first() |
1701 | | - subscription_filters.append(cf) |
| 1664 | + subscription_filters.append( |
| 1665 | + [filter_data["property_name"], filter_data["value"]] |
| 1666 | + ) |
1702 | 1667 | sr = SubscriptionRecord.create_subscription_record( |
1703 | 1668 | start_date=validated_data["start_date"], |
1704 | 1669 | end_date=validated_data.get("end_date"), |
@@ -1759,7 +1724,7 @@ class Meta: |
1759 | 1724 | help_text="Whether the subscription automatically renews. Defaults to true.", |
1760 | 1725 | ) |
1761 | 1726 | is_new = serializers.BooleanField(required=False) |
1762 | | - subscription_filters = SubscriptionCategoricalFilterSerializer( |
| 1727 | + subscription_filters = SubscriptionFilterSerializer( |
1763 | 1728 | many=True, |
1764 | 1729 | required=False, |
1765 | 1730 | help_text="Add filter key, value pairs that define which events will be applied to this plan subscription.", |
@@ -1825,22 +1790,9 @@ def create(self, validated_data): |
1825 | 1790 | filters = validated_data.pop("subscription_filters", []) |
1826 | 1791 | subscription_filters = [] |
1827 | 1792 | for filter_data in filters: |
1828 | | - sub_cat_filter_dict = { |
1829 | | - "organization": validated_data["customer"].organization, |
1830 | | - "property_name": filter_data["property_name"], |
1831 | | - "operator": CATEGORICAL_FILTER_OPERATORS.ISIN, |
1832 | | - "comparison_value": [filter_data["value"]], |
1833 | | - } |
1834 | | - try: |
1835 | | - cf, _ = CategoricalFilter.objects.get_or_create(**sub_cat_filter_dict) |
1836 | | - except CategoricalFilter.MultipleObjectsReturned: |
1837 | | - cf = ( |
1838 | | - CategoricalFilter.objects.filter(**sub_cat_filter_dict) |
1839 | | - .first() |
1840 | | - .delete() |
1841 | | - ) |
1842 | | - cf = CategoricalFilter.objects.filter(**sub_cat_filter_dict).first() |
1843 | | - subscription_filters.append(cf) |
| 1793 | + subscription_filters.append( |
| 1794 | + [filter_data["property_name"], filter_data["value"]] |
| 1795 | + ) |
1844 | 1796 | sr = SubscriptionRecord.create_subscription_record( |
1845 | 1797 | start_date=validated_data["start_date"], |
1846 | 1798 | end_date=validated_data.get("end_date"), |
@@ -1869,9 +1821,7 @@ class Meta(SubscriptionRecordSerializer.Meta): |
1869 | 1821 | plan_detail = LightweightPlanVersionSerializer( |
1870 | 1822 | source="billing_plan", read_only=True |
1871 | 1823 | ) |
1872 | | - subscription_filters = SubscriptionCategoricalFilterSerializer( |
1873 | | - source="filters", many=True, read_only=True |
1874 | | - ) |
| 1824 | + subscription_filters = SubscriptionFilterSerializer(many=True, read_only=True) |
1875 | 1825 |
|
1876 | 1826 |
|
1877 | 1827 | class SubscriptionInvoiceSerializer(SubscriptionRecordSerializer): |
@@ -2137,7 +2087,7 @@ class SubscriptionRecordFilterSerializer(serializers.Serializer): |
2137 | 2087 | required=True, |
2138 | 2088 | help_text="Filter to a specific plan.", |
2139 | 2089 | ) |
2140 | | - subscription_filters = SubscriptionCategoricalFilterSerializer( |
| 2090 | + subscription_filters = SubscriptionFilterSerializer( |
2141 | 2091 | many=True, |
2142 | 2092 | required=False, |
2143 | 2093 | help_text="Filter to a specific set of subscription filters. If your billing model only allows for one subscription per customer, you very likely do not need this field. Must be formatted as a JSON-encoded + stringified list of dictionaries, where each dictionary has a key of 'property_name' and a key of 'value'.", |
@@ -2222,7 +2172,7 @@ class AddOnSubscriptionRecordFilterSerializer(serializers.Serializer): |
2222 | 2172 | required=True, |
2223 | 2173 | help_text="Filter to a specific plan.", |
2224 | 2174 | ) |
2225 | | - attached_subscription_filters = SubscriptionCategoricalFilterSerializer( |
| 2175 | + attached_subscription_filters = SubscriptionFilterSerializer( |
2226 | 2176 | many=True, |
2227 | 2177 | required=False, |
2228 | 2178 | help_text="Filter to a specific set of subscription filters. If your billing model only allows for one subscription per customer, you very likely do not need this field. Must be formatted as a JSON-encoded + stringified list of dictionaries, where each dictionary has a key of 'property_name' and a key of 'value'.", |
|
0 commit comments