Created
April 25, 2018 09:27
-
-
Save BuildWithLal/e4972a6106cfb7b624c573c649f50068 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SocialLoginSerializer(UserDeviceMixin, serializers.Serializer): | |
PROVIDER_CHOICES = Provider.objects.values_list('name') | |
# first_name = serializers.CharField(required=False, allow_blank=True) | |
# last_name = serializers.CharField(required=False, allow_blank=True) | |
# email = serializers.EmailField() | |
# mac_address = serializers.CharField() | |
name = serializers.CharField(required=False, allow_blank=True) | |
provider_id = serializers.CharField() | |
#last_name = serializers.CharField(required=False, allow_blank=True) | |
email = serializers.EmailField(required=False, allow_null=True, allow_blank=True) # twitter does not take "email" filed thts why email filed is null and blank =TRUE | |
mac_address = serializers.CharField() | |
provider_name = serializers.ChoiceField( | |
choices=PROVIDER_CHOICES, | |
error_messages={ | |
'invalid_choice': 'provider_name must be one of the these. {}'.format(", ".join(PROVIDER_CHOICES)) | |
}) | |
class Meta: | |
fields = '__all__' | |
def __init__(self, *args, **kwargs): | |
self.auth_fields = None | |
self.auth_data = None | |
self.provider_fields = None | |
self.provider_data = None | |
if kwargs.get('data'): | |
self.setup_dynamic_fields() | |
super().__init__(*args, **kwargs) | |
def setup_dynamic_fields(self): | |
data = self._kwargs.get('data') | |
print(data) | |
try: | |
provider = Provider.objects.get(name__iexact=data.get('provider_name')) | |
except Provider.DoesNotExist: | |
msg = 'provider_name must be one of the these. {}'.format(", ".join(SocialLoginSerializer.PROVIDER_CHOICES)) | |
raise serializers.ValidationError({'provider_name': [msg]}) | |
provider_settings = ProviderSettings.objects.get(provider=provider.to_dbref()) | |
#pf = xmltodict.parse(provider_settings.provider_fields) | |
# print(pf) | |
#data save in db in xml form .... provider fields | |
self.auth_fields = xmltodict.parse(provider_settings.auth_fields.strip()) | |
self.provider_fields = xmltodict.parse(provider_settings.provider_fields.strip()) | |
for field_name, field_attrs in self.auth_fields['fields'].items(): | |
field = fields_mapper.get(field_attrs.pop('type')) | |
attrs = {} | |
if field_attrs.get('required'): | |
attrs.update({'required': attr_mapper.get(field_attrs.get('required'))}) | |
if field_attrs.get('max_length'): | |
attrs.update({'max_length': int(field_attrs.get('max_length'))}) | |
if field_attrs.get('min_length'): | |
attrs.update({'min_length': int(field_attrs.get('min_length'))}) | |
if field_attrs.get('allow_null'): | |
attrs.update({'allow_null': attr_mapper.get(field_attrs.get('allow_null'))}) | |
self.fields[field_name] = field(**attrs) | |
for field_name, field_attrs in self.provider_fields['fields'].items(): | |
field = fields_mapper.get(field_attrs.pop('type')) | |
attrs = {} | |
if field_attrs.get('required'): | |
attrs.update({'required': attr_mapper.get(field_attrs.get('required'))}) | |
# if field_attrs.get('max_length'): | |
# attrs.update({'max_length': int(field_attrs.get('max_length'))}) | |
# | |
# if field_attrs.get('min_length'): | |
# attrs.update({'min_length': int(field_attrs.get('min_length'))}) | |
# if field_attrs.get('allow_null'): | |
# attrs.update({'allow_null': attr_mapper.get(field_attrs.get('allow_null'))}) | |
self.fields[field_name] = field(**attrs) | |
def login_user(self): | |
data = self.validated_data | |
user = authenticate(request=self.context['request'], email=data.get('email')) | |
self.auth_data = {field_name: data.get(field_name) for field_name in self.auth_fields['fields']} | |
self.auth_data = dict2xml(self.auth_data) | |
self.provider_data = {field_name: data.get(field_name) for field_name in self.provider_fields['fields']} | |
self.provider_data = dict2xml(self.provider_data) | |
print(self.provider_data) | |
# def update_provider_fields(self): | |
# data = self.validated_data | |
# print(data) | |
# try: | |
# #provider = Provider.objects.get(name__iexact=data.get('provider_name').lower()) | |
# #user_provider = UserProvider.objects.get(user=user.to_dbref(), provider=provider.to_dbref()) | |
# provider_settings = ProviderSettings.objects.get(provider_fields=data.provider_fields) | |
# provider_settings.update(provider_fields=self.provider_fields) | |
# | |
# except ProviderSettings.DoesNotExist: | |
# print("Error") | |
""" | |
in case user is trying to login with any social site i.e Facebook, Google | |
Twitter, we are first checking if user already exist with given email and | |
provider_name (twitter, facebook etc). If exist then do nothing. | |
If user doesn't exist, create a new social user | |
""" | |
if not user: | |
user = self.create_user() | |
else: | |
self.create_or_update_profile(user) | |
self.add_user_device(user) | |
if not user.token: | |
user.token = Token() | |
user.save() | |
return data | |
def create_user(self): | |
data = self.validated_data | |
user = UserModel( | |
first_name=data.get('first_name'), | |
last_name=data.get('last_name')) | |
# email=data.get('email')) | |
user.save() | |
self.create_social_profile(user) | |
return user | |
def create_social_profile(self, user): | |
data = self.validated_data | |
provider = Provider.objects.get(name__iexact=data.get('provider_name').lower()) | |
UserProvider.objects.create( | |
user=user.to_dbref(), | |
provider=provider.to_dbref(), | |
user_data=self.provider_data, | |
auth_data=self.auth_data | |
) | |
def create_or_update_profile(self, user): | |
data = self.validated_data | |
try: | |
provider = Provider.objects.get(name__iexact=data.get('provider_name').lower()) | |
user_provider = UserProvider.objects.get(user=user.to_dbref(), provider=provider.to_dbref()) | |
user_provider.update(auth_data=self.auth_data) | |
user_provider.update(user_data=self.provider_data) | |
except UserProvider.DoesNotExist: | |
self.create_social_profile(user) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment