Skip to content

Commit

Permalink
Flush sqlalchemy session to get the object ids. Refs #390
Browse files Browse the repository at this point in the history
  • Loading branch information
omab committed Mar 19, 2015
1 parent 183a57a commit a52ee69
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/pyramid_example/example/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from example.models import DBSession, User


def login_user(strategy, user, user_social_auth):
strategy.strategy.session_set('user_id', user.id)
def login_user(backend, user, user_social_auth):
backend.strategy.session_set('user_id', user.id)


def login_required(request):
Expand Down
2 changes: 0 additions & 2 deletions social/apps/pyramid_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ def init_social(config, Base, session):
app_session = session

class _AppSession(object):
COMMIT_SESSION = False

@classmethod
def _session(cls):
return app_session
Expand Down
23 changes: 13 additions & 10 deletions social/storage/sqlalchemy_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import six
import json

import transaction

from sqlalchemy import Column, Integer, String
from sqlalchemy.exc import IntegrityError
from sqlalchemy.types import PickleType, Text
Expand All @@ -22,8 +24,6 @@ def __init__(self, *args, **kwargs):


class SQLAlchemyMixin(object):
COMMIT_SESSION = True

@classmethod
def _session(cls):
raise NotImplementedError('Implement in subclass')
Expand All @@ -39,11 +39,18 @@ def _new_instance(cls, model, *args, **kwargs):
@classmethod
def _save_instance(cls, instance):
cls._session().add(instance)
if cls.COMMIT_SESSION:
cls._session().commit()
cls._session().flush()
cls._flush()
return instance

@classmethod
def _flush(cls):
try:
cls._session().flush()
except AssertionError:
with transaction.manager as manager:
print "COMMIT 5"
manager.commit()

def save(self):
self._save_instance(self)

Expand Down Expand Up @@ -84,11 +91,7 @@ def allowed_to_disconnect(cls, user, backend_name, association_id=None):
@classmethod
def disconnect(cls, entry):
cls._session().delete(entry)
try:
cls._session().commit()
except AssertionError:
import transaction
transaction.commit()
cls._flush()

@classmethod
def user_query(cls):
Expand Down

0 comments on commit a52ee69

Please sign in to comment.