Monday, February 25, 2019

[Solved] Re: How can I refresh admin_urls

Thank you all for your attention. Kept me going. (another resend this time hopefully it will be laid out better)

The fix is to initialise the self.change_form_template to None *every* time ModelAdmin.change_form() is called. If it is None the ModelAdmin.render_change_form() method will dynamically create the model instance change form. Otherwise it uses the form I give it. My problem was it remembered that instead of doing its own thing when I had finished with it.

I reckon it was designed to swap out the default form and completely replace it forever. Thank heavens for open source.

Here is the only change to my code ...

    def change_view(self, request, object_id, form_url='', extra_context=None):
        """
        self = SubstanceAdmin
        request = wsgi request object
        object_id = substance
        form_url = no idea!
        extra_context = dict of apps, models, admin_urls and permissions
        """
        self.change_form_template = None  ############## this works #############
        ingredients = Substance_Ingredients.objects.filter(substance_id=object_id)
        subscription = None
        for sm2mi in ingredients:
            payable, fee_type = sm2mi.fee_payable()  # eg., True, PAID_DATA
            if payable:
                subscription = billing_subscribe(sm2mi, fee_type)
                if subscription:    # we need to collect money for the owner
                    self.change_form_template = 'payment.html'
                    context = billing_collect_context(
                        sm2mi,
                        subscription,
                    )
                    # get everything into the payment_view context
                    if not extra_context:
                        extra_context = dict()
                    extra_context.update(self.admin_site.each_context(request))
                    extra_context.update(context)
                    self.admin_site.admin_view(
                        # call the Stripe mechanism
                        billing_payment_view(
                            request,
                            sm2mi,
                            subscription,
                            context=extra_context,
                        )
                    )
                    # only one sm2mi at a time
                    break
        #if extra_context:
        #    logging.log(logging.DEBUG, '205 admin {0}'.format(extra_context))
        return super(SubstanceAdmin, self).change_view(
            request, object_id, form_url, extra_context
        )

 I reckon the Admin can do anything. It just takes some head scratching

:)

Mike

On 23/02/2019 6:58 pm, Mike Dewhirst wrote:
(Resend with urls which I previously omitted)

In a 'Substance' app I have a Stripe payment facility working from within the Admin. The problem is it spoils admin_urls so the user cannot get back to the Substance Admin. When the Stripe work is done, how do I re-initialise the admin urls?

I want to display a success page with a link back to the substance page. I can provide exactly the correct url but all it shows is an admin page with no content. The correct url is /admin/substance/substance/<pk>/ and that works only if the Stripe mechanism has not been called since the dev server reloaded itself.

I probably have an incorrect diagnosis here. The problem is most likely my misunderstanding of how to use the Admin properly. Any help will be very much appreciated.

Thanks

Mike

The process uses ModelAdmin.change_view()[1] to call my billing_payment_view()[2] which uses PaymentForm[3] and payment.html[4] template to pass the necessary hidden vars.

When the view executes (depending on whether payment is required) it pops up a (Stripe js) credit card collection form with a [Pay] button and that submits the detail to the Stripe API which in turn generates a token if successful or an error message and re-executes the billing_payment_view with request.POST including that detail plus all the hidden fields in the form. The view goes on to process the detail, generate a receipt, send an email to the payer and launch the billing_success_view()[5] and success.html[6] template.



No comments:

Post a Comment