How to modify kwargs in Django?

3

I am making the confirmation page of a purchase and I want to pass a hash of the order identifier, previously generated by the system, to a url and with a decorator to check if the user that is accessing is the owner of that order by a decorator .

To speed up the task I want to modify the kwargs by changing the hash for the order and thus not having to do a second search for that order.

This is the view I'm trying to access:

Class

@method_decorator(login_required, name='dispatch')
@method_decorator(propietarioPedido, name='dispatch')
class ConfirmacionCompra(TemplateView):
    template_name = 'intranet/confirmacion_comprar.html'

    def get_context_data(self, **kwargs):
        context = super(ConfirmacionCompra, self).get_context_data(**kwargs)
        context['perfil'] = perfil
        pedido = Pedido.objects.get(id = self.kwargs['pedido'])
        context['pedido'] = pedido
        fotos = Foto.objects.filter(producto = pedido.producto)
        return context

This is the decorator I'm using:

Decorator

def propietarioPedido(funcion):

    def error():
        raise PermissionDenied

    def comprobarPropiedadPedido(request,*args,**kwargs):
        perfil = Perfil.objects.get(usuario=request.user)
        pedidos = Pedido.objects.filter(perfil = perfil)
        for p in pedidos:
            hash = hashlib.sha256(str(p.id).encode('utf-8'))
            if hash.hexdigest() == kwargs['pedido']:
                kwargs.update({'pedido': p.id})
                return funcion(request,*args,**kwargs)
        return error()

    return comprobarPropiedadPedido

Doing print(kwargs) in the decorator just before return returns {'pedido': 8564} but doing so in the view returns {'pedido':'9f14025af0065b30e47e23ebb3b491d39ae8ed17d33739e5ff3827ffb3634953'}

URL

url(r'^confirmacion_compra/(?P<pedido>[-\w]+)/$',
    intranet_views.ConfirmacionCompra.as_view(), name='confirmacion-compra'),
    
asked by F Delgado 19.04.2018 в 11:02
source

0 answers