Skip to content

Commit

Permalink
datastore: prevent autoflush
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastro2 committed Oct 13, 2023
1 parent 3caced5 commit 0fe6875
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 3 additions & 1 deletion invenio_accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from flask import request
from flask_security.forms import NextFormMixin
from flask_wtf import FlaskForm, Recaptcha, RecaptchaField
from invenio_db import db
from invenio_i18n import gettext as _
from wtforms import FormField, HiddenField

Expand Down Expand Up @@ -90,7 +91,8 @@ class SendConfirmationEmailView(Form):
"""

def validate(self, extra_validators=None):
self.user = current_datastore.get_user(self.data["email"])
with db.session.no_autoflush:
self.user = current_datastore.get_user(self.data["email"])
# Form is valid if user exists and they are not yet confirmed
if self.user is not None and self.user.confirmed_at is None:
return True
Expand Down
15 changes: 10 additions & 5 deletions invenio_accounts/views/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,18 @@ def handle_error(self, error, *args, **kwargs):
#
def user_exists(email):
"""Validate that a user exists."""
if not current_datastore.get_user(email):
raise ValidationError(get_message("USER_DOES_NOT_EXIST")[0])
with db.session.no_autoflush:
if not current_datastore.get_user(email):
raise ValidationError(get_message("USER_DOES_NOT_EXIST")[0])


def unique_user_email(email):
"""Validate unique user email."""
if current_datastore.get_user(email) is not None:
raise ValidationError(get_message("EMAIL_ALREADY_ASSOCIATED", email=email)[0])
with db.session.no_autoflush:
if current_datastore.get_user(email) is not None:
raise ValidationError(
get_message("EMAIL_ALREADY_ASSOCIATED", email=email)[0]
)


def default_user_payload(user):
Expand Down Expand Up @@ -222,7 +226,8 @@ class UserViewMixin(object):

def get_user(self, email=None, **kwargs):
"""Retrieve a user by the provided arguments."""
return current_datastore.get_user(email)
with db.session.no_autoflush:
return current_datastore.get_user(email)


class LoginView(MethodView, UserViewMixin):
Expand Down

0 comments on commit 0fe6875

Please sign in to comment.