# Buggy code
def on_mailbox_modified(mailbox):
    """Update amavis records if address has changed."""
    if (
        parameters.get_admin("MANUAL_LEARNING") == "no"
        or not hasattr(mailbox, "old_full_address")
        or mailbox.full_address == mailbox.old_full_address
    ):
        return
    user = Users.objects.select_related.get(email=mailbox.old_full_address)
    full_address = mailbox.full_address
    user.email = full_address
    user.policy.policy_name = full_address[:32]
    user.policy.sa_username = full_address
    user.policy.save()
    user.save()


# Traceback (AttributeError)
Traceback (most recent call last):

File "/srv/python-envs/service/lib/python2.7/site-packages/django/core/handlers/base.py", line 112, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/srv/python-envs/service/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
return view_func(request, *args, **kwargs)

File "/srv/python-envs/service/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
return view_func(request, *args, **kwargs)

File "/srv/python-envs/service/lib/python2.7/site-packages/reversion/revisions.py", line 297, in do_revision_context
return func(*args, **kwargs)

File "/srv/python-envs/service/lib/python2.7/site-packages/modoboa/extensions/admin/views/identity.py", line 177, in editaccount
return AccountForm(request, instances=instances).process()

File "/srv/python-envs/service/lib/python2.7/site-packages/modoboa/lib/formutils.py", line 315, in process
self.save()

File "/srv/python-envs/service/lib/python2.7/site-packages/modoboa/extensions/admin/forms/account.py", line 427, in save
self.forms[1]["instance"].save(self.user, self.account)

File "/srv/python-envs/service/lib/python2.7/site-packages/modoboa/extensions/admin/forms/account.py", line 305, in save
self.update_mailbox(user, account)

File "/srv/python-envs/service/lib/python2.7/site-packages/modoboa/extensions/admin/forms/account.py", line 257, in update_mailbox
events.raiseEvent('MailboxModified', self.mb)

File "/srv/python-envs/service/lib/python2.7/site-packages/modoboa/lib/events.py", line 145, in raiseEvent
callback(*args, **kwargs)

File "/srv/python-envs/service/lib/python2.7/site-packages/modoboa/lib/events.py", line 98, in wrapped_f
return f(*args, **kwargs)

File "/srv/python-envs/service/lib/python2.7/site-packages/modoboa/extensions/amavis/general_callbacks.py", line 67, in on_mailbox_modified
user = Users.objects.select_related.get(email=mailbox.old_full_address)

AttributeError: 'function' object has no attribute 'get'

# Fixed code
def on_mailbox_modified(mailbox):
    """Update amavis records if address has changed."""
    if (
        parameters.get_admin("MANUAL_LEARNING") == "no"
        or not hasattr(mailbox, "old_full_address")
        or mailbox.full_address == mailbox.old_full_address
    ):
        return
    user = Users.objects.select_related().get(email=mailbox.old_full_address)
    full_address = mailbox.full_address
    user.email = full_address
    user.policy.policy_name = full_address[:32]
    user.policy.sa_username = full_address
    user.policy.save()
    user.save()