use with_for_update for User class too

This commit is contained in:
j 2019-01-20 15:28:38 +05:30
parent 469cdf8fb7
commit a576de8a82
1 changed files with 8 additions and 5 deletions

View File

@ -42,8 +42,11 @@ class User(db.Model):
return self.id
@classmethod
def get(cls, id):
user = cls.query.filter_by(id=id).first()
def get(cls, id, for_update=False):
qs = cls.query.filter_by(id=id)
if for_update:
qs = qs.with_for_update()
user = qs.first()
if user and not user.info:
user.info = {}
return user
@ -596,13 +599,13 @@ def export_list(data):
def update_user_peering(user_id, peered, username=None):
with db.session():
u = User.get(user_id)
u = User.get(user_id, for_update=True)
if u:
u.update_peering(peered, username)
def remove_local_info(id):
with db.session():
u = User.get(id)
u = User.get(id, for_update=True)
if u and 'local' in u.info:
del u.info['local']
u.save()
@ -610,7 +613,7 @@ def remove_local_info(id):
def add_local_info(data):
with db.session():
u = User.get(data['id'])
u = User.get(data['id'], for_update=True)
if u:
if u.info['username'] != data['username']:
u.info['username'] = data['username']