'Updates an existing account in the database.'

def update(self, account_id, account_type, name, active, third_party, notes, identifier, custom_fields=None):

    _get_or_create_account_type(account_type)
    if account_id:
        account = Account.query.filter((Account.id == account_id)).first()
        if (not account):
            app.logger.error('Account    with    ID    {}    does    not    exist.'.format(account_id))
            return None
        if (account.name != name):
            if Account.query.filter((Account.name == name)).first():
                app.logger.error('Account    with    name:    {}    already    exists.'.format(name))
                raise AccountNameExists(name)
            account.name = name
    else:
        account = Account.query.filter((Account.name == name)).first()
        if (not account):
            app.logger.error('Account    with    name    {}    does    not    exist.'.format(name))
            return None
    account.active = active
    account.notes = notes
    account.active = active
    account.third_party = third_party
    account.identifier = self.sanitize_account_identifier(identifier)
    self._update_custom_fields(account, custom_fields)
    db.session.add(account)
    db.session.commit()
    db.session.refresh(account)
    account = self._load(account)
    db.session.expunge(account)
    return account
