lundi 17 août 2020

Nesting If statements to detect request value in Django views

I am trying to get third button to work on a simple app I am making. I set up if statements to detect the value of the request for two buttons, but I am getting stuck on how to configure the logic statement to get a third parameter through.

My issue is that I need to have the form filled with any data before the subscriptions button will work properly. When the form is blank the app will indicate the form needs to be filled out. I am not familiar with javascript to have the buttons run from that. My hope is that I can learn how to configure the if statement to have the subscriptions call work without the form being filled out.

Below is the UI as and the views.py def. The Amount and Delete functions work fine. It is just the subscription button that won't work until I put literally any data into the two text fields fields.

enter image description here

    def usage(request):

    if request.method == 'POST':
        form = UsageForm(request.POST)
        if form.is_valid():
                    if request.POST.get('subscription') == 'Subscription':
                        dt = datetime.today()
                        month = dt.month
                        year = dt.year
                        day = calendar.monthrange(year, month)[1]
                        eom = str(month) + "/" + str(day) + "/" + str(year)
                        anchor = int(time.mktime(datetime.strptime(eom, "%m/%d/%Y").timetuple()) + 68400)
                        cust_list = json.loads(str(stripe.Customer.list(limit=100)))

                        for item in cust_list['data']:

                            try:
                                print(item['subscriptions']['data'][0]['id'], item['email'], item['id'])

                            except:

                                stripe.Subscription.create(
                                    customer=item['id'],
                                    items=[{'plan': 'plan_DFnNVzXKJ2w0Ad'}],
                                    billing_cycle_anchor=anchor,
                                )

                    if request.POST.get('delete') == 'Delete':
                        cust_info = custinfo.objects.all()
                        cust_info.delete()
                        all_custinfo_items = custinfo.objects.all()

                    else:
                        Sub_ID = form.cleaned_data['Sub_ID']
                        amount = form.cleaned_data['amount']


                        stripe_data = stripe.SubscriptionItem.list(subscription=Sub_ID)

                        sub_item = stripe_data["data"][0]["id"]

                        stripe.UsageRecord.create(
                            quantity=amount,
                            timestamp=int(time.time()),
                            subscription_item=sub_item,
                            action='increment')
                        form.save()

                        print("Last Ran: ", Sub_ID, amount)
                        all_custinfo_items = custinfo.objects.all()

                        return render(request, 'form.html', {'form': form, 'custinfo_items': all_custinfo_items})

    else:
        form = UsageForm()
#        all_subid_items = custinfo.objects.filter(Sub_ID__startswith='sub')
    return render(request, 'form.html', {'form': form})

Aucun commentaire:

Enregistrer un commentaire