How to update oxmail/contextId from Python?

Hi,
we are trying to setup OX in our environment.
For this we have to create the contextIDs first which I tried to do with Python script.
Basically the script works, but I cannot update an existing record. Could you please help?
The similar steps work for updating users, but here the cntext is different and therefore it fails I think.
I’m afraid that I need another approach here, but don’t know what.

Script basically looks like:

from univention.udm import UDM
oxcts=UDM.admin().version(2).get('oxmail/oxcontext')
for i in oxcts.search('name=Context_ToUpdate'):
...    obj.position=obj.position
...    obj.props.contextid='88456'
...    obj.save()

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/usr/lib/python3/dist-packages/univention/udm/modules/generic.py", line 176, in save
    self._copy_to_udm_obj()
  File "/usr/lib/python3/dist-packages/univention/udm/modules/generic.py", line 367, in _copy_to_udm_obj
    self._orig_udm_object[k] = new_val2
  File "/usr/lib/python3/dist-packages/univention/admin/handlers/__init__.py", line 400, in __setitem__
    raise univention.admin.uexceptions.valueMayNotChange(_('key=%(key)s old=%(old)s new=%(new)s') % {'key': key, 'old': self[key], 'new': value}, property=key)
univention.admin.uexceptions.valueMayNotChange: Value may not change: key=contextid old=88016 new=88456.
>>> oxobj=oxc.get_by_id('Context_ToUpdate')
>>> oxobj.props.contextid=88456
>>> oxobj.save()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/univention/udm/modules/generic.py", line 176, in save
    self._copy_to_udm_obj()
  File "/usr/lib/python3/dist-packages/univention/udm/modules/generic.py", line 367, in _copy_to_udm_obj
    self._orig_udm_object[k] = new_val2
  File "/usr/lib/python3/dist-packages/univention/admin/handlers/__init__.py", line 400, in __setitem__
    raise univention.admin.uexceptions.valueMayNotChange(_('key=%(key)s old=%(old)s new=%(new)s') % {'key': key, 'old': self[key], 'new': value}, property=key)
univention.admin.uexceptions.valueMayNotChange: Value may not change: key=contextid old=88016 new=88456.

Thanks for help in advance,
Michael

Hi,

unsure what exactly you want to do. You said: “we have to create the contextIDs”. But then you go and try to update an existing context, instead of creating a new one.

oxmail/oxcontext objects cannot be altered to have a new contextid. Once set, you cannot change it (univention.admin.uexceptions.valueMayNotChange). You need to create a new object instead:

from univention.udm import UDM
udm = UDM.admin().version(2)
ctx = udm.get('oxmail/oxcontext').new()
ctx.props.contextid = '88456'
ctx.props.name = 'context88456'
ctx.save()

Something like that should work. No sure what to do with the users now, but this sets the users to the new context:

from univention.udm import UDM
udm = UDM.admin().version(2)
for user in udm.get('users/user').search():
    user.props.oxContext = '88456'
    user.save()

Not sure if you really want to put all users to the new context. So please be careful copying that.

Kind regards,
Dirk

Hi Dirk,
thanks for your response, which seems to answer almost completely.
I’m sorry that I causes irritation with my description.
The original request is to create the contextIDs for OX. The problem was/is that we created a handful manually and I wanted to overwrite/update existing contextIDs (also in case of a future change), either name or ID.
Because Python throws exception when a new duplicate entry is attempted to create I wanted to update these IDs instead.I also was looking for an option to update IDs/change context Name in case something changes.
Hope it explains why I start with creation of IDs and end with update request.

From your explanation I understand that it is not possible to update an alter an existing ID. It has to be deleted and then recreated with new data,right?

Your point is correct that after changing an ContextID the users need to be reassigned to the changed contextID.
I only had in mind that there’s no update required for users as long as the context name remains the same.
But it seems not be possible to keep the context record on a change and therefore we have to update users as well on a Id change.
Even if i think, that contextID will live a long time I wanted to be prepared for such a request.

Thanks & regards, Michael

Mastodon