markciecior / ConnectPyse

ConnectWise (Manage) REST API client written in Python 3.x
MIT License
21 stars 8 forks source link

_info object doesn't work with Django template system #6

Closed emichaud closed 2 years ago

emichaud commented 4 years ago

Hey there,

kudos for a nice implementation of the CW api's.

I'm building an app with Django and I've had issues with their template system when trying to extract elements from an _info object.

Accessing this object in Python is straightforward, but retrieving it via Django template fails.

works fine def get_ticket_by_id(ticket_id): api = tickets_api.TicketsAPI(url=BASE_URL, auth=AUTH) ticket = api.get_ticket_by_id(ticket_id) return ticket

t = get_ticket_by_id('442996') print(t._info['lastUpdated'])

fails {{ t._info['lastUpdated'] }}

My work around was to simply load the object into a separate dictionary and pass that along with the ticket object to the template system.

context = {} ticket = cwapi.get_ticket_by_id(item) info = ticket._info if ticket: context.update({'ticket':ticket}) context.update({'info':info})

return render(request, page_url, context)

While it seems obvious now, it wasn't at first and took me a bit to figure out what was going on. Maybe this will be helpful to someone else.

markciecior commented 4 years ago

I think Django's template system uses dot-syntax when accessing dictionary entries. In your template variable, what if you use {{ t._info.lastUpdated }} instead?

emichaud commented 4 years ago

Hey Mark, I gave that a try and it did not work. Based on the error message, I don't believe it will work. TemplateSyntaxError at /support/ Variables and attributes may not begin with underscores: 'ticket._info.lastUpdated'

I appreciate the feedback. I have it working now with the workaround I mentioned. One thought for an improvement might be to add a instance method that returns a dict containing the "_info". Thats all I did basically as a work around. I believe that many of the CW api's have similar notation. However this only affects Django templates as far as I know and I don't know how many people are using it that way.

markciecior commented 4 years ago

Gotcha.

For posterity's sake, I found this line from the Django docs here:

Variable attributes that begin with an underscore may not be accessed as they’re generally considered private.

I'll look at the instance method option.