# Creating failing webuser from flask still adds the user

**URL:** https://discuss.tryton.org/t/creating-failing-webuser-from-flask-still-adds-the-user/5275
**Category:** Developer
**Tags:** web-user, flask
**Created:** [May 18, 2022, 9:37pm UTC](https://discuss.tryton.org/t/creating-failing-webuser-from-flask-still-adds-the-user/5275 "2022-05-18T21:37:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)
#### Post date: [May 18, 2022, 9:37pm UTC](https://discuss.tryton.org/t/creating-failing-webuser-from-flask-still-adds-the-user/5275/1 "2022-05-18T21:37:56Z")

</div>

Using the `web_user` module, the email address has a SQL constraint to be unique. When adding a new web user from the desktop client with an email address already in the database it will fail and no new user is created. This is the right behavior.

Now I want to do the same from my webapp. The app sends an email address and password in JSON format to the backend which is Flask with the flask-tryton package. The backend then calls the `create` function on `web.user` to create the user. When using an email address which is already in the database I get an error back which is expected.

What is not expected, is that even an error is returned, the user is still created in the database.

My code:

```python
app.config['TRYTON_USER'] = 2 # normal user

WebUser = tryton.pool.get('web.user')

@app.route('/register_email', methods=['POST'])
@tryton.transaction()
def register_email():
    data = request.get_json()
    res = {}
    if 'username' and 'password' in data:
        try:
            user = WebUser.create([{
                'email': data['username'],
                'password': data['password']
            }])
            res['result'] = []
        except Exception as e:
            # return error. This can be about unique email addresses
            res['error'] = {'error': "{0}".format(str(e))}
    else:
        res['error'] = {'error': 'Wrong data entered.'}

    return json.dumps(res)

```

So I get the error “`E-mail of active web user must be unique.`” back, but the user is still created in the database.

Python 3.9.6, flask-tryton 0.11.0 and trytond 6.4.0 with a sqlite database (will that be a problem?)

Do I need some extra checks to overcome this problem?

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [May 18, 2022, 9:49pm UTC](https://discuss.tryton.org/t/creating-failing-webuser-from-flask-still-adds-the-user/5275/2 "2022-05-18T21:49:53Z")

</div>

If you do not raise an exception, `transaction` decorator does not know that it has to rollback the transaction.  
The best is to use the `abort` function from Flask.

---

<div class="post-metadata">

### Author: ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)
#### Post date: [May 18, 2022, 9:58pm UTC](https://discuss.tryton.org/t/creating-failing-webuser-from-flask-still-adds-the-user/5275/3 "2022-05-18T21:58:41Z")

</div>

Indeed, adding `abort` fixes this.

```python
try:
    # ......
except Exception as e:
    # return error. This can be about unique email addresses
    res['error'] = {'error': "{0}".format(str(e))}
    abort(400, json.dumps(res))

```

---

<div class="post-metadata">

### Author: ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)
#### Post date: [June 17, 2022, 9:59pm UTC](https://discuss.tryton.org/t/creating-failing-webuser-from-flask-still-adds-the-user/5275/4 "2022-06-17T21:59:29Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
