offline avatar offline committed 013b240

PROFILE_MAPPING values are form fields instances

Comments (0)

Files changed (5)

openauth/backends/__init__.py

 from django.utils.translation import ugettext as _
 from django.contrib.auth.models import User
 from django.contrib import auth
+from django.conf import settings as global_settings
 from django import forms
 
 from annoying.exceptions import Redirect
 
 class BaseBackend(forms.Form):
 
+    PROFILE_MAPPING = property(lambda self: getattr(global_settings, "%s_PROFILE_MAPPING" % self.privider.upper(), {}))
+
     def __init__(self, *args, **kwargs):
         self._identity = None
         self.request = kwargs.pop('request', {})
         login user.
         """
         data = {}
-        if extra:
-            for field in getattr(settings, "%s_EXTRA_FIELDS" % self.provider.upper()):
-                if self._have_profile_name(field):
-                    profile_field = self._get_profile_field_name(field)
-                    data.update(self.extract_data(extra, profile_field, field))
 
-        form = str_to_class(settings.SOCIALID_EXTRA_FORM)(data)
+        for field in self.PROFILE_MAPPING:
+            data.update(self.extract_data(extra, field))
+
+        form = str_to_class(settings.SOCIALID_EXTRA_FORM)(data, fields=self.PROFILE_MAPPING:)
         if form.is_valid():
             form.save(self.get_identity(), self.provider)
             user = auth.authenticate(identity=self.get_identity(), provider=self.provider)
         else:
             return data
 
-    def extract_data(self, data, profile_field, field):
-        return {profile_field: data.get(field, '')}
-
-    def _have_extra_fields(self): 
-        pass
-
-    def _have_profile_name(self, field):
-        return field in getattr(settings, "%s_PROFILE_MAPPING" % self.provider.upper())
-
-    def _get_profile_field_name(self, field):
-        mapping = getattr(settings,"%s_PROFILE_MAPPING" % self.provider.upper())
-        return mapping[field]
+    def extract_data(self, data, field):
+        try:
+            return {field: data.get(field, '')}
+        except AttributeError:
+            return {field: ''}
 
     def validate_response(self, request):
         pass

openauth/backends/google.py

     def get_extra_data(self, resp):
         return FetchResponse.fromSuccessResponse(resp)
 
-    def extract_data(self, data, profile_field, field):
-        return {profile_field: data.getSingle(settings.AX_URIS[field], '')}
+    def extract_data(self, data, field):
+        try:
+            return {field: data.getSingle(settings.AX_URIS[field], '')}
+        except:
+            return {field: ''}
 

openauth/backends/twitter.py

 
 from oauth.oauth import OAuthConsumer, OAuthToken, OAuthRequest, OAuthSignatureMethod_HMAC_SHA1
 
+from django.conf import settings as global_settings
 from django.utils.translation import ugettext as _
 from django.core.urlresolvers import reverse
 from django.contrib.auth.models import User
 
 class TwitterBackend(BaseBackend):
 
-    CONSUMER_KEY = property(lambda self: getattr(settings, "%s_CONSUMER_KEY" % self.privider.upper()))
-    CONSUMER_SECRET = property(lambda self: getattr(settings, "%s_CONSUMER_SECRET" % self.privider.upper()))
-    REQUEST_TOKEN_URL = property(lambda self: getattr(settings, "%s_REQUEST_TOKEN_URL" % self.privider.upper()))
-    ACCESS_TOKEN_URL = property(lambda self: getattr(settings, "%s_ACCESS_TOKEN_URL" % self.privider.upper()))
-    AUTHORIZE_URL = property(lambda self: getattr(settings, "%s_AUTHORIZE_URL" % self.privider.upper()))
+    CONSUMER_KEY = property(lambda self: getattr(global_settings, "%s_CONSUMER_KEY" % self.privider.upper()))
+    CONSUMER_SECRET = property(lambda self: getattr(global_settings, "%s_CONSUMER_SECRET" % self.privider.upper()))
+    REQUEST_TOKEN_URL = property(lambda self: getattr(global_settings, "%s_REQUEST_TOKEN_URL" % self.privider.upper()))
+    ACCESS_TOKEN_URL = property(lambda self: getattr(global_settings, "%s_ACCESS_TOKEN_URL" % self.privider.upper()))
+    AUTHORIZE_URL = property(lambda self: getattr(global_settings, "%s_AUTHORIZE_URL" % self.privider.upper()))
 
     def clean(self):
         consumer = OAuthConsumer(self.CONSUMER_KEY, self.CONSUMER_SECRET)

openauth/forms.py

 
 class SocialIDExtraForm(forms.Form):
     def __init__(self, *args, **kwargs):
-        for key, value in settings.SOCIALID_EXTRA_FIELDS_MAPPING.items():
-            self.base_fields[value] = forms.CharField()
-            if value == 'email':
-                self.base_fields[value] = forms.EmailField()
+        fields = kwargs.pop("fields", None)
+        for key, value in fields:
+            self.base_fields[key] = value
         super(SocialIDExtraForm, self).__init__(*args, **kwargs)
 
     def clean_login(self):

openauth/settings.py

 import re
 
+from django import forms
+
 from annoying.functions import get_config
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.