diff --git a/manage_subscriptions/forms.py b/manage_subscriptions/forms.py index d4afaee..05dc2fc 100644 --- a/manage_subscriptions/forms.py +++ b/manage_subscriptions/forms.py @@ -21,7 +21,6 @@ class SubscriptionForm(forms.ModelForm): "active", "is_free", ] - exclude = [] def __init__(self, *args, **kwargs): super(SubscriptionForm, self).__init__(*args, **kwargs) @@ -31,23 +30,17 @@ class SubscriptionForm(forms.ModelForm): id__in=[event_user.id, event_manager.id] ) - if self.instance: - # If there is an instance (i.e. we're editing an existing subscription) - - # Use a dictionary comprehension to create a new dictionary of fields - # that excludes the readonly fields - self.fields = { - field_name: field - for field_name, field in self.fields.items() - if field_name not in [ - "interval", - "interval_count", - "amount", - "high_amount", - "principal_types", - ] - } - +class SubscriptionUpdateForm(forms.ModelForm): + class Meta: + model = Subscription + fields = [ + "title", + "short_description", + "long_description", + "referral_percentage", + "active", + "is_free", + ] class PrincipalSubscriptionForm(forms.ModelForm): class Meta: diff --git a/manage_subscriptions/migrations/0014_alter_subscription_long_description.py b/manage_subscriptions/migrations/0014_alter_subscription_long_description.py index 51a85f9..d117a17 100644 --- a/manage_subscriptions/migrations/0014_alter_subscription_long_description.py +++ b/manage_subscriptions/migrations/0014_alter_subscription_long_description.py @@ -14,6 +14,6 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='subscription', name='long_description', - field=django_quill.fields.QuillField(), + field=django_quill.fields.QuillField(default=''), ), ] diff --git a/manage_subscriptions/models.py b/manage_subscriptions/models.py index 70bba3b..a15766d 100644 --- a/manage_subscriptions/models.py +++ b/manage_subscriptions/models.py @@ -58,8 +58,9 @@ class Subscription(BaseModel): def save(self, *args, **kwargs): from goodtimes.services import StripeService - if not self.delete: - self.clean() + if self.pk and self.deleted: + return super().save(*args, **kwargs) + self.clean() if self.is_free: # If is_free is True, set amounts to 0 and remove Stripe price and product IDs @@ -68,7 +69,7 @@ class Subscription(BaseModel): self.price_id = None self.product_id = None else: - if self.id and self.price_id: # Update existing subscription + if self.pk and self.price_id: # Update existing subscription # Retrieve existing price and product from Stripe price = StripeService.retrieve_price(self.price_id) if not price["success"]: @@ -77,7 +78,7 @@ class Subscription(BaseModel): # Update price active status if it differs from local active status if self.active != price["data"].active: StripeService.update_price(price_id=self.price_id, active=self.active) - + # Retrieve existing product from Stripe product = StripeService.retrive_product(self.product_id) if not product["success"]: @@ -90,7 +91,8 @@ class Subscription(BaseModel): name=self.title, description=self.short_description ) - else: # Create new subscription + else: + print("new pricde create is clled =========================================================") # Create new product and price price = StripeService.create_price( product_data={ @@ -175,7 +177,7 @@ class PrincipalSubscription(BaseModel): # If the active flag is False, set the status to inactive if not self.active: self.status = SubscriptionStatus.INACTIVE - super.save(*args, **kwargs) + super().save(*args, **kwargs) def generate_order_id(email): diff --git a/manage_subscriptions/views.py b/manage_subscriptions/views.py index 1c5e00b..75335bf 100644 --- a/manage_subscriptions/views.py +++ b/manage_subscriptions/views.py @@ -14,6 +14,7 @@ from manage_coupons.models import Coupon from manage_subscriptions.forms import ( SubscriptionForm, PrincipalSubscriptionForm, + SubscriptionUpdateForm, ) from manage_wallets.models import ( PaymentMethod, @@ -83,6 +84,11 @@ class SubscriptionCreateOrUpdateView(LoginRequiredMixin, generic.View): if self.object is not None: self.action = resource_action.ACTION_UPDATE + if self.object: + self.form_class = SubscriptionUpdateForm + else: + self.form_class = self.form_class + form = self.form_class(instance=self.object) context = self.get_context_data(form=form) return render(request, self.template_name, context=context) @@ -94,7 +100,11 @@ class SubscriptionCreateOrUpdateView(LoginRequiredMixin, generic.View): if self.object is not None: self.action = resource_action.ACTION_UPDATE - form = self.form_class(request.POST, instance=self.object) + if self.object: + form = SubscriptionUpdateForm(request.POST, instance=self.object) + else: + form = self.form_class(request.POST) + if not form.is_valid(): print(form.errors) context = self.get_context_data(form=form)