From 14d91c6ada6251fd738b811c25d8f01b06fd5821 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mat=C3=ADas=20Aguirre?= Date: Wed, 15 Feb 2012 12:57:12 -0200 Subject: [PATCH] Save model instances into session in a easy format to retrieve it later. Refs #251 --- social_auth/backends/__init__.py | 18 ++++++++++-------- social_auth/utils.py | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/social_auth/backends/__init__.py b/social_auth/backends/__init__.py index af1922b..b4a59a4 100644 --- a/social_auth/backends/__init__.py +++ b/social_auth/backends/__init__.py @@ -26,7 +26,7 @@ from django.contrib.auth.backends import ModelBackend from django.utils import simplejson from django.utils.importlib import import_module -from social_auth.utils import setting, log +from social_auth.utils import setting, log, model_to_ctype, ctype_to_model from social_auth.store import DjangoOpenIDStore from social_auth.backends.exceptions import StopPipeline @@ -312,8 +312,9 @@ class BaseAuth(object): return { 'next': next_idx, 'backend': self.AUTH_BACKEND.name, - 'args': args, - 'kwargs': kwargs + 'args': tuple(map(model_to_ctype, args)), + 'kwargs': dict((key, model_to_ctype(val)) + for key, val in kwargs.iteritems()) } def from_session_dict(self, entry, *args, **kwargs): @@ -321,11 +322,12 @@ class BaseAuth(object): any new extra argument needed. Returns tuple with next pipeline index entry, arguments and keyword arguments to continue the process.""" - session_kwargs = entry['kwargs'] - session_kwargs.update(kwargs) - return ( entry['next'], - list(entry['args']) + list(args), - session_kwargs ) + args = args[:] + tuple(map(ctype_to_model, entry['args'])) + + kwargs = kwargs.copy() + kwargs.update((key, ctype_to_model(val)) + for key, val in entry['kwargs'].iteritems()) + return (entry['next'], args, kwargs) def continue_pipeline(self, *args, **kwargs): """Continue previous halted pipeline""" diff --git a/social_auth/utils.py b/social_auth/utils.py index b5de003..9f288ed 100644 --- a/social_auth/utils.py +++ b/social_auth/utils.py @@ -3,6 +3,8 @@ import logging from collections import defaultdict from django.conf import settings +from django.db.models import Model +from django.contrib.contenttypes.models import ContentType def sanitize_log_data(secret, data=None, leave_characters=4): @@ -102,6 +104,26 @@ def log(level, *args, **kwargs): 'warn': logger.warn }[level](*args, **kwargs) +def model_to_ctype(val): + """Converts values that are instance of Model to a dictionary + with enough information to retrieve the instance back later.""" + if isinstance(val, Model): + val = { + 'pk': val.pk, + 'ctype': ContentType.objects.get_for_model(val).pk + } + return val + + +def ctype_to_model(val): + """Converts back the instance saved by model_to_ctype function.""" + if isinstance(val, dict) and 'pk' in val and 'ctype' in val: + ctype = ContentType.objects.get_for_id(val['ctype']) + ModelClass = ctype.model_class() + val = ModelClass.objects.get(pk=val['pk']) + return val + + if __name__ == '__main__': import doctest doctest.testmod() -- 2.39.5