Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions uber/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ def create(self, first_name, last_name, email, params):
Example `params` dictionary for setting extra parameters:
<pre>{"placeholder": "yes", "legal_name": "First Last", "cellphone": "5555555555"}</pre>
"""
with Session() as session:
with Session(create_savepoint=True) as session:
attendee_query = session.query(Attendee).filter(Attendee.first_name.ilike(first_name),
Attendee.last_name.ilike(last_name),
Attendee.email.ilike(email))
Expand Down Expand Up @@ -820,7 +820,7 @@ def update(self, id, params):
Example:
<pre>{"first_name": "First", "paid": "doesn't need to", "ribbon": "Volunteer, Panelist"}</pre>
"""
with Session() as session:
with Session(create_savepoint=True) as session:
attendee = session.attendee(id, allow_invalid=True)

if not attendee:
Expand Down
33 changes: 30 additions & 3 deletions uber/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2376,11 +2376,38 @@ def __getattr__(self, name):
"""
return getattr(_ScopedSession, name)

def __call__(self, *args, **kwargs):
def __call__(self, create_savepoint=False, *args, **kwargs):
"""
Create isolated session, to match old behavior on 'with Session() as session'
Creates a new session or returns the current session.

create_savepoint (bool): Flush the current session to the DB and create a savepoint.
This lets you use session.rollback() to undo only changes made after the savepoint.
This is ignored if we're returning a new session.
"""
return SessionFactory(*args, **kwargs)
try:
req = cherrypy.request

if 'bind' not in kwargs:
if not hasattr(req, 'db_connection'):
req.db_connection = engine.connect()

def release_connection():
req.db_connection.close()
req.hooks.attach('on_end_request', release_connection)

kwargs['bind'] = req.db_connection

session = SessionFactory(*args, **kwargs)

if create_savepoint and kwargs.get('bind') == getattr(req, 'db_connection', None) and req.db_connection.in_transaction():
session.begin_nested()

return session

except AttributeError:
# We are outside of a web request (e.g., Celery workers, cron jobs, test scripts)
# Fall back to standard unmanaged behavior.
return SessionFactory(*args, **kwargs)

Session = HybridSessionProxy()

Expand Down
5 changes: 2 additions & 3 deletions uber/site_sections/promo_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ def add_promo_code_words(self, session, text='', part_of_speech=0):
text = text.strip()
words = []
if text:
with Session() as session:
old_words = set(s for (s,) in session.query(PromoCodeWord.normalized_word).filter(
PromoCodeWord.part_of_speech == part_of_speech).all())
old_words = set(s for (s,) in session.query(PromoCodeWord.normalized_word).filter(
PromoCodeWord.part_of_speech == part_of_speech).all())

for word in [s for s in shlex.split(text.replace(',', ' ')) if s]:
if PromoCodeWord.normalize_word(word) not in old_words:
Expand Down
2 changes: 1 addition & 1 deletion uber/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2109,7 +2109,7 @@ def attendee_import(import_job):
from uber.models import Attendee, AttendeeAccount, DeptMembership, DeptRole
from functools import partial

with uber.models.Session() as session:
with uber.models.Session(create_savepoint=True) as session:
service, message, target_url = get_api_service_from_server(import_job.target_server,
import_job.api_token)

Expand Down
Loading