migrate sugarcrm python2 code to sugarcrm python3 code

Hi, 

We use SugarCRM (Version 6.5.23 (Build 1061)) to manage our products delivery to customers. One of our products is special USB keys that has a unique number for each USB key. The unique number is tracked in SugarCRM along with other information related to the USB keys.

The SugarCRM is hosted in our company server room, we also have our other servers locally to talk to the SugarCRM through python code. The integration between SugarCRM and Python was done with Python 2.6.6. Sample python2 code and results are listed below:

>>> import sugarcrm

Then I ran following commands, they are all successful with python2.6.6

>>> session = sugarcrm.Sugarcrm(url, passwd.login['username'], passwd.login['password'])
>>> query = session['sent_usbkeys'].query().filter(key_id_c__gte = '1')
>>> list_serial = []
>>> for usbkey in query:
... list_serial.append(int(usbkey['key_id_c']))
...
>>> list_serial
[1, 141, 2, 142, 143, 3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 4, 21, 22, 23, 24, 2 5, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 51 , 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,

I need to migrate the above piece of code to python 3.8.10. In order to do that, I searched online, found https://pypi.org/project/sugarcrm/0.1.2 might be something that I can refer to. So in python3.8.10, I replaced sugarcrm.Sugarcrm() to sugarcrm.Session() for the first command, it seems successful as it did not complain anything:

>>> session = sugarcrm.Session(url, passwd.login['username'], passwd.login['password'])

In addition, I compared the two sessions’ interface, looks like it is a right change.

In Python2.6.6

>>> dir(session)
['__doc__', '__getitem__', '__init__', '__module__', '_isldap', '_login', '_password', '_sendRequest' , '_session', '_url', '_username', 'get_available_modules', 'get_document_revision', 'get_entries', ' get_entries_count', 'get_entry', 'get_entry_list', 'get_module_fields', 'get_note_attachment', 'get_r elationships', 'get_report_entries', 'get_server_info', 'get_user_id', 'get_user_team_id', 'logout', 'modules', 'password', 'relate', 'rst_modules', 'search_by_module', 'set_document_revision', 'set_ent ries', 'set_entry', 'set_note_attachment', 'set_relationship', 'set_relationships']

In Python3.8.10

>>> dir(session)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_request', 'application', 'get_available_modules', 'get_document_revision', 'get_entries', 'get_entries_count', 'get_entry', 'get_entry_list', 'get_language_definition', 'get_last_viewed', 'get_modified_relationships', 'get_module_fields', 'get_module_fields_md5', 'get_module_layout', 'get_note_attachment', 'get_quotes_pdf', 'get_relationships', 'get_report_entries', 'get_report_pdf', 'get_server_info', 'get_upcoming_activities', 'get_user_id', 'get_user_team_id', 'job_queue_cycle', 'job_queue_next', 'job_queue_run', 'language', 'login', 'logout', 'oauth_access', 'seamless_login', 'search_by_module', 'session_id', 'set_campaign_merge', 'set_document_revision', 'set_entries', 'set_entry', 'set_note_attachment', 'set_relationship', 'set_relationships', 'snip_import_emails', 'snip_update_contacts', 'url', 'username', 'verify']

Then I move on to the 2nd command, it was not successful with python3.8.10

>>> query = session['sent_usbkeys'].query().filter(key_id_c__gte = '1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'Session' object is not subscriptable

‘sent_usbkeys’ is a customized module in our SugarCRM. I can see it from the session when loop through all the modules, modules start with sent_ were added by ourselves, others are built-in modules.

>>> modules = session.get_available_modules()
>>> for m in modules:
... print(m.module_key)
...
Home
Accounts
Bugs
Contacts
Opportunities
Leads
Calendar
Documents
Emails
Calls
Campaigns
Meetings
Tasks
Notes
Project
Cases
Prospects
ProspectLists
sent_licensekeys
sent_run_object
sent_mail_object
sent_invoices
sent_quotes
sent_usbkeys
sent_expire_object

The sent_usbkeys module has following fields in CRM, fields start with * are added by ourselves.

name
date_entered
date_modified
description
deleted
* invoiced_c
* key_id_c
* chip_id_c

My question is: how to convert the query = session['sent_usbkeys'].query().filter(key_id_c__gte = '1') to python3 code? 

Thank you for reading through my long post.

Joyce